Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help with snake game
#1
question: I have a check if the snake.left(x coordinate) == fruit.left, the rand function should be called and the fruit spawns at a different location. It works for the first time their x coordinates are the same, but afterwords it doesn't work anymore. Can anyone help me with this problem?

import pygame
import time
import random

# global variables
game = True
width, height = 500, 500
x, y = 20, 20
x2, y2 = 240, 250

# setting up screen
WIN = pygame.display.set_mode((500,500))
pygame.display.set_caption("Snake by Sam")

# load the background
BG = pygame.image.load("snake.png")
BG_resize = pygame.transform.scale(BG, (width, height))
WIN.blit(BG_resize,(0,0))


# draw square on screen
snake = pygame.draw.rect(WIN, (255,0,0), (x,y,20,20))
key = None

# draw fruit(circle), setup
fruit = pygame.draw.rect(WIN, (255,0,0), (x2,y2,20,20))

def rand():
    global x2, y2
    x2 = random.randint(0, 490)
    y2 = random.randint(0, 490)

def move():
    global key
    global snake
    global x
    global y
    global fruit
    if key == "d":        
        x = snake.left
        time.sleep(0.03)
        x = x + 10
        WIN.blit(BG_resize,(0,0))
        snake = pygame.draw.rect(WIN, (255,0,0), (x,y,20,20))
        fruit = pygame.draw.rect(WIN, (255,0,0), (x2,y2,20,20))

    if key == "s":        
        y = snake.top
        time.sleep(0.03)
        y = y + 10
        WIN.blit(BG_resize,(0,0))
        snake = pygame.draw.rect(WIN, (255,0,0), (x,y,20,20))
        fruit = pygame.draw.rect(WIN, (255,0,0), (x2,y2,20,20))
    if key == "w":        
        y = snake.top
        time.sleep(0.03)
        y = y - 10
        WIN.blit(BG_resize,(0,0))
        snake = pygame.draw.rect(WIN, (255,0,0), (x,y,20,20))
        fruit = pygame.draw.rect(WIN, (255,0,0), (x2,y2,20,20))

    if key == "a":        
        x = snake.left
        time.sleep(0.03)
        x = x - 10
        WIN.blit(BG_resize,(0,0))
        snake = pygame.draw.rect(WIN, (255,0,0), (x,y,20,20))
        fruit = pygame.draw.rect(WIN, (255,0,0), (x2,y2,20,20))

# mainloop
while game:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_d:
                WIN.blit(BG_resize,(0,0))
                key = "d"
            if event.key == pygame.K_w:
                WIN.blit(BG_resize,(0,0))
                key = "w"
            if event.key == pygame.K_s:
                WIN.blit(BG_resize,(0,0))
                key = "s"
            if event.key == pygame.K_a:
                WIN.blit(BG_resize,(0,0))
                key = "a"
    move()

    # border control
    if snake.left > 490:
        snake = pygame.draw.rect(WIN, (255,0,0), (250,250,20,20))
        x,y = 250, 250
        key = None
    if snake.left == 0:
        WIN.blit(BG_resize,(0,0))
        snake = pygame.draw.rect(WIN, (255,0,0), (250,250,20,20))
        x,y = 250, 250
        key = None
    if snake.top > 490:
        snake = pygame.draw.rect(WIN, (255,0,0), (250,250,20,20))
        x,y = 250, 250
        key = None
    if snake.top == 0:
        WIN.blit(BG_resize,(0,0))
        snake = pygame.draw.rect(WIN, (255,0,0), (250,250,20,20))
        x,y = 250, 250
        key = None

    # check if the snake has made contact with the fruit.   
    if snake.left == fruit.left: 
        WIN.blit(BG_resize,(0,0))
        rand()
        fruit = pygame.draw.rect(WIN, (255,0,0), (x2,y2,20,20))


    pygame.display.update()
Reply
#2
When in doubt, print it out!
I added this to your loop: print(f"{snake.left} == {fruit.left}: {snake.left == fruit.left}"), and saw the issue almost immediately.

Your rand() function sets x2 to a random int. In my case, 159. But the snake moves in multiples of 10. So it'll never be at 159. 150 or 160, sure, but never 159.
Reply
#3
Hi, thanks for your help. After reading your explanation it was clear as day. Didn't realize that because I set the change of the snake to 10 it could never be like 249 or something like that. I also put your print function in my loop and I think it only prints out the x coordinate if x coordinates of both objects are the same. I don't however understand, how you put a conditional in the print statement like that. Could you please explain that to me. It would be much appreciated.
Reply
#4
That's an "f" string, which means anything inside the curly braces is just python code. You can do whatever you want inside the braces, and whatever it returns (in that case, True/False) will be added to the string. It's not a great idea to do complicated stuff in there, but I didn't really want to write more than one line.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to add segments to the snake body blacklight 1 2,869 Sep-13-2023, 07:33 AM
Last Post: angelabarrios
  [PyGame] Snake game: how to get an instance for my snake's body multiple times? hajebazil 2 2,139 Jan-30-2022, 04:58 AM
Last Post: hajebazil
  Snake Game - obstacle problem Samira 3 5,588 Oct-31-2019, 02:58 PM
Last Post: Samira
  [PyGame] Made my first Python program: Snake. Please help me improve it andrerocha1998 7 6,130 Feb-19-2019, 07:08 PM
Last Post: Windspar
  Creating Snake game in Turtle Shadower 1 8,642 Feb-11-2019, 07:00 PM
Last Post: woooee
  [PyGame] Basic Snake game (using OOP) PyAlex 1 12,526 Sep-10-2018, 09:02 PM
Last Post: Mekire
  [PyGame] Snake not changing directions in Snake Game Bjdamaster 4 4,927 Aug-13-2018, 05:09 AM
Last Post: Bjdamaster
  [PyGame] Snake controls not working jakegold98 5 6,372 Dec-12-2017, 01:45 AM
Last Post: Windspar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020