所有spritesheet都显示在屏幕

时间:2015-11-20 17:52:58

标签: python pygame

我的游戏存在问题,这不是一个错误,而是一个问题。我正在使用spritesheets制作我的游戏并使用for循环自动调用我的Platform类。但是,当我尝试调用平台类时,它只显示整个spritesheet。类平台或level_1可能存在错误,但我不确定它是什么。如果你有pygame,请帮助我。 An image of what is happening

import pygame
# Colours + Global constants
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0,  0, 255)
BLACK = (0, 0, 0)
RANDOM = (12, 211, 123)
WIDTH = 800
HEIGHT = 600
SIZE = (WIDTH, HEIGHT)


# CLASSES
# Block is the common platform

平台类:

class Platform(pygame.sprite.Sprite):
    def __init__(self, display, x_screen, y_screen, x_sheet, y_sheet, height, length):
        super().__init__()

        self.image = pygame.image.load("tiles_spritesheet.png")
        self.rect = self.image.get_rect()
        display.blit(self.image, (x_screen, y_screen), (x_sheet, y_sheet, height, length))

Level_1课程:

class Level01(Level):
    def __init__(self, player1, monster, display):
        # Initialise level1
        super().__init__()
        # Level01 things
        block = Block(WIDTH*3, 3, BLACK)
        Level.all_things = self.all_things

        self.sprites.add(player1, monster)
        self.platforms.add(block)
        self.all_things.add(player1, block, monster)

        theLevel = [[1, HEIGHT, 0, 0],
                    [1, HEIGHT, WIDTH*3, 0]]

        level = [[600, 400, 710, 0, 70, 70]]

        for platform in theLevel:
            block = Block(platform[0], platform[1], RED)
            block.rect.x = platform[2]
            block.rect.y = platform[3]
            self.platforms.add(block)
            self.all_things.add(block)

        for goodPlatform in level:
            platform = Platform(display, goodPlatform[0], goodPlatform[1], goodPlatform[2], goodPlatform[3], goodPlatform[4], goodPlatform[5])
            self.platforms.add(platform)
            self.all_things.add(platform)

主:

def main():
    # Init pygame
    pygame.init()
    # Set screen
    background = pygame.image.load("background.jpg")
    backgroundRect = background.get_rect()
    display = pygame.display.set_mode(background.get_size())
    # Creating FPS thingy
    clock = pygame.time.Clock()
    # Making levels  + Player
    monster = Monster(30, 30, RANDOM)
    player = Player()
    level_1 = Level01(player, monster, display)
    # Choosing level
    levelList = []
    levelList.append(level_1)
    currentLevelNumber = 0
    currentLevel = levelList[currentLevelNumber]
    y_speed = -5
    # Game loop
    loop = True
    while loop == True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
            if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_RIGHT:
                        player.move_right()
                    if event.key == pygame.K_LEFT:
                        player.move_left()
                    if event.key == pygame.K_UP:
                        player.move_up(currentLevel.platforms)
                    if event.key == pygame.KMOD_LSHIFT and event.key == pygame.K_RIGHT:
                        player.sprint()
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT and player.sprite_x_change < 0:
                    player.stop()
                if event.key == pygame.K_RIGHT and player.sprite_x_change > 0:
                    player.stop()
                if event.key == pygame.KMOD_LSHIFT:
                    player.sprite_x_change -= 10
        # Update things
        #monster.jump()
        if player.rect.x > 400:
            player.rect.x = 400
            currentLevel.scroll_x(-10)

        if player.rect.x >= WIDTH:
            player.rect.x = WIDTH
            currentLevel.scroll(0)

        if player.rect.y >= HEIGHT:
            main()

        if player.sprite_x_change < 0 and player.rect.x >= 120:
            currentLevel.scroll_x(0)

        if currentLevel.shift_x >= WIDTH *3 - 400:
            currentLevel.scroll_x(0)
            sprite_x_change = 0

        if player.rect.left <= 120 and player.sprite_x_change < 0:
            player.rect.x = 120
            player.rect.left = 120
            currentLevel.scroll_x(10)
        '''
        if player.rect.y <= 300:
            if player.standing == False and player.sprite_y_change < 0:
                currentLevel.scroll_y(10)

        if currentLevel.shift_y > 0:
            y_speed = -4
            if player.standing == True and player.rect.y < 300:
                y_speed = 4
            print(currentLevel.shift_y)
            currentLevel.scroll_y(y_speed)
        '''
        display.blit(background, backgroundRect)
        currentLevel.drawer(display)
        # Refresh screen
        player.updater(currentLevel.platforms)
        clock.tick(30)
        pygame.display.update()
        pygame.quit()
        loop = False

if __name__ == "__main__":
    main()

1 个答案:

答案 0 :(得分:1)

Group使用self.imageself.rectblit元素

  1. 使用pygame.Surface.subsurface(x_sheet, y_sheet, height, length)创建地下 - 并分配给self.image

  2. (x_screen, y_screen, height, length)分配给self.rect

  3. class Platform(pygame.sprite.Sprite):
    
        def __init__(self, display, x_screen, y_screen, x_sheet, y_sheet, height, length):
            super().__init__()
    
            self.tiles = pygame.image.load("tiles_spritesheet.png")
    
            self.image = self.tiles.subsurface(pygame.Rect(x_sheet, y_sheet, height, length))
            self.rect = self.image.get_rect(x=x_screen, y=y_screen)
    
    
            #display.blit(self.image, self.rect)