0

I want it so that when i press my left arrow key (for example) the snake head will move towards the left. I have been using the pygame.display.flip command for that but that would mean if i keep clicking my left key it continually flips left and right.

if event.key == K_LEFT:  
                block = pygame.transform.flip(block, True, False) 
                block_x -= 20

(rest of my code if it helps)

        import pygame
from pygame.locals import *

def draw_block():   
    surface.fill((44, 250, 150))
    surface.blit(block, (block_x, block_y))
    pygame.display.update

def change_direction():
    block = pygame.display.flip


pygame.init()

WND_RES = (800, 600)

surface = pygame.display.set_mode(WND_RES)
surface.fill((44, 250, 150))

block_x = 100
block_y = 100

block = pygame.image.load("resources/pogchamp.jpg").convert()

surface.blit(block,(block_x,block_y))

pygame.display.update()

running = True

while running:
    
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            pass
            if event.key == K_ESCAPE:
                running = False 
            if event.key == K_UP:
                block_y -= 20
                draw_block()
            if event.key == K_DOWN:
                block_y += 20
                draw_block()
            if event.key == K_LEFT:  
                block = pygame.transform.flip(block, True, False) 
                block_x -= 20
                draw_block()
            if event.key == K_RIGHT:
                block_x += 20
                block = pygame.transform.flip(block, True, False)
                draw_block()
        elif event.type == QUIT:
            running = False

    pygame.display.update()
Annsh
  • 39
  • 4
  • Related [How do I chain the movement of a snake's body?](https://stackoverflow.com/questions/62010434/how-do-i-chain-the-movement-of-a-snakes-body/62010435#62010435) – Rabbid76 Oct 22 '21 at 12:04

2 Answers2

0

Ok, from what I've seen every time you enter the event, you flip your image like that.

    if event.key == K_LEFT:  
                block = pygame.transform.flip(block, True, False) 
                block_x -= 20
                draw_block()

So I added conditions to know when the head must turn right or left

    if event.key == K_LEFT:
                if flip_left is False and flip_right is True: ## HERE ##
                    block = pygame.transform.flip(block, True, False)
                    flip_left, flip_right = True, False ## HERE ##
                block_x -= 20
                draw_block()
                
            if event.key == K_RIGHT:
                if flip_right is False and flip_left == True:## HERE ##
                    block = pygame.transform.flip(block, True, False)
                    flip_left, flip_right = False, True ## HERE ##

So here is the rest of your code, I marked the changes with "## HERE ##"

import pygame
from pygame.locals import *

def draw_block():   
    surface.fill((44, 250, 150))
    surface.blit(block, (block_x, block_y))
    pygame.display.update

def change_direction():
    block = pygame.display.flip


pygame.init()

WND_RES = (1080, 960)

surface = pygame.display.set_mode(WND_RES)
surface.fill((44, 250, 150))

block_x = 100
block_y = 100

block = pygame.image.load("pogchamp.jpg").convert()


surface.blit(block,(block_x,block_y))

pygame.display.update()

running = True



flip_left = False  ## HERE ##
flip_right = True ## HERE ##  He starts to look to the right

while running:
    
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            pass
            if event.key == K_ESCAPE:
                running = False 
            if event.key == K_UP:
                block_y -= 20
                draw_block()
            if event.key == K_DOWN:
                block_y += 20
                draw_block()


            if event.key == K_LEFT:
                if flip_left is False and flip_right is True: ## HERE ##
                '''when you click to look on the left, it checks if it is not 
                already done and if it is not it exchanges the status'''
    
                    block = pygame.transform.flip(block, True, False)
                    flip_left, flip_right = True, False ## HERE ##
                block_x -= 20
                draw_block()
                
            if event.key == K_RIGHT:
                if flip_right is False and flip_left == True: ## HERE ##
                    block = pygame.transform.flip(block, True, False)
                    flip_left, flip_right = False, True ## HERE ##

                block_x += 20
                draw_block()
        elif event.type == QUIT:
            running = False

    pygame.display.update()
TeemSy
  • 28
  • 3
0

pygame.display.update is a function. You need the Parentheses (see Calls) to call a function:

pygame.display.update

pygame.display.update()

The typical PyGame application loop has to:

Add a variable that indicates the direction of the obejct:

direction = (1, 0)

Change the variable in the event loop depending on the key pressed:

for event in pygame.event.get():
    if event.type == KEYDOWN:
        if event.key == K_ESCAPE:
            running = False 
        if event.key == K_UP:
            direction = (0, -1)
        if event.key == K_DOWN:
            direction = (0, 1)
        if event.key == K_LEFT:  
            direction = (-1, 0)
            block = block_left 
        if event.key == K_RIGHT:
            direction = (1, 0)
            block = block_right

Move the block in the application loop, depending on the direction:

step = 20
block_x += step * direction[0]
block_y += step * direction[1]

Minimal example:

import pygame
from pygame.locals import *

pygame.init()
WND_RES = (800, 600)
surface = pygame.display.set_mode(WND_RES)

block_x, block_y = 100, 100
# block = pygame.image.load("resources/pogchamp.jpg").convert()
block = pygame.image.load(r"C:\source\PyGameExamplesAndAnswers\resource\icon\Bird64.png")
block_right = block
block_left = pygame.transform.flip(block, True, False) 
direction = (1, 0)

def draw_block():   
    surface.blit(block, (block_x, block_y))

clock = pygame.time.Clock()
running = True
while running:
    clock.tick(10)
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            pass
            if event.key == K_ESCAPE:
                running = False 
            if event.key == K_UP:
                direction = (0, -1)
            if event.key == K_DOWN:
                direction = (0, 1)
            if event.key == K_LEFT:  
                block = block_left 
                direction = (-1, 0)
            if event.key == K_RIGHT:
                direction = (1, 0)
                block = block_right
        elif event.type == QUIT:
            running = False

    step = 20
    block_x += step * direction[0]
    block_y += step * direction[1]

    surface.fill((44, 250, 150))
    draw_block()
    pygame.display.update()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174