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.