[英]how to pause one script without pausing everithing in a pygame screen
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.我正在使用 pygame.key.get_pressed() 所以很明显为什么会这样做,我只是想设置一个计时器或其他东西来阻止在接下来的几个毫秒内使用相同键的可能性。
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
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.在此处设置一个标志was_pressed
(在您的类的构造函数或任何地方将其初始化为False
),因此除非用户先释放按钮,否则您不会更改任何内容。
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
.我创建了一个包含两组精灵all_sprites
和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.当KEYUP
事件发生时,所有精灵检查它是否是它们的键,并通过将自己删除或添加到drawn_sprites
组来切换它们的可见性。
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.我添加了打印语句,以便您可以看到在控制台上随机选择了哪些键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.