随机生成敌人

3

我正在制作一个小型的2D游戏,目标是尽可能多地吃屎,但我在随机时间生成屎时遇到了问题。我希望屎可以在敌人的y位置生成,然后像RPG一样向前发射。

import pygame
from pygame.locals import *
from numpy.random import rand

pygame.init()
pygame.display.set_caption('STINKY BEETLE')

screen_width = 800
screen_height = 600
game_running = True
pl_x = int(screen_width/10)
pl_y = int(screen_height/2)
pl_width = 80
pl_height = 40
pl_vel = 30
en_width = 80
en_height = 40
en_x = screen_width - screen_width/10 - en_width
en_y = int(screen_height/2)
en_yvel = -10
po_width = 50
po_height = 30
po_x = 720
po_y = en_y
po_xvel = 15

screen = pygame.display.set_mode((screen_width, screen_height))
clock = pygame.time.Clock()

while game_running:
    clock.tick(10)

    po_delay = rand(1)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_running = False

        if event.type == MOUSEBUTTONDOWN:
            if event.button == 4 and pl_y > pl_vel:
                pl_y -= pl_vel

            elif event.button == 5 and pl_y < screen_height - pl_width:
                pl_y += pl_vel

    if po_delay < 0.01:
        poop(po_x, po_y)

    en_y += en_yvel
    if en_y <= 0 or en_y >= screen_height - en_height:
        en_yvel =- en_yvel

    screen.fill((0, 0, 0))
    pygame.draw.rect(screen, (105, 255, 125), (pl_x, pl_y, pl_width, pl_height))
    pygame.display.update()

    pygame.draw.rect(screen, (255, 125, 115), (en_x, en_y, en_width, en_height))
    pygame.display.update()

pygame.quit()

你到目前为止尝试了什么?有什么东西是有效的吗?看起来你在随机帧中调用了poop(po_x, po_y)函数,这个poop()函数是做什么的?你是如何将这个“poop”移动到屏幕上的?如果你能更详细地解释一下你现在已经实现了什么,并且在下一步遇到了什么问题,我们就可以更容易地帮助你解决这个问题! - undefined
我尝试了几种方法,比如,如果随机生成的数字低于某个阈值,就会创建一个show_poop变量。我曾经设为true,并且poop函数只是另一次失败的尝试,我试图在其中创建一个包含有关poop的所有信息的函数,然后如果随机生成的数字低于阈值,我只需调用该函数。 - user10567555
2个回答

2
如果您想管理多个"poops",则需要创建一个列表。每个"poop"都是一个对象(class的实例)。
创建一个名为Poop的类,它可以update位置并draw poop:
class Poop:
    def __init__(self, x, y, w, h):
        self.rect = pygame.Rect(x, y-h//2, w, h)
        self.vel = -15
    def update(self):
        self.rect.x += self.vel
    def draw(self, surf):
        pygame.draw.rect(surf, (255, 200, 125), self.rect)

poops = []

使用计时器事件生成粪便。使用pygame.time.set_timer()重复创建USEREVENT。时间以毫秒为单位设置。通过random.randint(a, b)设置随机时间,例如设置在0.5到4秒之间的时间(当然,您可以选择自己的时间间隔):
min_time, max_time = 500, 4000 # 0.5 seconds to to 4 seconds
spawn_event = pygame.USEREVENT + 1
pygame.time.set_timer(spawn_event, random.randint(min_time, max_time))

注意,在pygame中可以定义自定义事件。每个事件需要一个唯一的id。用户事件的id必须从pygame.USEREVENT开始。在这种情况下,pygame.USEREVENT+1是定时器事件的事件id,它生成了粪便。
在事件循环中发生事件时,创建新的粪便并设置新的随机时间:
for event in pygame.event.get():
    # [...]
    if event.type == spawn_event:
        pygame.time.set_timer(spawn_event, random.randint(min_time, max_time))
        poops.append(Poop(en_x, en_y+en_yvel+en_height//2, 50, 30))

循环中改变便便的位置,并在它们离开左侧窗口时从列表中移除:

for poop in poops[:]:
    poop.update()
    if poop.rect.right <= 0:
        poops.remove(poop)

在循环中绘制它们

for poop in poops:
    poop.draw(screen)

看例子:
import pygame
from pygame.locals import *
import random

pygame.init()
pygame.display.set_caption('STINKY BEETLE')

class Poop:
    def __init__(self, x, y, w, h):
        self.rect = pygame.Rect(x, y-h//2, w, h)
        self.vel = -15
    def update(self):
        self.rect.x += self.vel
    def draw(self, surf):
        pygame.draw.rect(surf, (255, 200, 125), self.rect)

screen_width = 800
screen_height = 600
game_running = True
pl_x, pl_y = screen_width//10, screen_height//2
pl_width, pl_height, pl_vel = 80, 40, 30
en_width, en_height, en_yvel = 80, 40, -10
en_x, en_y,  = screen_width - screen_width//10 - en_width, screen_height//2

screen = pygame.display.set_mode((screen_width, screen_height))
clock = pygame.time.Clock()

min_time, max_time = 500, 4000 # 0.5 seconds up to 4 seconds 
spawn_event = pygame.USEREVENT + 1
pygame.time.set_timer(spawn_event, random.randint(min_time, max_time))
poops = []

while game_running:
    clock.tick(10)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_running = False

        if event.type == MOUSEBUTTONDOWN:
            if event.button == 4 and pl_y > pl_vel:
                pl_y -= pl_vel
            elif event.button == 5 and pl_y < screen_height - pl_width:
                pl_y += pl_vel

        if event.type == spawn_event:
            pygame.time.set_timer(spawn_event, random.randint(min_time, max_time))
            poops.append(Poop(en_x, en_y+en_yvel+en_height//2, 50, 30))

    en_y += en_yvel
    if en_y <= 0 or en_y >= screen_height - en_height:
        en_yvel =- en_yvel

    for poop in poops[:]:
        poop.update()
        if poop.rect.right <= 0:
            poops.remove(poop)

    screen.fill((0, 0, 0))
    for poop in poops:
        poop.draw(screen)
    pygame.draw.rect(screen, (105, 255, 125), (pl_x, pl_y, pl_width, pl_height))
    pygame.draw.rect(screen, (255, 125, 115), (en_x, en_y, en_width, en_height))
    pygame.display.update()

pygame.quit()

0
你可以使用pygame的time模块来随机生成敌人。我假设你正在使用面向对象编程(OOP)来实现这个功能。首先,在初始化敌人类时记录它第一次生成的时间。
class Enemy:
    def __init__(self):
        self.start = time.time()
        # other code

然后你可以计算自敌人上次生成以来经过的时间。在你的主游戏循环中,你可以像这样做:now = time.time()并获取差异。

enemy = Enemy()
while True:
    now = time.time()
    time_passed = now - enemy.start()

现在你可以将这个time_passed作为参数传递给你可能创建的spawn_enemy()函数,它可能会像这样:

def spawn(self, t):
   counter = t % random.randint(1, 10)
   if counter >= 0 and counter <=  0.2:
       #spawn enemy

将此函数称为spawn(time_passed)进行调用。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接