PyGame - Multi-Directional Movement and Sprite Transformations

Started by
-1 comments, last by Safixk 11 years, 5 months ago
Hey There! I'm making a game using Python(v2.6.6), and the PyGame library.
It's a RPG game, and I've barely started... But questions have already come up.
I want to make a way in which the character will not only go in all directions...
But I also want it so the sprite changes according to the direction I'm going.
I've managed to create a loop that animates the walking movement - For the Up and Down directions
but the image doesn't turn around according to the direction... Also the way I made to loop the images
was a I thing that came up to me - I'm not sure if it's correct, nor if its the best way to do so.

Here's my code, and thanks for any and all help:
[source lang="python"]
import pygame, sys, glob
from pygame import *

pygame.init()

size = width, height = 600,400

clock = pygame.time.Clock()

black = 0,0,0

screen = pygame.display.set_mode(size)

class player:
def __init__(self):
self.x = 300
self.y = 200
self.ani_speed_init=10
self.ani_speed=self.ani_speed_init
self.ani = glob.glob("chardown*.png")
self.ani.sort()
self.ani_pos=0
self.ani_max = len(self.ani) -1
self.img = pygame.image.load(self.ani[0])
self.update(0)
def update(self, pos):
if pos != 0:
self.ani_speed-=1
self.y+=pos
if self.ani_speed == 0:
self.img = pygame.image.load(self.ani[self.ani_pos])
self.ani_speed = self.ani_speed_init
if self.ani_pos == self.ani_max:
self.ani_pos = 0
else:
self.ani_pos+=1
screen.blit(self.img,(self.x,self.y))

player1 = player()

pos = 0

while 1:
clock.tick(60)
screen.fill(black)

for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == KEYDOWN and event.key == K_DOWN:
pos = 1
elif event.type == KEYUP and event.key == K_DOWN:
pos = 0
elif event.type == KEYDOWN and event.key == K_UP:
pos = -1
elif event.type == KEYUP and event.key == K_UP:
pos = 0



player1.update(pos)

pygame.display.update()
[/source]

This topic is closed to new replies.

Advertisement