Pygame中的带边框块

时间:2019-01-03 19:17:46

标签: python pygame

我正在尝试在pygame中制作一些带有黑色边框的简单块。我这样做的方法是创建一个矩形,然后将其填充两次,一次使用轮廓颜色(黑色),然后再次使用实际的块颜色(但使用rect.inflate())以仅填充块,从而产生边界。我的问题是:当我只做一个方块时,这就像一个护身符。但是,连续进行更多操作会导致正确制作第一个程序块,而其他程序块则全都是黑色,这是我不理解的。我产生此问题的MWE如下:

import pygame

class Block(pygame.sprite.Sprite):
    def __init__(self, area, pos):
        super().__init__()
        self.area = area
        self.pos = pos
        self.image = pygame.Surface(self.area)
        self.rect = self.image.get_rect(topleft = self.pos)

def draw_brick(brick, outline_color, fill_color):
    # Fill with the outline color
    brick.image.fill(outline_color, brick.rect)
    # Fill with the actual block color
    brick.image.fill(fill_color, brick.rect.inflate(-10, -10))
    return brick.image

def get_brick(area, pos, outline_color, fill_color):
    # Create brick object
    b = Block(area, pos)
    # Draw the object
    b.image = draw_brick(b, outline_color, fill_color)
    return b


# Initialize
pygame.init()

# Set up screen
screen = pygame.display.set_mode((960, 540))

# Colors
white = [255,255,255]
black = [0,0,0]
red = [255,0,0]

# Clock
clock = pygame.time.Clock()

# Make bricks
nbricks = 2
length = int(960. / nbricks)
width = 54
area = (length, width)
y = 0
bricks = []
for i in range(nbricks):
    x = i * (length + 1)
    pos = (x, y)
    bricks.append(get_brick(area, pos, black, red))

# Main loop
running = True

while running:
    # Check for events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    # Draw to screen
    screen.fill(white)
    for b in bricks:
        screen.blit(b.image, (b.rect.x, b.rect.y))
    pygame.display.flip()
    clock.tick(60)

# Clean up
pygame.quit()

我尝试逐步使用pdb并检查了每个块的rect(和膨胀的rect)的topleft,topright,bottomleft,bottomright和center属性是否正确。块绘制在正确的位置,但是第二个块的颜色全是黑色,而不是像第一个块那样带有黑色边框的红色,这使我感到困惑。谢谢!

2 个答案:

答案 0 :(得分:2)

我弄清楚了什么地方出了问题。在您的代码中:

self.rect = self.image.get_rect(topleft = self.pos)

self.rect获取图像的大小以及应该用self.pos处理的位置。

下面的代码使用它来使其中心带有颜色。

def draw_brick(brick, outline_color, fill_color):
    # Fill with the outline color
    brick.image.fill(outline_color, brick.rect)
    # Fill with the actual block color
    brick.image.fill(fill_color, brick.rect.inflate(-10, -10))
    return brick.image

brick.rect返回应填充颜色的位置。 brick.rect错误地将颜色放置在错误的位置。

尝试以下方法:

import pygame

class Block(pygame.sprite.Sprite):
    def __init__(self, area, pos):
        super().__init__()
        self.area = area
        self.pos = pos
        self.image = pygame.Surface(self.area)
        self.rect = self.image.get_rect()  # note the removed 'topleft = self.pos'

def draw_brick(brick, outline_color, fill_color):
    # Fill with the outline color
    brick.image.fill(outline_color, brick.rect)
    # Fill with the actual block color
    brick.image.fill(fill_color, brick.rect.inflate(-10, -10))
    return brick.image

def get_brick(area, pos, outline_color, fill_color):
    # Create brick object
    b = Block(area, pos)
    # Draw the object
    b.image = draw_brick(b, outline_color, fill_color)
    return b


# Initialize
pygame.init()

# Set up screen
screen = pygame.display.set_mode((960, 540))

# Colors
white = [255,255,255]
black = [0,0,0]
red = [255,0,0]

# Clock
clock = pygame.time.Clock()

# Make bricks
nbricks = 2
length = int(960. / nbricks)
width = 54
area = (length, width)
y = 0
bricks = []
for i in range(nbricks):
    x = i * (length + 1)
    pos = (x, y)
    bricks.append(get_brick(area, pos, black, red))

# Main loop
running = True

while running:
    # Check for events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    # Draw to screen
    screen.fill(white)
    for b in bricks:
        screen.blit(b.image, b.pos)        # note it uses b.pos instead of b.rect
    pygame.display.flip()
    clock.tick(60)

# Clean up
pygame.quit()

答案 1 :(得分:1)

GeeTransit已经告诉您代码有什么问题(基本上是将相对坐标和绝对坐标混合在一起),但是我认为您的代码太复杂了。

可以简化为此。

import pygame

class Block(pygame.sprite.Sprite):
    def __init__(self, rect, outline_color, fill_color):
        super().__init__()
        self.image = pygame.Surface(rect.size)
        self.image.fill(outline_color)
        self.image.fill(fill_color, self.image.get_rect().inflate(-10, -10))
        self.rect = rect

pygame.init()
screen = pygame.display.set_mode((960, 540))

# Colors
white = [255,255,255]
black = [0,0,0]
red = [255,0,0]

clock = pygame.time.Clock()

nbricks = 2
length = int(960. / nbricks)
height = 54

y = 0
bricks = pygame.sprite.Group()
for i in range(nbricks):
    x = i * (length + 1)
    bricks.add(Block(pygame.Rect(x, y, length, height), black, red))

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill(white)
    bricks.draw(screen)
    pygame.display.flip()
    clock.tick(60)

现在我们有两个函数,少了一堆变量,Block类是独立的(它知道如何自己绘制Surface),并使用Group (而不是列表)进行绘制有助于保持主循环的清洁。