JavaScript
35
1
import pygame
2
import player
3
4
play = player.player()
5
pygame.init()
6
time = pygame.time.Clock()
7
key = 0
8
move = ""
9
List =[]
10
pygame.display.set_mode((100, 100))
11
while True:
12
for event in pygame.event.get():
13
if event.type == pygame.KEYDOWN:
14
time_down = pygame.time.get_ticks()
15
if event.key == pygame.K_LEFT:
16
move = "l"
17
if event.key == pygame.K_RIGHT:
18
move = "r"
19
if event.key == pygame.K_DOWN:
20
move = "d"
21
print('UNTEN')
22
if event.key == pygame.K_UP:
23
move = "u"
24
if event.key == pygame.K_ESCAPE:
25
break
26
else:
27
continue
28
key += 1
29
play.handlerevent(event)
30
if event.type == pygame.KEYUP:
31
time_elapsed = (pygame.time.get_ticks() - time_down) / 1000.0
32
print("Nummer: ", key, "Zeit: ", time_elapsed)
33
tmp = (move, key)
34
List.extend(tmp)
35
Hello I’m new here and would like to know, why my for-loop doesn’t react to the continue. It goes into the else branch. But just ignores the continue.
Advertisement
Answer
I suspect you have it backwards: continue
is being executed when it shouldn’t be.
You need to use elif
for your sequence of conditions. Your else:
block is only associated with the last if
condition. So if the key is K_LEFT
it will go into the else:
block and continue, instead of executing the rest of the loop.
JavaScript
14
1
if event.key == pygame.K_LEFT:
2
move = "l"
3
elif event.key == pygame.K_RIGHT:
4
move = "r"
5
elif event.key == pygame.K_DOWN:
6
move = "d"
7
print('UNTEN')
8
elif event.key == pygame.K_UP:
9
move = "u"
10
elif event.key == pygame.K_ESCAPE:
11
break
12
else:
13
continue
14
This way, the else:
block will be executed only when none of the if
conditions succeeded.