samedi 25 septembre 2021

how to set a randomizer correctly

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.




Aucun commentaire:

Enregistrer un commentaire