import pygame import os import random import math from AnimatedSprite import AnimatedSprite STOP = 0 LEFT = 1 RIGHT = 2 UP = 3 DOWN = 4 TALKING = 5 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 elif self.move == TALKING: pass 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() elif self.move == TALKING: pass 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_talking_range(self, screen): pygame.draw.arc(screen, (200, 0, 0), (self.x+9, self.y, 48, 48), -math.pi/4, math.pi/4, 2) pygame.draw.arc(screen, (200, 0, 0), (self.x+9, self.y, 48, 48), math.pi/4*3, math.pi/4*5, 2) def draw_talking(self, screen): pygame.draw.arc(screen, (0, 0, 200), (self.x+9, self.y, 48, 48), -math.pi/4, math.pi/4, 2) pygame.draw.arc(screen, (0, 0, 200), (self.x+9, self.y, 48, 48), math.pi/4*3, math.pi/4*5, 2) 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() fms = [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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] playing = True talking_range = False talking = False 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 and not talking: sp.update_move(UP) sp.update_frame(0) elif event.key == pygame.K_DOWN and not talking: sp.update_move(DOWN) sp.update_frame(8) elif event.key == pygame.K_LEFT and not talking: sp.update_move(LEFT) sp.update_frame(16) elif event.key == pygame.K_RIGHT and not talking: sp.update_move(RIGHT) sp.update_frame(24) elif event.key == pygame.K_SPACE and talking_range: if not talking: talking = True sp.update_move(TALKING) sp2.update_move(TALKING) print("Talking...") else: talking = False sp.update_move(STOP) sp2.update_move(STOP) print("Not talking...") elif event.type == pygame.KEYUP: keys_pressed = pygame.key.get_pressed() if event.key == pygame.K_UP and not talking: if keys_pressed[pygame.K_LEFT]: sp.update_move(LEFT) sp.update_frame(16) elif keys_pressed[pygame.K_RIGHT]: sp.update_move(RIGHT) sp.update_frame(24) elif keys_pressed[pygame.K_DOWN]: sp.update_move(DOWN) sp.update_frame(8) else: sp.update_move(0) sp.update_frame(0) elif event.key == pygame.K_DOWN and not talking: if keys_pressed[pygame.K_LEFT]: sp.update_move(LEFT) sp.update_frame(16) elif keys_pressed[pygame.K_RIGHT]: sp.update_move(RIGHT) sp.update_frame(24) elif keys_pressed[pygame.K_UP]: sp.update_move(UP) sp.update_frame(0) else: sp.update_move(0) sp.update_frame(8) elif event.key == pygame.K_LEFT and not talking: if keys_pressed[pygame.K_UP]: sp.update_move(UP) sp.update_frame(0) elif keys_pressed[pygame.K_DOWN]: sp.update_move(DOWN) sp.update_frame(8) elif keys_pressed[pygame.K_RIGHT]: sp.update_move(RIGHT) sp.update_frame(24) else: sp.update_move(0) sp.update_frame(16) elif event.key == pygame.K_RIGHT and not talking: if keys_pressed[pygame.K_UP]: sp.update_move(UP) sp.update_frame(0) elif keys_pressed[pygame.K_DOWN]: sp.update_move(DOWN) sp.update_frame(8) elif keys_pressed[pygame.K_LEFT]: sp.update_move(LEFT) sp.update_frame(16) else: sp.update_move(0) sp.update_frame(24) 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) (ulx, uly) = sp.upper_left_tile() (ulx2, uly2) = sp2.upper_left_tile() if ulx-ulx2 >= -1 and ulx-ulx2 <=1 \ and uly-uly2 >= -1 and uly-uly2<=1: talking_range = True else: talking_range = False if sp.y < sp2.y: sp.render(screen) sp2.render(screen) else: sp2.render(screen) sp.render(screen) if talking_range and not talking: sp2.draw_talking_range(screen) elif talking: sp2.draw_talking(screen) pygame.display.flip()