1

I am writing simple snake game using pygame library. Now I am writing the checkPosition() function. It uses the contains() method from pygame. Problem is that it takes the coordinates prom the start of the loop and it is not updating. How can i reset theese variables or make to update the loop? The whole code is here:

import pygame
import random
pygame.init()

screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption("Snake Game using Pygame")

#colors (RGB code)
blue = (3, 119, 252)
yellow = [251, 255, 36]
gray = [48, 48, 47]

# Variables for control
# Speed of movement
vel = 10 
# Snake width and height
width = 35
height = 35
#Snake spawn position
x = 25
y = 25

clock = pygame.time.Clock()

# Random coordinates for spawning snake "snack"
randomSnackX = random.randrange(0, 500, 20)
randomSnackY = random.randrange(0, 500, 20)

# Snack width and height - thickness
snackThickness = 10

# Variable for initial game loop
run = True

# Draw snack and snake
def drawInitialElements():
    # Draw raadom snak position from variables
    snack = pygame.draw.rect(screen, (255, 255, 255), [randomSnackX,randomSnackY,snackThickness,snackThickness])

    #Draw snake
    snake = pygame.draw.rect(screen, (255, 255, 255), (x, y, width, height))

    return snake, snack

snake, snack = drawInitialElements()

def checkPosition():
    if (snake.contains(snack)) == True:
        print("Eated snack")

#Initial game loop
while run:
    pygame.time.delay(100)
    screen.fill((0, 0, 0))

    # If quit
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    #Controls
    keys = pygame.key.get_pressed()

    if keys[pygame.K_RIGHT]:
        x += vel

    if keys[pygame.K_LEFT]:
        x -= vel

    if keys[pygame.K_UP]:
        y -= vel

    if keys[pygame.K_DOWN]:
        y += vel

    drawInitialElements()
    checkPosition()
    pygame.display.update()

pygame.quit()

Thanks for your help, Tom.

  • You don't use the function's return values when you run `drawInitialElements()` inside the loop. I recommend refactoring this into more functions so you don't get weird variable scoping bugs like this. – 0x5453 Apr 14 '20 at 12:41
  • How can i do this? – Tomáš Roj Apr 14 '20 at 12:43
  • Unfortunatly no, It does nothing because program still thinks that the position of theese two are the inital one (25, 25). So it does not work still – Tomáš Roj Apr 15 '20 at 09:37
  • @TomášRoj So you did something wrong. My solution works fine. I've added the complete application code to the answer. – Rabbid76 Apr 17 '20 at 17:33

1 Answers1

1

To verify if pygame.Rect objects are colliding, you have to use colliderect:

def checkPosition():
    if snake.colliderect(snack):
        print("Eated snack")

drawInitialElements returns a tuple containing the rectangle of the snake and the snack. Assign the return value the variables snake and snack in globale namespace:

while run:
    # [...]

    snake, snack = drawInitialElements()

    # [...]

Complete application code:

import pygame
import random
pygame.init()

screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption("Snake Game using Pygame")

#colors (RGB code)
blue = (3, 119, 252)
yellow = [251, 255, 36]
gray = [48, 48, 47]

# Variables for control
# Speed of movement
vel = 10 
# Snake width and height
width = 35
height = 35
#Snake spawn position
x = 25
y = 25

clock = pygame.time.Clock()

# Random coordinates for spawning snake "snack"
randomSnackX = random.randrange(0, 500, 20)
randomSnackY = random.randrange(0, 500, 20)

# Snack width and height - thickness
snackThickness = 10

# Variable for initial game loop
run = True

# Draw snack and snake
def drawInitialElements():
    # Draw raadom snak position from variables
    snack = pygame.draw.rect(screen, (255, 255, 255), [randomSnackX,randomSnackY,snackThickness,snackThickness])

    #Draw snake
    snake = pygame.draw.rect(screen, (255, 255, 255), (x, y, width, height))

    return snake, snack

snake, snack = drawInitialElements()

def checkPosition():
    if (snake.contains(snack)) == True:
        print("Eated snack")

#Initial game loop
while run:
    pygame.time.delay(100)
    screen.fill((0, 0, 0))

    # If quit
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    #Controls
    keys = pygame.key.get_pressed()

    if keys[pygame.K_RIGHT]:
        x += vel

    if keys[pygame.K_LEFT]:
        x -= vel

    if keys[pygame.K_UP]:
        y -= vel

    if keys[pygame.K_DOWN]:
        y += vel

    snake, snack = drawInitialElements()
    checkPosition()
    pygame.display.update()

pygame.quit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174