如何解决Pygame 在点击之间重置计时器
我正在尝试做一个小型学校项目,它非常简单,基本上,您要做的就是单击屏幕上随机出现的甜甜圈,每次单击都会给出一个点,直到那里一切正常,我尝试做一个计时器每次点击甜甜圈时都会重置,因此基本上,每次点击之间大约有 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)
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
模块并创建一个pygame
窗口:
pygame.init()
wn = pygame.display.set_mode((600,600))
- 定义最基本的
Button
类作为点击对象的示例:
class Button:
def __init__(self):
self.rect = pygame.Rect(250,self.rect)
- 从上面定义的类创建一个
Button
:
button = Button()
- 将变量
t
定义为分数,将变量score
定义为当前时间(以秒为单位):
score = 0
t = time()
- 在
while
循环中,检查while
循环迭代期间的当前时间是否比定义的t
变量大 1.5 秒以上。如果是,则从1
变量中递减score
并将t
变量重置为当前时间:
while True:
if time() - t > 1.5:
score -= 1
t = time()
- 使用
for
循环遍历pygame
事件以检查退出事件:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
- 如果按钮被点击,将
score
变量增加1
并将t
变量重置为当前时间:
elif event.type == pygame.MOUSEBUTTONDOWN:
if button.clicked(event.pos):
score += 1
t = time()
- 最后,绘制按钮,并打印分数以查看它是否正常工作:
wn.fill((255,255))
button.draw()
pygame.display.update()
print(score)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。