Python 在pygame中用轮廓填充曲面
期望的结果:Python 在pygame中用轮廓填充曲面,python,pygame,Python,Pygame,期望的结果: 相邻绘制两个相同的工具栏: 颜色=浅灰色 轮廓=黑色 实际结果: 左侧工具栏是: 颜色=浅灰色 轮廓=黑色 右侧工具栏是: 颜色=黑色 轮廓=黑色 问题:我如何实现我想要的结果 我的代码: import pygame import random from os import path WIDTH = 480 HEIGHT = 720 FPS = 30 WHITE = (255, 255, 255) BLACK = (0, 0, 0) LIGHTGREY = (2
- 相邻绘制两个相同的工具栏:
- 颜色=浅灰色
- 轮廓=黑色
- 左侧工具栏是:
- 颜色=浅灰色
- 轮廓=黑色
- 右侧工具栏是:
- 颜色=黑色
- 轮廓=黑色
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()
该方法的第二个参数指定应填充的曲面面积。如果在一个曲面上blit/draw某物,则左上角坐标始终(0,0)独立于曲面在屏幕上的位置。因此,因为您使用世界坐标传递
rect
s,所以您永远不会在第二个条上绘制任何东西(默认情况下,曲面用黑色填充)
要修复fill\u woutline
函数,只需生成一个新的rect并对其进行放气即可
def fill_woutline(surface, fill_color, outline_color, border=1):
surface.fill(outline_color)
surface.fill(fill_color, surface.get_rect().inflate(-border, -border))
在
fill\u woutline
函数中,通过打印outline\u color
和fill\u color
的值来调试程序。@MichealO'Dwyer我这样做了,它两次都显示了正确的值(220,220,220)(0,0,0)我试着调试程序,发现问题在于使用了充气方法。我仍然没有找到一个令人满意的方法来画出你想要的矩形,但希望这将为你指明正确的方向。@MichealO'Dwyer谢谢,我也有同样的想法,但我还没有弄明白为什么…哦,谢谢,这是有意义的,这也是为什么它能与第一个一起工作的原因