gpt4 book ai didi

python - 在pygame中用轮廓填充表面

转载 作者:行者123 更新时间:2023-12-04 13:03:23 25 4
gpt4 key购买 nike

预期结果:

  • 绘制两个相同的工具栏:
  • 颜色 = 浅灰色
  • 轮廓 = 黑色

  • 实际结果:
  • 左侧工具栏是:
  • 颜色 = 浅灰色
  • 轮廓 = 黑色
  • 右手边的工具栏是:
  • 颜色 = 黑色
  • 轮廓 = 黑色

  • 问题:我如何达到我想要的结果?

    我的代码:
    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 的第二个参数方法指定应填充的表面区域。如果您在表面上 blit/绘制某些东西,则左上角坐标将始终为 (0, 0),与表面在屏幕上的位置无关。所以因为你通过了 rect s 与世界坐标,你永远不会在第二条上绘制任何东西(默认情况下表面填充黑色)。

    修复 fill_woutline函数,你可以生成一个新的矩形并将其放气。

    def fill_woutline(surface, fill_color, outline_color, border=1):
    surface.fill(outline_color)
    surface.fill(fill_color, surface.get_rect().inflate(-border, -border))

    关于python - 在pygame中用轮廓填充表面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48127128/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com