Pygame绘图未显示在Pygame窗口中

时间:2019-01-26 04:14:04

标签: python pygame

所以我正在测试pygame,我想画一个简单的矩形。当我运行代码时没有错误消息,但是矩形未显示在窗口中。我看到的是一个空白的白色Pygame窗口弹出。有人知道为什么吗? 目前在我的Mac上使用Python3和Pygame 1.9.4。 这是我的代码,

import pygame
import pygame.font
pygame.init()

# Colours
BLACK   = (  0,  0,  0)
WHITE   = (255,255,255)
GREEN   = (  0,255,  0)
RED     = (255,  0,  0)
BLUE    = (  0,  0,255)

# Dimensions of screen
size = (400,500)
WIDTH = 500
HEIGHT = 400
screen = pygame.display.set_mode(size)

# Loop Switch
done = False

# Screen Update Speed (FPS)
clock = pygame.time.Clock()

# ------- Main Program Loop -------
while not done:
    # --- Main Event Loop ---
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    pygame.draw.rect(screen,(78,203,245),(0,0,250,500),5)


    screen.fill(GREEN)
    pygame.display.flip()

    #Setting FPS
    clock.tick(60)

#Shutdown
pygame.quit()

6 个答案:

答案 0 :(得分:1)

我发现了问题: 首先,Glitchd是正确的,但是您忘了更新:

import pygame
import pygame.font
pygame.init()

# Colours
BLACK   = (  0,  0,  0)
WHITE   = (255,255,255)
GREEN   = (  0,255,  0)
RED     = (255,  0,  0)
BLUE    = (  0,  0,255)

# Dimensions of screen
size = (400,500)
WIDTH = 500
HEIGHT = 400
screen = pygame.display.set_mode(size)

# Loop Switch
done = False

# Screen Update Speed (FPS)
clock = pygame.time.Clock()

# ------- Main Program Loop -------
while not done:
    # --- Main Event Loop ---
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
    screen.fill(GREEN)
    pygame.draw.rect(screen,(78,203,245),(0,0,250,500),5)


    pygame.display.flip()
    pygame.display.update()

    #Setting FPS
    clock.tick(60)

#Shutdown
pygame.quit()

答案 1 :(得分:0)

U正在绘制形状,然后用绿色覆盖它,交换

pygame.draw.rect(screen,(78,203,245),(0,0,250,500),5)
screen.fill(GREEN)

周围2个

答案 2 :(得分:0)

问题是您绘制了形状,然后用绿色“填充”(覆盖)它,因此请尝试如下操作:

    screen.fill(GREEN) #first fill the screen with green
    pygame.draw.rect(screen,(78,203,245),(0,0,250,500),5) #and after that draw the rectangle

答案 3 :(得分:0)

您不想每60刻将绿色填满屏幕

要解决此问题,只需将screen.fill(GREEN)置于Main循环之外。

您唯一希望screen.fill进入while循环的时间是在您向程序中添加移动时。

我强烈建议您创建一个名为draw的函数,并在while循环之外绘制事物。

答案 4 :(得分:0)

该错误很明显,因为首先绘制一个形状,然后用color覆盖它。您的代码正确,但是需要重新排列。

screen.fill(“您的颜色”)#首先,您应该用颜色填充屏幕

pygame.draw.rect(screen,(78,203,245),(0,0,250,500),5)#然后,您应该绘制任何形状

答案 5 :(得分:-1)

您应该先用绿色覆盖屏幕,然后绘制形状,否则它将被覆盖。