如何使用 pygame.transform.scale 调整图像大小?

How to resize image with pygame.transform.scale?

得到了一些代码,但遇到了一个问题: 如何使用 pygame.transform.scale 调整图像大小? 查看了文档,但无法理解。 我对 player.py 部分感到困惑。

所以这是我的代码:

elves.py:

import pygame
import config
from game_state import GameState

from game import Game

pygame.init()

screen = pygame.display.set_mode((config.SCREEN_WIDTH, config.SCREEN_HEIGHT))

pygame.display.set_caption("Elves")

clock = pygame.time.Clock()

game = Game(screen)
game.set_up()

while game.game_state == GameState.RUNNING:
    clock.tick(50)
    game.update()
    pygame.display.flip()

game.py:

import pygame
import config
import math
from player import Player
from game_state import GameState


class Game:
    def __init__(self, screen):
        self.screen = screen
        self.objects = []
        self.game_state = GameState.NONE
        self.map = []
        self.camera = [0, 0]

    def set_up(self):
        player = Player(1, 1)
        self.player = player
        self.objects.append(player)
        self.game_state = GameState.RUNNING

        self.load_map('01')

    def update(self):
        self.screen.fill(config.BLACK)
        self.handle_events()

        self.render_map(self.screen)

        for object in self.objects:
            object.render(self.screen, self.camera)

    def handle_events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.game_state = GameState.ENDED
            #     handle key events
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    self.game_state = GameState.ENDED
                elif event.key == pygame.K_UP:  # up
                    self.move_unit(self.player, [0, -1])
                elif event.key == pygame.K_DOWN:  # down
                    self.move_unit(self.player, [0, 1])
                elif event.key == pygame.K_LEFT:  # left
                    self.move_unit(self.player, [-1, 0])
                elif event.key == pygame.K_RIGHT:  # right
                    self.move_unit(self.player, [1, 0])

    def load_map(self, file_name):
        with open('maps/' + file_name + '.txt') as map_file:
            for line in map_file:
                tiles = []
                for i in range(0, len(line) - 1, 2):
                    tiles.append(line[i])
                self.map.append(tiles)
            print(self.map)

    def render_map(self, screen):
        self.determine_camera()

        y_pos = 0
        for line in self.map:
            x_pos = 0
            for tile in line:
                image = map_tile_images[tile]
                rect = pygame.Rect(x_pos * config.SCALE, y_pos * config.SCALE - (self.camera[1] * config.SCALE),
                                   config.SCALE, config.SCALE)
                screen.blit(image, rect)
                x_pos = x_pos + 1

            y_pos = y_pos + 1

    def move_unit(self, unit, position_change):
        new_position = [unit.position[0] + position_change[0], unit.position[1] + position_change[1]]

        try:
            if self.map[new_position[1]][new_position[0]] == '':
                return
        except IndexError:
            return

        if new_position[0] < 0 or new_position[0] > (len(self.map[0]) - 1):
            return

        if new_position[1] < 0 or new_position[1] > (len(self.map) - 1):
            return

        if self.map[new_position[1]][new_position[0]] == 'W':
            return

        if self.map[new_position[1]][new_position[0]] == '0':
            return

        if self.map[new_position[1]][new_position[0]] == 'T':
            return

        unit.update_position(new_position)

    def determine_camera(self):
        max_y_position = len(self.map) - config.SCREEN_HEIGHT / config.SCALE
        y_position = self.player.position[1] - math.ceil(round(config.SCREEN_HEIGHT / config.SCALE / 2))

        if y_position <= max_y_position and y_position >= 0:
            self.camera[1] = y_position
        elif y_position < 0:
            self.camera[1] = 0
        else:
            self.camera[1] = max_y_position


map_tile_images = {
    "G": pygame.transform.scale(pygame.image.load('imgs/grass.png'), (config.SCALE, config.SCALE)),
    "W": pygame.transform.scale(pygame.image.load('imgs/water.png'), (config.SCALE, config.SCALE)),
    "0": pygame.transform.scale(pygame.image.load('imgs/none.png'), (config.SCALE, config.SCALE)),
    "T": pygame.transform.scale(pygame.image.load('imgs/tree1.png'), (config.SCALE, config.SCALE)),
}

player.py:

import pygame
import config

screen = pygame.display.set_mode((config.SCREEN_WIDTH, config.SCREEN_HEIGHT))


class Player:
    def __init__(self, x_postition, y_position):
        self.load_image = pygame.image.load('imgs/stepped_on_grass.png')
        self.position = [x_postition, y_position]
        self.image = pygame.image.load('imgs/goblin.png')
        self.image = pygame.transform.scale(self.image, (config.SCALE, config.SCALE))
        self.rect = pygame.Rect(self.position[0] * config.SCALE, self.position[1] * config.SCALE, config.SCALE, config.SCALE)

    def update(self):
        pass

    def update_position(self, new_position):
        self.position[0] = new_position[0]
        self.position[1] = new_position[1]
        img = pygame.Surface.blit(screen, self.load_image, (self.position[0], self.position[1]))
        pygame.transform.scale(screen, (config.SCALE, config.SCALE))


    def render(self, screen, camera):
        self.rect = pygame.Rect(self.position[0] * config.SCALE, self.position[1] * config.SCALE - (camera[1] * config.SCALE), config.SCALE, config.SCALE)
        screen.blit(self.image, self.rect)

gamestate.py

from enum import Enum

class GameState(Enum):
    NONE = 0,
    RUNNING = 1,
    ENDED = 2

config.py:

# colours
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

SCALE = 32

SCREEN_HEIGHT = 480
SCREEN_WIDTH = 640

好的,我知道您不需要所有其他文件,但以防万一,对吧? 当我上下移动地精时,我没有看到地面发生变化。

编辑: 问题是:如何使用 pygame.transform.scale 调整图像大小? 我想要玩家站在上面的瓷砖来切换图像。 但事实并非如此。 没有错误。只是错误。

在不了解您的确切需求的情况下,请尝试一下,因为它对我来说效果很好。

pygame.transform.scale(pygame.image.load(os.path.join('assets', 'image.png')), ((x, y))).convert_alpha(),

使用您要缩放的图像更新 'image.png'。同时使用所需的尺寸更新 'x' 和 'y'。

将您的图像放在资产文件夹中

要获得“踩踏”效果,您必须更改地图的内容。对踩过的草使用新字母(例如“S”)(stepped_on_grass.png):

map_tile_images = {
    "G": pygame.transform.scale(pygame.image.load('imgs/grass.png'), (SCALE, SCALE)),
    "W": pygame.transform.scale(pygame.image.load('imgs/water.png'), (SCALE, SCALE)),
    "0": pygame.transform.scale(pygame.image.load('imgs/none.png'), (SCALE, SCALE)),
    "T": pygame.transform.scale(pygame.image.load('imgs/tree1.png'), (SCALE, SCALE)),
    "S": pygame.transform.scale(pygame.image.load('imgs/stepped_on_grass.png'), (SCALE, SCALE)), 
}

map 传递给 Player.update_position 方法:

class Game:
    # [...]

    def move_unit(self, unit, position_change):
        # [...]

        unit.update_position(new_position, self.map)

更改Player.update_position中的地图内容:

class Player:
    # [...]
   
    def update_position(self, new_position, map):
        game.map[self.position [1]][self.position [0]] = 'S'
        self.position = new_position