#!/usr/bin/env python # -*- coding: utf-8 -*- # filename: universe.py # # Ceibal Rol # Copyright (C) 2008,2009 Gabriel Balbuena # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # Contact information: # Ceibal Jam http://ceibaljam.org import pygame SCREENSIZE = (640,480) WIDTH = 640 HEIGHT = 480 class World: def __init__(self): pass class Map: def __init__(self,path): self.level = self._loadlevel(path) # matriz del nivel self.tiles = self._loadtiles() # matriz de bloques del nivel self.field = self._loadfield() # crea el fondo self._MapFactory() # coordenada superior izquierda de la cámara. # estas variables intentan alcanzar el valor de las variables # 'destino_camara_x' y 'destino_camara_y'. self.camara_x = 0 self.camara_y = 0 # indican la posición a la que debe llegar la cámara self.destino_camara_x = 0 self.destino_camara_y = 0 def _loadlevel(self, path): #def _cargar_grilla(self, ruta): datafile = file(path, 'rt') level = datafile.readlines() datafile.close() return level def _loadtiles(self): # Carga las texturas y les asigna un id. tiles = [] for n in range(5): tiles.append(pygame.image.load('./data/textures/%d.png' %n).convert_alpha()) return tiles def _loadfield(self): # establece el campo de el mapa, osea el surface reservado para que el factory lo defina. filas = len(self.level) columnas = len(self.level[0]) - 1 ancho = columnas * 96 alto = filas * 96 return pygame.Surface((ancho, alto)).convert() def _MapFactory(self): # crea el Surface del mapa, por eso lo de factory (fabrica) row_lenght = len(self.level) col_lenght = len(self.level[0]) - 1 self.field.fill((0, 0, 0)) for row in range(row_lenght): # iteracion renglones verticalmente for col in range(col_lenght): # iteracion columnas horizontalmente try: index = int(self.level[row][col]) # Index es el valor del nivel en una determinada cordenada except ValueError, strerror: index = int( 0 ) # Si no existe un valor asignado asume que es vacio ese bloque del mapa pos_x = (col) * ((96/4)*2)#(col-row) * (96/3) pos_y = (row) * ((96/4)*2)#(col+row) * (96/3) self.field.blit(self.tiles[index], (pos_x, pos_y+100)) def imprimir(self, screen): # imprime en pantalla origen = (self.camara_x, self.camara_y, WIDTH, HEIGHT) destino = ( 0, 0, WIDTH, HEIGHT) screen.blit(self.field, destino, origen) def update(self): self.camara_x += (self.destino_camara_x - self.camara_x) / 80.0 self.camara_y += (self.destino_camara_y - self.camara_y) / 80.0 def mover(self, dx, dy): self.destino_camara_x += dx self.destino_camara_y += dy self._aplicar_limites() def mirar_a(self, rect): self.destino_camara_x = rect.x - WIDTH / 2 self.destino_camara_y = rect.y - HEIGHT / 2 self._aplicar_limites() def _aplicar_limites(self): if self.destino_camara_x < 0: self.destino_camara_x = 0 if self.destino_camara_y < 0: self.destino_camara_y = 0 if self.destino_camara_x + WIDTH > self.field.get_width(): self.destino_camara_x = self.field.get_width() - WIDTH if self.destino_camara_y + HEIGHT > self.field.get_height(): self.destino_camara_y = self.field.get_height() - HEIGHT pass def is_walkable(self, x, y): return True