为什么在 PyGame 中根本没有绘制任何内容? [英] Why is nothing drawn in PyGame at all?
问题描述
我使用 pygame 在 python 中启动了一个新项目,对于背景,我希望下半部分填充灰色,顶部填充黑色.我以前在项目中使用过 rect 绘图,但由于某种原因它似乎被破坏了?我不知道我做错了什么.最奇怪的是每次运行程序结果都不一样.有时只有一个黑屏,有时一个灰色矩形覆盖了屏幕的一部分,但从来没有半个屏幕.
import pygame, sys从 pygame.locals 导入 *pygame.init()显示=pygame.display.set_mode((800,800))pygame.display.set_caption(东西")pygame.draw.rect(显示,(200,200,200),pygame.Rect(0,400,800,400))为真:对于 pygame.event.get() 中的事件:如果 event.type == 退出:pygame.quit()系统退出()
您需要更新显示.您实际上是在 另见 事件和应用程序循环
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. 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. If you draw on the Surface associated to the PyGame display, this is not immediately visible in the display. The changes become visibel, when the display is updated with either pygame.display.update()
or 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.update()
is an optimized version of pygame.display.flip()
for software displays, but doesn't work for hardware accelerated displays.
The typical PyGame application loop has to:
- handle the events by calling either
pygame.event.pump()
orpygame.event.get()
. - update the game states and positions of objects dependent on the input events and time (respectively frames)
- clear the entire display or draw the background
- draw the entire scene (draw all the objects)
- update the display by calling either
pygame.display.update()
orpygame.display.flip()
- limit frames per second to limit CPU usage with
pygame.time.Clock.tick
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()
See also Event and application loop
这篇关于为什么在 PyGame 中根本没有绘制任何内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!