Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Pygame事件:在不释放密钥的情况下触发向上键事件_Pygame_Keyboard_Keydown_Keyup - Fatal编程技术网

Pygame事件:在不释放密钥的情况下触发向上键事件

Pygame事件:在不释放密钥的情况下触发向上键事件,pygame,keyboard,keydown,keyup,Pygame,Keyboard,Keydown,Keyup,我正在尝试做一个俄罗斯方块游戏,下面是我的代码,让方块移动。我希望块在按下箭头键时保持移动,在释放时停止。但是当我运行代码时,即使没有释放密钥,它也会停止块 def set_keyboard_dirs(cur_block, width, height, events): # events stores pygame.event.get() as I use it multiple times in my code global move_timer # only moves

我正在尝试做一个俄罗斯方块游戏,下面是我的代码,让方块移动。我希望块在按下箭头键时保持移动,在释放时停止。但是当我运行代码时,即使没有释放密钥,它也会停止块

   def set_keyboard_dirs(cur_block, width, height, events): # events stores pygame.event.get() as I use it multiple times in my code
        global move_timer # only moves block one time per set number of frames
        cur_block.reset_collisions(height)
        for event in events:
            if event.type == pygame.KEYDOWN:  # sets magnitude and direction of movement
                if event.key == pygame.K_RIGHT:
                    cur_block.dir = (cur_block.length, 0)
                elif event.key == pygame.K_LEFT:
                    cur_block.dir = (-cur_block.length, 0)
                elif event.key == pygame.K_DOWN:
                    cur_block.dir = (0, cur_block.length)
                elif event.key == pygame.K_UP:
                    cur_block.rotate(height, width)
                elif event.key == pygame.K_SPACE: # just moves the block instantly to the bottom of the screen by equating it to a 'projection' already there. 
                    cur_block.shape = deepcopy(cur_block.projection)
            elif event.type == pygame.KEYUP:  # stops block from moving
                print(event.key) # I called this to see what's happening, and event.key is printed event when I didn't release the key. 
                cur_block.dir = (0, 0)

        cur_block.move()
由于上述原因,积木一次只移动一步,而不是像我所希望的那样连续移动(只要他们按住它)。我怎样才能修好它?游戏的其余部分都很有效,所以我真的希望这也能起作用。先谢谢你

编辑: 我还尝试使用pygame.key.get_pressed()设置控件,如下所示:

def set_keyboard_dirs(cur_block, width, height):
    global move_timer, keys
    cur_block.reset_collisions(height)
    cur_block.dir = (0, 0)
    if keys[pygame.K_UP] or keys[pygame.K_w]:
        cur_block.rotate(height, width)
    elif keys[pygame.K_SPACE]:
        cur_block.shape = deepcopy(cur_block.projection)
    elif (keys[pygame.K_DOWN] or keys[pygame.K_s]) and 2 not in cur_block.collisions: # 1, 2, 3 are collisions in left, down and right directions
        cur_block.dir = (0, cur_block.length)
    elif (keys[pygame.K_RIGHT] or keys[pygame.K_d]) and 1 not in cur_block.collisions:
        cur_block.dir = (cur_block.length, 0)
    elif (keys[pygame.K_LEFT] or keys[pygame.K_a]) and 3 not in cur_block.collisions:
        cur_block.dir = (-cur_block.length, 0)
    else:
        print('ran reset') # this statement print even as i'm holding down a key for some reason
        cur_block.dir = (0, 0)
    if cur_block.dir != (0, 0) and move_timer == 0:
        cur_block.move()
        move_timer = 7

在前一种情况下,如果我删除KEYUP事件,在后一种情况下,如果我删除else语句,则块会不断移动(但不能停止),这也表明这些语句是导致问题的原因,我认为。这是我的代码中唯一定义cur_block.dir的地方

这可能是因为for循环每帧运行1次,所以如果您按住按钮,它将在该特定帧按1次计数,与KEYUP事件相同。 您可以尝试在mainloop之外设置两个变量left=False和right=False 在当前for循环之外,检查是否按下了任何按钮

#these are outside of the mainloop
left=False
right=False



keys = pygame.key.get_pressed()  # checks for key events (put this outside of the for loop, but still be inside your mainloop)
if keys[pygame.K_LEFT]:
    right = False
    left = True
elif keys[pygame.K_RIGHT]:
    left = False
    right = True

# if none of those buttons pressed, it sets the values back to False
else:
    right = False
    left = False

# using the left value, if its true, it will do your action every frame, 
#(you can adjust the speed of your block later)

if left:
    #  do something
    cur_block.dir = (-cur_block.length, 0)
if right:
    # do something else


然后,如果你按LEFT键,LEFT的值将为真,然后你可以检查:如果这些值中有任何一个为真>做点什么


