What all things happens inside pygame when I press a key? When to use pygame.event==KEYDOWN
I have been trying to code a small 2D game using python.
checkp_list[0]=head_pos
pressed_key= pygame.key.get_pressed()
if pressed_key[pygame.K_ESCAPE]:
running=False
if pressed_key[pygame.K_UP]:
if dir_ not in ["up", "down"]:
dir_= "up"
checkp_no= checkp_no+1
#head_pos_next=head_move(head_pos, dir_)
checkp_list.insert(checkp_no,head_pos)
log_file_obj.write("chekp_list {}, checkp_no {} after append dir {}\n".format(checkp_list,checkp_no,dir_))
if pressed_key[pygame.K_DOWN]:
if dir_ not in ["up", "down"]:
dir_= "down"
checkp_no= checkp_no+1
#head_pos_next = head_move(head_pos, dir_)
checkp_list.insert(checkp_no,head_pos)
log_file_obj.write("chekp_list {}, checkp_no {} after append dir {}\n".format(checkp_list,checkp_no,dir_))
if pressed_key[pygame.K_RIGHT]:
if dir_ not in ["left", "right"]:
dir_= "right"
checkp_no= checkp_no+1
#head_pos_next = head_move(head_pos, dir_)
checkp_list.insert(checkp_no,head_pos)
log_file_obj.write("chekp_list {}, checkp_no {} after append dir {}\n".format(checkp_list,checkp_no,dir_))
if pressed_key[pygame.K_LEFT]:
if dir_ not in ["left", "right"]:
dir_= "left"
checkp_no= checkp_no+1
#head_pos_next = head_move(head_pos, dir_)
checkp_list.insert(checkp_no,head_pos)
log_file_obj.write("chekp_list {}, checkp_no {} after append dir {}\n".format(checkp_list,checkp_no,dir_))
All this is running inside a while True:
loop. Basically, whenever an arrow key is pressed, direction indicator dir_ changes and it'll add 1 to checkpoint number checkp_no
and insert current head position(head_pos
of the head object in checkp_no
index position.
But unfortunately, all in the checkp_list
points turns out to be the latest head_pos
. The list checkp_list
is an important factor to implement my logic.
chekp_list
while start [[325, 791], [325, 791], [325, 791]]
,
this is the checkp_list
when checkp_no
is 2 and when while
loop is starting another iteration (taken from log file created).
All the points where checkp_list
is getting appended are above.
Please help me to identify the issue.
pygame.key.get_pressed()
returns a list with the current states of a key. When a key is hold down, the state for the key is True
, else it is False
. Use pygame.key.get_pressed()
to evaluate if a key is continuously pressed.
while True:
pressed_key= pygame.key.get_pressed()
if pressed_key[pygame.K_UP]:
# the code in this condition is executed as long UP is hold down
# [...]
The keyboard events (see pygame.event module) occur only once when the state of a key changes. The KEYDOWN
event occurs once every time a key is pressed. KEYUP
occurs once every time a key is released.
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
# The following code is executed once, every time when ESC is pressed.
# [...]
if event.type == pygame.KEYUP:
if event.key == pygame.K_ESCAPE:
# The following code is executed once, every time when ESC is released.
# [...]