问题描述
我一直在尝试使用python编写一个小型2D游戏.
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_))
所有这些都在True:循环的中运行.基本上,每当按下箭头键时,方向指示器dir_都会更改,它将在检查点编号
code> checkp_no 索引位置.但是不幸的是,所有 checkp_no
上加1,然后在 head_pos checkp_list
点中的所有内容都是最新的 head_pos
.列表 checkp_list
是实现我的逻辑的重要因素.
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
,同时启动 [[325,791],[325,791],[325,791]]
,这是当 checkp_no
为2且 while
循环开始另一个迭代(取自创建的日志文件)时的 checkp_list
.上面附有 checkp_list
的所有要点.请帮助我确定问题所在.
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()
返回包含键当前状态的列表.按住某个键时,该键的状态为 True
,否则为 False
.使用 pygame.key.get_pressed()
评估是否连续按下某个键.
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
# [...]
键盘事件(请参阅 pygame.event 模块)仅发生一次当键的状态改变时.每次按下一个键,都会发生一次 KEYDOWN
事件. KEYUP
会在每次释放键时发生一次.
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.
# [...]
这篇关于当我按下一个键时,pygame内部发生了什么?何时使用pygame.event == KEYDOWN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!