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, K_UP, K_DOWN, K_ESCAPE blue = (0, 0, 255) black = (0, 0, 0) screen = esenario.esenario(320, 240, "Caminar") player = actor.actor(blue, (0, 0)) while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: exit() keyboard = pygame.key.get_pressed() if keyboard[K_ESCAPE]: exit() player.caminar() screen.fill(black) screen.blit(player.image, player.rect) pygame.display.flip() screen.tick(60)
actor.py
Código python:
Ver original
import pygame from pygame import K_LEFT, K_RIGHT, K_UP, K_DOWN, K_ESCAPE class actor(pygame.sprite.Sprite): def __init__(self, color, initial_position): pygame.sprite.Sprite.__init__(self) self.image = pygame.Surface((16, 16)) self.image.fill(color) self.rect = self.image.get_rect() self.rect.topleft = initial_position self.lock = False self.lock_e = False self.lock_t = True self.t = 0 self.c = 0 self.vx = 0 self.vy = 0 def move(self, vx, vy): self.rect = self.rect.move(vx, vy) def estado(self): keyboard = pygame.key.get_pressed() if keyboard[K_LEFT] or keyboard[K_RIGHT] or keyboard[K_UP] or keyboard[K_DOWN]: if not self.lock_e: if keyboard[K_LEFT]: self.vx = -1 elif keyboard[K_RIGHT]: self.vx = 1 elif keyboard[K_UP]: self.vy = -1 elif keyboard[K_DOWN]: self.vy = 1 self.lock_e = True self.lock = True def caminar(self): if self.lock_t: self.estado() if self.lock: self.lock = True self.c += self.vx or self.vy self.move(self.vx, self.vy) if self.c >= 8 or self.c <= -8: self.vx = 0 self.vy = 0 self.c = 0 self.lock = False self.lock_e = False self.lock_t = False self.t = 0 if self.t >= 2: self.lock_t = True self.t = 2 else: self.t += 1
esenario.py
Código python:
Ver original
import pygame class esenario: def __init__(self, width, height, title): pygame.init() self.__width, self.__height = width, height self.__size = self.__width, self.__height self.screen = pygame.display.set_mode(self.__size) pygame.display.set_caption(title) self.clock = pygame.time.Clock() def fill(self, color): self.screen.fill(color) def blit(self, surface, rect): self.screen.blit(surface, rect) def tick(self, frames): 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