1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
#!/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 <http://www.gnu.org/licenses/>.
#
# 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
|