All you have to do is evaluate that the speed is 0 when a key is pressed:
for event in pygame.event.get():
# check for closing window
if event.type == pygame.QUIT:
if self.playing:
self.playing = False
self.running = False
if event.type == pygame.KEYDOWN:
if self.player.speedx == 0 and self.player.speedy == 0:
if event.key == pygame.K_LEFT:
self.player.speedx = -48
if event.key == pygame.K_RIGHT:
self.player.speedx = 48
if event.key == pygame.K_UP:
self.player.speedy = -48
if event.key == pygame.K_DOWN:
self.player.speedy = 48
# [...]
Another option is to reset the speed when the a key is pressed:
for event in pygame.event.get():
# check for closing window
if event.type == pygame.QUIT:
if self.playing:
self.playing = False
self.running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.player.speedx = -48
self.player.speedy = 0
if event.key == pygame.K_RIGHT:
self.player.speedx = 48
self.player.speedy = 0
if event.key == pygame.K_UP:
self.player.speedx = 0
self.player.speedy = -48
if event.key == pygame.K_DOWN:
self.player.speedx = 0
self.player.speedy = 48
The two implementations behave differently. While the first implementation does not allow you to change direction while a key is pressed, the second implementation changes direction immediately. Try both and choose the one that suits your needs.