1

I'm creating a 2d platformer and I'm having issues with getting my platforms to display. I can blit them as standard images but when I try to display them using a class it doesn't work. Any idea why?

Displays the platforms

#Level 1
for platform in platforms:
    platform.draw(screen, platform)

Rest of the code

import pygame
pygame.init()

#Game Window
screen = pygame.display.set_mode((1024, 640))
pygame.display.set_caption("A Spirit's End")
clock = pygame.time.Clock()
gameRun = False

#Colors
black = (0, 0, 0)
white = (255,255,255)

#Entities
class Entity(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

entities = pygame.sprite.Group()

#Player Classes and Assets

player = pygame.image.load('spirit.png')
player = pygame.transform.scale(player, (60,68))
player_left = pygame.transform.flip(player, True, False)
player_right = pygame.transform.flip(player_left, True, False)
velocity = 6

class Player(Entity):

    def __init__(self, x, y):

        Entity. __init__(self)

        self.image = player
        self.rect = self.image.get_rect()
        self.rect.y = 0
        self.rect.x = 0

    def update(self, platforms):

        keys = pygame.key.get_pressed()

        if keys[pygame.K_a]:
            self.rect.x -= velocity
            self.image = player_left

        elif keys[pygame.K_d]:
            self.rect.x += velocity
            self.image = player_right

    def draw(self, screen):
        screen.blit(self.image, self.rect)

player = Player(0,0)

#Level Classes and Assets

stone = pygame.image.load('stone.png')
stone = pygame.transform.scale(stone, (64, 64))

class Platform(Entity):

    def __init__(self, x, y):

        Entity.__init__(self)
        self.image = stone
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

    def update(self):
        pass

    def draw (self, screen, platform):
        screen.blit(self.image, self.rect, platform)


platform = Platform(0, 0)

#Level_1 Design

x = y = 0

level = ['    P           ',
         '                ',
         '                ',
         '                ',
         '                ',
         '                ',
         '                ',
         '                ',
         'PP              ',
         'PPPPPPPPPPPPPPPP',]

platforms = []

for row in level:
    for col in row:
        if col == 'P':
            P = Platform(x, y)
            platforms.append(P)
            entities.add(P)
        x += 64
    y += 64
    x = 0

entities.add(player)

#Game Loop
while not gameRun:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameRun = True

    #Background
    screen.fill(white)

    #Player Updates
    player.update(platforms)
    player.draw(screen)

    #Level 1
    for platform in platforms:
        platform.draw(screen, platform)

    pygame.display.update()
    clock.tick(60)

pygame.quit()
  • what means `"doesn't work"? did this burn your house or kill your cat ? – furas Jan 23 '17 at 07:42
  • BTW: I thing I saw this code few times in last week or two weeks. – furas Jan 23 '17 at 07:44
  • why do you use third argument in `blit` ? `platform` is `Sprite` and `blit` doesn't expect this type of argument. You need only `platform.draw(screen)` and `screen.blit(self.image, self.rect)` – furas Jan 23 '17 at 07:48
  • to make code more readable put all classes before `pygame.init()` - see [simple template](https://github.com/furas/python-examples/blob/master/pygame/__templates__/1__simple__.py) – furas Jan 23 '17 at 07:51
  • you can use `entities.draw(screen)` and don't need `for platform` loop – furas Jan 23 '17 at 07:55
  • I apologize, using _entities.draw(screen)_ helped answer my question. –  Jan 24 '17 at 01:54

0 Answers0