如何正确设置随机发生器

how to set a randomizer correctly

我不太确定问题是否出在 randrange() 函数中。 我正在尝试在 pygame 中创建一个基本的贪吃蛇游戏,如果蛇吃了食物,它会在不同的地方重生,但在第一次尝试时,蛇吃了食物,一切都很好,但是当食物重生时在不同的地方,由于某种原因,蛇不能吃它。

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)

不知道是不是随机发生器的问题

单元格的大小是 20 而不是 10。为单元格的大小定义一个变量:

cell_size = 20

使用单元格的大小为食物生成随机位置:

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)

我推荐使用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)

我看到的一个问题是,您每次迭代以 20 的增量移动玩家,但 rectcordinatesx 和 y 控制的食物设置为 10 个单位间隔。在一半的情况下,他们会处于蛇无法到达的位置。要修复它,可以将“plus”变量更改为 10 或将 rectcordinates 更改为 20 间隔。如果您想正确地做到这一点,我建议您编写代码以防止出现这种错误,而不是按原样对值进行硬编码

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)

您的主要问题是您在整个代码中都使用了硬编码值。如果您选择在某处更改您的值,这将是潜在错误的雷区。将您的价值观及其关系定义在一个位置始终是一个好习惯,因为它们迟早会发生变化,这会导致问题。