Python Pygame Player总是居中

时间:2014-01-27 01:59:10

标签: python camera pygame center

我看过滚动相机和相机移动的页面,但我只是想让它更简单。我不希望相机跟随播放器关闭屏幕,我只是希望相机始终以播放器为中心,就像播放器始终位于屏幕中心一样。 如果你需要,继承文件+代码: 我的main.py

    import pygame, sys
    from pygame.locals import *
    from engineTiles import Tile
    from classlibEntities import *
    import Functions
    from interaction import interaction
    pygame.init()
    pygame.font.init()


    invalids =   (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,
                 19,37,55,73,91,109,127,145,163,181,
                 182,183,184,185,186,187,188,189,190,
                 191,192,193,194,195,196,197,198,
                 36,54,72,90,108,126,144,162,180,198)
    SCREENWIDTH = 720
    SCREENHEIGHT = 440
    DrawNumbers = True
    window = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))
    for y in range(0, window.get_height(), 40):
        for x in range(0, window.get_width(), 40):
            if Tile.total_tiles in invalids:
                Tile(x, y, 'solid')
            else:
                Tile(x, y, 'empty')
    pygame.display.set_caption("Until Death - Core Architecture Build 04")
    fpsclock = pygame.time.Clock()
    FPS = 60
    zombie1 = EntityZombie(200, 240)
    player = EntityPlayer(400, 120)
    # ------- Main Game Loop -------
    while True:
        interaction(window, player)
    # ------- All GameLogic -------
        Tile.draw_tiles(window)
        if DrawNumbers == True:
            Tile.draw_tilenumbers(window)
        player.draw(window)
        EntityZombie.draw_zombies(window)
        pygame.display.update()

        fpsclock.tick(FPS)

我的任何瓷砖类

    import pygame, Functions

    class Tile(pygame.Rect):
        List = []
        width, height = 40, 40
        total_tiles = 1
        H, V = 1, 18
        def __init__(self, x, y, Type):

            self.type = Type
            self.number = Tile.total_tiles
            Tile.total_tiles += 1

            if Type == 'empty':
                self.walkable = True
            else:
                self.walkable = False

            pygame.Rect.__init__(self, (x, y), (Tile.width, Tile.height))

            Tile.List.append(self)

        @staticmethod
        def get_tile(number):
            for tile in Tile.List:
                if tile.number == number:
                    return tile

        @staticmethod
        def draw_tiles(window):
            for tile in Tile.List:
                if not(tile.type == 'empty'):
                    if tile.type == 'solid':
                        pygame.draw.rect(window, [40, 40, 40], tile)
                else:
                    pygame.draw.rect(window, [0, 0, 0], tile)
        @staticmethod
        def draw_tilenumbers(window):
            for tile in Tile.List:
                    Functions.text_to_screen(window, tile.number, tile.x, tile.y)

最后是有玩家内容的类。

    import pygame
    from engineTiles import Tile
    class Entity(pygame.Rect):
        width, height = 40, 40

        def __init__(self, x, y):

            pygame.Rect.__init__(self, x, y, EntityPlayer.width, EntityPlayer.height)
        def __str__(self):
            return str(self.get_number())
        def get_number(self):

            return ((self.x / self.width) + Tile.H) + (self.y / self.height) * Tile.V

        def get_tile(self):

            return Tile.get_tile(self.get_number())

    class EntityZombie(Entity):

        List = []

        def __init__(self, x, y):

            Entity.__init__(self, x, y)
            EntityZombie.List.append(self)

        @staticmethod
        def draw_zombies(window):
            for zombie in EntityZombie.List:
                pygame.draw.rect(window, [210, 24, 77], zombie)

    class EntityPlayer(Entity):

        def __init__(self, x, y):

            Entity.__init__(self, x, y)

        def draw(self, window):
            r = int (self.width / 2)
            pygame.draw.circle(window, [77, 234, 156], (self.x + r, self.y + r), r)

1 个答案:

答案 0 :(得分:0)

你需要做的就是在你对它们进行blit时拥有对象的所有坐标,但是在你对它们进行blit之前,减去玩家的位置。因此,如果玩家向下移动了50个像素,则在对其进行blit之前从所有对象中减去50个像素,即。向上移动50像素。确保你测量玩家移动的数量是以像素为单位测量的,而不是用瓷砖测量的!