我有以下代码,但由于某种原因,在运行程序时,白色矩形在屏幕上不显示。它可能与我放置各种代码块或其他东西的地方有关。有人可以建议吗?非常感谢提前:
import pygame
from pygame.locals import*
pygame.init()
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 300
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# Create the surface and pass in a tuple with its length and width
surf = pygame.Surface((50, 50))
# Give the surface a color to differentiate it from the background
surf.fill((255, 255, 255))
done = False
#---GAME LOGIC HERE -------------#
#This is our main loop
while not done:
#for loop that goes through the event queue
for event in pygame.event.get():
#check for user input (int his case quitting the program)
if event.type == pygame.QUIT:
#if user quits then the done variable is set to True, and exits the loop!
done = True
# This line says "Draw surf onto screen at coordinates x:400, y:300"
rect = surf.get_rect()
screen.blit(surf, (400, 300))
pygame.display.flip()
答案 0 :(得分:3)
我刚看了你的代码,以及为什么你看不到白色表面/矩形'在屏幕上是因为它在屏幕的宽度和高度之外被遮挡了!
当对曲面或图像等进行blitting时,为该位置传递的x和y坐标将在该位置设置曲面的左上角像素。由于您在位置(400,300)处显示了矩形:screen.blit(surf, (400, 300))
,这是窗口的屏幕宽度和高度...实际上是在屏幕外面的矩形。所以要修复,您需要做的就是将x和y位置更改为屏幕内。在屏幕上尝试一个位置,也许(100,100)?希望有所帮助!
-Travis