Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ogre.py
blob: 6900190f7319c35b61254853e60d7751a6b97e13 (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# filename: ogre.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

"""
Ogre in Rol Ceibal

"""

import pygame
from pygame import *
pygame.init()

#const direccionales
EAST      = 0
NORTHEAST = 1
NORTH     = 2
NORTHWEST = 3
WEST      = 4
SOUTHWEST = 5
SOUTH     = 6
SOUTHEAST = 7

class Ogre(pygame.sprite.Sprite):

	def __init__(self, screen):
		pygame.sprite.Sprite.__init__(self)

		self.screen = screen
		self.image = pygame.image.load("./data/entity/ogre/stopped 0000.png")

		#self.image = self.image.convert_alpha()
		tranColor = self.image.get_at((1, 1))
		self.image.set_colorkey(tranColor)

		self.rect = self.image.get_rect()
		self.rect.center = (320, 240)
		
		# - - - - - - - - - - - - - - - - 
		self.pos_xi = 0 # posicion x
		self.pos_yi = 0 # posicion y
		# - - - - - - - - - - - - - - - - 

		self.imgList = [] # lista de frames
		self.loadPics()
		
		self.dir = EAST
		
		self.frame = 0
		self.delay = 3
		self.pause = self.delay
		self.speed = 0

		self.dxVals = (1,  .7,  0, -.7, -1, -.7, 0, .7)
		self.dyVals = (0, -.7, -1, -.7,  0,  .7, 1, .7)

		self.moving = True


	def loadPics(self):
		fileBase = [
				"./data/entity/ogre/walking e000",
				"./data/entity/ogre/walking ne000",
				"./data/entity/ogre/walking n000",
				"./data/entity/ogre/walking nw000",
				"./data/entity/ogre/walking w000",
				"./data/entity/ogre/walking sw000",
				"./data/entity/ogre/walking s000",
				"./data/entity/ogre/walking se000"]
		for dir in range(8):
			tempList = []
			tempFile = fileBase[dir]
			for frame in range(8):
				imgName = "%s%d.png" % (tempFile, frame)
				tmpImg  = pygame.image.load(imgName)

				#tmpImg.convert_alpha() 
				tmpImg.convert()
				tranColor = tmpImg.get_at((0, 0))
				tmpImg.set_colorkey(tranColor)

				tempList.append(tmpImg)
			self.imgList.append(tempList)
				
	def update(self):
		self.pause -= 1
		if self.pause <= 0:
			self.pause = self.delay
			self.frame += 1
			if self.frame > 7:
				self.frame = 0
			
			self.calcVector()

			if self.moving == False:
				if self.dir == NORTH:
					self.image = pygame.image.load("./data/entity/ogre/stopped 0003.png")
				if self.dir == WEST:
					self.image = pygame.image.load("./data/entity/ogre/stopped 0002.png")
				if self.dir == EAST:
					self.image = pygame.image.load("./data/entity/ogre/stopped 0001.png")
				if self.dir == SOUTH:
					self.image = pygame.image.load("./data/entity/ogre/stopped 0000.png")
			else:
				self.image = self.imgList[self.dir][self.frame]

			self.rect.centerx += self.dx
			self.rect.centery += self.dy


			self.screen.mirar_a(self.rect)	

	def calcVector(self): # calculo vector segun direccion.
		self.dx = self.dxVals[self.dir]
		self.dy = self.dyVals[self.dir]
		
		self.dx *= self.speed /(3)*2 # sacar /2
		self.dy *= self.speed /(3)*2

	def turnUp(self):
		self.dir = NORTH

	def turnLeft(self): 
		self.dir = WEST

	def turnRight(self):
		self.dir = EAST

	def turnDown(self):
		self.dir = SOUTH

	def _print(self, screen): # maallll no respeta la camara
		"Imprime el personaje en la pantalla respetando la camara."
		#x = self.rect.x - self.screen.camara_x
		#y = self.rect.y - self.screen.camara_y
		#screen.blit(self.image, (x, y))
def main():
	print "screen init"
	#screen = pygame.display.set_mode((640, 480))
	screen = pygame.display.set_mode((640,480),FULLSCREEN)
	pygame.display.set_caption(".... TESTING ....")

	background = pygame.Surface(screen.get_size())
	background.fill((200, 255, 200))
	screen.blit(background, (0, 0))
	

	ogre = Ogre(screen)

	# Probando grupos de sprites para manejo de coliciones.
	allSprites = pygame.sprite.Group(ogre)

	# probando time
	clock = pygame.time.Clock()
	
	run = True

	while run:
		clock.tick(30)
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				run = False
			elif event.type == pygame.KEYDOWN:
				if event.key == pygame.K_UP:
					ogre.moving = True
					ogre.speed += 10
					ogre.turnUp()
				if event.key == pygame.K_DOWN:
					ogre.moving = True
					ogre.speed += 10
					ogre.turnDown()
				if event.key == pygame.K_LEFT:
					ogre.moving = True
					ogre.speed += 10
					ogre.turnLeft()
				if event.key == pygame.K_RIGHT:
					ogre.moving = True
					ogre.speed += 10
					ogre.turnRight()

		print "ogre speed:", ogre.speed

		if ogre.speed > 0:
			if ogre.speed >= 35:
				ogre.speed = 35

			ogre.speed = ogre.speed - 1

			if ogre.speed == 0:
				ogre.moving = False

		allSprites.clear(screen, background)
		allSprites.update()
		allSprites.draw(screen)


		pygame.display.flip()


if __name__ == "__main__":
    main()