Как назначить спрайт (изображение) на прямоугольник? - PullRequest
1 голос
/ 09 апреля 2019

Я пытаюсь узнать, как создать игру-лабиринт, используя python и pygame для проекта.Проблема, с которой я столкнулся, заключается в том, что во всех примерах, которые я нахожу, они просто имеют простой квадрат в качестве символа.У меня есть изображение (символьный спрайт, который я хочу использовать) размером 16x16 пикселей, так что это правильный размер для примера, с которым я работаю, и мне даже удалось заставить спрайт загрузить один раз.Но когда это произошло, мой персонаж не мог двигаться ...

Итак, как мне представить этот прямоугольник как изображение размером 16x16 пикселей, которое я сохранил в той же папке, что и мой код, вместо цветного квадрата?

Это две области, которые я даю мне при попытке изменить спрайт игрока:

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 Ответ

1 голос
/ 09 апреля 2019

Хорошо, так что первое, что вам нужно, это загрузить изображение, которое выглядит так, как будто вы уже сделали.

Чтобы поместить в вашего спрайта, вы должны сделать это:

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.
...