1

So, I want to have one part of my code refresh at a certain rate, while the other refreshes instantly.

import pygame
import random

WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
YELLOW = (255,255,0)
PINK = (255,182,193)
INDIGO = (111,0,255)

pygame.init()
pygame.display.set_caption("Game TITLE")
screen = pygame.display.set_mode((400,400))
myText = "Score: 0"
myTextScore = 0
quitVar = True

FPS = 2
fpsClock = pygame.time.Clock()



while quitVar == True:


    screen.fill(WHITE)

    x = (random.randrange(1, 300))
    y = (random.randrange(1, 300))
    red_rect = pygame.draw.rect(screen, RED, (x, y, 100, 100))

    #this is the code that i want to refresh slower
    font = pygame.font.Font('./Roboto-Regular.ttf',20)
    text = font.render(myText, True, GREEN)
    textRect = text.get_rect(center = (200,350))
    screen.blit(text, textRect)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            quitVar = False
                
        if event.type == pygame.MOUSEBUTTONDOWN:
            position = pygame.mouse.get_pos()
            #i want the code below to refresh at a faster rate than what is above
            if red_rect.collidepoint(position):
                myTextScore += 1
                myText = "Score " + str(myTextScore)
        
    pygame.display.update()
    fpsClock.tick(FPS)

pygame.quit()

is there a way for different code fps/refreshing different parts at different rates? The fps i have currently affects EVERYTHING in the loop, but i want it to only affect a certain part, while another part goes faster, and refreshes at 10-20 fps instead of the 2 that the other code does. My GOAL is to have a square that moves around the screen, and that when the user clicks on it, their score updates. I am trying to recreate "Whack-A-Mole"

Pythonyay
  • 25
  • 4

1 Answers1

1

No, there is no way. See also How to run multiple while loops at a time in Python.

If you want to control something over time in Pygame you have two options:

  1. Use pygame.time.get_ticks() to measure time and and implement logic that controls the object depending on the time.

  2. Use the timer event. Use pygame.time.set_timer() to repeatedly create a USEREVENT in the event queue. Change object states when the event occurs.

For examples see Spawning multiple instances of the same object concurrently in python or Adding a particle effect to my clicker game (and many more with a little search).


Minimal example based on your code:

import pygame
import random

WHITE, BLACK = (255,255,255), (0,0,0)
RED, GREEN, BLUE = (255,0,0), (0,255,0), (0,0,255)
YELLOW, PINK, INDIGO = (255,255,0), (255,182,193), (111,0,255)

pygame.init()
pygame.display.set_caption("Game TITLE")
screen = pygame.display.set_mode((400,400))

myTextScore = 0
#font = pygame.font.Font('./Roboto-Regular.ttf',20)
font = pygame.font.SysFont(None, 20)
text = font.render("Score " + str(myTextScore), True, GREEN)

change_event = pygame.USEREVENT
pygame.time.set_timer(change_event, 500) # 0.5 seconds
red_rect = pygame.Rect((random.randrange(1, 300)), (random.randrange(1, 300)), 100, 100)

FPS = 100
fpsClock = pygame.time.Clock()
quitVar = True
while quitVar == True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            quitVar = False

        if event.type == change_event:
            red_rect.x = (random.randrange(1, 300))
            red_rect.y = (random.randrange(1, 300))    

        if event.type == pygame.MOUSEBUTTONDOWN:
            if red_rect.collidepoint(event.pos):
                myTextScore += 1
                text = font.render("Score " + str(myTextScore), True, GREEN)

    screen.fill(WHITE)
    pygame.draw.rect(screen, RED, red_rect)
    screen.blit(text, text.get_rect(center = (200,350)))
    pygame.display.update()
    fpsClock.tick(FPS)

pygame.quit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174