0
votes

I am making a platformer game in pygame. I want to have a floor on the bottom of the level that the player can walk on. In my Floor class, I used a grass image and made it much longer. However, when I run the program, the image stays the same. Can someone fix this? Is it something in my code?

import pygame
from spritesheet_functions import SpriteSheet

GRASS = (648, 0, 70, 70)

   class Floor(pygame.sprite.Sprite):

        def __init__(self, sprite_sheet_data):

            super().__init__()

            sprite_sheet = SpriteSheet('tiles_spritesheet.png')

            self.image=sprite_sheet.get_image(sprite_sheet_data[0],

                                              sprite_sheet_data[1],

                                              sprite_sheet_data[2],

                                              sprite_sheet_data[3])

            self.floor = pygame.transform.scale(self.image, 
                                               (6000, 50))

            self.rect = self.floor.get_rect()


class Level(object):

    def __init__(self, player):

        self.floor_list = None

        self.floor_list = pygame.sprite.Group()

    def update(self):
        self.floor_list.update()

    def draw(self, screen):
        self.floor_list.draw(screen)


class Level01(Level):

    def __init__(self, player):

        Level.__init__(self, player)

        level_floor = [[floors.GRASS, 0, 550]]

        for ground in level_floor:
            floor = floors.Floor(ground[0])
            floor.rect.x = ground[1]
            floor.rect.y = ground[2]
            floor.player = self.player
            self.floor_list.add(floor)
1
where do you draw it ? Probably you draw self.image. You should keep original image in different variables and use self.image only to keep current displayed image (ie. rescaled) - furas
Where do I do that? - Blazian444
I draw it all in another file. That works, but the image isn't transformed. - Blazian444
if you keep Sprites in Group and run group.draw() then it use self.image, self.rect to display it. - furas
how do you assign rescaled image to self.image ? self.image = pygame.transform.scale(self.image, (6000, 50)) - furas

1 Answers

0
votes

pygame.sprite.Group.draw() is a methods which are provided by pygame.sprite.Group. pygame.sprite.Group.draw() uses the image and rect attributes of the contained pygame.sprite.Sprites to draw the objects - you have to ensure that the pygame.sprite.Sprites have the required attributes. See pygame.sprite.Group.draw():

Draws the contained Sprites to the Surface argument. This uses the Sprite.image attribute for the source surface, and Sprite.rect. [...]

You must reassign the scaled image to the attribute image:

self.floor = pygame.transform.scale(self.image, (6000, 50))

self.image = pygame.transform.scale(self.image, (6000, 50))