我正在制作一个小型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()
答案 0 :(得分:2)
如果要管理多个“便便”,则必须创建一个列表。而且每个“便便”都是一个对象(class的实例)。
创建一个类Poop
,该类可以update
的位置和draw
的便便:
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()
答案 1 :(得分:0)
您可以使用pygame的时间模块随机生成敌人。我假设您为此使用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)