背景颜色辅助

时间:2015-04-05 18:07:38

标签: python pygame

出于某种原因,我正在进行一个基本的pygame项目,我的背景似乎是黑色的,但我将其指定为白色。请尽可能提供帮助。

这是代码(顺便说一句,这是python中的PYGAME):



import pygame
pygame.init()

white = (255,255,255)
black=(0,0,0)
red=(255,0,0)

gameDisplay = pygame.display.set_mode((800,600))
pygame.display.set_caption('Slither')

gameExit=False

lead_x = 300
lead_y = 300

while not gameExit:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameExit = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                lead_x -= 10

            if event.key == pygame.K_RIGHT:
                lead_x +=10

gameDisplay.fill(white)
pygame.draw.rect(gameDisplay, black,[lead_x,lead_y,10,100])
pygame.display.update()

pygame.quit()
quit()




1 个答案:

答案 0 :(得分:2)

问题是你的绘图代码在while循环之外。将它缩进一个标签,它应该可以工作,所以,像这样:

while not gameExit:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameExit = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                lead_x -= 10

            if event.key == pygame.K_RIGHT:
                lead_x +=10

    gameDisplay.fill(white)
    pygame.draw.rect(gameDisplay, black,[lead_x,lead_y,10,100])
    pygame.display.update()