根据您的示例代码,这是我唯一能想到的。我希望它能起作用。

这可能是因为for循环每帧运行1次,所以如果您按住按钮,它将计为您在该特定帧中按了1次,与KEYUP事件相同。 您可以尝试在mainloop之外设置两个变量left=False和right=False 在当前for循环之外,检查是否按下了任何按钮

#these are outside of the mainloop
left=False
right=False



keys = pygame.key.get_pressed()  # checks for key events (put this outside of the for loop, but still be inside your mainloop)
if keys[pygame.K_LEFT]:
    right = False
    left = True
elif keys[pygame.K_RIGHT]:
    left = False
    right = True

# if none of those buttons pressed, it sets the values back to False
else:
    right = False
    left = False

# using the left value, if its true, it will do your action every frame, 
#(you can adjust the speed of your block later)

if left:
    #  do something
    cur_block.dir = (-cur_block.length, 0)
if right:
    # do something else


然后,如果你按LEFT键,LEFT的值将为真,然后你可以检查:如果这些值中有任何一个为真>做点什么


根据您的示例代码,这是我唯一能想到的。我希望它能起作用。

在您的代码中,如果释放任何键,块的移动将被取消,而不是释放特殊键。如果松开一个光标键,则必须实现特殊情况:

elif event.type==pygame.KEYUP:
如果event.key==pygame.K_RIGHT:
# [...]
elif event.key==pygame.K_左:
# [...]
# [...]
我建议根据以下返回的键状态设置移动方向:

def设置键盘指令(当前块、宽度、高度、事件):
全局移动计时器
当前块。重置碰撞(高度)
keys=pygame.key.get_pressed()
cur_块=[0,0]
如果键[pygame.K_RIGHT]:
cur_block.dir[0]+=cur_block.length
如果键[pygame.K_左]:
cur_block.dir[0]=cur_block.length
如果键[pygame.K_DOWN]:
cur_block.dir[1]+=cur_block.length
如果键[pygame.K_UP]:
当前块旋转(高度、宽度)
如果键[pygame.K_SPACE]:
cur_block.shape=deepcopy(cur_block.projection)
cur_block.move()

在您的代码中,如果释放任何键,则会取消块的移动,而不是释放特殊键。如果松开一个光标键,则必须实现特殊情况:

elif event.type==pygame.KEYUP:
如果event.key==pygame.K_RIGHT:
# [...]
elif event.key==pygame.K_左:
# [...]
# [...]
我建议根据以下返回的键状态设置移动方向:

def设置键盘指令(当前块、宽度、高度、事件):
全局移动计时器
当前块。重置碰撞(高度)
keys=pygame.key.get_pressed()
cur_块=[0,0]
如果键[pygame.K_RIGHT]:
cur_block.dir[0]+=cur_block.length
如果键[pygame.K_左]:
cur_block.dir[0]=cur_block.length
如果键[pygame.K_DOWN]:
cur_block.dir[1]+=cur_block.length
如果键[pygame.K_UP]:
当前块旋转(高度、宽度)
如果键[pygame.K_SPACE]:
cur_block.shape=deepcopy(cur_block.projection)
cur_block.move()

Hi,我也尝试过先使用pygame.key.get_pressed(),但由于某些原因,else语句每次都会运行,即使我按的是正确的say键。我将在顶部编辑我的问题,以显示您希望我以前做过的事情。Idk为什么会发生这种情况,我以前从未遇到过。嗨,我也尝试过先使用pygame.key.get_pressed(),但由于某些原因,else语句每次都会运行,即使我按的是正确的say键。我将在顶部编辑我的问题,以显示您希望我以前做过的事情。Idk为什么会发生这种情况,我以前从未遇到过。如果我尝试pygame.key.get_pressed(),我会遇到类似的问题。我编辑了上面的评论来告诉你我的意思。之前我在主循环中调用它以在每一帧中得到它,但即使我尝试像你所说的那样在函数中调用它,块仍然在我每次按住键时只移动一步,而不是连续移动。这很奇怪,因为我以前使用pygame.key.get_pressure()时玩过一个游戏,它运行得很好,但现在不行了,我不知道为什么,即使我看了彼此旁边的代码,也几乎和以前一样。@Vijay那我该怎么办?我看不到你的代码。您必须自己调试代码。您是否在应用程序循环中调用
pygame.display.set_mode()
?哦,是的,在主循环中每次都会调用它loop@Vijay把它拿走!
pygame.key.get_pressed()
返回的信息由事件处理设置。当
pygame.display.set_mode()
重新初始化显示和io事件时,如果我尝试pygame.key.get_pressed(),我会遇到类似的问题。我编辑了上面的评论来告诉你我的意思。之前我在主循环中调用它以在每一帧中都得到它,但即使我尝试调用它