Advertisement
Humphryy

Untitled

Jun 13th, 2024 (edited)
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 14.28 KB | Gaming | 0 0
  1. import pygame
  2. import sys
  3. import os
  4.  
  5. # Change the working directory
  6. os.chdir(r'D:\Python Game')
  7.  
  8. # Initialize Pygame
  9. pygame.init()
  10.  
  11. # Set up the display
  12. screen_width = 1920
  13. screen_height = 1080
  14. screen = pygame.display.set_mode((screen_width, screen_height))
  15. pygame.display.set_caption("Atomic's Legacy")
  16.  
  17. # Set up the clock for managing the frame rate
  18. clock = pygame.time.Clock()
  19.  
  20. # Load tile images
  21. TILE_SIZE = 64
  22. ground_img = pygame.Surface((TILE_SIZE, TILE_SIZE))
  23. ground_img.fill((64, 224, 208))  # Turquoise color
  24. wall_img = pygame.Surface((TILE_SIZE, TILE_SIZE))
  25. wall_img.fill((128, 128, 128))  # Grey color (for testing)
  26.  
  27. # Load and scale player animation frames and create masks
  28. def load_animation_frames(base_path, frame_count, scale_factor=5):
  29.     frames_right = []
  30.     frames_left = []
  31.     masks_right = []
  32.     masks_left = []
  33.     for i in range(frame_count):
  34.         frame = pygame.image.load(f'{base_path}_{i}.png').convert_alpha()
  35.         frame = pygame.transform.scale(frame, (frame.get_width() * scale_factor, frame.get_height() * scale_factor))
  36.         frames_right.append(frame)
  37.         frames_left.append(pygame.transform.flip(frame, True, False))
  38.         masks_right.append(pygame.mask.from_surface(frame))
  39.         masks_left.append(pygame.mask.from_surface(pygame.transform.flip(frame, True, False)))
  40.     return frames_right, frames_left, masks_right, masks_left
  41.  
  42. # Load animations
  43. player_frames_right, player_frames_left, player_masks_right, player_masks_left = load_animation_frames('Textures\\Player\\Knight\\Running\\run', 8)
  44. idle_frames_right, idle_frames_left, idle_masks_right, idle_masks_left = load_animation_frames('Textures\\Player\\Knight\\Idle\\idle', 8)
  45. jumping_frames_right, jumping_frames_left, jumping_masks_right, jumping_masks_left = load_animation_frames('Textures\\Player\\Knight\\Jumping\\jump', 7)
  46. crouch_frames_right, crouch_frames_left, crouch_masks_right, crouch_masks_left = load_animation_frames('Textures\\Player\\Knight\\Crouch\\crouch_idle', 8)
  47. melee_attack_frames_right, melee_attack_frames_left, melee_attack_masks_right, melee_attack_masks_left = load_animation_frames('Textures\\Player\\Knight\\Melee\\attacks', 5)
  48.  
  49. # Player settings
  50. player_frame_index = 0
  51. player_frame_speed = 0.2
  52. player_frame_counter = 0
  53. idle_frame_index = 0
  54. idle_frame_counter = 0
  55. jump_frame_index = 0
  56. jump_frame_counter = 0
  57. crouch_frame_index = 0
  58. crouch_frame_counter = 0
  59. attack_frame_index = 0
  60. attack_frame_counter = 0
  61. landing_time = 0  # Timer for landing animation
  62. fall_height = 0  # Track the height from which the player falls
  63. player_image = idle_frames_right[idle_frame_index]
  64. player_mask = idle_masks_right[idle_frame_index]
  65. player_rect = player_image.get_rect(topleft=(100, 100))  # Initial player position
  66. player_facing_right = True
  67. is_jumping = False
  68. is_landing = False
  69. is_attacking = False  # Flag for attack state
  70. attack_cooldown = 300  # Milliseconds between attacks
  71. last_attack_time = 0
  72.  
  73. # Function to load a map from a text file
  74. def load_map(file_name):
  75.     file_path = os.path.join('levels', file_name)
  76.     with open(file_path, 'r') as file:
  77.         tile_map = [list(map(int, line.strip())) for line in file if line.strip()]
  78.     return tile_map
  79.  
  80. # Function to draw the map
  81. def draw_map(tile_map):
  82.     for row_index, row in enumerate(tile_map):
  83.         for col_index, tile in enumerate(row):
  84.             x = col_index * TILE_SIZE
  85.             y = row_index * TILE_SIZE
  86.             if tile == 1:
  87.                 screen.blit(ground_img, (x, y))
  88.             elif tile == 0:
  89.                 pygame.draw.rect(screen, (135, 206, 235), (x, y, TILE_SIZE, TILE_SIZE))  # Clear empty tiles
  90.  
  91. # Load the initial map
  92. current_level = 'level1.txt'
  93. tile_map = load_map(current_level)
  94.  
  95. # Player movement variables
  96. player_speed = 10
  97. player_vel_y = 0
  98. gravity = 2
  99. jump_strength = -30
  100. is_falling = False
  101.  
  102. # Function to handle player movement and collisions
  103. def move_player(tile_map):
  104.     global player_vel_y, player_rect, player_mask, player_frame_index, player_frame_counter, idle_frame_index, idle_frame_counter, jump_frame_index, jump_frame_counter, crouch_frame_index, crouch_frame_counter, attack_frame_index, attack_frame_counter, landing_time, fall_height, player_image, player_facing_right, is_falling, is_jumping, is_landing, is_attacking, last_attack_time
  105.     keys = pygame.key.get_pressed()
  106.    
  107.     # Horizontal movement
  108.     dx = 0
  109.     if not is_landing and not is_attacking:  # Prevent movement during landing and attack animation
  110.         if keys[pygame.K_a] and not keys[pygame.K_d]:
  111.             dx = -player_speed
  112.             player_facing_right = False
  113.         elif keys[pygame.K_d] and not keys[pygame.K_a]:
  114.             dx = player_speed
  115.             player_facing_right = True
  116.  
  117.     # Prevent movement when both left and right keys are pressed
  118.     if keys[pygame.K_a] and keys[pygame.K_d]:
  119.         dx = 0
  120.  
  121.     # Jumping
  122.     if keys[pygame.K_w] and not is_falling and not is_jumping and not is_landing and not is_attacking:
  123.         player_rect.y += 1  # Temporarily move the player down to check for collision
  124.         for row_index, row in enumerate(tile_map):
  125.             for col_index, tile in enumerate(row):
  126.                 if tile == 1:
  127.                     tile_rect = pygame.Rect(col_index * TILE_SIZE, row_index * TILE_SIZE, TILE_SIZE, TILE_SIZE)
  128.                     if player_mask.overlap(pygame.mask.from_surface(ground_img), (tile_rect.x - player_rect.x, tile_rect.y - player_rect.y)):
  129.                         player_vel_y = jump_strength  # Apply jump velocity
  130.                         is_jumping = True
  131.                         fall_height = 0  # Reset fall height on jump
  132.         player_rect.y -= 1  # Restore player's position
  133.  
  134.     # Attacking
  135.     current_time = pygame.time.get_ticks()
  136.     if keys[pygame.K_e] and not is_attacking and current_time - last_attack_time > attack_cooldown:
  137.         is_attacking = True
  138.         attack_frame_index = 0  # Reset attack animation frame
  139.         last_attack_time = current_time
  140.  
  141.     # Apply gravity
  142.     player_vel_y += gravity
  143.     dy = player_vel_y
  144.  
  145.     # Check if player is falling
  146.     was_falling = is_falling
  147.     is_falling = True
  148.     player_rect.y += 1  # Temporarily move the player down to check for collision
  149.     for row_index, row in enumerate(tile_map):
  150.         for col_index, tile in enumerate(row):
  151.             if tile == 1:
  152.                 tile_rect = pygame.Rect(col_index * TILE_SIZE, row_index * TILE_SIZE, TILE_SIZE, TILE_SIZE)
  153.                 if player_mask.overlap(pygame.mask.from_surface(ground_img), (tile_rect.x - player_rect.x, tile_rect.y - player_rect.y)):
  154.                     is_falling = False  # Player is on the ground
  155.     player_rect.y -= 1  # Restore player's position
  156.  
  157.     # Calculate fall height in tiles
  158.     if is_falling:
  159.         fall_height += dy / TILE_SIZE
  160.  
  161.     # Horizontal collision detection
  162.     if dx != 0:
  163.         player_rect.x += dx
  164.         player_mask = player_masks_right[player_frame_index] if player_facing_right else player_masks_left[player_frame_index]
  165.         for row_index, row in enumerate(tile_map):
  166.             for col_index, tile in enumerate(row):
  167.                 if tile == 1:
  168.                     tile_rect = pygame.Rect(col_index * TILE_SIZE, row_index * TILE_SIZE, TILE_SIZE, TILE_SIZE)
  169.                     offset_x = tile_rect.x - player_rect.x
  170.                     offset_y = tile_rect.y - player_rect.y
  171.                     if player_mask.overlap(pygame.mask.from_surface(ground_img), (offset_x, offset_y)):
  172.                         if dx > 0:  # Moving right
  173.                             player_rect.right = tile_rect.left
  174.                         elif dx < 0:  # Moving left
  175.                             player_rect.left = tile_rect.right
  176.                         dx = 0  # Stop horizontal movement after collision
  177.                         break
  178.  
  179.     # Vertical collision detection
  180.     player_rect.y += dy
  181.     for row_index, row in enumerate(tile_map):
  182.         for col_index, tile in enumerate(row):
  183.             if tile == 1:
  184.                 tile_rect = pygame.Rect(col_index * TILE_SIZE, row_index * TILE_SIZE, TILE_SIZE, TILE_SIZE)
  185.                 offset_x = tile_rect.x - player_rect.x
  186.                 offset_y = tile_rect.y - player_rect.y
  187.                 if player_mask.overlap(pygame.mask.from_surface(ground_img), (offset_x, offset_y)):
  188.                     if dy > 0:  # Falling down
  189.                         player_rect.bottom = tile_rect.top
  190.                         player_vel_y = 0
  191.                         if is_falling:
  192.                             if fall_height >= 3:  # Check fall height for landing animation
  193.                                 is_landing = True
  194.                                 landing_time = pygame.time.get_ticks()
  195.                                 crouch_frame_index = 0
  196.                             fall_height = 0  # Reset fall height on landing
  197.                         is_falling = False  # Player landed on the ground
  198.                         is_jumping = False
  199.                     elif dy < 0:  # Jumping up
  200.                         player_rect.top = tile_rect.bottom
  201.                         player_vel_y = 0
  202.                     break  # Exit the loop once a collision is detected to avoid multiple adjustments
  203.  
  204.     # Update player position and animation
  205.     if is_landing:  # Landing animation
  206.         if pygame.time.get_ticks() - landing_time > 200:  # Landing animation duration
  207.             is_landing = False
  208.             crouch_frame_index = 0  # Reset crouch frame index
  209.         else:
  210.             crouch_frame_counter += player_frame_speed
  211.             if crouch_frame_counter >= 1:
  212.                 crouch_frame_counter = 0
  213.                 crouch_frame_index = (crouch_frame_index + 1) % len(crouch_frames_right)
  214.             player_image = crouch_frames_right[crouch_frame_index] if player_facing_right else crouch_frames_left[crouch_frame_index]
  215.             player_mask = crouch_masks_right[crouch_frame_index] if player_facing_right else crouch_masks_left[crouch_frame_index]
  216.     elif is_attacking:  # Attack animation
  217.         attack_frame_counter += player_frame_speed
  218.         if attack_frame_counter >= 1:
  219.             attack_frame_counter = 0
  220.             attack_frame_index += 1
  221.             if attack_frame_index >= len(melee_attack_frames_right):
  222.                 is_attacking = False  # End attack animation
  223.                 attack_frame_index = 0
  224.         player_image = melee_attack_frames_right[attack_frame_index] if player_facing_right else melee_attack_frames_left[attack_frame_index]
  225.         player_mask = melee_attack_masks_right[attack_frame_index] if player_facing_right else melee_attack_masks_left[attack_frame_index]
  226.     elif is_jumping:  # Jumping animation
  227.         jump_frame_counter += player_frame_speed
  228.         if jump_frame_counter >= 1:
  229.             jump_frame_counter = 0
  230.             jump_frame_index = (jump_frame_index + 1) % 7  # Cycle through jump frames
  231.         player_image = jumping_frames_right[jump_frame_index] if player_facing_right else jumping_frames_left[jump_frame_index]
  232.         player_mask = jumping_masks_right[jump_frame_index] if player_facing_right else jumping_masks_left[jump_frame_index]
  233.     elif is_falling:  # Falling animation
  234.         jump_frame_counter += player_frame_speed
  235.         if jump_frame_counter >= 1:
  236.             jump_frame_counter = 0
  237.             jump_frame_index = (jump_frame_index + 1) % 4 + 2  # Cycle through fall frames (2 to 5)
  238.         player_image = jumping_frames_right[jump_frame_index] if player_facing_right else jumping_frames_left[jump_frame_index]
  239.         player_mask = jumping_masks_right[jump_frame_index] if player_facing_right else jumping_masks_left[jump_frame_index]
  240.     elif dx != 0:  # Running animation
  241.         player_frame_counter += player_frame_speed
  242.         if player_frame_counter >= 1:
  243.             player_frame_counter = 0
  244.             player_frame_index = (player_frame_index + 1) % len(player_frames_right)
  245.         player_image = player_frames_right[player_frame_index] if player_facing_right else player_frames_left[player_frame_index]
  246.         player_mask = player_masks_right[player_frame_index] if player_facing_right else player_masks_left[player_frame_index]
  247.     else:  # Idle animation
  248.         idle_frame_counter += player_frame_speed
  249.         if idle_frame_counter >= 1:
  250.             idle_frame_counter = 0
  251.             idle_frame_index = (idle_frame_index + 1) % len(idle_frames_right)
  252.         player_image = idle_frames_right[idle_frame_index] if player_facing_right else idle_frames_left[idle_frame_index]
  253.         player_mask = idle_masks_right[idle_frame_index] if player_facing_right else idle_masks_left[idle_frame_index]
  254.  
  255. # Main game loop
  256. running = True
  257. in_menu = True
  258.  
  259. while running:
  260.     # Handle events
  261.     for event in pygame.event.get():
  262.         if event.type == pygame.QUIT:
  263.             running = False
  264.         elif event.type == pygame.MOUSEBUTTONDOWN:
  265.             if in_menu:
  266.                 mouse_pos = event.pos
  267.                 if start_button.collidepoint(mouse_pos):
  268.                     in_menu = False
  269.                     current_level = 'level1.txt'
  270.                     tile_map = load_map(current_level)
  271.                     player_rect.topleft = (100, 100)
  272.         elif event.type == pygame.KEYDOWN:
  273.             if event.key == pygame.K_SPACE and not in_menu:
  274.                 current_level = 'level2.txt' if current_level == 'level1.txt' else 'level1.txt'
  275.                 tile_map = load_map(current_level)
  276.                 player_rect.topleft = (100, 100)
  277.  
  278.     if in_menu:
  279.         # Main menu
  280.         screen.fill((0, 0, 0))
  281.         start_button = pygame.Rect(screen_width // 2 - 100, screen_height // 2 - 50, 200, 100)
  282.         pygame.draw.rect(screen, (0, 255, 0), start_button)
  283.         font = pygame.font.Font(None, 74)
  284.         text = font.render('Start', True, (255, 255, 255))
  285.         screen.blit(text, (screen_width // 2 - text.get_width() // 2, screen_height // 2 - text.get_height() // 2))
  286.     else:
  287.         # Move the player
  288.         move_player(tile_map)
  289.  
  290.         # Clear the screen with a sky blue color
  291.         screen.fill((135, 206, 235))
  292.  
  293.         # Draw the map
  294.         draw_map(tile_map)
  295.  
  296.         # Draw the player
  297.         screen.blit(player_image, player_rect.topleft)
  298.  
  299.     # Update the display
  300.     pygame.display.flip()
  301.  
  302.     # Cap the frame rate
  303.     clock.tick(60)
  304.  
  305. # Quit Pygame
  306. pygame.quit()
  307. sys.exit()
  308.  
Tags: help
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement