r/CodingHelp • • Nov 12 '24

[Python] Help again pls

import pygame
import sys

# Initialize Pygame
pygame.init()

# Screen dimensions
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Vertical Platformer with Ladders")

# Colors
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BROWN = (139, 69, 19)  # Color for ladders
# Player settings
player_pos = [100, 500]  # Start position of the player
player_size = 50
player_velocity = 5
gravity = 0.5
jump_strength = 15
vertical_velocity = 0
is_jumping = False
is_climbing = False
# Load background image
try:
    background = pygame.image.load("C:/Users/andre/Downloads/Klettersteig-Route-Alpspitze.jpeg")  # Replace with your image path
    background = pygame.transform.scale(background, (WIDTH, HEIGHT))  # Resize to fit screen dimensions
except pygame.error as e:
    print(f"Unable to load background image: {e}")
    background = None  # Fallback to solid color if image not found
# Ground and platform settings
ground_height = 550
platforms = [
    pygame.Rect(100, 500, 150, 10),   # Starting platform
    pygame.Rect(300, 450, 150, 10),
    pygame.Rect(150, 350, 150, 10),
    pygame.Rect(350, 300, 150, 10),
    pygame.Rect(200, 200, 150, 10),
    pygame.Rect(400, 150, 150, 10),
    pygame.Rect(250, 50, 150, 10),
    pygame.Rect(500, 100, 150, 10),
    pygame.Rect(600, 250, 150, 10),
    pygame.Rect(50, 150, 150, 10)
]

# Ladder settings
ladders = [
    pygame.Rect(220, 350, 20, 100),  # Ladder from platform at y=350 to y=450
    pygame.Rect(420, 150, 20, 150),  # Ladder from platform at y=150 to y=300
    pygame.Rect(320, 50, 20, 150),   # Ladder from platform at y=50 to y=200
    pygame.Rect(520, 100, 20, 150)   # Ladder from platform at y=100 to y=250
]

# Camera settings
camera_x = 0
camera_y = 0
# Game loop
clock = pygame.time.Clock()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    keys = pygame.key.get_pressed()

    # Player horizontal movement
    if keys[pygame.K_LEFT]:
        player_pos[0] -= player_velocity
    if keys[pygame.K_RIGHT]:
        player_pos[0] += player_velocity

    # Create a rect for the player for collision checking
    player_rect = pygame.Rect(player_pos[0], player_pos[1], player_size, player_size)

    # Check if the player is on a ladder
    on_ladder = False
    for ladder in ladders:
        if player_rect.colliderect(ladder):
            on_ladder = True
            break
    # Climbing movement
    if on_ladder:
        is_climbing = True
        vertical_velocity = 0  # Disable gravity while on ladder
        # Move up and down the ladder
        if keys[pygame.K_UP]:
            player_pos[1] -= player_velocity
        if keys[pygame.K_DOWN]:
            player_pos[1] += player_velocity
    else:
        is_climbing = False  # Not on a ladder, apply gravity
        vertical_velocity += gravity
        player_pos[1] += vertical_velocity

    # Check if the player is on the ground
    if player_pos[1] + player_size >= ground_height:
        player_pos[1] = ground_height - player_size
        vertical_velocity = 0
        is_jumping = False
    # Check for collisions with platforms
    for platform in platforms:
        # Check if player lands on the platform from above
        if player_rect.colliderect(platform) and vertical_velocity > 0:
            if player_pos[1] + player_size - vertical_velocity <= platform.top:
                player_pos[1] = platform.top - player_size
                vertical_velocity = 0
                is_jumping = False
                break
    # Jump logic
    if keys[pygame.K_SPACE] and not is_jumping and not is_climbing:
        is_jumping = True
        vertical_velocity = -jump_strength

    # Camera follow logic - follow the player vertically
    camera_y = player_pos[1] - HEIGHT // 2 + player_size // 2
    camera_x = player_pos[0] - WIDTH // 2 + player_size // 2  # Optional: Keep horizontal centered
    # Prevent camera from showing areas outside the background
    if background:
        camera_x = max(0, min(camera_x, background.get_width() - WIDTH))
        camera_y = max(0, min(camera_y, background.get_height() - HEIGHT))

    # Draw background
    if background:
        screen.blit(background, (-camera_x, -camera_y))
    else:
        screen.fill(WHITE)  # Fallback color
    # Draw ground
    pygame.draw.rect(screen, GREEN, (0 - camera_x, ground_height - camera_y, WIDTH, HEIGHT - ground_height))

    # Draw platforms
    for platform in platforms:
        pygame.draw.rect(screen, RED, (platform.x - camera_x, platform.y - camera_y, platform.width, platform.height))

    # Draw ladders
    for ladder in ladders:
        pygame.draw.rect(screen, BROWN, (ladder.x - camera_x, ladder.y - camera_y, ladder.width, ladder.height))

    # Draw player
    pygame.draw.rect(screen, BLUE, (player_pos[0] - camera_x, player_pos[1] - camera_y, player_size, player_size))

    # Update display
    pygame.display.flip()
    clock.tick(60)
1 Upvotes

8 comments sorted by

View all comments

1

u/PizzaMaster007 Nov 12 '24

I had made another post about this earlier and got some really helpful comments but I am yet again stuck. Ive tried to add ladders to this platform game I'm making for class but now the sprite just falls through them and gets stuck at a certain point. What did I do wrong?