简体   繁体   English

在 pygame 中按下另一个键时如何禁用某个键

[英]How to disbale certain key when another key is pressed in pygame

i am working on my university assignment and they have given me a task to make snake game in python using pygame, i have made the game but i am facing a problem the problem is, if i have pressed the right key and my snake is moving to the right then i presses the left key the snake is hitting itself and i am losing the game.我正在完成我的大学作业,他们给了我一项任务,即使用 pygame 在 python 中制作蛇游戏,我已经制作了游戏,但我面临一个问题,如果我按下了右键并且我的蛇在移动向右,然后我按下左键,蛇撞到自己,我输掉了比赛。

what i want is if my snake is moving in a certain direction, For example: right The left key should get disabled in other word the left key should not work when right key is pressed.我想要的是如果我的蛇朝某个方向移动,例如:右左键应该被禁用,换句话说,当按下右键时左键不应该工作。 i have been trying to solve the problem since 2 days but can't find anything, any help will be appreciated, Thank You 2天以来我一直在尝试解决问题,但找不到任何东西,任何帮助将不胜感激,谢谢

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        game_over = True
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
            x1_change = -snake_block
            y1_change = 0
        elif event.key == pygame.K_RIGHT:
            x1_change = snake_block
            y1_change = 0
        elif event.key == pygame.K_UP:
            x1_change = 0
            y1_change = -snake_block
        elif event.key == pygame.K_DOWN:
            x1_change = 0
            y1_change = snake_block

You need to add an additional condition that will check that the snake is not moving in the opposite direction.您需要添加一个额外的条件来检查蛇没有朝相反的方向移动。
For example, if the snake is moving to the right, pressing LEFT is not allowed:例如,如果蛇向右移动,则不允许按LEFT

if not x1_change > 0 and event.key == pygame.K_LEFT:

Event loop:事件循环:

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        game_over = True
    if event.type == pygame.KEYDOWN:
        if not x1_change > 0 and event.key == pygame.K_LEFT: 
            x1_change = -snake_block
            y1_change = 0
        elif not x1_change < 0 and event.key == pygame.K_RIGHT:
            x1_change = snake_block
            y1_change = 0
        elif not y1_change > 0 and event.key == pygame.K_UP:
            x1_change = 0
            y1_change = -snake_block
        elif not y1_change < 0 and event.key == pygame.K_DOWN:
            x1_change = 0
            y1_change = snake_block

You can also disable the keypressing by noticing whether the snake is not already moving in that direction:您还可以通过注意蛇是否尚未朝该方向移动来禁用按键:

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        game_over = True
    if event.type == pygame.KEYDOWN:
        if y1_change:
            if event.key == pygame.K_LEFT:
                x1_change = -snake_block
                y1_change = 0
            elif event.key == pygame.K_RIGHT:
                x1_change = snake_block
                y1_change = 0
        elif x1_change:
            elif event.key == pygame.K_UP:
                x1_change = 0
                y1_change = -snake_block
            elif event.key == pygame.K_DOWN:
                x1_change = 0
                y1_change = snake_block

This code will only allow the player to change its direction to up/down if he is moving horizontally and to left/right when he's moving vertically.此代码仅允许玩家在水平移动时将其方向更改为上/下,当他垂直移动时将其更改为左/右。 When the player moves to the left, y1_change is 0, making if y1_change not to be executed.当玩家向左移动时, y1_change为 0,使得if y1_change不被执行。 Then the player cannot move to the left (as he's already doing it) and not to the right (making the snake collide with itself).然后玩家不能向左移动(因为他已经这样做了)而不是向右移动(使蛇与自身相撞)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM