期望结果:
在一起的两个工具栏旁边绘制两个相同的工具栏:
- 颜色=浅灰色
- 轮廓=黑色
实际结果:
左侧工具栏是:
- 颜色=浅灰色
- 轮廓=黑色
右侧工具栏是:
- 颜色=黑色
- 轮廓=黑色
问题:如何实现期望的结果?
我的代码:
import pygame
import random
from os import path
WIDTH = 480
HEIGHT = 720
FPS = 30
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
LIGHTGREY = (220, 220, 220)
pygame.init()
pygame.mixer.init()
pygame.display.set_caption("tests")
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
def fill_woutline(surface, fill_color, outline_color, rect, border=1):
surface.fill(outline_color, rect)
surface.fill(fill_color, rect.inflate(-border, -border))
class Bar(pygame.sprite.Sprite):
def __init__(self, centerx, y, width, height, fill_color, outline_color, border=1):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((width, height))
self.rect = self.image.get_rect()
self.rect.centerx = centerx
self.rect.y = y
fill_woutline(self.image, fill_color, outline_color, self.rect, border)
all_bars = pygame.sprite.Group()
toolbar = Bar(25, 0, 50, 360, LIGHTGREY, BLACK, 2)
toolbar2 = Bar(100, 0, 50, 360, LIGHTGREY, BLACK, 2)
all_bars.add(toolbar, toolbar2)
while True:
clock.tick(FPS)
for e in pygame.event.get():
if e.type == pygame.QUIT:
pygame.quit()
screen.fill(WHITE)
all_bars.draw(screen)
pygame.display.flip()
fill_woutline
函数中打印outline_color
和fill_color
的值来调试您的程序。 - Micheal O'Dwyerinflate
方法上。至今我还没有找到一个令人满意的方法来绘制你想要的矩形,但希望这能指引你朝着正确的方向前进。 - Micheal O'Dwyer