简体   繁体   中英

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.

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

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:

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. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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