import pygame import sys # 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("Simple Platformer") # Load background image background_image = pygame.image.load("background.jpg").convert() background_image = pygame.transform.scale(background_image, (SCREEN_WIDTH, SCREEN_HEIGHT)) # Load player image player_image_right = pygame.image.load("R2D2.png").convert_alpha() # Load image with alpha channel player_image_left = pygame.transform.flip(player_image_right, True, False) # Flip image horizontally player_width = 60 player_height = 80 player_image_right = pygame.transform.scale(player_image_right, (player_width, player_height)) # Resize image player_image_left = pygame.transform.scale(player_image_left, (player_width, player_height)) # Resize image # Define player properties player_rect = player_image_right.get_rect() player_rect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2) player_y_speed = 0 player_x_speed = 0 jumping = False facing_left = False # Initially, player faces right # Define platform properties platforms = [ pygame.Rect(200, SCREEN_HEIGHT - 40, 400, 20), pygame.Rect(0, SCREEN_HEIGHT - 20, SCREEN_WIDTH, 20), pygame.Rect(100, 400, 200, 20), # New platform 1 pygame.Rect(500, 300, 200, 20), # New platform 2 pygame.Rect(200, 200, 400, 20) # New platform 3 ] # Define collectible properties collectibles = [ pygame.Rect(250, 350, 20, 20), # Collectible 1 pygame.Rect(600, 250, 20, 20), # Collectible 2 pygame.Rect(400, 150, 20, 20) # Collectible 3 ] # Scoring system score = 0 # Timer timer_font = pygame.font.Font(None, 36) start_time = pygame.time.get_ticks() # Get the time at the start of the game # Game loop running = True while running: screen.fill((0, 0, 0)) # Draw background screen.blit(background_image, (0, 0)) # Event handling for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE and not jumping: jumping = True player_y_speed = -10 # Player movement keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: player_x_speed = -5 facing_left = True # Update direction elif keys[pygame.K_RIGHT]: player_x_speed = 5 facing_left = False # Update direction else: player_x_speed = 0 # Update player position player_rect.x += player_x_speed player_y_speed += 0.3 # Gravity player_rect.y += player_y_speed # Collision detection with platforms for platform in platforms: if player_rect.colliderect(platform): if player_y_speed > 0: player_rect.bottom = platform.top player_y_speed = 0 jumping = False elif player_y_speed < 0: player_rect.top = platform.bottom player_y_speed = 0 # Collision detection with collectibles for collectible in collectibles[:]: # Using [:] to make a copy of the list for safe removal if player_rect.colliderect(collectible): collectibles.remove(collectible) score += 1 # Draw player if facing_left: screen.blit(player_image_left, player_rect) else: screen.blit(player_image_right, player_rect) # Draw platforms for platform in platforms: pygame.draw.rect(screen, (0, 0, 0), platform) # Draw collectibles for collectible in collectibles: pygame.draw.rect(screen, (255, 255, 0), collectible) # Display score score_text = timer_font.render(f"Score: {score}", True, (255, 255, 255)) screen.blit(score_text, (10, 10)) # Timer display elapsed_time = pygame.time.get_ticks() - start_time seconds = (elapsed_time // 1000) % 60 milliseconds = (elapsed_time % 1000) // 10 # Get milliseconds timer_text = timer_font.render(f"Time: {seconds:02}:{milliseconds:03}", True, (255, 255, 255)) screen.blit(timer_text, (SCREEN_WIDTH - timer_text.get_width() - 10, 10)) # Game completion condition if score >= 3: font = pygame.font.Font(None, 72) game_complete_text = font.render("Game Complete!", True, (0, 255, 0)) screen.blit(game_complete_text, (SCREEN_WIDTH // 2 - game_complete_text.get_width() // 2, SCREEN_HEIGHT // 2 - game_complete_text.get_height() // 2)) pygame.display.flip() pygame.time.delay(2000) # Delay to show "Game Complete!" message # Reset game score = 0 player_rect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2) # Reset player position collectibles = [ pygame.Rect(250, 350, 20, 20), # Collectible 1 pygame.Rect(600, 250, 20, 20), # Collectible 2 pygame.Rect(400, 150, 20, 20) # Collectible 3 ] start_time = pygame.time.get_ticks() # Reset the start time pygame.display.flip() pygame.time.Clock().tick(60) pygame.quit() sys.exit()