how to pause one script without pausing everithing in a pygame screen

Viewed 32

i want to make a sprite and his rect disapear by pressing a key and reapear them by pressing the same key. but when i hold the key it disapear and reapear. im using pygame.key.get_pressed() so it's obvious why it does this, i just want to set a timer or something stoping the possibility of using the same key in the next couple milisecs.

def update(self):
    keys = pygame.key.get_pressed()

    if keys[pygame.key.key_code(self.letre)]:
        if self.compteur == 0:
            self.rect = pygame.Rect(-500,-500,-500,-500)
            self.compteur = 1
        elif self.compteur == 1:
            self.rect = self.image.get_rect(topleft = self.pos)
            self.compteur = 0
2 Answers

Set a flag, was_pressed here (initialize it to False in your class's constructor or wherever), so you don't change things unless the user releases the button first.

def update(self):
    keys = pygame.key.get_pressed()

    if keys[pygame.key.key_code(self.letre)]:  # key is down?
        if not self.was_pressed:  # key has not been down last frame?
            self.was_pressed = True  # well it is now!
            if self.compteur == 0:
                self.rect = pygame.Rect(-500,-500,-500,-500)
                self.compteur = 1
            else:
                self.rect = self.image.get_rect(topleft = self.pos)
                self.compteur = 0
    else:
        self.was_pressed = False  # okay, key no longer down

If you want to ensure a single key press doesn't continuously toggle your sprite visibility, you could change to an event based approach.

I've created a minimal example with two groups of sprites, all_sprites and drawn_sprites. When a KEYUP event occurs, all the sprites check if it was their key and toggle their visibility by removing or adding themselves to the drawn_sprites group.

import pygame
import random
import string

screen = pygame.display.set_mode((500, 500))
pygame.init()
all_sprites = pygame.sprite.Group()
drawn_sprites = pygame.sprite.Group()


class Block(pygame.sprite.Sprite):
    def __init__(self, size, pos):
        pygame.sprite.Sprite.__init__(self)
        self.size = size
        self.image = pygame.Surface([size[0], size[1]])
        self.image.fill(pygame.Color("blueviolet"))
        self.rect = self.image.get_rect()
        self.rect.center = pos
        self.secret = random.choice(string.ascii_lowercase)

    def update(self, key_pressed=None):
        """ Toggle visibility if my key was pressed"""
        if key_pressed == self.secret:
            if self in drawn_sprites:
                drawn_sprites.remove(self)
            else:
                drawn_sprites.add(self)


for _ in range(5):
    # create a randomly sized block in a random position
    block = Block((random.randint(40, 100), random.randint(40, 100)),
                  (random.randint(100, 400), random.randint(100, 400)))
    drawn_sprites.add(block)
    all_sprites.add(block)
    print(f"{block.secret} ", end="")
print()

run = True
clock = pygame.time.Clock()
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        elif event.type == pygame.KEYUP:
            all_sprites.update(event.unicode)

    screen.fill(pygame.Color("white"))
    # sprite_list.update()
    drawn_sprites.draw(screen)
    pygame.display.update()
    clock.tick(60)  # limit to 60 FPS

pygame.quit()

I added the print statements so you can see which keys have been randomly chosen on the console.

Related