Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import sys
- import os
- # Change the working directory
- os.chdir(r'D:\Python Game')
- # Initialize Pygame
- pygame.init()
- # Set up the display
- screen_width = 1920
- screen_height = 1080
- screen = pygame.display.set_mode((screen_width, screen_height))
- pygame.display.set_caption("Atomic's Legacy")
- # Set up the clock for managing the frame rate
- clock = pygame.time.Clock()
- # Load tile images
- TILE_SIZE = 64
- ground_img = pygame.Surface((TILE_SIZE, TILE_SIZE))
- ground_img.fill((64, 224, 208)) # Turquoise color
- wall_img = pygame.Surface((TILE_SIZE, TILE_SIZE))
- wall_img.fill((128, 128, 128)) # Grey color (for testing)
- # Load and scale player animation frames and create masks
- def load_animation_frames(base_path, frame_count, scale_factor=5):
- frames_right = []
- frames_left = []
- masks_right = []
- masks_left = []
- for i in range(frame_count):
- frame = pygame.image.load(f'{base_path}_{i}.png').convert_alpha()
- frame = pygame.transform.scale(frame, (frame.get_width() * scale_factor, frame.get_height() * scale_factor))
- frames_right.append(frame)
- frames_left.append(pygame.transform.flip(frame, True, False))
- masks_right.append(pygame.mask.from_surface(frame))
- masks_left.append(pygame.mask.from_surface(pygame.transform.flip(frame, True, False)))
- return frames_right, frames_left, masks_right, masks_left
- # Load animations
- player_frames_right, player_frames_left, player_masks_right, player_masks_left = load_animation_frames('Textures\\Player\\Knight\\Running\\run', 8)
- idle_frames_right, idle_frames_left, idle_masks_right, idle_masks_left = load_animation_frames('Textures\\Player\\Knight\\Idle\\idle', 8)
- jumping_frames_right, jumping_frames_left, jumping_masks_right, jumping_masks_left = load_animation_frames('Textures\\Player\\Knight\\Jumping\\jump', 7)
- crouch_frames_right, crouch_frames_left, crouch_masks_right, crouch_masks_left = load_animation_frames('Textures\\Player\\Knight\\Crouch\\crouch_idle', 8)
- 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)
- # Player settings
- player_frame_index = 0
- player_frame_speed = 0.2
- player_frame_counter = 0
- idle_frame_index = 0
- idle_frame_counter = 0
- jump_frame_index = 0
- jump_frame_counter = 0
- crouch_frame_index = 0
- crouch_frame_counter = 0
- attack_frame_index = 0
- attack_frame_counter = 0
- landing_time = 0 # Timer for landing animation
- fall_height = 0 # Track the height from which the player falls
- player_image = idle_frames_right[idle_frame_index]
- player_mask = idle_masks_right[idle_frame_index]
- player_rect = player_image.get_rect(topleft=(100, 100)) # Initial player position
- player_facing_right = True
- is_jumping = False
- is_landing = False
- is_attacking = False # Flag for attack state
- attack_cooldown = 300 # Milliseconds between attacks
- last_attack_time = 0
- # Function to load a map from a text file
- def load_map(file_name):
- file_path = os.path.join('levels', file_name)
- with open(file_path, 'r') as file:
- tile_map = [list(map(int, line.strip())) for line in file if line.strip()]
- return tile_map
- # Function to draw the map
- def draw_map(tile_map):
- for row_index, row in enumerate(tile_map):
- for col_index, tile in enumerate(row):
- x = col_index * TILE_SIZE
- y = row_index * TILE_SIZE
- if tile == 1:
- screen.blit(ground_img, (x, y))
- elif tile == 0:
- pygame.draw.rect(screen, (135, 206, 235), (x, y, TILE_SIZE, TILE_SIZE)) # Clear empty tiles
- # Load the initial map
- current_level = 'level1.txt'
- tile_map = load_map(current_level)
- # Player movement variables
- player_speed = 10
- player_vel_y = 0
- gravity = 2
- jump_strength = -30
- is_falling = False
- # Function to handle player movement and collisions
- def move_player(tile_map):
- 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
- keys = pygame.key.get_pressed()
- # Horizontal movement
- dx = 0
- if not is_landing and not is_attacking: # Prevent movement during landing and attack animation
- if keys[pygame.K_a] and not keys[pygame.K_d]:
- dx = -player_speed
- player_facing_right = False
- elif keys[pygame.K_d] and not keys[pygame.K_a]:
- dx = player_speed
- player_facing_right = True
- # Prevent movement when both left and right keys are pressed
- if keys[pygame.K_a] and keys[pygame.K_d]:
- dx = 0
- # Jumping
- if keys[pygame.K_w] and not is_falling and not is_jumping and not is_landing and not is_attacking:
- player_rect.y += 1 # Temporarily move the player down to check for collision
- for row_index, row in enumerate(tile_map):
- for col_index, tile in enumerate(row):
- if tile == 1:
- tile_rect = pygame.Rect(col_index * TILE_SIZE, row_index * TILE_SIZE, TILE_SIZE, TILE_SIZE)
- if player_mask.overlap(pygame.mask.from_surface(ground_img), (tile_rect.x - player_rect.x, tile_rect.y - player_rect.y)):
- player_vel_y = jump_strength # Apply jump velocity
- is_jumping = True
- fall_height = 0 # Reset fall height on jump
- player_rect.y -= 1 # Restore player's position
- # Attacking
- current_time = pygame.time.get_ticks()
- if keys[pygame.K_e] and not is_attacking and current_time - last_attack_time > attack_cooldown:
- is_attacking = True
- attack_frame_index = 0 # Reset attack animation frame
- last_attack_time = current_time
- # Apply gravity
- player_vel_y += gravity
- dy = player_vel_y
- # Check if player is falling
- was_falling = is_falling
- is_falling = True
- player_rect.y += 1 # Temporarily move the player down to check for collision
- for row_index, row in enumerate(tile_map):
- for col_index, tile in enumerate(row):
- if tile == 1:
- tile_rect = pygame.Rect(col_index * TILE_SIZE, row_index * TILE_SIZE, TILE_SIZE, TILE_SIZE)
- if player_mask.overlap(pygame.mask.from_surface(ground_img), (tile_rect.x - player_rect.x, tile_rect.y - player_rect.y)):
- is_falling = False # Player is on the ground
- player_rect.y -= 1 # Restore player's position
- # Calculate fall height in tiles
- if is_falling:
- fall_height += dy / TILE_SIZE
- # Horizontal collision detection
- if dx != 0:
- player_rect.x += dx
- player_mask = player_masks_right[player_frame_index] if player_facing_right else player_masks_left[player_frame_index]
- for row_index, row in enumerate(tile_map):
- for col_index, tile in enumerate(row):
- if tile == 1:
- tile_rect = pygame.Rect(col_index * TILE_SIZE, row_index * TILE_SIZE, TILE_SIZE, TILE_SIZE)
- offset_x = tile_rect.x - player_rect.x
- offset_y = tile_rect.y - player_rect.y
- if player_mask.overlap(pygame.mask.from_surface(ground_img), (offset_x, offset_y)):
- if dx > 0: # Moving right
- player_rect.right = tile_rect.left
- elif dx < 0: # Moving left
- player_rect.left = tile_rect.right
- dx = 0 # Stop horizontal movement after collision
- break
- # Vertical collision detection
- player_rect.y += dy
- for row_index, row in enumerate(tile_map):
- for col_index, tile in enumerate(row):
- if tile == 1:
- tile_rect = pygame.Rect(col_index * TILE_SIZE, row_index * TILE_SIZE, TILE_SIZE, TILE_SIZE)
- offset_x = tile_rect.x - player_rect.x
- offset_y = tile_rect.y - player_rect.y
- if player_mask.overlap(pygame.mask.from_surface(ground_img), (offset_x, offset_y)):
- if dy > 0: # Falling down
- player_rect.bottom = tile_rect.top
- player_vel_y = 0
- if is_falling:
- if fall_height >= 3: # Check fall height for landing animation
- is_landing = True
- landing_time = pygame.time.get_ticks()
- crouch_frame_index = 0
- fall_height = 0 # Reset fall height on landing
- is_falling = False # Player landed on the ground
- is_jumping = False
- elif dy < 0: # Jumping up
- player_rect.top = tile_rect.bottom
- player_vel_y = 0
- break # Exit the loop once a collision is detected to avoid multiple adjustments
- # Update player position and animation
- if is_landing: # Landing animation
- if pygame.time.get_ticks() - landing_time > 200: # Landing animation duration
- is_landing = False
- crouch_frame_index = 0 # Reset crouch frame index
- else:
- crouch_frame_counter += player_frame_speed
- if crouch_frame_counter >= 1:
- crouch_frame_counter = 0
- crouch_frame_index = (crouch_frame_index + 1) % len(crouch_frames_right)
- player_image = crouch_frames_right[crouch_frame_index] if player_facing_right else crouch_frames_left[crouch_frame_index]
- player_mask = crouch_masks_right[crouch_frame_index] if player_facing_right else crouch_masks_left[crouch_frame_index]
- elif is_attacking: # Attack animation
- attack_frame_counter += player_frame_speed
- if attack_frame_counter >= 1:
- attack_frame_counter = 0
- attack_frame_index += 1
- if attack_frame_index >= len(melee_attack_frames_right):
- is_attacking = False # End attack animation
- attack_frame_index = 0
- player_image = melee_attack_frames_right[attack_frame_index] if player_facing_right else melee_attack_frames_left[attack_frame_index]
- player_mask = melee_attack_masks_right[attack_frame_index] if player_facing_right else melee_attack_masks_left[attack_frame_index]
- elif is_jumping: # Jumping animation
- jump_frame_counter += player_frame_speed
- if jump_frame_counter >= 1:
- jump_frame_counter = 0
- jump_frame_index = (jump_frame_index + 1) % 7 # Cycle through jump frames
- player_image = jumping_frames_right[jump_frame_index] if player_facing_right else jumping_frames_left[jump_frame_index]
- player_mask = jumping_masks_right[jump_frame_index] if player_facing_right else jumping_masks_left[jump_frame_index]
- elif is_falling: # Falling animation
- jump_frame_counter += player_frame_speed
- if jump_frame_counter >= 1:
- jump_frame_counter = 0
- jump_frame_index = (jump_frame_index + 1) % 4 + 2 # Cycle through fall frames (2 to 5)
- player_image = jumping_frames_right[jump_frame_index] if player_facing_right else jumping_frames_left[jump_frame_index]
- player_mask = jumping_masks_right[jump_frame_index] if player_facing_right else jumping_masks_left[jump_frame_index]
- elif dx != 0: # Running animation
- player_frame_counter += player_frame_speed
- if player_frame_counter >= 1:
- player_frame_counter = 0
- player_frame_index = (player_frame_index + 1) % len(player_frames_right)
- player_image = player_frames_right[player_frame_index] if player_facing_right else player_frames_left[player_frame_index]
- player_mask = player_masks_right[player_frame_index] if player_facing_right else player_masks_left[player_frame_index]
- else: # Idle animation
- idle_frame_counter += player_frame_speed
- if idle_frame_counter >= 1:
- idle_frame_counter = 0
- idle_frame_index = (idle_frame_index + 1) % len(idle_frames_right)
- player_image = idle_frames_right[idle_frame_index] if player_facing_right else idle_frames_left[idle_frame_index]
- player_mask = idle_masks_right[idle_frame_index] if player_facing_right else idle_masks_left[idle_frame_index]
- # Main game loop
- running = True
- in_menu = True
- while running:
- # Handle events
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- elif event.type == pygame.MOUSEBUTTONDOWN:
- if in_menu:
- mouse_pos = event.pos
- if start_button.collidepoint(mouse_pos):
- in_menu = False
- current_level = 'level1.txt'
- tile_map = load_map(current_level)
- player_rect.topleft = (100, 100)
- elif event.type == pygame.KEYDOWN:
- if event.key == pygame.K_SPACE and not in_menu:
- current_level = 'level2.txt' if current_level == 'level1.txt' else 'level1.txt'
- tile_map = load_map(current_level)
- player_rect.topleft = (100, 100)
- if in_menu:
- # Main menu
- screen.fill((0, 0, 0))
- start_button = pygame.Rect(screen_width // 2 - 100, screen_height // 2 - 50, 200, 100)
- pygame.draw.rect(screen, (0, 255, 0), start_button)
- font = pygame.font.Font(None, 74)
- text = font.render('Start', True, (255, 255, 255))
- screen.blit(text, (screen_width // 2 - text.get_width() // 2, screen_height // 2 - text.get_height() // 2))
- else:
- # Move the player
- move_player(tile_map)
- # Clear the screen with a sky blue color
- screen.fill((135, 206, 235))
- # Draw the map
- draw_map(tile_map)
- # Draw the player
- screen.blit(player_image, player_rect.topleft)
- # Update the display
- pygame.display.flip()
- # Cap the frame rate
- clock.tick(60)
- # Quit Pygame
- pygame.quit()
- sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement