game/lib/character.py
2020-05-12 07:20:00 -05:00

54 lines
1.3 KiB
Python

from pygame.sprite import Sprite
import pygame.image
import os
from . import constants
class Character(Sprite):
@staticmethod
def loadFrames(type):
frames={}
dirs=["up","down","left","right"]
num_frames=3
for direction in dirs:
frame_array=[0,0,0]
i=0
while i<num_frames:
img_name="{}_{}.png".format(direction,i)
frame_array[i]=pygame.image.load(os.path.join("sprites",type,img_name))
i+=1
frames[direction]=frame_array
return frames
def __init__(self,x,y,type,map,screen,*groups):
super().__init__(groups)
self.x=x
self.y=y
self.screen=screen
self.frames=self.loadFrames(type)
self.frame=1
self.direction="right"
self.map=map
def draw(self):
img=self.frames[self.direction][self.frame]
self.screen.blit(img,(self.x*constants.TILESIZE,self.y*constants.TILESIZE))
def move(self,direction):
old_x=self.x
old_y=self.y
self.direction=direction
if direction=="up":
self.y-=1
elif direction=="down":
self.y+=1
elif direction=="left":
self.x-=1
elif direction=="right":
self.x+=1
self.frame+=1
if self.frame>2:
self.frame=0
tile=self.map.tileAt(self.x,self.y)
if not tile.clear:
self.x=old_x
self.y=old_y