【问题标题】:how to set a randomizer correctly [closed]如何正确设置随机化器[关闭]
【发布时间】:2021-09-25 09:52:25
【问题描述】:

我不确定问题是否出在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)

我不知道问题是否出在随机发生器上。

【问题讨论】:

    标签: python random pygame


    【解决方案1】:

    我看到的一个问题是,您每次迭代以 20 的增量移动播放器,但 rectcordinatesx 和 y 控制的食物设置为 10 个单位间隔。在一半的情况下,它们会处于蛇无法到达的位置。要修复它,要么将“加号”变量更改为 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)
    
    

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

    【讨论】:

      【解决方案2】:

      单元格的大小是 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 对象进行碰撞检测(参见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)
      

      【讨论】:

        猜你喜欢
        • 2012-12-03
        • 2021-08-25
        • 2019-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-20
        相关资源
        最近更新 更多