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