如何在特定时间间隔连续触发动作?敌人在 pygame 中发射恒定光束而不是子弹
How do I continuously trigger an action at certain time intervals? Enemy shoots constant beam instead of bullets in pygame
试图让 pygame 中的敌人在 pygame 中直线发射子弹。我设法让敌人开枪,但它射出的是一束恒定的子弹,而不是将它们隔开。有什么办法可以space出子弹吗?
这是敌人的class
class Boss(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((100, 70))
self.image.fill(white)
self.rect = self.image.get_rect()
self.rect.centerx = WIDTH / 2
self.rect.y = (WIDTH / 2) - 500
self.speedy = 3
def update(self):
self.rect.y += self.speedy
if self.rect.y >= 30:
self.rect.y = 30
def shoot(self):
bossbullet = Bossbullet(self.rect.centerx, self.rect.bottom)
all_sprites.add(bossbullet)
bossbullets.add(bossbullet)
class Bossbullet(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((10, 20))
self.image.fill(white)
self.rect = self.image.get_rect()
self.rect.bottom = y
self.rect.centerx = x
self.speedy = -10
def update(self):
self.rect.y -= self.speedy
if self.rect.bottom < 0:
self.kill()
all_sprites = pygame.sprite.Group()
boss = Boss()
all_sprites.add(boss)
bossbullets = pygame.sprite.Group()
这是游戏运行和敌人射击的循环
running = True
while running:
clock.tick(FPS)
if boss.rect.y >= 30:
boss.shoot()
我推荐使用定时器事件。使用 pygame.time.set_timer()
to repeatedly create an USEREVENT
。例如:
milliseconds_delay = 500 # 0.5 seconds
bullet_event = pygame.USEREVENT + 1
pygame.time.set_timer(bullet_event, milliseconds_delay)
请注意,在 pygame 中可以定义客户事件。每个事件都需要一个唯一的 ID。用户事件的 ID 必须从 pygame.USEREVENT
开始。在这种情况下,pygame.USEREVENT+1
是生成子弹的计时器事件的事件 ID。
在事件循环中发生事件时创建一个新项目符号:
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == bullet_event:
if boss.rect.y >= 30:
boss.shoot()
is there any way to make it so the enemy pauses for a while after..let's say 5 shots, then starts shooting again after the pause
可以通过将 0 传递给时间参数来停止计时器事件。例如:
delay_time = 500 # 0.5 seconds
pause_time = 3000 # 3 seconds
bullet_event = pygame.USEREVENT + 1
pygame.time.set_timer(bullet_event, delay_time)
no_shots = 0
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == bullet_event:
if boss.rect.y >= 30:
boss.shoot()
# change the timer event time
if no_shots == 0:
pygame.time.set_timer(bullet_event, delay_time)
no_shots += 1
if no_shots == 5:
pygame.time.set_timer(bullet_event, pause_time)
no_shots = 0
killed = # [...] set state when killed
# stop timer
if killed:
pygame.time.set_timer(bullet_event, 0)
试图让 pygame 中的敌人在 pygame 中直线发射子弹。我设法让敌人开枪,但它射出的是一束恒定的子弹,而不是将它们隔开。有什么办法可以space出子弹吗?
这是敌人的class
class Boss(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((100, 70))
self.image.fill(white)
self.rect = self.image.get_rect()
self.rect.centerx = WIDTH / 2
self.rect.y = (WIDTH / 2) - 500
self.speedy = 3
def update(self):
self.rect.y += self.speedy
if self.rect.y >= 30:
self.rect.y = 30
def shoot(self):
bossbullet = Bossbullet(self.rect.centerx, self.rect.bottom)
all_sprites.add(bossbullet)
bossbullets.add(bossbullet)
class Bossbullet(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((10, 20))
self.image.fill(white)
self.rect = self.image.get_rect()
self.rect.bottom = y
self.rect.centerx = x
self.speedy = -10
def update(self):
self.rect.y -= self.speedy
if self.rect.bottom < 0:
self.kill()
all_sprites = pygame.sprite.Group()
boss = Boss()
all_sprites.add(boss)
bossbullets = pygame.sprite.Group()
这是游戏运行和敌人射击的循环
running = True
while running:
clock.tick(FPS)
if boss.rect.y >= 30:
boss.shoot()
我推荐使用定时器事件。使用 pygame.time.set_timer()
to repeatedly create an USEREVENT
。例如:
milliseconds_delay = 500 # 0.5 seconds
bullet_event = pygame.USEREVENT + 1
pygame.time.set_timer(bullet_event, milliseconds_delay)
请注意,在 pygame 中可以定义客户事件。每个事件都需要一个唯一的 ID。用户事件的 ID 必须从 pygame.USEREVENT
开始。在这种情况下,pygame.USEREVENT+1
是生成子弹的计时器事件的事件 ID。
在事件循环中发生事件时创建一个新项目符号:
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == bullet_event:
if boss.rect.y >= 30:
boss.shoot()
is there any way to make it so the enemy pauses for a while after..let's say 5 shots, then starts shooting again after the pause
可以通过将 0 传递给时间参数来停止计时器事件。例如:
delay_time = 500 # 0.5 seconds
pause_time = 3000 # 3 seconds
bullet_event = pygame.USEREVENT + 1
pygame.time.set_timer(bullet_event, delay_time)
no_shots = 0
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == bullet_event:
if boss.rect.y >= 30:
boss.shoot()
# change the timer event time
if no_shots == 0:
pygame.time.set_timer(bullet_event, delay_time)
no_shots += 1
if no_shots == 5:
pygame.time.set_timer(bullet_event, pause_time)
no_shots = 0
killed = # [...] set state when killed
# stop timer
if killed:
pygame.time.set_timer(bullet_event, 0)