2

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()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Irkl1_
  • 71
  • 2
  • 8

2 Answers2

2

Use pygame.time.get_ticks to get the number of milliseconds since pygame.init() was called.
Set the start time when a new donut appears. Calculate the difference between the current time and the start time. Decrease the number of lives if the difference exceeds the limit:

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()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
2

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:

  1. Import the necessary modules and function:
import pygame
from time import time
  1. Initialize the pygame module and create a pygame window:
pygame.init()
wn = pygame.display.set_mode((600, 600))
  1. Define the most basic 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)
  1. Create a Button from the class defined above:
button = Button()
  1. Define a variable, t, to be the score, and a variable, score, to be the current time in seconds:
score = 0
t = time()
  1. In the 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()
  1. Use a 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()
  1. If the button is clicked, increment the 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()
  1. Finally, draw the button, and print the score to see it working:
    wn.fill((255, 255, 255))
    button.draw()
    pygame.display.update()
    print(score)
Red
  • 26,798
  • 7
  • 36
  • 58
  • Thanks man it actually worked and you explained it very well in details you are a god – Irkl1_ Feb 18 '21 at 12:48
  • Avoid mixing different libraries. Pygame provides the [`pygame.time`](https://www.pygame.org/docs/ref/time.html) module. So use it, but don't use additionally the `time` module. – Rabbid76 Feb 18 '21 at 12:55
  • 1
    @AnnZen Technically it doesn't make a difference. However, applications are more maintainable and less complex when the number of different modules is small. – Rabbid76 Feb 18 '21 at 13:07