当我按下一个键时,pygame 内部会发生什么?何时使用 pygame.event==KEYDOWN [英] What all things happens inside pygame when I press a key? When to use pygame.event==KEYDOWN
问题描述
我一直在尝试使用 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 {}
".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 {}
".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 {}
".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 {}
".format(checkp_list,checkp_no,dir_))
所有这些都在 while True:
循环中运行.基本上,只要按下箭头键,方向指示器 dir_ 就会发生变化,它会在检查点编号 checkp_no
上加 1 并插入当前的头部位置(head_pos
中的头部对象的head_pos
code>checkp_no 索引位置.但不幸的是,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_list
当 checkp_no
为 2 并且 while
循环开始另一个迭代时(取自创建的日志文件).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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!