import pygame import random # Initialize Pygame pygame.init() # Set up screen dimensions screen_width = 800 screen_height = 600 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("Basic Scrolling Parkour Runner") # Constants GRAVITY = 0.5 # Magnitude of gravity force SCROLL_SPEED = 3 # Background scroll speed # Load player sprite class Player(pygame.sprite.Sprite): def __init__(self): super().__init__() # Define image paths base_path = "sprites/BaseCharacter/" left_images = [f"{base_path}left{i}.png" for i in range(1, 5)] right_images = [f"{base_path}right{i}.png" for i in range(1, 5)] # Load original images self.original_images = { "left": [pygame.image.load(image).convert_alpha() for image in left_images], "right": [pygame.image.load(image).convert_alpha() for image in right_images] } # Scale images self.images = {direction: [pygame.transform.scale(image, (image.get_width() * 2, image.get_height() * 2)) for image in images_list] for direction, images_list in self.original_images.items()} self.index = 0 self.image = self.images["right"][0] self.rect = self.image.get_rect() self.rect.center = (screen_width // 2, screen_height // 2) self.speed = 5 self.jump_height = 120 self.jump_vel = 8 self.is_jumping = False self.jump_count = self.jump_height self.direction = "right" self.standing_frames = { "left": self.images["left"][0], "right": self.images["right"][0] } self.y_velocity = 0 # Vertical velocity def update(self, keys): # Apply gravity self.y_velocity += GRAVITY # Jumping if keys[pygame.K_SPACE] and not self.is_jumping: self.y_velocity = -self.jump_vel self.is_jumping = True # Move vertically self.rect.y += self.y_velocity # Check for collision with ground if self.rect.bottom >= screen_height: self.rect.bottom = screen_height self.y_velocity = 0 self.is_jumping = False # Movement if keys[pygame.K_LEFT]: self.rect.x -= self.speed self.direction = "left" elif keys[pygame.K_RIGHT]: self.rect.x += self.speed self.direction = "right" # Ensure player stays within screen bounds self.rect.x = max(0, min(self.rect.x, screen_width - self.rect.width)) # Animate player if not self.is_jumping and not (keys[pygame.K_LEFT] or keys[pygame.K_RIGHT]): self.image = self.standing_frames[self.direction] else: self.index += 0.2 # Adjust animation speed if needed if self.index >= len(self.images[self.direction]): self.index = 0 self.image = self.images[self.direction][int(self.index)] # Create player sprite player = Player() # Create sprite group for player all_sprites = pygame.sprite.Group() all_sprites.add(player) # Load background image background_image = pygame.image.load("assets/background.png").convert() background_image = pygame.transform.scale(background_image, (screen_width * 2, screen_height)) background_rect = background_image.get_rect() background_scroll = 0 # Font for text display font = pygame.font.Font(None, 36) # Main game loop running = True clock = pygame.time.Clock() while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Background scroll background_scroll += SCROLL_SPEED if background_scroll >= background_rect.width: background_scroll = 0 # Draw background screen.blit(background_image, (background_scroll - background_rect.width, 0)) screen.blit(background_image, (background_scroll, 0)) # Get pressed keys keys = pygame.key.get_pressed() # Update player position and animation based on pressed keys player.update(keys) # Draw all sprites all_sprites.draw(screen) # Update the display pygame.display.flip() # Cap the frame rate clock.tick(60) # Quit Pygame pygame.quit()