Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGabriel Eirea <geirea@gmail.com>2011-01-13 13:02:37 (GMT)
committer Gabriel Eirea <geirea@gmail.com>2011-01-13 13:02:37 (GMT)
commit39a83c2b1510ba18dcc5cab96a31fe62c7e72d85 (patch)
tree6e82de43bafd2fc0be179171e7192d92d12ff61d
parent5539bb744513f2b235b069eb5f9b14ab71a68c02 (diff)
Separated AnimatedSprite class from rest
-rw-r--r--#Game.py#131
-rw-r--r--AnimatedSprite.py274
-rw-r--r--AnimatedSprite.pycbin3385 -> 2519 bytes
-rw-r--r--DerechosSprites.py312
4 files changed, 453 insertions, 264 deletions
diff --git a/#Game.py# b/#Game.py#
index 0b2c4ea..7541cb9 100644
--- a/#Game.py#
+++ b/#Game.py#
@@ -5,17 +5,79 @@ from AnimatedSprite import *
class Game():
def __init__(self):
- pygame.display.init()
- self.screen = pygame.display.set_mode((800,600))
- self.clock = pygame.time.Clock()
+# pygame.display.init()
+# self.screen = pygame.display.set_mode((1200,900))
+# self.clock = pygame.time.Clock()
# load images
- images_main = self.load_sprite_group(50,50,"main.png")
+# images_main = self.load_sprite_group(50,50,"main.png")
# load levels
# setup objects
- self.me = AnimatedSprite(images_main,30)
+# self.me = AnimatedSprite(images_main,30)
# start
- print("initializing...")
+
+ print("initializing game...")
+
+ pygame.display.init()
+ self.screen = pygame.display.set_mode((1200,900))
+ self.clock = pygame.time.Clock()
+
+ print("loading images...")
+
+ self.images = []
+ master_image = pygame.image.load(os.path.join('images',
+ 'xoxi-ninios-sprite-01.png')).convert_alpha()
+ master_width, master_height = master_image.get_size()
+ w = int(master_width/4)
+ h = int(master_height/4)
+ for i in xrange(4):
+ self.images.append(master_image.subsurface((i*w,0,w,h)))
+ for i in xrange(4):
+ self.images.append(master_image.subsurface((i*w,h,w,h)))
+ for i in xrange(4):
+ self.images.append(master_image.subsurface((i*w,2*h,w,h)))
+ for i in xrange(4):
+ self.images.append(master_image.subsurface((i*w,3*h,w,h)))
+
+ self.bg_image = pygame.image.load(os.path.join('images',
+ 'derechos-plaza-fondo.jpg')).convert_alpha()
+
+ sps = [0, 1, 0, 2, 0, 1, 0, 3, 4, 5, 4, 6, 4, 5, 4, 7, 8, 9, 8, 10,
+ 8, 9, 8, 11, 15, 13, 15, 14, 15, 13, 15, 12]
+ nf = [1, 2, 3, 4, 5, 6, 7, 0, 9, 10, 11, 12, 13, 14, 15, 8, 17, 18, 19,
+ 20, 21, 22, 23, 16, 25, 26, 27, 28, 29, 30, 31, 24]
+ dxs = [5, 5, 5, 5, 5, -5, -5, -5, -5, -5]
+ dys = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
+
+ self.sp = AnimatedSprite(images, sps, nf, dxs, dys, 50, 400, 10)
+ self.sp2 = AnimatedSprite(images, sps, nf, dxs, dys, 50, 400, 10)
+
+ self.grilla = [[0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1],
+ [0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0],
+ [0,0,1,1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,0,0,0,0],
+ [0,0,1,1,1,1,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1],
+ [0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1],
+ [0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,0,0,1,1,1,0,0,1,1],
+ [1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,0,0,1,1,1,0,0,1,1],
+ [0,0,0,0,0,0,0,1,1,1,1,0,0,1,1,0,0,1,1,1,0,0,1,1],
+ [0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0],
+ [0,0,1,1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0],
+ [0,0,1,0,0,0,0,1,1,1,1,0,0,1,1,1,1,0,0,1,1,0,0,0],
+ [0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,1,1,1,0,0],
+ [0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,1,1,1,0,0],
+ [0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1],
+ [1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1],
+ [1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
+ [1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1],
+ [1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1]]
+
+
+ playing = True
+
+ sp.updateMove(0)
+ sp2.updateMove(random.randint(1,4))
+
+
def load_sprite_group(self, w, h, filename):
'''
@@ -52,8 +114,61 @@ class Game():
def main():
- game = Game()
- game.start()
+# game = Game()
+# game.start()
+
+
+
+ while playing:
+ for event in pygame.event.get():
+ if event.type == pygame.QUIT:
+ playing = False
+ elif event.type == pygame.MOUSEBUTTONDOWN:
+ pass
+ elif event.type == pygame.KEYDOWN:
+ if event.key == pygame.K_UP:
+ sp.updateMove(UP)
+ sp.updateFrame(0)
+ if event.key == pygame.K_DOWN:
+ sp.updateMove(DOWN)
+ sp.updateFrame(8)
+ if event.key == pygame.K_LEFT:
+ sp.updateMove(LEFT)
+ sp.updateFrame(16)
+ if event.key == pygame.K_RIGHT:
+ sp.updateMove(RIGHT)
+ sp.updateFrame(24)
+ elif event.type == pygame.KEYUP:
+ if event.key == pygame.K_UP:
+ sp.updateMove(0)
+ sp.updateFrame(0)
+ if event.key == pygame.K_DOWN:
+ sp.updateMove(0)
+ sp.updateFrame(8)
+ if event.key == pygame.K_LEFT:
+ sp.updateMove(0)
+ sp.updateFrame(16)
+ if event.key == pygame.K_RIGHT:
+ sp.updateMove(0)
+ sp.updateFrame(24)
+
+ sp.domove(grilla)
+ sp2.ai(grilla)
+
+ screen.blit(bg_image, (0, 0))
+
+# dibujar_grilla(grilla,screen)
+
+ time_passed = clock.tick(20)
+ sp.update(time_passed)
+ sp2.update(time_passed)
+
+ sp.render(screen)
+ sp2.render(screen)
+
+ pygame.display.flip()
+
+
if __name__ == "__main__":
main()
diff --git a/AnimatedSprite.py b/AnimatedSprite.py
index 084f03f..0eaab83 100644
--- a/AnimatedSprite.py
+++ b/AnimatedSprite.py
@@ -1,18 +1,13 @@
import pygame
-import os
-import random
-
-LEFT = 1
-RIGHT = 2
-UP = 3
-DOWN =4
+#import os
+#import random
class AnimatedSprite(pygame.sprite.Sprite):
"""
AnimatedSprite is a custom Sprite class for animated objects
images: list with sprite images
- sprites: list with image# for every frame
+ frames: list with image# for every frame
next_frames: list with next frame from current one
dxs: list with dx for current frame
dys: list with dy for current frame
@@ -21,19 +16,17 @@ class AnimatedSprite(pygame.sprite.Sprite):
fps: fps
"""
- def __init__(self, images, sprites, next_frames,
+ def __init__(self, images, frames, next_frames,
dxs, dys, xini, yini, fps = 10):
pygame.sprite.Sprite.__init__(self)
self._images = images
- # Track the time we started, and the time between updates.
- # Then we can figure out when we have to switch the image.
self._start = pygame.time.get_ticks()
self._delay = 1000 / fps
self._last_update = 0
self._frame = 0
- self._sprites = sprites
+ self._frames = frames
self._next_frames = next_frames
self._dxs = dxs
self._dys = dys
@@ -41,266 +34,35 @@ class AnimatedSprite(pygame.sprite.Sprite):
self.y = yini
self.move = 0
- self.image = self._images[self._sprites[self._frame]]
-
- # Call update to set our first image.
- self.update(pygame.time.get_ticks())
+ self.image = self._images[self._frames[self._frame]]
- def update(self, t):
- # Note that this doesn't work if it's been more that self._delay
- # time between calls to update(); we only update the image once
- # then, but it really should be updated twice.
+# self.update(pygame.time.get_ticks())
-# print t
+ def update(self, t, grid):
# if t - self._last_update > self._delay:
-# if self._frame >= 2: #len(self._images):
-# self._frame = 0
- print self._frame
- self.image = self._images[self._sprites[self._frame]]
-# self.x += self._dxs[self._frame]
-# if move == RIGHT:
-# self.x += 5
-# elif move == LEFT:
-# self.x -= 5
-# self.y += self._dys[self._frame]
+# print("Can't reach fps.")
+
+ self.do_move(grid)
+ self.image = self._images[self._frames[self._frame]]
self._last_update = t
def render(self, screen):
-# self.update(pygame.time.get_ticks())
screen.blit(self.image, (self.x, self.y))
# pygame.draw.rect(screen,(0,0,0),(self.x+15, self.y+38, 38, 38),1)
- def domove(self, grilla):
- if self.move == RIGHT:
- if self.x+62<1200:
- if grilla[(self.y+78)/50][(self.x+62)/50] == 0 and \
- grilla[(self.y+40)/50][(self.x+62)/50] == 0:
- self.x += 8
- self._frame = self._next_frames[self._frame]
- else:
- self.x += 8 - (self.x+62)%50 -1
- else:
- self.x = 1146
- elif self.move == LEFT:
- if self.x+8 > 0:
- if grilla[(self.y+78)/50][(self.x+8)/50] == 0 and \
- grilla[(self.y+40)/50][(self.x+8)/50] == 0:
- self.x -= 8
- self._frame = self._next_frames[self._frame]
- else:
- self.x += -8 + 50 - (self.x+8)%50 +1
- else:
- self.x = -16
- elif self.move == UP:
- if self.y+16 > 0:
- if grilla[(self.y+34)/50][(self.x+16)/50] == 0 and \
- grilla[(self.y+34)/50][(self.x+54)/50] == 0:
- self.y -= 6
- self._frame = self._next_frames[self._frame]
- else:
- self.y += -6 + 50 - (self.y+34)%50 +1
- else:
- self.y = -22
- elif self.move == DOWN:
- if self.y+84 < 900:
- if grilla[(self.y+84)/50][(self.x+16)/50] == 0 and \
- grilla[(self.y+84)/50][(self.x+54)/50] == 0:
- self.y += 6
- self._frame = self._next_frames[self._frame]
- else:
- self.y += 6 - (self.y+84)%50 -1
- else:
- self.y = 900-78
-
- def ai(self, grilla):
- if self.move == RIGHT:
- if self.x+62<1200:
- if grilla[(self.y+78)/50][(self.x+62)/50] == 0 and \
- grilla[(self.y+40)/50][(self.x+62)/50] == 0:
- self.x += 8
- self._frame = self._next_frames[self._frame]
- else:
- self.x += 8 - (self.x+62)%50 -1
- self.move = random.randint(1,4)
- self.startupFrame()
- else:
- self.x = 1146
- self.move = random.randint(1,4)
- self.startupFrame()
- elif self.move == LEFT:
- if self.x+8 > 0:
- if grilla[(self.y+78)/50][(self.x+8)/50] == 0 and \
- grilla[(self.y+40)/50][(self.x+8)/50] == 0:
- self.x -= 8
- self._frame = self._next_frames[self._frame]
- else:
- self.x += -8 + 50 - (self.x+8)%50 +1
- self.move = random.randint(1,4)
- self.startupFrame()
- else:
- self.x = -16
- self.move = random.randint(1,4)
- self.startupFrame()
- elif self.move == UP:
- if self.y+16 > 0:
- if grilla[(self.y+34)/50][(self.x+16)/50] == 0 and \
- grilla[(self.y+34)/50][(self.x+54)/50] == 0:
- self.y -= 6
- self._frame = self._next_frames[self._frame]
- else:
- self.y += -6 + 50 - (self.y+34)%50 +1
- self.move = random.randint(1,4)
- self.startupFrame()
- else:
- self.y = -22
- self.move = random.randint(1,4)
- self.startupFrame()
- elif self.move == DOWN:
- if self.y+84 < 900:
- if grilla[(self.y+84)/50][(self.x+16)/50] == 0 and \
- grilla[(self.y+84)/50][(self.x+54)/50] == 0:
- self.y += 6
- self._frame = self._next_frames[self._frame]
- else:
- self.y += 6 - (self.y+84)%50 -1
- self.move = random.randint(1,4)
- self.startupFrame()
- else:
- self.y = 900-78
- self.move = random.randint(1,4)
- self.startupFrame()
+ def do_move(self, grid):
+ pass
-
- def updateFrame(self, fr):
+ def update_frame(self, fr):
self._frame = fr
- def startupFrame(self):
- if self.move == UP:
- self._frame = 0
- elif self.move == DOWN:
- self._frame = 8
- elif self.move == LEFT:
- self._frame = 16
- elif self.move == RIGHT:
- self._frame = 24
-
-
- def updateMove(self,move):
+ def update_move(self,move):
self.move = move
-
-def dibujar_grilla(grilla,screen):
- for yi in range(18):
- for xi in range(24):
- if grilla[yi][xi] == 1:
- pygame.draw.rect(screen,(100,100,100),(xi*50,yi*50,50,50),2)
-
-
-
if __name__ == "__main__":
- pygame.display.init()
- screen = pygame.display.set_mode((1200,900))
- clock = pygame.time.Clock()
-
- images = []
- master_image = pygame.image.load(os.path.join('images', 'xoxi-ninios-sprite-01.png')).convert_alpha()
- master_width, master_height = master_image.get_size()
- w = int(master_width/4)
- h = int(master_height/4)
- for i in xrange(4):
- images.append(master_image.subsurface((i*w,0,w,h)))
- for i in xrange(4):
- images.append(master_image.subsurface((i*w,h,w,h)))
- for i in xrange(4):
- images.append(master_image.subsurface((i*w,2*h,w,h)))
- for i in xrange(4):
- images.append(master_image.subsurface((i*w,3*h,w,h)))
-
- bg_image = pygame.image.load(os.path.join('images', 'derechos-plaza-fondo.jpg')).convert_alpha()
-
- sps = [0, 1, 0, 2, 0, 1, 0, 3, 4, 5, 4, 6, 4, 5, 4, 7, 8, 9, 8, 10,
- 8, 9, 8, 11, 15, 13, 15, 14, 15, 13, 15, 12]
- nf = [1, 2, 3, 4, 5, 6, 7, 0, 9, 10, 11, 12, 13, 14, 15, 8, 17, 18, 19,
- 20, 21, 22, 23, 16, 25, 26, 27, 28, 29, 30, 31, 24]
- dxs = [5, 5, 5, 5, 5, -5, -5, -5, -5, -5]
- dys = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
-
- sp = AnimatedSprite(images, sps, nf, dxs, dys, 50, 400, 10)
- sp2 = AnimatedSprite(images, sps, nf, dxs, dys, 50, 400, 10)
-
- grilla = [[0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1],
- [0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0],
- [0,0,1,1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,0,0,0,0],
- [0,0,1,1,1,1,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1],
- [0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1],
- [0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,0,0,1,1,1,0,0,1,1],
- [1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,0,0,1,1,1,0,0,1,1],
- [0,0,0,0,0,0,0,1,1,1,1,0,0,1,1,0,0,1,1,1,0,0,1,1],
- [0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0],
- [0,0,1,1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0],
- [0,0,1,0,0,0,0,1,1,1,1,0,0,1,1,1,1,0,0,1,1,0,0,0],
- [0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,1,1,1,0,0],
- [0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,1,1,1,0,0],
- [0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1],
- [1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1],
- [1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1],
- [1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1]]
-
-
- playing = True
-
- sp.updateMove(0)
- sp2.updateMove(random.randint(1,4))
-
- while playing:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- playing = False
- elif event.type == pygame.MOUSEBUTTONDOWN:
- pass
- elif event.type == pygame.KEYDOWN:
- if event.key == pygame.K_UP:
- sp.updateMove(UP)
- sp.updateFrame(0)
- if event.key == pygame.K_DOWN:
- sp.updateMove(DOWN)
- sp.updateFrame(8)
- if event.key == pygame.K_LEFT:
- sp.updateMove(LEFT)
- sp.updateFrame(16)
- if event.key == pygame.K_RIGHT:
- sp.updateMove(RIGHT)
- sp.updateFrame(24)
- elif event.type == pygame.KEYUP:
- if event.key == pygame.K_UP:
- sp.updateMove(0)
- sp.updateFrame(0)
- if event.key == pygame.K_DOWN:
- sp.updateMove(0)
- sp.updateFrame(8)
- if event.key == pygame.K_LEFT:
- sp.updateMove(0)
- sp.updateFrame(16)
- if event.key == pygame.K_RIGHT:
- sp.updateMove(0)
- sp.updateFrame(24)
-
- sp.domove(grilla)
- sp2.ai(grilla)
-
- screen.blit(bg_image, (0, 0))
-
-# dibujar_grilla(grilla,screen)
-
- time_passed = clock.tick(20)
- sp.update(time_passed)
- sp2.update(time_passed)
- sp.render(screen)
- sp2.render(screen)
-
- pygame.display.flip()
+ print("You can't run this file.")
+ print("You have to import AnimatedSprite from another Python file.")
diff --git a/AnimatedSprite.pyc b/AnimatedSprite.pyc
index 30a0149..75fc1cf 100644
--- a/AnimatedSprite.pyc
+++ b/AnimatedSprite.pyc
Binary files differ
diff --git a/DerechosSprites.py b/DerechosSprites.py
new file mode 100644
index 0000000..48f26f8
--- /dev/null
+++ b/DerechosSprites.py
@@ -0,0 +1,312 @@
+import pygame
+import os
+import random
+from AnimatedSprite import AnimatedSprite
+
+STOP = 0
+LEFT = 1
+RIGHT = 2
+UP = 3
+DOWN =4
+
+class MainSprite(AnimatedSprite):
+ """
+ MainSprite is an AnimatedSprite class for the main character
+
+ images: list with sprite images
+ frames: list with image# for every frame
+ next_frames: list with next frame from current one
+ dxs: list with dx for current frame
+ dys: list with dy for current frame
+ xini: initial x position
+ yini: initial y position
+ fps: fps
+ """
+
+ def __init__(self, images, frames, next_frames,
+ dxs, dys, xini, yini, fps = 10):
+
+ AnimatedSprite.__init__(self, images, frames, next_frames,
+ dxs, dys, xini, yini, fps)
+ self.move = STOP
+
+
+ def do_move(self,grid):
+
+ if self.move == RIGHT:
+ if self.x+62<1200:
+ if grid[(self.y+78)/50][(self.x+62)/50] == 0 and \
+ grid[(self.y+40)/50][(self.x+62)/50] == 0:
+ self.x += 8
+ self._frame = self._next_frames[self._frame]
+ else:
+ self.x += 8 - (self.x+62)%50 -1
+ else:
+ self.x = 1146
+ elif self.move == LEFT:
+ if self.x+8 > 0:
+ if grid[(self.y+78)/50][(self.x+8)/50] == 0 and \
+ grid[(self.y+40)/50][(self.x+8)/50] == 0:
+ self.x -= 8
+ self._frame = self._next_frames[self._frame]
+ else:
+ self.x += -8 + 50 - (self.x+8)%50 +1
+ else:
+ self.x = -16
+ elif self.move == UP:
+ if self.y+16 > 0:
+ if grid[(self.y+34)/50][(self.x+16)/50] == 0 and \
+ grid[(self.y+34)/50][(self.x+54)/50] == 0:
+ self.y -= 6
+ self._frame = self._next_frames[self._frame]
+ else:
+ self.y += -6 + 50 - (self.y+34)%50 +1
+ else:
+ self.y = -22
+ elif self.move == DOWN:
+ if self.y+84 < 900:
+ if grid[(self.y+84)/50][(self.x+16)/50] == 0 and \
+ grid[(self.y+84)/50][(self.x+54)/50] == 0:
+ self.y += 6
+ self._frame = self._next_frames[self._frame]
+ else:
+ self.y += 6 - (self.y+84)%50 -1
+ else:
+ self.y = 900-78
+
+
+class BoySprite(AnimatedSprite):
+ """
+ BoySprite is an AnimatedSprite class for the boys in the game
+
+ images: list with sprite images
+ frames: list with image# for every frame
+ next_frames: list with next frame from current one
+ dxs: list with dx for current frame
+ dys: list with dy for current frame
+ xini: initial x position
+ yini: initial y position
+ fps: fps
+ """
+
+ def __init__(self, images, frames, next_frames,
+ dxs, dys, xini, yini, fps = 10):
+
+ AnimatedSprite.__init__(self, images, frames, next_frames,
+ dxs, dys, xini, yini, fps)
+ self.move = random.randint(0,4)
+
+ def do_move(self,grid):
+
+ if self.move == RIGHT:
+ rn = random.randint(1,100)
+ if rn > 97:
+ self.startup_frame()
+ self.move = STOP
+ elif self.x+62<1200:
+ if grid[(self.y+78)/50][(self.x+62)/50] == 0 and \
+ grid[(self.y+40)/50][(self.x+62)/50] == 0:
+ self.x += 8
+ self._frame = self._next_frames[self._frame]
+ else:
+ self.x += 8 - (self.x+62)%50 -1
+ self.move = random.randint(1,4)
+ self.startup_frame()
+ else:
+ self.x = 1146
+ self.move = random.randint(1,4)
+ self.startup_frame()
+ elif self.move == LEFT:
+ rn = random.randint(1,100)
+ if rn > 97:
+ self.startup_frame()
+ self.move = STOP
+ elif self.x+8 > 0:
+ if grid[(self.y+78)/50][(self.x+8)/50] == 0 and \
+ grid[(self.y+40)/50][(self.x+8)/50] == 0:
+ self.x -= 8
+ self._frame = self._next_frames[self._frame]
+ else:
+ self.x += -8 + 50 - (self.x+8)%50 +1
+ self.move = random.randint(1,4)
+ self.startup_frame()
+ else:
+ self.x = -16
+ self.move = random.randint(1,4)
+ self.startup_frame()
+ elif self.move == UP:
+ rn = random.randint(1,100)
+ if rn > 97:
+ self.startup_frame()
+ self.move = STOP
+ elif self.y+16 > 0:
+ if grid[(self.y+34)/50][(self.x+16)/50] == 0 and \
+ grid[(self.y+34)/50][(self.x+54)/50] == 0:
+ self.y -= 6
+ self._frame = self._next_frames[self._frame]
+ else:
+ self.y += -6 + 50 - (self.y+34)%50 +1
+ self.move = random.randint(1,4)
+ self.startup_frame()
+ else:
+ self.y = -22
+ self.move = random.randint(1,4)
+ self.startup_frame()
+ elif self.move == DOWN:
+ rn = random.randint(1,100)
+ if rn > 97:
+ self.startup_frame()
+ self.move = STOP
+ elif self.y+84 < 900:
+ if grid[(self.y+84)/50][(self.x+16)/50] == 0 and \
+ grid[(self.y+84)/50][(self.x+54)/50] == 0:
+ self.y += 6
+ self._frame = self._next_frames[self._frame]
+ else:
+ self.y += 6 - (self.y+84)%50 -1
+ self.move = random.randint(1,4)
+ self.startup_frame()
+ else:
+ self.y = 900-78
+ self.move = random.randint(1,4)
+ self.startup_frame()
+ elif self.move == STOP:
+ rn = random.randint(1,100)
+ if rn > 98:
+ self.move = random.randint(1,4)
+ self.startup_frame()
+
+ def startup_frame(self):
+ if self.move == UP:
+ self._frame = 0
+ elif self.move == DOWN:
+ self._frame = 8
+ elif self.move == LEFT:
+ self._frame = 16
+ elif self.move == RIGHT:
+ self._frame = 24
+
+
+def draw_grid(grid,screen):
+ for yi in range(18):
+ for xi in range(24):
+ if grid[yi][xi] == 1:
+ pygame.draw.rect(screen,(100,100,100),(xi*50,yi*50,50,50),2)
+
+
+if __name__ == "__main__":
+
+ pygame.display.init()
+ screen = pygame.display.set_mode((1200,900))
+ clock = pygame.time.Clock()
+
+ images = []
+ master_image = pygame.image.load(os.path.join('images',
+ 'xoxi-ninios-sprite-01.png')).convert_alpha()
+ master_width, master_height = master_image.get_size()
+ w = int(master_width/4)
+ h = int(master_height/4)
+ for i in xrange(4):
+ images.append(master_image.subsurface((i*w,0,w,h)))
+ for i in xrange(4):
+ images.append(master_image.subsurface((i*w,h,w,h)))
+ for i in xrange(4):
+ images.append(master_image.subsurface((i*w,2*h,w,h)))
+ for i in xrange(4):
+ images.append(master_image.subsurface((i*w,3*h,w,h)))
+
+ bg_image = pygame.image.load(os.path.join('images',
+ 'derechos-plaza-fondo.jpg')).convert_alpha()
+
+ sps = [0, 1, 0, 2, 0, 1, 0, 3, # up
+ 4, 5, 4, 6, 4, 5, 4, 7, # down
+ 8, 9, 8, 10, 8, 9, 8, 11, # left
+ 15, 13, 15, 14, 15, 13, 15, 12] # right
+ nf = [1, 2, 3, 4, 5, 6, 7, 0,
+ 9, 10, 11, 12, 13, 14, 15, 8,
+ 17, 18, 19, 20, 21, 22, 23, 16,
+ 25, 26, 27, 28, 29, 30, 31, 24]
+ dxs = [0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ -8, -8, -8, -8, -8, -8, -8, -8,
+ 8, 8, 8, 8, 8, 8, 8, 8]
+ dys = [-6, -6, -6, -6, -6, -6, -6, -6,
+ 6, 6, 6, 6, 6, 6, 6, 6,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0]
+
+ sp = MainSprite(images, sps, nf, dxs, dys, 50, 400, 10)
+ sp2 = BoySprite(images, sps, nf, dxs, dys, 50, 400, 10)
+
+ grid = [[0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1],
+ [0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0],
+ [0,0,1,1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,0,0,0,0],
+ [0,0,1,1,1,1,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1],
+ [0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1],
+ [0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,0,0,1,1,1,0,0,1,1],
+ [1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,0,0,1,1,1,0,0,1,1],
+ [0,0,0,0,0,0,0,1,1,1,1,0,0,1,1,0,0,1,1,1,0,0,1,1],
+ [0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0],
+ [0,0,1,1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0],
+ [0,0,1,0,0,0,0,1,1,1,1,0,0,1,1,1,1,0,0,1,1,0,0,0],
+ [0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,1,1,1,0,0],
+ [0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,1,1,1,0,0],
+ [0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1],
+ [1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1],
+ [1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
+ [1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1],
+ [1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1]]
+
+
+ playing = True
+
+ while playing:
+ for event in pygame.event.get():
+ if event.type == pygame.QUIT:
+ playing = False
+ elif event.type == pygame.MOUSEBUTTONDOWN:
+ pass
+ elif event.type == pygame.KEYDOWN:
+ if event.key == pygame.K_UP:
+ sp.update_move(UP)
+ sp.update_frame(0)
+ if event.key == pygame.K_DOWN:
+ sp.update_move(DOWN)
+ sp.update_frame(8)
+ if event.key == pygame.K_LEFT:
+ sp.update_move(LEFT)
+ sp.update_frame(16)
+ if event.key == pygame.K_RIGHT:
+ sp.update_move(RIGHT)
+ sp.update_frame(24)
+ elif event.type == pygame.KEYUP:
+ if event.key == pygame.K_UP:
+ sp.update_move(0)
+ sp.update_frame(0)
+ if event.key == pygame.K_DOWN:
+ sp.update_move(0)
+ sp.update_frame(8)
+ if event.key == pygame.K_LEFT:
+ sp.update_move(0)
+ sp.update_frame(16)
+ if event.key == pygame.K_RIGHT:
+ sp.update_move(0)
+ sp.update_frame(24)
+
+# sp.domove(grid)
+# sp2.ai(grid)
+
+ screen.blit(bg_image, (0, 0))
+
+# draw_grid(grid,screen)
+
+ time_passed = clock.tick(20)
+
+ sp.update(time_passed, grid)
+ sp2.update(time_passed, grid)
+
+ sp.render(screen)
+ sp2.render(screen)
+
+ pygame.display.flip()
+