我正在学习如何使用 Pygame ,但是当我运行该程序时,Pygame窗口只是黑色而且没有矩形?有人可以帮忙吗?
import pygame, sys
from pygame.locals import *
pygame.init()
screen=pygame.display.set_mode((640,360),0,32)
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
screen.lock()
pygame.draw.rect (screen, (140,240,130), Rect((100,100), (130,170)))
screen.unlock()
pygame.display.update()
答案 0 :(得分:2)
您无需锁定屏幕。
问题是你没有在你的循环中画画。
import pygame, sys
from pygame.locals import *
pygame.init()
screen=pygame.display.set_mode((640,360),0,32)
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
screen.fill((255,255,255))
pygame.draw.rect (screen, (140,240,130), Rect((100,100), (130,170)))
pygame.display.update()
答案 1 :(得分:1)
这并不复杂。您所需要做的就是:
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 320))
screen.fill((255,255,255))
pygame.draw.rect(screen, (140,240,130), (100, 100, 130, 170), 0)
pygame.display.flip()
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
pygame.display.flip()
部分与pygame.display.update()