我正在尝试做一个小型学校项目,它非常简单,基本上,您所做的就是单击屏幕上随机出现的甜甜圈,每次单击都会给出一个点,直到那里一切正常,我尝试做一个计时器,它将重置当您每次单击甜甜圈时,基本上,每次单击之间大约有 1.5 秒的时间,如果时间用完,您将失去生命,但我不知道如何实现在单击甜甜圈之间运行的计时器并在每次单击时重置,我在整个互联网上进行了搜索,发现没有人可以帮助。
donut_width, donut_height = 110, 95
score = 0
lives = 4
class Donut:
def __init__(self, x, y):
self.donut_original = pygame.image.load(os.path.join('icon.png'))
self.donutImg = pygame.transform.scale(self.donut_original, (donut_width, donut_height))
self.donut = self.donutImg.get_rect()
self.donut.x = x
self.donut.y = y
def draw(self):
screen.blit(self.donutImg, self.donut)
def collision(donut1, mouse):
return donut1.collidepoint(mouse)
donut = Donut(width//2, height//2)
def graphics():
screen.fill(uwred)
donut.draw()
text_score = pygame.font.SysFont('comicsans', 80).render('SCORE: ' + str(score), True, white)
screen.blit(text_score, (0, 0))
run = True
out_of_time = False
while run:
mouse_pos = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
if collision(donut.donut, mouse_pos) and event.type == pygame.MOUSEBUTTONDOWN:
donut.donut.x = random.randint(donut_width * 2, width - donut_width * 2)
donut.donut.y = random.randint(donut_height * 2, height - donut_height * 2)
score += 1
graphics()
pygame.display.update()
pygame.quit()