如何在pygame中绘制精灵或图像周围的边框?

时间:2017-10-14 08:15:10

标签: python image pygame sprite surface

我想在图像周围画一个红色边框,但无法弄清楚如何。

我已尝试填充Hero精灵的图像,并将颜色键设置为红色self.image.set_colorkey(red),但这会使图像不可见。

在图像上绘制一个红色矩形,只需将其完全填满:pygame.draw.rect(self.image, red, [0, 0, width, height])

我只想要一个红色边框,以便将来有助于碰撞检测。

main.py 中的代码:

import pygame
from pygame import *
import sprites
from sprites import *
pygame.init()

width = 640
height = 480
color = (255, 255, 255) #white
x = 0
y = 0
speed = 3

screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Mushroom")
icon = pygame.image.load('icon.bmp')
pygame.display.set_icon(icon)
sprites_list = pygame.sprite.Group()

hero = Hero('mushroom.png', 48, 48)
hero.rect.x = 200;
hero.rect.y = 300;

sprites_list.add(hero)

running = True
clock = pygame.time.Clock()
while running:
    sprites_list.update()
    screen.fill((color))
    sprites_list.draw(screen)
    hero.draw(screen)
    pygame.display.flip()
    pygame.display.update()

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

    key = pygame.key.get_pressed()
    if key[pygame.K_LEFT]:
       hero.left(speed)
    if key[pygame.K_RIGHT]:
       hero.right(speed)
    if key[pygame.K_UP]:
       hero.up(speed)
    if key[pygame.K_DOWN]:
       hero.down(speed)

    clock.tick(60)

sprites.py 中的代码:

import pygame
from pygame import *

red = (255, 0, 0)

class Hero(pygame.sprite.Sprite):
    def __init__(self, color, width, height):

        super().__init__()

        self.image = pygame.image.load('mushroom.png')
        self.image = pygame.Surface((width, height))
        self.image.fill(red)
        self.image.set_colorkey(red)

        pygame.draw.rect(self.image, red, [0, 0, width, height])
        self.rect = self.image.get_rect()

    def draw(self, screen):
        screen.blit(self.image, self.rect)

    def right(self, pixels):
        self.rect.x += pixels
    def left(self, pixels):
        self.rect.x -= pixels
    def up(self, pixels):
        self.rect.y -= pixels
    def down(self, pixels):
        self.rect.y += pixels

2 个答案:

答案 0 :(得分:1)

您有两种选择:

  1. 在蘑菇图像/表面上绘制一个未填充的红色矩形。
  2. draw方法中每帧绘制矩形。
  3. 如果你想要一个非填充的rect,你必须将一个int作为最后一个参数(行width)传递给pygame.draw.rect

    import pygame
    
    
    pygame.init()
    
    white = (255, 255, 255)
    red = (255, 0, 0)
    
    # The original
    hero_img = pygame.Surface((48, 48), pygame.SRCALPHA)
    pygame.draw.circle(hero_img, (30, 90, 170), (24, 24), 20)
    # ---Solution 1---
    # You could create a copy of the original image and
    # then draw the rect onto it.
    hero_img_with_border = hero_img.copy()
    # Draw the red border. Pass an int as the last argument (width)
    # to draw a non-filled rect. For 2 pixel linewidth the rect has
    # to be one pixel smaller.
    pygame.draw.rect(hero_img_with_border, red, (0, 0, 47, 47), 2)
    
    
    class Hero(pygame.sprite.Sprite):
    
        def __init__(self, position):
            super().__init__()
            self.image = hero_img
            self.rect = self.image.get_rect(center=position)
            self.speed = 3
    
        def draw(self, screen):
            # ---Solution 2---
            # Just draw the non-filled rect here.
            pygame.draw.rect(screen, red, self.rect, 2)
    
        def right(self):
            self.rect.x += self.speed
        def left(self):
            self.rect.x -= self.speed
        def up(self):
            self.rect.y -= self.speed
        def down(self):
            self.rect.y += self.speed
    
    
    screen = pygame.display.set_mode((640, 480))
    
    sprites_list = pygame.sprite.Group()
    # You can pass the coords and change the rect in the __init__ method.
    hero = Hero((200, 300))
    sprites_list.add(hero)
    
    running = True
    clock = pygame.time.Clock()
    
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
    
        key = pygame.key.get_pressed()
        if key[pygame.K_LEFT]:
           hero.left()
        if key[pygame.K_RIGHT]:
           hero.right()
        if key[pygame.K_UP]:
           hero.up()
        if key[pygame.K_DOWN]:
           hero.down()
    
        sprites_list.update()
    
        screen.fill(white)
        sprites_list.draw(screen)
        # If you use solution 1, you don't need the draw method, since
        # sprites_list.draw blits the image already.
        hero.draw(screen)
        screen.blit(hero_img, (100, 100))
    
        pygame.display.flip()
        clock.tick(60)
    

    我更改了一些内容:speed可能是精灵的一个属性,您可以将带坐标的元组传递给__init__方法,然后将其传递给get_rect作为centertopleft参数。

    self.image.get_rect(center=position)
    

答案 1 :(得分:1)

您可以将 width 参数传递给 pygame.draw.rect()

换行

die()

pygame.draw.rect(self.image, red, [0, 0, width, height])

应该做你想要的!

编辑:我意识到这篇文章很旧,但这是我遇到的一个问题,目前的最佳答案没有帮助。我找到了这个解决方案,希望它也能帮助其他人!