Foros del Web » Programando para Internet » Python »

Pygame: Caminar como en pokemon

Estas en el tema de Pygame: Caminar como en pokemon en el foro de Python en Foros del Web. main.py @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código python: Ver original #!/usr/bin/env python #coding: UTF-8   import pygame import esenario import actor from pygame import K_LEFT , K_RIGHT , ...
  #1 (permalink)  
Antiguo 03/05/2009, 16:57
Avatar de razpeitia
Moderador
 
Fecha de Ingreso: marzo-2005
Ubicación: Monterrey, México
Mensajes: 7.321
Antigüedad: 19 años, 1 mes
Puntos: 1360
Pygame: Caminar como en pokemon

main.py
Código python:
Ver original
  1. #!/usr/bin/env python
  2. #coding: UTF-8
  3.  
  4. import pygame
  5. import esenario
  6. import actor
  7. from pygame import K_LEFT, K_RIGHT, K_UP, K_DOWN, K_ESCAPE
  8.  
  9. blue = (0, 0, 255)
  10. black = (0, 0, 0)
  11.  
  12. screen = esenario.esenario(320, 240, "Caminar")
  13. player = actor.actor(blue, (0, 0))
  14.  
  15. while 1:
  16.     for event in pygame.event.get():
  17.         if event.type == pygame.QUIT:
  18.             exit()
  19.    
  20.     keyboard = pygame.key.get_pressed()
  21.     if keyboard[K_ESCAPE]:
  22.         exit()
  23.    
  24.     player.caminar()
  25.        
  26.     screen.fill(black)
  27.     screen.blit(player.image, player.rect)
  28.     pygame.display.flip()
  29.     screen.tick(60)

actor.py
Código python:
Ver original
  1. import pygame
  2. from pygame import K_LEFT, K_RIGHT, K_UP, K_DOWN, K_ESCAPE
  3. class actor(pygame.sprite.Sprite):
  4.     def __init__(self, color, initial_position):
  5.         pygame.sprite.Sprite.__init__(self)
  6.         self.image = pygame.Surface((16, 16))
  7.         self.image.fill(color)
  8.         self.rect = self.image.get_rect()
  9.         self.rect.topleft = initial_position
  10.         self.lock = False
  11.         self.lock_e = False
  12.         self.lock_t = True
  13.         self.t = 0
  14.         self.c = 0
  15.         self.vx = 0
  16.         self.vy = 0
  17.        
  18.     def move(self, vx, vy):
  19.         self.rect = self.rect.move(vx, vy)
  20.        
  21.     def estado(self):
  22.         keyboard = pygame.key.get_pressed()
  23.         if keyboard[K_LEFT] or keyboard[K_RIGHT] or keyboard[K_UP] or keyboard[K_DOWN]:
  24.             if not self.lock_e:
  25.                 if keyboard[K_LEFT]:
  26.                     self.vx = -1
  27.                 elif keyboard[K_RIGHT]:
  28.                     self.vx = 1
  29.                 elif keyboard[K_UP]:
  30.                     self.vy = -1
  31.                 elif keyboard[K_DOWN]:
  32.                     self.vy = 1
  33.                 self.lock_e = True
  34.                 self.lock = True
  35.                
  36.    
  37.     def caminar(self):
  38.         if self.lock_t:
  39.             self.estado()
  40.         if self.lock:
  41.             self.lock = True
  42.             self.c += self.vx or self.vy
  43.             self.move(self.vx, self.vy)
  44.             if self.c >= 8 or self.c <= -8:
  45.                 self.vx = 0
  46.                 self.vy = 0
  47.                 self.c = 0
  48.                 self.lock = False
  49.                 self.lock_e = False
  50.                 self.lock_t = False
  51.                 self.t = 0
  52.         if self.t >= 2:
  53.             self.lock_t = True
  54.             self.t = 2
  55.         else:
  56.             self.t += 1

esenario.py
Código python:
Ver original
  1. import pygame
  2.  
  3. class esenario:
  4.     def __init__(self, width, height, title):
  5.         pygame.init()
  6.  
  7.         self.__width, self.__height = width, height
  8.         self.__size = self.__width, self.__height
  9.         self.screen = pygame.display.set_mode(self.__size)
  10.         pygame.display.set_caption(title)
  11.         self.clock = pygame.time.Clock()
  12.        
  13.     def fill(self, color):
  14.         self.screen.fill(color)
  15.        
  16.     def blit(self, surface, rect):
  17.         self.screen.blit(surface, rect)
  18.    
  19.     def tick(self, frames):
  20.         self.clock.tick(frames)

Solo le falta un bonito sprite que simule el caminar, pero espero que mi codigo ayude a algun pequeño RPG. xD
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 10:50.