如何在Pygame中使用类来显示平台

时间:2017-01-23 04:15:39

标签: python pygame

我正在创建一个2d平台游戏,我遇到了让平台显示的问题。我可以将它们作为标准图像进行blit,但是当我尝试使用类显示它们时它不起作用。知道为什么吗?

显示平台

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

其余代码

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()

0 个答案:

没有答案