随机模块的randrange在pygame中不起作用

时间:2016-08-08 11:49:11

标签: python random pygame

我正在尝试用pygame制作游戏,并希望在随机位置添加苹果,但随机模块无效。我尝试在网上看,但那里的人能够毫无问题地使用它 我的代码和输出低于

impoort pygame
imporrt random

pygame.init()

display_width = 1000
display_height = 500

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("SlikiSnake")

clock = pygame.time.Clock()
FPS = 15

block_size = 10

def GameLoop():    
    lead_x = display_width/2
    lead_y = display_height/2
    lead_x_change =0
    lead_y_change =0

    randAppleX = random.randint(0, display_width - block_size)
    randAppleY = random.randint(0 ,display_height - block_size)

    pygame.display.update()

    gameExit = False
    gameOver = False

    while not gameExit:

        while gameOver == True:
            gameDisplay.fill(white)
            message_on_screen("Game Over,press r to start again or Q to quit", black)
            pygame.display.update()
            for event in pygame.event.get():
                if event.type ==pygame.KEYDOWN:
                    if event.key == pygame.K_r:
                        GameLoop()
                    if event.key == pygame.K_q:
                        gameExit = True
                        gameOver = False

        if lead_x >= display_width or lead_x < 0 or lead_y >=display_height or lead_y < 0:
            gameOver = True

        lead_x += lead_x_change
        lead_y += lead_y_change

        gameDisplay.fill(random)
        pygame.draw.rect(gameDisplay,red,[randAppleX,randAppleY,block-size,block_size])
        pygame.draw.rect(gameDisplay,blue,[lead_x,lead_y,block_size,block_size])

        clock.tick(FPS)    
        pygame.display.flip()
    pygame.quit()
    quit()

GameLoop()
`

但错误是:

http://i.stack.imgur.com/caaPn.png

1 个答案:

答案 0 :(得分:1)

  1. 下次将代码和错误直接写在问题正文中,而不是截图。

  2. 您必须已将random分配给import random到您发布的行之间某个元组。