1

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

scaled sprites

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)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Jax
  • 11
  • 3

1 Answers1

1

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))
Rabbid76
  • 202,892
  • 27
  • 131
  • 174