我是pygame的新手,我期待显示为青色,并绘制矩形。窗口出现但不是青色而没有矩形?
我认为这与订单或间距有关。 在添加rect之前一切正常。
import pygame
import sys
from pygame.locals import *
pygame.init()
cyan = (0,255,255)
soft_pink = (255,192,203)
screen_width = 800
screen_height = 600
gameDisplay = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('''example''')
pygame.draw.rect(gameDisplay,soft_pink,(389,200),(300,70),4)
gameDisplay.fill(cyan)
gameExit = True
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
答案 0 :(得分:3)
您应该非常小心Python代码的格式。测试代码并修复while循环的格式会显示问题:
C:\src\python\pygame1>python buggy.py
Traceback (most recent call last):
File "buggy.py", line 16, in <module>
pygame.draw.rect(gameDisplay,soft_pink,(389,200),(300,70),4)
TypeError: function takes at most 4 arguments (5 given)
如果您只是使用正确数量的参数替换pygame.draw.rect
来电,则会显示青色窗口。我测试了以下替换线:
pygame.draw.rect(gameDisplay,soft_pink,(389,200,300,70))
答案 1 :(得分:0)
初始化pygame屏幕时,会返回一个表面,您可以填充该表面并在while循环中连续填充。我建议您也为矩形使用表面对象。只需更改您的代码:
import pygame
import sys
from pygame.locals import *
pygame.init()
cyan = (0,255,255)
soft_pink = (255,192,203)
screen_width = 800
screen_height = 600
gameDisplay = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Example') # shouldn't use triple quotes
gameExit = True
surf = pygame.Surface((200, 75)) # takes tuple of width and height
rect = surf.get_rect()
rect.center = (400, 300)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
gameDisplay.fill(cyan) # continuously paint screen cyan
surf.fill(soft_pink) # continuously paint rectangle
gameDisplay.blit(surf, rect) # position rectangle at position
记住连续渲染是游戏开发的基本秘密,对象按照您指定的顺序绘制。因此,在这种情况下,您实际上会看到矩形,因为它是在屏幕之后绘制的。