简体   繁体   English

为什么 PyGame 中什么也没有画?

[英]Why is nothing drawn in PyGame at all?

i have started a new project in python using pygame and for the background i want the bottom half filled with gray and the top black.我在 python 使用 pygame 开始了一个新项目,对于背景,我希望下半部分填充灰色,顶部填充黑色。 i have used rect drawing in projects before but for some reason it seems to be broken?我以前在项目中使用过矩形绘图,但由于某种原因它似乎被破坏了? i don't know what i am doing wrong.我不知道我做错了什么。 the weirdest thing is that the result is different every time i run the program.最奇怪的是每次运行程序时结果都不一样。 sometimes there is only a black screen and sometimes a gray rectangle covers part of the screen, but never half of the screen.有时只有黑屏,有时灰色矩形会覆盖部分屏幕,但不会覆盖屏幕的一半。

import pygame, sys
from pygame.locals import *
pygame.init()

DISPLAY=pygame.display.set_mode((800,800))
pygame.display.set_caption("thing")
pygame.draw.rect(DISPLAY, (200,200,200), pygame.Rect(0,400,800,400))

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

You need to update the display.您需要更新显示。 You are actually drawing on a Surface object.您实际上是在Surface object 上绘图。 If you draw on the Surface associated to the PyGame display, this is not immediately visible in the display.如果您在与 PyGame 显示器关联的Surface上绘图,这不会立即在显示器中可见。 The changes become visibel, when the display is updated with either pygame.display.update() or pygame.display.flip() .当使用pygame.display.update()pygame.display.flip()更新显示时,更改变得可见。

See pygame.display.flip() :pygame.display.flip()

This will update the contents of the entire display.这将更新整个显示的内容。

While pygame.display.flip() will update the contents of the entire display, pygame.display.update() allows updating only a portion of the screen to updated, instead of the entire area.虽然pygame.display.flip()将更新整个显示器的内容,但pygame.display.update()只允许更新屏幕的一部分,而不是整个区域。 pygame.display.update() is an optimized version of pygame.display.flip() for software displays, but doesn't work for hardware accelerated displays. pygame.display.update()是 pygame.display.flip() 的优化版本, pygame.display.flip()于软件显示,但不适用于硬件加速显示。

The typical PyGame application loop has to:典型的 PyGame 应用循环必须:

import pygame
from pygame.locals import *
pygame.init()

DISPLAY = pygame.display.set_mode((800,800))
pygame.display.set_caption("thing")
clock = pygame.time.Clock()

run = True
while run:
    # handle events
    for event in pygame.event.get():
        if event.type == QUIT:
            run = False

    # clear display
    DISPLAY.fill(0)

    # draw scene
    pygame.draw.rect(DISPLAY, (200,200,200), pygame.Rect(0,400,800,400))

    # update display
    pygame.display.flip()

    # limit frames per second
    clock.tick(60)

pygame.quit()
exit()

repl.it/@Rabbid76/PyGame-MinimalApplicationLoop See also Event and application loop repl.it/@Rabbid76/PyGame-MinimalApplicationLoop另见事件和应用程序循环

simply change your code to:只需将您的代码更改为:

import pygame, sys
from pygame.locals import *
pygame.init()

DISPLAY=pygame.display.set_mode((800,800))
pygame.display.set_caption("thing")
pygame.draw.rect(DISPLAY, (200,200,200), pygame.Rect(0,400,800,400))
pygame.display.flip() #Refreshing screen

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

it should help它应该有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM