1

I'm not really sure if the problem is in the randrange() function. I'm trying to create a basic snake game in pygame and if the snake eats the food it will respawn in a different place, but in the first try the snake eats the food and everything is fine but when the food respawns in a different place the snake can't eat it for some reason.

import pygame, sys
from pygame.locals import *
from random import randrange

pygame.init()
DISPLAYSURF = pygame.display.set_mode((400, 400))
pygame.display.set_caption('Hello World!')
green = (0,255,0)
randomnum = [20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 260, 270, 280, 290, 300, 310, 320, 330, 340, 350, 360, 370, 380, 390, 400, 10]
times = 5
list = (10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
green = (0,255,0)
FPS = 10
FpsClock = pygame.time.Clock()
playerY = 0
playerX = 0
plus = 20
rectcordinatesx = 200
rectcordinatesy = 200
isgoingdown = False
isgoingright = False
isgoingleft = False
isgoingUp = False
color = (255, 0, 0)


while True:
    BG = pygame.draw.rect(DISPLAYSURF,(255, 255,255),(0,0,400,400))
    rect = pygame.draw.rect(DISPLAYSURF,(color),(rectcordinatesx, rectcordinatesy,20, 20))
    player = pygame.draw.rect(DISPLAYSURF,(green),(playerX, playerY,20, 20))
    keys = pygame.key.get_pressed()
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        #ALL COLLISION CONDITIONS
    if playerX == rectcordinatesx and playerY == rectcordinatesy :
        rectcordinatesx = randrange(0, 40) * 10
        rectcordinatesy = randrange(0, 40) * 10


        #LOOKING FOR KEYS
    if keys[pygame.K_s]:
        isgoingdown = True
        isgoingright = False
        isgoingleft = False
        isgoingUp = False
    if keys[pygame.K_d]:
        isgoingdown = False
        isgoingright = True
        isgoingleft = False
        isgoingUp = False
    if keys[pygame.K_z]:
        isgoingdown = False
        isgoingright = False
        isgoingleft = False
        isgoingUp = True
    if keys[pygame.K_q]:
        isgoingdown = False
        isgoingright = False
        isgoingleft = True
        isgoingUp = False

        # MOVING CONDITIONS
    if isgoingdown == True:
        playerY += plus
    if isgoingleft == True:
        playerX -= plus
    if isgoingUp == True:
        playerY -= plus
    if isgoingright == True:
        playerX += plus
    if playerY + 20 >= 400:
        isgoingdown = False
    if playerY <= 0 :
        isgoingUp = False
    if playerX + 20 >= 400:
        isgoingright = False
    if playerX <= 0:
        isgoingleft = False

    print ('x ' + str(playerX) + 'y' + str(playerY) + 'food x' + str(rectcordinatesx) +'food y ' + str(rectcordinatesy))


    pygame.display.update(player)
    pygame.display.update()
    FpsClock.tick(FPS)

I don't know if the problem is in the randomizer.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
erramidev
  • 27
  • 4

2 Answers2

1

The size of a cell is 20 instead of 10. Define a variable for the size of a cell:

cell_size = 20

Use the size of a cell to generate random positions for the food:

rectcordinatesx = randrange(0, 40) * 10
rectcordinatesy = randrange(0, 40) * 10

rectcordinatesx = randrange(0, DISPLAYSURF.get_width(), cell_size)
rectcordinatesy = randrange(0, DISPLAYSURF.get_height(), cell_size)

I recommend to use pygame.Rect objects for the collision detection (see How do I detect collision in pygame?):

player_rect = pygame.Rect(playerX, playerY, cell_size, cell_size)
food_rect = pygame.Rect(rectcordinatesx, rectcordinatesy, cell_size, cell_size)
if player_rect.colliderect(food_rect):
    rectcordinatesx = randrange(0, DISPLAYSURF.get_width(), cell_size)
    rectcordinatesy = randrange(0, DISPLAYSURF.get_height(), cell_size)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
1

The one problem I see is that you move your player in increments of 20 each iteration but the food controlled by the rectcordinatesx and y are set at 10 unit interval. In half the cases, they would en up in a position that the snake can't reach. To fix it either change the "plus" variable to 10 or change the rectcordinates a 20 interval. If you want to do it properly I'd suggest writing the code so that this mistakes can't be made, instead of hard coding the values as they are

MAX_X = 400
MAX_Y = 400
plus = 20
...
...
def randomize_food(max):
    return randrange(0, max, plus)

...
...
rectcordinatesx = randomize_food(MAX_X)
rectcordinatesy = randomize_food(MAX_Y)

You're main problem is that you are using hard coded values all over your code. This will be minefield of potential bugs if you choose to change your values somewhere. It's always good practice to keep your values and their relations defined in a single position, because sooner or later they might change and that will cause issues.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174