英文:
How to get the right position while resizing a sprite group in pygame?
问题
我的精灵是16x16像素。我想要对它们进行缩放,使它们在游戏中看起来更大,但当我尝试这样做时,精灵的位置完全混乱。
我看到了很多关于如何对单个图像进行缩放的示例,但我需要对该组中的所有精灵进行缩放。
我敢打赌有一个愚蠢的错误我没有看到。
已经在这里待了2天,但仍然无法弄清楚。
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're appearing bigger in the game, but when I try to do this, the positions of the sprites are totally messed up.
I've sawn many examples for doing so with just one image, but I'll need to do it for all of my sprites in that group.
I'll bet there is one silly mistake I just don't see.
Been here for 2 days but still can't figure it out.
[unscaled Sprites][1]
[![][1]][1]
[scaled sprites][2]
[![][2]][2]
> 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)
> 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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论