import pygame import os pygame.init() screen_width = 800 screen_height = int(screen_width * 0.8) screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption('SAMURAI') #set fps clock = pygame.time.Clock() FPS = 60 #define game variables GRAVITY = 0.75 #define player action variables moving_left = False moving_right = False #define colors BG = (144, 201, 120) RED = (255, 0, 0) def draw_bg(): screen.fill(BG) pygame.draw.line(screen, RED, (0, 300), (screen_width, 400)) class samurai(pygame.sprite.Sprite): def __init__(self, char_type, x, y, scale, speed): pygame.sprite.Sprite.__init__(self) self.alive = True # type: ignore self.char_type = char_type self.speed = speed self.direction = 1 self.vel_y = 0 self.jump = False self.in_air = True self.flip = False self.animation_list = [] self.frame_index = 0 self.action = 0 self.update_time = pygame.time.get_ticks() animation_types = ['idle', 'run', 'jump'] for animation in animation_types: # reset temporary list of images temp_list = [] #check how many images there are in a file num_of_frames = len(os.listdir('img/{self.char_type}/{animation}')) for i in range(num_of_frames): img = pygame.image.load(f'img/{self.char_type}/{animation}/{i}.png') img = pygame.transform.scale(img, (int(img.get_width() * scale), int(img.get_height() * scale))) temp_list.append(img) self.animation_list.append(temp_list) self.image = self.animation_list[self.action][self.frame_index] self.rect = self.image.get_rect() self.rect.center = (x, y) def move(self, moving_left, moving_right): #reset movement variables dx = 0 dy = 0 #assign movement variables if moving left or right if moving_left: dx =- self.speed self.flip = True self.direction = -1 if moving_right: dx = self.speed self.flip = False self.direction = 1 #jump if self.jump == True and self.in_air == False: self.vel_y = -11 self.jump= False self.in_air = True #apply gravity self.vel_y += GRAVITY if self.vel_y > 10: self.vel_y dy += self.vel_y #check the floor if self.rect.bottom + dy > 300: dy = 300 - self.rect.bottom self.in_air = False #update rectangle position self.rect.x += dx self.rect.y += dy def update_animation(self): # update animation animation_cooldown = 100 #update image depending on current frame self.image = self.animation_list[self.action][self.frame_index] #make sure enough time has passed since the last image change if pygame.time.get_ticks() - self.update_time > animation_cooldown: self.update_time = pygame.time.get_ticks() self.frame_index += 1 if self.frame_index >= len(self.animation_list[self.action]): self.frame_index = 0 def update_action(self, new_action): if new_action != self.action: self.action = new_action self.frame_index = 0 self.update_time = pygame.time.get_ticks() def draw(self): screen.blit(pygame.transform.flip(self.image, self.flip, False),self.rect) player = samurai('Player', 200, 200, 3, 5) run = True while run: clock.tick(FPS) draw_bg() player.update_animation() player.draw() if player.alive: if player.in_air: player.update_action(2) #2: jump elif moving_left or moving_right: player.update_action(1) #1: run else: player.update_action(0) player.move(moving_left, moving_right) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_a: moving_left = True if event.key == pygame.K_d: moving_right = True if event.key == pygame.K_w and player.alive: player.jump = True if event.key == pygame.K_ESCAPE: run = False if event.type == pygame.KEYUP: if event.key == pygame.K_a: moving_left = False if event.key == pygame.K_d: moving_right = False pygame.display.update() pygame.quit()