Python Pygame在点击之间重置计时器
我试着做一个小的学校项目,非常简单,基本上,你所做的就是点击屏幕上随机出现的甜甜圈,每次点击都会给出一个点,一切都正常,直到那里,我试着做一个计时器,每次点击甜甜圈时会重置,所以基本上,每次点击之间有大约1.5秒的间隔,如果时间不够,你就会失去生命,但我不知道如何实现一个计时器,在每次点击甜甜圈之间运行,并在每次点击时重置。我在互联网上搜索了所有内容,没有找到任何人可以提供帮助Python Pygame在点击之间重置计时器,python,timer,pygame,pygame-tick,Python,Timer,Pygame,Pygame Tick,我试着做一个小的学校项目,非常简单,基本上,你所做的就是点击屏幕上随机出现的甜甜圈,每次点击都会给出一个点,一切都正常,直到那里,我试着做一个计时器,每次点击甜甜圈时会重置,所以基本上,每次点击之间有大约1.5秒的间隔,如果时间不够,你就会失去生命,但我不知道如何实现一个计时器,在每次点击甜甜圈之间运行,并在每次点击时重置。我在互联网上搜索了所有内容,没有找到任何人可以提供帮助 donut_width, donut_height = 110, 95 score = 0 lives = 4 c
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.init()
后的毫秒数。设置新油炸圈饼出现时的开始时间。计算当前时间和开始时间之间的差值。如果差异超过限制,则减少生命数:
lifes=4
start\u time=pygame.time.get\u ticks()
运行=真
运行时:
当前时间=pygame.time.get_ticks()
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
pygame.quit()
如果event.type==pygame.MOUSEBUTTONDOWN和collision(donut.donut,event.pos),并且:
甜甜圈.donut.x=random.randint(甜甜圈宽度*2,宽度-甜甜圈宽度*2)
donut.donut.y=random.randint(甜甜圈高度*2,高度-甜甜圈高度*2)
分数+=1
开始时间=当前时间
增量时间=当前时间-开始时间
如果增量时间>1500:#1.5秒
寿命-=1
开始时间=当前时间
打印(“生活:”,生活)
图形()
pygame.display.update()
您可以尝试使用以下方法:
说明:
pygame
模块并创建pygame
窗口:按钮
类作为要单击的对象的示例:按钮
:t
定义为分数,将变量score
定义为以秒为单位的当前时间:while
循环中,检查while
循环迭代期间的当前时间是否比定义的t
变量大1.5秒以上。如果是,则从score
变量中减量1
,并将t
变量重置为当前时间:for
循环来循环pygame
事件以检查退出事件:得分
变量增加1
,并将t
变量重置为当前时间:谢天谢地,它真的很管用,你解释得很详细,你是一个上帝,避免混合不同的库。Pygame提供了该模块。所以请使用它,但不要另外使用
时间模块。@Rabbid76哦,谢谢@从技术上讲,这没有什么区别。然而,当不同模块的数量较少时,应用程序更易于维护,也不那么复杂。
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)
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)