我正在尝试两个绘制两个表面,一个在另一个的顶部,每个表面周围都有一个边界。屏幕上将显示顶部曲面(topSurf)和底部曲面(botSurf)。
当我尝试使用pygame.draw.rect命令在每个曲面周围绘制边界时,将显示顶部曲面上的矩形,但不显示底部曲面上的矩形。
这一切为什么要发生?
代码如下:
import sys
import pygame
from pygame.locals import *
pygame.init()
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
WHITE = (255,255,255)
screen = pygame.display.set_mode((600,600))
#top surface
topRect = pygame.Rect(0,0,600,500)
topSurf = pygame.Surface((600,500))
topSurf = topSurf.convert()
topSurf.fill(BLUE)
#bottom surface
botSurf = pygame.Surface((600,100))
botRect = pygame.Rect(0,500,600,100)
botSurf = botSurf.convert()
botSurf.fill(GREEN)
#rectangle
pygame.draw.rect(topSurf,RED,topRect,10)
pygame.draw.rect(botSurf,WHITE,botRect,10)
### main game loop
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.blit(topSurf,topRect)
screen.blit(botSurf,botRect)
pygame.display.flip()发布于 2017-11-22 18:21:25
你正在使用botRect的坐标来绘制白色矩形,这意味着你在botSurf区域之外的曲面局部坐标(0, 500)处绘制它。如果你还想将其用作曲面的斑点位置,则必须首先在(0, 0)处绘制矩形,然后再移动矩形。
botSurf = pygame.Surface((600,100))
botSurf.fill(GREEN)
botRect = pygame.Rect(0,0,600,100)
pygame.draw.rect(botSurf,WHITE,botRect,10)
botRect.y = 500https://stackoverflow.com/questions/47429756
复制相似问题