import pygame pygame.init() # Variables run = True FPS = 20 clock = pygame.time.Clock() screen_width = 1000 screen_height = 1000 screen = pygame.display.set_mode((screen_width, screen_height)) velocity = 15 x = 50 y = 850 height = 50 width = 69 jumping = False walk_count = 0 # Counter for animation frames jumpcount = 10 direction = 1 # Set name of game window pygame.display.set_caption('Platformer') # Load images idle_img = pygame.image.load('img/main_idle.png') idle_img_left = pygame.transform.flip(idle_img, True, False) #Fliping the image horizontally dead_img = pygame.image.load('img/main_dead.png') run1_img = pygame.image.load('img/main_run1.png') run2_img = pygame.image.load('img/main_run2.png') bg1_img = pygame.image.load('img/bg_1.png') # Create a list of images for walking animation in the right direction walk_right = [run1_img, idle_img, run2_img] # Flip the images horizontally to create a list of images for walking animation in the left direction walk_left = [pygame.transform.flip(img, True, False) for img in walk_right] # Game window function def update_game_window(): global walk_count # Indicates that we're modifying the global walk_count variable screen.blit(bg1_img, (0, 0)) # Display background # Animation left = keys[pygame.K_a] right = keys[pygame.K_d] if left: # Only update animation if moving left screen.blit(walk_left[walk_count // 3], (x, y)) # Blit the appropriate image from walk_left list walk_count = (walk_count + 1) % (len(walk_left) * 3) # Update walk_count, looping it back when needed elif right: # Only update animation if moving right screen.blit(walk_right[walk_count // 3], (x, y)) # Blit the appropriate image from walk_right list walk_count = (walk_count + 1) % (len(walk_right) * 3) # Update walk_count, looping it back when needed else: # If no keys pressed, show idle animation if direction == -1: screen.blit(idle_img_left, (x, y)) else: screen.blit(idle_img, (x, y)) pygame.display.update() # Update display # Main game loop while run: screen.fill((0, 0, 0)) # Clear the screen for event in pygame.event.get(): # Check if game has been closed if event.type == pygame.QUIT: run = False keys = pygame.key.get_pressed() # Get the current state of keys # Character movement if keys[pygame.K_a] and x > velocity: # Move left within screen boundaries x -= velocity direction = -1 elif keys[pygame.K_d] and x < screen_width - width - velocity: # Move right within screen boundaries x += velocity direction = 1 if not jumping: # Allow actions when not jumping if keys[pygame.K_SPACE]: # Check if space key is pressed jumping = True # Start jumping else: if jumpcount >= -10: # Control jump height and direction neg = 1 if jumpcount < 0: neg = -1 y -= (jumpcount ** 2) * 0.5 * neg # Apply jump physics jumpcount -= 1 else: jumping = False jumpcount = 10 update_game_window() # Update game window with animations clock.tick(FPS) # Limit frames per second pygame.quit()