简体   繁体   English

Pygame程序无法按预期绘制

[英]Pygame program failing to draw as expected

I am new to pygame, and I was expecting the display be cyan and the rectangle to be drawn. 我是pygame的新手,我期待显示为青色并绘制矩形。 The window appears but not in cyan and without the rectangle? 窗口出现了,但是不是青色的并且没有矩形?

I believe it has something to do with the order or spacing. 我相信这与顺序或间距有关。 Everything was working before I added the 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()

You should be very careful with your formatting for Python code. 您对Python代码的格式应格外小心。 Testing your code and fixing up the formatting for the while loop reveals the problem: 测试您的代码并修复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)

If you just replace the pygame.draw.rect call with the correct number of parameters it shows a cyan window. 如果仅用正确数量的参数替换pygame.draw.rect调用,它将显示一个青色窗口。 I tested the following replacement line: 我测试了以下替换行:

pygame.draw.rect(gameDisplay,soft_pink,(389,200,300,70))

When initializing a pygame screen, a surface is returned which you can fill and should fill continuously in your while loop. 初始化pygame屏幕时,将返回一个可以填充的表面,并且应在while循环中连续填充该表面。 I suggest you use a surface object for the rectangle as well. 我建议您也为矩形使用表面对象。 Just change your code like so: 只需像这样更改代码:

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

Remember continuous rendering is the underlying secret to game development, and objects are painted in the order you specify. 请记住,连续渲染是游戏开发的基本秘密,并且按照指定的顺序绘制对象。 So in this case you'd actually see the rectangle because it's painted after the screen is. 因此,在这种情况下,您实际上会看到矩形,因为它是在屏幕之后绘制的。

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

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