繁体   English   中英

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

[英]How to draw a border around a sprite or image in pygame?

我想在图像周围绘制红色边框,但不知道如何绘制。

我试图填充Hero精灵的图像并将self.image.set_colorkey(red)设置为红色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

你有两个选择:

  1. 在蘑菇图像/表面上绘制一个红色的、未填充的矩形。
  2. draw方法中draw每一帧的矩形。

如果你想要一个非填充的矩形,你必须将一个 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__方法,然后将其作为centertopleft参数传递给 get_rect。

self.image.get_rect(center=position)

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

换线

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

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

应该做你正在寻找的!

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM