0

I've seen the pages on like scrolling cameras, and the camera movement, but I just want it simpler. I don't want the camera to follow the player off screen, I just want the camera to always center on the player, like the player is always in the center of the screen. If you need, heres the files + code: my 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)

Any my tile class

    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)

And finally the class that has the player stuff in it.

    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)
user3238885
  • 31
  • 1
  • 3
  • Have you looked at [this questions?](http://stackoverflow.com/questions/14354171/add-scrolling-to-a-platformer-in-pygame) – sloth Jan 27 '14 at 15:14
  • Yes but I have a hard time understanding it. mainly because many variable names throw me off. plus this is a top down game, not platformer. – user3238885 Jan 27 '14 at 20:26
  • AND I ALWAYS want the player to be in the center. I never want the player to leave the center of the screen. – user3238885 Jan 27 '14 at 20:26

2 Answers2

0

What you need to do is have all the co-ordinates of the objects when you blit them, but before you blit them, subtract the player position. So if the player had moved 50 pixels down, subtract 50 pixels from all your objects before you blit them ie. move them 50 pixels up. Make sure you measure the amount the player has moved is measured in pixels, not in tiles!

Nabushika
  • 364
  • 1
  • 17
0

Here is the way to do it:

main file:

import pygame
from colors import COLORS
from player import Player
from camera import Camera
from tree import Tree

pygame.init()

class Game:
    def __init__(self):
        self.WIDTH = 1000
        self.HEIGHT = 700
        self.screen = pygame.display.set_mode((self.WIDTH, self.HEIGHT))
        pygame.display.set_caption("BlossomCraft")
        self.background_image = pygame.image.load("images/land.png").convert()
        self.clock = pygame.time.Clock()
        self.FPS = 60
        self.game_over = False

        # Creating player
        self.player = Player(2000, 1000, 70, 70, speed=5)
        self.tree = Tree(1000, 300, 100, 100)
        self.camera = Camera(self.WIDTH, self.HEIGHT, self.background_image)
        self.sprite_group = pygame.sprite.Group()
        self.sprite_group.add(self.player)
        self.sprite_group.add(self.tree)


    def run(self):
        while not self.game_over:
            self.update()
            self.handle_events()
            self.clock.tick(self.FPS)

    def handle_events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                self.game_over = True

    def update(self):
        self.camera.custom_draw(self.sprite_group ,self.player, self.screen)
        self.sprite_group.update()

        pygame.display.update()


game = Game()
game.run()

camera file:

import pygame

class Camera:
    def __init__(self, width, height, background):
        self.offset = pygame.math.Vector2() 
        self.width = width
        self.height = height
        self.background = background
        self.ground_rect = self.background.get_rect(topleft = (0, 0))

    def center_target_camera(self, target):
        self.offset.x = target.rect.centerx - self.width//2
        self.offset.y = target.rect.centery - self.height//2

    def custom_draw(self, sprite_group, player, screen):
        self.center_target_camera(player)

        # Blit the background image
        ground_offset = self.ground_rect.topleft - self.offset
        screen.blit(self.background, ground_offset)

        # Blit the sprites
        for sprite in sprite_group:
            offset_pos = sprite.rect.topleft - self.offset
            screen.blit(sprite.image, offset_pos)

player file:

import pygame


class Player(pygame.sprite.Sprite):
    def __init__(self, pos_x, pos_y, width, height, speed):
        super().__init__()
        self.width = width
        self.height = height
        self.pos_x = pos_x 
        self.pos_y = pos_y
        self.original_image = pygame.image.load("images/Knight.png")
        self.image = pygame.transform.scale(self.original_image, (self.width, self.height))
        self.rect = self.image.get_rect()
        self.rect.center = (self.pos_x, self.pos_y)
        self.speed = speed

    def update(self):
        self.move()

    def move(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_RIGHT]:
            self.rect.x += self.speed
        if keys[pygame.K_LEFT]:
            self.rect.x -= self.speed
        if keys[pygame.K_UP]:
            self.rect.y -= self.speed
        if keys[pygame.K_DOWN]:
            self.rect.y += self.speed
DaveL17
  • 1,673
  • 7
  • 24
  • 38