I'm trying to do a small school project it's very simple and basically, all you do is click on randomly appearing donuts on the screen each click gives a point and everything is ok up until there, I tried to do a timer that will reset when you click on a donut each time so basically, you have around 1.5 seconds between each click and if you run out of time you lose a life but I can't figure out how to implement a timer that will run between clicks on the donut and reset each time you click, I searched all across the internet and found nothing can someone please help.
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()
You can try using the time.time()
method:
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)
Explanation:
import pygame
from time import time
pygame
module and create a pygame
window:pygame.init()
wn = pygame.display.set_mode((600, 600))
Button
class as an example for the object to click: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
from the class defined above:button = Button()
t
, to be the score, and a variable, score
, to be the current time in seconds:score = 0
t = time()
while
loop, check if the current time during that iteration of the while
loop is more than 1.5 seconds greater than the t
variable defined. If so, decrement 1
from the score
variable and reset the t
variable to be the current time:while True:
if time() - t > 1.5:
score -= 1
t = time()
for
loop to loop over the pygame
events to check for an exit event: for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
score
variable by 1
and reset the t
variable to be the current time: 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)