0

I have problem with the movement of my snake game. I have drawn two rectangles and now I wonder how I can get these rectangles to turn on the same coordinates. I have already defined were the first rectangle turns. I also want the second rectangle to turn were the first cube turned. Here is my code:

import pygame
import random
pygame.init()

win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("Snake")


class player:
    def __init__(self, x, y, width, height, colour, vel):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.colour = colour
        self.vel = vel


class food:

    def __init__(self, radius, colour):
        self.radius = radius
        self.colour = colour


# Variables
run = True
fo = food(10, (100, 0, 0))
man = player(50, 50, 20, 20, (200, 0, 0), 10)
direction = 0
y_food = 100
x_food = 100
y_change = 0
x_change = 0
snake_length = [1,2,3]
y_pos = 0
x_pos= 0
pos_x=50
pos_y =50

while run:
    pygame.time.delay(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        direction = 0
        x_change = -50
        y_change = 0
        pos_x = man.x
        pos_y = man.y
    elif keys[pygame.K_RIGHT]:
        direction = 1
        x_change = 50
        y_change = 0
        pos_x = man.x
        pos_y = man.y
    elif keys[pygame.K_UP]:
        direction = 2
        x_change = 0
        y_change = -50
        pos_x = man.x
        pos_y = man.y
    elif keys[pygame.K_DOWN]:
        direction = 3
        x_change = 0
        y_change = 50
        pos_x = man.x
        pos_y = man.y

    x_pos = (pos_x - x_change)
    y_pos = (pos_y - y_change)

# head
    if direction == 0:
        man.x = man.x - man.vel
    elif direction == 1:
        man.x = man.x + man.vel
    elif direction == 2:
        man.y = man.y - man.vel
    elif direction == 3:
        man.y = man.y + man.vel

    if y_food - 10 < man.y < y_food + 10:
        if x_food < man.x < x_food + 10 or x_food < man.x +20 < x_food + 15:
            x_food = random.randint(0, 500)
            y_food = random.randint(0, 500)

    win.fill((0, 0, 0))

    pygame.draw.circle(win, fo.colour, (x_food, y_food), fo.radius)
    pygame.draw.rect(win, man.colour, (man.x, man.y, man.width, man.height))
    pygame.draw.rect(win, man.colour, (x_pos, y_pos, man.width, man.height))

    pygame.display.update()

pygame.quit()
Kingsley
  • 12,897
  • 5
  • 28
  • 50
  • 1
    Please describe in more detail what you mean about getting the rectangles to turn on the same coordinates — it's not clear what you want to happen / it to look like. – martineau Mar 26 '20 at 20:12
  • If I press any of the direction keys i don't want both rectangles to change direction directly. Is it possible to get the second rectangle to turn after the first, so that they change direction on the same coordinates. And by doing this make it look like a snake movement. – Fredrik Schultz Mar 26 '20 at 20:53
  • 1
    Take a look at this answer for an example: https://stackoverflow.com/a/60833216/142637 – sloth Mar 27 '20 at 08:35

0 Answers0