如何在pygame中调整精灵组大小时获得正确的位置?

huangapple go评论72阅读模式
英文:

How to get the right position while resizing a sprite group in pygame?

问题

我的精灵是16x16像素。我想要对它们进行缩放,使它们在游戏中看起来更大,但当我尝试这样做时,精灵的位置完全混乱。

我看到了很多关于如何对单个图像进行缩放的示例,但我需要对该组中的所有精灵进行缩放。

我敢打赌有一个愚蠢的错误我没有看到。

已经在这里待了2天,但仍然无法弄清楚。

未缩放的精灵
如何在pygame中调整精灵组大小时获得正确的位置?

缩放后的精灵
如何在pygame中调整精灵组大小时获得正确的位置?

tiles.py

import pygame
from pytmx import util_pygame
from settings import *

tmx_data = util_pygame.load_pygame('../levels/level_0/level_0_tilemap.tmx', pixelalpha=True)
sprite_group = pygame.sprite.Group()

# 将tmx瓷砖转换为精灵

class Tile(pygame.sprite.Sprite):

    def __init__(self, pos, surf, groups):
        super().__init__(groups)
        self.image = surf
        self.rect = self.image.get_rect(topleft=pos)
        self.surf = surf

    def run():
        for layer in tmx_data.visible_layers:
            for x, y, gid in layer:
                tile = tmx_data.get_tile_image_by_gid(gid)
                if tile is not None:
                    Tile(pos=(x * 16, y * 16), surf=tile, groups=sprite_group)
        for obj in sprite_group:
            obj.surf = pygame.transform.scale(obj.image, (obj.image.get_width() * 2, obj.image.get_height() * 2))
            screen.blit(obj.surf, obj.rect)

settings.py

import pygame

vertical_tile_number = 30
tile_size = 16
screen_height = vertical_tile_number * tile_size
screen_width = 920

pygame.display.set_caption('Platformer')
screen = pygame.display.set_mode((screen_width, screen_height))

clock = pygame.time.Clock()
clock.tick(60)

<details>
<summary>英文:</summary>

My sprites are 16x16 pixels. I want to scale them, so they&#39;re appearing bigger in the game, but when I try to do this, the positions of the sprites are totally messed up.

I&#39;ve sawn many examples for doing so with just one image, but I&#39;ll need to do it for all of my sprites in that group. 

I&#39;ll bet there is one silly mistake I just don&#39;t see.
Been here for 2 days but still can&#39;t figure it out.

[unscaled Sprites][1]  
[![][1]][1]

[scaled sprites][2]  
[![][2]][2]

&gt; tiles.py

import pygame
from pytmx import util_pygame
from settings import *

tmx_data = util_pygame.load_pygame('../levels/level_0/level_0_tilemap.tmx', pixelalpha = True)
sprite_group = pygame.sprite.Group()

#convert tmx tiles to sprites

class Tile(pygame.sprite.Sprite):

def __init__(self, pos, surf, groups):
    super().__init__(groups)
    self.image= surf
    self.rect = self.image.get_rect(topleft= pos)
    self.surf = surf

    #print(dir(self.rect))

def run():

    for layer in tmx_data.visible_layers:
        for x, y, gid in layer:
            tile = tmx_data.get_tile_image_by_gid(gid)
            if tile is not None:
                Tile(pos=(x * 16, y * 16), surf=tile, groups=sprite_group)
    for obj in sprite_group:
        obj.surf = pygame.transform.scale_by(obj.image, 2)
        screen.blit(obj.surf, obj.rect)

&gt; settings.py

import pygame

vertical_tile_number = 30
tile_size = 16
screen_height = vertical_tile_number * tile_size
screen_width = 920

pygame.display.set_caption('Platformer')
screen = pygame.display.set_mode((screen_width, screen_height))

clock = pygame.time.Clock()
clock.tick(60)


  [1]: https://i.stack.imgur.com/DdvZt.png
  [2]: https://i.stack.imgur.com/hQBFo.png

</details>


# 答案1
**得分**: 1

在缩放精灵本身之外,还必须缩放精灵的位置:

for obj in sprite_group:
    obj.surf = pygame.transform.scale_by(obj.image, 2)
    x = obj.rect.x * 2
    y = obj.rect.y * 2
    screen.blit(obj.surf, (x, y))

请注意,可能还需要重新对齐屏幕底部的精灵:

for obj in sprite_group:
    obj.surf = pygame.transform.scale_by(obj.image, 2)
    x = obj.rect.x * 2
    y = obj.rect.y * 2 - screen_height
    screen.blit(obj.surf, (x, y))

<details>
<summary>英文:</summary>

It is not sufficient to scale the sprite itself, you must also scale the position of the sprite:

for obj in sprite_group:
obj.surf = pygame.transform.scale_by(obj.image, 2)
x = obj.rect.x * 2
y = obj.rect.y * 2
screen.blit(obj.surf, (x, y))


Note that it may also be necessary to realign the sprites at the bottom of the screen:

for obj in sprite_group:
obj.surf = pygame.transform.scale_by(obj.image, 2)
x = obj.rect.x * 2
y = obj.rect.y * 2 - screen_height
screen.blit(obj.surf, (x, y))


</details>



huangapple
  • 本文由 发表于 2023年4月10日 22:58:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978191.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定