My current code the character can move and he faces the direction he is suppose to. I know i need to get him to swap back and forth between images while moving. I have the entire spritesheet and i've also tried breaking it up. I'm really having a hard time figuring out how to loop through a list. I've watched a lot of tutorials but he code is slightly different and it will crash. Me being such a noob Its hard to dissect my own syntax let alone trying to implement someone elses to mine. I have a main.py, functions.py warrior.py and settings.py
import sys
import pygame
from settings import Settings
import functions as gf
from warrior import Player
def rungame():
pygame.init()
pygame.display.set_caption('warrior Arcade')
screen_settings = Settings()
screen = pygame.display.set_mode((screen_settings.screen_width,screen_settings.screen_height))
mycharacter = Player(screen)
while True:
gf.check_events(mycharacter)
gf.update_screen(screen_settings,screen,mycharacter)
mycharacter.update()
pygame.display.flip()
rungame()
the functions.py file follows
import pygame
import sys
clock = pygame.time.Clock()
movement_right = ['standright.png','stepright1.png','stepright2.png','stepright3.png','stepright4.png','stepright5.png','stepright6.png','stepright7.png','stepright8.png','stepright 9.png']
current_frame = 0
num_frames = 9
def check_events(mycharacter):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
sys.exit()
if event.key == pygame.K_RIGHT:
mycharacter.moving_right = True
mycharacter.image = pygame.image.load(movement_right[current_frame])
if event.key == pygame.K_LEFT:
mycharacter.moving_left = True
mycharacter.image = pygame.image.load('faceleft.png')
if event.key == pygame.K_DOWN:
mycharacter.moving_down = True
mycharacter.image = pygame.image.load('idle.png')
if event.key == pygame.K_UP:
mycharacter.moving_up = True
mycharacter.image = pygame.image.load('faceup.png')
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
mycharacter.moving_right = False
mycharacter.image = pygame.image.load('idle.png')
if event.key == pygame.K_LEFT:
mycharacter.moving_left = False
mycharacter.image = pygame.image.load('idle.png')
if event.key == pygame.K_DOWN:
mycharacter.moving_down = False
mycharacter.image = pygame.image.load('idle.png')
if event.key == pygame.K_UP:
mycharacter.moving_up = False
mycharacter.image = pygame.image.load('idle.png')
def update_screen(screen_settings,screen,mycharacter):
screen.blit(screen_settings.background, [0, 0])
mycharacter.blit()
settings.py follows
import pygame
class Settings():
def __init__(self):
self.screen_width = 1200
self.screen_height = 800
self.background = pygame.image.load('grass.png')
warrior.py follows
import pygame
from itertools import cycle
movement_right = ['standright.png','stepright1.png','stepright2.png','stepright3.png','stepright4.png','stepright5.png','stepright6.png','stepright7.png','stepright8.png','stepright 9.png']
class Player():
def __init__(self,screen):
self.screen = screen
self.image = pygame.image.load(movement_right[0])
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
self.rect.centerx = self.screen_rect.centerx
self.rect.centery = self.screen_rect.centery
self.moving_right = False
self.moving_left = False
self.moving_down = False
self.moving_up = False
def update(self):
if self.moving_right:
self.rect.centerx += 1
if self.moving_left:
self.rect.centerx -= 1
if self.moving_down:
self.rect.centery += 1
if self.moving_up:
self.rect.centery -= 1
def blit(self):
self.screen.blit(self.image,self.rect)