在Pygame中围绕两个Surface绘制两个rects

时间:2017-11-22 08:16:04

标签: python pygame draw surface rect

我正在尝试两个绘制两个表面,一个在另一个上面,每个表面都有一个边框。顶部表面(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()

Screenshot

1 个答案:

答案 0 :(得分:0)

您正在使用botRect的坐标绘制白色矩形,这意味着您要在{{1}区域之外的表面局部坐标(0, 500)处绘制它}。您必须首先在botSurf绘制矩形,然后如果您还想将其用作曲面的blit位置,则移动矩形。

(0, 0)