Map scrolling / camera in python with pygame

0

It seems that the camera follows the character but the camera moves slower than the character. I thank you in advance for any help or advice.

Mapa.py

import pygame 
import random
from pygame.locals import *
from Superficie import *
from Pared import *
from Protagonista import *
from os import path

ANCHO=1024
ALTO=768
HALF_WIDTH=int(ANCHO/2)
HALF_HEIGHT=int(ALTO/2)
ROJO=(255,0,0)

class Mapa():

    pared_lista = None
    sprites_enemigos = None
    super_lista=None
    sprite_protagonista=None
    sprite_enemigos=None

    def __init__(self):
        super().__init__()
        self.super_lista = pygame.sprite.Group()
        self.pared_lista = pygame.sprite.Group()
        self.sprite_protagonista = pygame.sprite.Group()
        self.protagonista = Protagonista(1,1)

    def load_maps(self,files):
        game_folder = path.dirname(__file__)
        self.map_data=[]
        with open(path.join(game_folder,files),'r') as f:
            for line in f:
                self.map_data.append(line.strip())

        self.tilewidht = len(self.map_data[0])
        self.tileheight = len(self.map_data)
        self.width = self.tilewidht * 32
        self.height = self.tileheight * 32

    def new(self):
        for row, tiles in enumerate(self.map_data):
            for col, tile in enumerate(tiles):
                if tile == '#':
                    paredes = Pared(col, row)
                    self.pared_lista.add(paredes)
                if tile == '*':
                    superficie = Superficie(col, row)
                    self.super_lista.add(superficie)
                if tile == 'P':
                    superficie = Superficie(col, row)
                    self.super_lista.add(superficie)
                    self.protagonista = Protagonista(col, row)
                    self.sprite_protagonista.add(self.protagonista)
            self.camera = Camera(self.width,self.height)

class Camera:
    def __init__(self,width,height):
        self.camera = pygame.Rect(0,0,width,height)
        self.width = width
        self.height = height

    def apply(self,entity):
        return entity.rect.move(self.camera.topleft)

    def update(self,target):
        x = -target.rect.x / int(ANCHO/2)
        y = -target.rect.y / int(ALTO/2)
        self.camera = pygame.Rect(x,y,self.width,self.height)

Juego.py

import pygame
import random
from pygame.locals import *
from Protagonista import *
from Mapa import *
from Settings import *

pygame.init() 
reloj = pygame.time.Clock()
mapa= Mapa()
lista_todos = pygame.sprite.Group()
pantalla = pygame.display.set_mode((ANCHO, ALTO))

def draw_grid():
    for x in range(0, ANCHO, 32):
        pygame.draw.line(pantalla, AZUL, (x, 0), (x, ALTO))
    for y in range(0, ALTO, 32):
        pygame.draw.line(pantalla, AZUL, (0, y), (ANCHO, y))

def draw():
    lista_todos.add(mapa.pared_lista,mapa.super_lista)
    for sprite in lista_todos:
        pantalla.blit(sprite.image,mapa.camera.apply(sprite))
    mapa.sprite_protagonista.draw(pantalla)
    draw_grid()

def main():

    mapa.load_maps('mapa.txt')
    mapa.new()
    reloj = pygame.time.Clock()
    hecho = False

    while not hecho:
        for evento in pygame.event.get():
            if evento.type == pygame.QUIT:
                hecho = True
            if evento.type == pygame.KEYDOWN:
                if evento.key==pygame.K_ESCAPE:
                    hecho = True
                if evento.key == pygame.K_LEFT:
                    mapa.protagonista.cambiovelocidad(-5,0)
                    dir_target="izq"
                if evento.key == pygame.K_RIGHT:
                    mapa.protagonista.cambiovelocidad(5,0)
                    dir_target ="der"
                if evento.key == pygame.K_UP:
                    mapa.protagonista.cambiovelocidad(0,-5)
                    dir_target ="arr"
                if evento.key == pygame.K_DOWN:
                    mapa.protagonista.cambiovelocidad(0,5)
                    dir_target ="aba"
                if evento.key == pygame.K_q:    
                    main()

            if evento.type == pygame.KEYUP:
                if evento.key == pygame.K_LEFT:
                    mapa.protagonista.cambiovelocidad(5,0)
                if evento.key == pygame.K_RIGHT:
                    mapa.protagonista.cambiovelocidad(-5,0)
                if evento.key == pygame.K_UP:
                    mapa.protagonista.cambiovelocidad(0,5)
                if evento.key == pygame.K_DOWN:
                    mapa.protagonista.cambiovelocidad(0,-5)


        mapa.protagonista.update(mapa.pared_lista)
        mapa.camera.update(mapa.protagonista)

        pantalla.fill(NEGRO)
        draw()
        pygame.display.flip()
        reloj.tick(60)         
    pygame.quit()
main()
pygame.quit()
exit()
    
asked by Santiago Faverio 01.09.2018 в 00:15
source

0 answers