It's easier to handle allJust iterate over the events inas you retrieve them from the same placequeue, so you don't reuseand handle them right then and there, without keeping a reference to it. You could move the player sprite
You can use a secondary handler in Player class to do the game event loop like this:actual work for keypresses if you want, as show below.
import pygame_sdl2 as pg
# colors
WHITE = (255, 255, 255)
GREY = (100, 100, 100)
class Game:
def __init__(self):
pg.init()
self.screen = pg.display.set_mode((640, 640))
self.clock = pg.time.Clock()
pg.key.set_repeat()
def new(self):
self.all_sprites = pg.sprite.Group()
self.player = Player(self, 100,100)
def run(self):
self.playing = True
while self.playing:
self.clock.tick(60)
self.events()
self.update()
self.draw()
def quit(self):
pg.quit()
def update(self):
self.all_sprites.update()
def draw(self):
self.screen.fill(GREY)
for sprite in self.all_sprites:
self.screen.blit(sprite.image, sprite.rect)
pg.display.flip()
def events(self):
for self.event in pg.event.get():
if self.event.type == pg.QUIT:
self.quit()
if self.event.type == pg.KEYDOWN:
if self.player.handle_keydown( event.key ==)
class Player(pg.K_LEFTsprite.Sprite):
def __init__(self, game, x, y):
self.player.pos[0]groups -= 1game.all_sprites
printpg.sprite.Sprite.__init__("This should only be printed once per keyself, press"self.groups)
self.game = game
elif self.event.keyimage === pg.K_RIGHT:Surface((32,32))
self.image.fill(WHITE)
self.rect = self.playerimage.pos[0]get_rect()
+= 1
self.pos = [x, y]
def handle_keydown( self, key ) :
print("This should only be printed once perif key press")
class== Player(pg.sprite.Sprite)K_LEFT:
def __init__(self, game, x, y):
self.groupspos[0] -= game.all_sprites1
pg.sprite.Sprite.__init__(self, self.groups)
print("This should only be printed once self.gameper =key gamepress")
self.imageelif =key == pg.Surface((32,32))K_RIGHT:
self.image.fill(WHITE)
pos[0] += 1
self.rect = self.image.get_rect()
print("This should only be self.posprinted =once [x,per y]key press")
def update(self):
self.rect.centery = self.pos[1]
self.rect.centerx = self.pos[0]
g = Game()
while True:
g.new()
g.run()
If you really want to handle key presses in Player that is fine too. Just add a handler func and pass in the event (or key) from Game.