如何将一个精灵(图像)分配给一个矩形?

时间:2019-04-09 14:13:43

标签: python pygame sprite

我正在尝试学习如何使用python和pygame创建一个迷宫游戏。我遇到的问题是,在所有示例中,我发现它们只是一个简单的正方形作为字符。我有一个16x16px的图像(我想使用的字符精灵),所以它是我正在处理的示例的正确大小,而且我什至设法使精灵可以加载一次。但是当它做到时,我的角色无法动弹...

那么我如何将这个矩形表示为与代码存储在同一文件夹中的16x16px图像,而不是彩色正方形?

以下是我尝试更改播放器精灵时给我的2个结果:

class Player(object):

    def __init__(self):
        self.rect = pygame.Rect(32, 32, 16, 16)

    for wall in walls:
        pygame.draw.rect(screen, (255, 255, 255), wall.rect)
    pygame.draw.rect(screen, (255, 0, 0), end_rect)
    pygame.draw.rect(screen, (255, 200, 0), player.rect)
    pygame.display.flip()

任何帮助将不胜感激。

(已更新)完整版游戏:

import os
import random
import pygame

IMAGE = pygame.image.load('mudkip.png')
all_sprites = pygame.sprite.Group()


# Class for character

class Player(pygame.sprite.Sprite):

    def __init__(self, pos):

        super().__init__(all_sprites)
        # Get the image from the variable
        self.image = IMAGE
        # Get the rect from the image
        self.rect = self.image.get_rect(topleft=pos)

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

    def move(self, dx, dy):

        # Move each axis separately. Note that this checks for collisions both times.
        if dx != 0:
            self.move_single_axis(dx, 0)
        if dy != 0:
            self.move_single_axis(0, dy)

    def move_single_axis(self, dx, dy):

        # Move the rect
        self.rect.x += dx
        self.rect.y += dy

        # If you collide with a wall, move out based on velocity
        for wall in walls:
            if self.rect.colliderect(wall.rect):
                if dx > 0:  # Moving right; Hit the left side of the wall
                    self.rect.right = wall.rect.left
                if dx < 0:  # Moving left; Hit the right side of the wall
                    self.rect.left = wall.rect.right
                if dy > 0:  # Moving down; Hit the top side of the wall
                    self.rect.bottom = wall.rect.top
                if dy < 0:  # Moving up; Hit the bottom side of the wall
                    self.rect.top = wall.rect.bottom


# Nice class to hold a wall rect
class Wall(object):

    def __init__(self, pos):
        walls.append(self)
        self.rect = pygame.Rect(pos[0], pos[1], 16, 16)


# Initialise pygame
os.environ["SDL_VIDEO_CENTERED"] = "1"
pygame.init()

# Set up the display
pygame.display.set_caption("Get to the red square!")
screen = pygame.display.set_mode((560, 240))

clock = pygame.time.Clock()
walls = []  # List to hold the walls
player = Player((30, 30))  # Create the player

# Holds the level layout in a list of strings.
level = [
    "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
    "W    W                            W",
    "W    W      WWWWWW                W",
    "W   WWWW       W                  W",
    "W   W        WWWW       wwwwww    W",
    "W WWW  WWWW                       W",
    "W   W     W W                     W",
    "WE  W     W   WWW W               W",
    "W   WWW WWW   W W                 W",
    "W     W   W   W W                 W",
    "WWW   W   WWWWW W                 W",
    "W W      WW           WWWWWWWWW   W",
    "W W   WWWW   WWW                  W",
    "W     W        W                  W",
    "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
]

# Parse the level string above. W = wall, E = exit
x = y = 0
for row in level:
    for col in row:
        if col == "W":
            Wall((x, y))
        if col == "E":
            end_rect = pygame.Rect(x, y, 16, 16)
        x += 16
    y += 16
    x = 0

running = True
while running:

    clock.tick(60)

    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            running = False
        if e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE:
            running = False

    # Move the player if an arrow key is pressed
    key = pygame.key.get_pressed()
    if key[pygame.K_LEFT]:
        player.move(-2, 0)
    if key[pygame.K_RIGHT]:
        player.move(2, 0)
    if key[pygame.K_UP]:
        player.move(0, -2)
    if key[pygame.K_DOWN]:
        player.move(0, 2)

    # Just added this to make it slightly fun ;)
    if player.rect.colliderect(end_rect):
        raise SystemExit('You win!')

    # Draw the scene
    all_sprites.draw(0)
    screen.fill((0, 0, 0))
    for wall in walls:
        pygame.draw.rect(screen, (255, 255, 255), wall.rect)
    pygame.draw.rect(screen, (255, 0, 0), end_rect)
    pygame.display.flip()

all_sprites.draw()错误:

all_sprites.draw()
TypeError: draw() missing 1 required positional argument: 'surface'

all_sprites.draw(0)错误:

line 129, in <module>
    all_sprites.draw(0)
lib\site-packages\pygame\sprite.py", line 474, in draw
    surface_blit = surface.blit
AttributeError: 'int' object has no attribute 'blit'

1 个答案:

答案 0 :(得分:1)

好的,所以您需要做的第一件事就是加载图像,看起来好像已经完成了。

要将那个放入您的精灵中,您必须执行以下操作:

class Player(pygame.sprite.Sprite):

    def __init__(self, pos): # Additional position argument, formatted like (x, y)
        # Get the image from the variable
        self.image = IMAGE

        # Get the rect from the image
        self.rect = self.image.get_rect(topleft=pos)

然后,您添加一个draw函数,将您的角色添加到屏幕上。它相对简单。

def draw(self):
    screen.blit(self.image, self.rect) 
    # Note, you may have move your global vars before the sprite classes to prevent a NameError now.

最后,您将它们与一些添加的内容放在一起,以获取新的基类,以便稍后添加。

您不应将def draw添加到主游戏循环中;)

# Earlier in the code...
all_sprites = pygame.sprite.Group() # This will allow all the sprites to be drawn in one fell swoop instead of it being done manually.

class Player(pygame.sprite.Sprite): # Make sure to inherit the sprite object!

    def __init__(self, pos):

        super().__init__(all_sprites) # Add the new sprite to the sprite group
        self.image = IMAGE

        # Get the rect from the image
        self.rect = self.image.get_rect(topleft=pos)

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

# Then in the game loop...
all_sprites.draw() # This will call draw() in every active sprite object.