Python 如何在一个时间限制后产生一个精灵,以及如何在游戏中显示一个计时器
我想在一段时间过去后,或者已经产生了x个怪物,我怎么能在屏幕上显示计时器,然后再产生一个“老板”精灵 给老板上课Python 如何在一个时间限制后产生一个精灵,以及如何在游戏中显示一个计时器,python,pygame,Python,Pygame,我想在一段时间过去后,或者已经产生了x个怪物,我怎么能在屏幕上显示计时器,然后再产生一个“老板”精灵 给老板上课 class Boss(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image = pygame.transform.scale(boss_img, (150, 200)) self.image.set_colorkey(Black)
class Boss(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.transform.scale(boss_img, (150, 200))
self.image.set_colorkey(Black)
self.rect = self.image.get_rect()
self.rect.center = ((Width / 2, -70))
self.speedy = 1
self.shoot_delay = 250
self.last_shot = pygame.time.get_ticks()
self.hp = 150
self.dead = False
def update(self):
if self.hp <= 25:
self.speedy = 3
if self.rect.top > Height + 10:
player.lives = 0
class Boss(pygame.sprite.sprite):
定义初始化(自):
pygame.sprite.sprite.\uuuuu init\uuuuuuu(自我)
self.image=pygame.transform.scale(boss_img,(150200))
self.image.set_颜色键(黑色)
self.rect=self.image.get_rect()
self.rect.center=((宽度/2,-70))
self.speeded=1
自拍照延迟=250
self.last\u shot=pygame.time.get\u ticks()
self.hp=150
self.dead=False
def更新(自我):
如果self.hp高度+10:
player.lifes=0
在pygame中有几种实现计时器的方法。您可以使用pygame.Clock.tick
返回的时间来增加或减少计时器变量,使用pygame.time.get_ticks
计算时差,或者将自定义事件与pygame.time.set_timer
结合使用
示例1-增量时间:
import sys
import random
import pygame as pg
class Block(pg.sprite.Sprite):
def __init__(self, pos):
super().__init__()
self.image = pg.Surface((40, 40))
self.image.fill(pg.Color('sienna1'))
self.rect = self.image.get_rect(topleft=pos)
def main():
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
font = pg.font.Font(None, 30)
all_sprites = pg.sprite.Group()
# Delta time is the time that has passed since clock.tick
# was called the last time.
dt = 0
# We'll subtract dt (delta time) from this timer variable.
timer = 1 # 1 means one second.
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
# Decrease timer to get a countdown.
timer -= dt
# When the timer is below or equal to 0, we spawn
# a new block.
if timer <= 0:
all_sprites.add(Block((random.randrange(600),
random.randrange(440))))
# Reset the countdown timer to one second.
timer = 1
all_sprites.update()
screen.fill(pg.Color('gray15'))
all_sprites.draw(screen)
timer_surface = font.render(
str(round(timer, 3)), True, pg.Color('yellow'))
screen.blit(timer_surface, (20, 20))
pg.display.flip()
# dt = time in seconds that passed since last tick.
# Divide by 1000 to convert milliseconds to seconds.
dt = clock.tick(30) / 1000
if __name__ == '__main__':
pg.init()
main()
pg.quit()
sys.exit()
或者在繁殖后将计时器设置为0,只有在!=0
if timer != 0:
timer -= dt
if timer <= 0:
all_sprites.add(Block((random.randrange(600),
random.randrange(440))))
timer = 0
如果你只想计算杀死或繁殖的怪物的数量,你可以增加一个计数器变量,然后当它超过某个限制时繁殖敌人boss。下面的示例仅计算鼠标单击次数,并在单击3次后生成一个块
def main():
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
font = pg.font.Font(None, 30)
all_sprites = pg.sprite.Group()
# We'll just count mouse clicks in this example.
# You can replace it with the kill count in your game.
clicks = 0
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
if event.type == pg.MOUSEBUTTONDOWN:
clicks += 1
if clicks >= 3:
all_sprites.add(Block((random.randrange(600),
random.randrange(440))))
clicks = 0
all_sprites.update()
screen.fill(pg.Color('gray15'))
all_sprites.draw(screen)
clicks_surface = font.render(str(clicks), True, pg.Color('yellow'))
screen.blit(clicks_surface, (20, 20))
pg.display.flip()
clock.tick(30)
在pygame中有几种实现计时器的方法。您可以使用
pygame.Clock.tick
返回的时间来增加或减少计时器变量,使用pygame.time.get_ticks
计算时差,或者将自定义事件与pygame.time.set_timer
结合使用
示例1-增量时间:
import sys
import random
import pygame as pg
class Block(pg.sprite.Sprite):
def __init__(self, pos):
super().__init__()
self.image = pg.Surface((40, 40))
self.image.fill(pg.Color('sienna1'))
self.rect = self.image.get_rect(topleft=pos)
def main():
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
font = pg.font.Font(None, 30)
all_sprites = pg.sprite.Group()
# Delta time is the time that has passed since clock.tick
# was called the last time.
dt = 0
# We'll subtract dt (delta time) from this timer variable.
timer = 1 # 1 means one second.
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
# Decrease timer to get a countdown.
timer -= dt
# When the timer is below or equal to 0, we spawn
# a new block.
if timer <= 0:
all_sprites.add(Block((random.randrange(600),
random.randrange(440))))
# Reset the countdown timer to one second.
timer = 1
all_sprites.update()
screen.fill(pg.Color('gray15'))
all_sprites.draw(screen)
timer_surface = font.render(
str(round(timer, 3)), True, pg.Color('yellow'))
screen.blit(timer_surface, (20, 20))
pg.display.flip()
# dt = time in seconds that passed since last tick.
# Divide by 1000 to convert milliseconds to seconds.
dt = clock.tick(30) / 1000
if __name__ == '__main__':
pg.init()
main()
pg.quit()
sys.exit()
或者在繁殖后将计时器设置为0,只有在!=0
if timer != 0:
timer -= dt
if timer <= 0:
all_sprites.add(Block((random.randrange(600),
random.randrange(440))))
timer = 0
如果你只想计算杀死或繁殖的怪物的数量,你可以增加一个计数器变量,然后当它超过某个限制时繁殖敌人boss。下面的示例仅计算鼠标单击次数,并在单击3次后生成一个块
def main():
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
font = pg.font.Font(None, 30)
all_sprites = pg.sprite.Group()
# We'll just count mouse clicks in this example.
# You can replace it with the kill count in your game.
clicks = 0
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
if event.type == pg.MOUSEBUTTONDOWN:
clicks += 1
if clicks >= 3:
all_sprites.add(Block((random.randrange(600),
random.randrange(440))))
clicks = 0
all_sprites.update()
screen.fill(pg.Color('gray15'))
all_sprites.draw(screen)
clicks_surface = font.render(str(clicks), True, pg.Color('yellow'))
screen.blit(clicks_surface, (20, 20))
pg.display.flip()
clock.tick(30)
您是否也要上载图像/声音,以便代码可以在执行时不会因为缺少图像/声音而出错?这样可以更容易地查看…已编辑添加的imgur相册,但尚未添加声音。是否还要上载图像/声音,以便代码可以在执行时不会因缺少图像/声音而出错?这使它更容易查看…编辑添加的imgur专辑,尚未添加声音。