2

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

2 回答 2

2

用于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()
于 2021-02-17T19:57:47.963 回答
2

您可以尝试使用以下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窗口:
pygame.init()
wn = pygame.display.set_mode((600, 600))
  1. 定义最基本的Button类作为对象点击的例子:
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. 从上面定义的类创建一个Button
button = Button()
  1. 将变量 , 定义为t分数,将变量 ,定义score为当前时间(以秒为单位):
score = 0
t = time()
  1. while循环中,检查循环迭代期间的当前时间while是否比t定义的变量大 1.5 秒以上。1如果是这样,从变量递减并将score变量重置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)
于 2021-02-17T20:02:40.550 回答