Pygame 重置点击之间的计时器

Pygame Resetting Timer between Clicks

我正在尝试做一个小型学校项目,它非常简单,基本上,您只需点击屏幕上随机出现的甜甜圈,每次点击都会给出一个分数,直到那里一切正常,我尝试做一个计时器,当你每次点击一个甜甜圈时都会重置所以基本上,你每次点击之间有大约 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()

使用 pygame.time.get_ticks 获取自调用 pygame.init() 以来的毫秒数。
设置新甜甜圈出现的开始时间。计算当前时间和开始时间之间的差值。如果差异超过限制,则减少生命数:

lives = 4
start_time = pygame.time.get_ticks()

run = True
while run:
    current_time = pygame.time.get_ticks()

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

        if event.type == pygame.MOUSEBUTTONDOWN and collision(donut.donut, event.pos) and :
            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
            start_time = current_time

    delta_time = current_time - start_time
    if delta_time > 1500: # 1.5 sceonds
        lives -= 1
        start_time = current_time
        print("lives:", lives)

    graphics()
    pygame.display.update()

您可以尝试使用time.time()方法:

import pygame
from time import time

pygame.init()
wn = pygame.display.set_mode((600, 600))

class Button:
    def __init__(self):
        self.rect = pygame.Rect(250, 250, 100, 100)
        self.color = (255, 0, 0)
    def clicked(self, pos):
        return self.rect.collidepoint(pos)
    def draw(self):
        pygame.draw.rect(wn, self.color, self.rect)

button = Button()

score = 0
t = time()
while True:
    if time() - t > 1.5:
        score -= 1
        t = time()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if button.clicked(event.pos):
                score += 1
                t = time()
                    
    wn.fill((255, 255, 255))
    button.draw()
    pygame.display.update()
    print(score)

解释:

  1. 导入必要的模块和功能:
import pygame
from time import time
  1. 初始化pygame模块并创建pygame window:
pygame.init()
wn = pygame.display.set_mode((600, 600))
  1. 定义最基本的Buttonclass作为对象点击的例子:
class Button:
    def __init__(self):
        self.rect = pygame.Rect(250, 250, 100, 100)
        self.color = (255, 0, 0)
    def clicked(self, pos):
        return self.rect.collidepoint(pos)
    def draw(self):
        pygame.draw.rect(wn, self.color, self.rect)
  1. 从上面定义的 class 创建一个 Button
button = Button()
  1. 定义一个变量 t 作为分数,定义一个变量 score 作为当前时间(以秒为单位):
score = 0
t = time()
  1. while 循环中,检查 while 循环迭代期间的当前时间是否比定义的 t 变量长 1.5 秒以上。如果是这样,从 score 变量中减去 1 并将 t 变量重置为当前时间:
while True:
    if time() - t > 1.5:
        score -= 1
        t = time()
  1. 使用 for 循环遍历 pygame 事件以检查退出事件:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
  1. 如果单击按钮,将 score 变量增加 1 并将 t 变量重置为当前时间:
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if button.clicked(event.pos):
                score += 1
                t = time()
  1. 最后,绘制按钮,并打印乐谱以查看效果:
    wn.fill((255, 255, 255))
    button.draw()
    pygame.display.update()
    print(score)