""" ------------------------------------------------- Project: Take-Away Game Standard: 91883 (AS1.7) v.1 School: Tauranga Boys' College Author: YOUR FULL NAME NATE WHINNEY Date: THE DATE YOU COMPLETED THE CODE Python: 3.5 ------------------------------------------------- """ 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("Simple Super Mario") # Load background image background = pygame.image.load("background.png").convert() background = pygame.transform.scale(background, (WIDTH, HEIGHT)) # Scale to fit screen # Load character sprite character_image = pygame.image.load("character.png").convert_alpha() character_image = pygame.transform.scale(character_image, (40, 60)) # Match Mario's dimensions # Load platform image platform_image = pygame.image.load("platform.png").convert_alpha() platform_width, platform_height = platform_image.get_size() # Colors WHITE = (255, 255, 255) BLUE = (50, 50, 255) # FPS controller clock = pygame.time.Clock() FPS = 60 # Mario settings mario_width, mario_height = 40, 60 mario_x = WIDTH // 2 mario_y = HEIGHT - mario_height - 50 # ground level mario_vel_x = 0 mario_vel_y = 0 GRAVITY = 0.8 JUMP_POWER = -15 GROUND_LEVEL = HEIGHT - mario_height - 50 on_ground = False # Define platforms (x, y) platforms = [ pygame.Rect(150, 450, platform_width, platform_height), pygame.Rect(350, 350, platform_width, platform_height), pygame.Rect(550, 250, platform_width, platform_height), pygame.Rect(100, 150, platform_width, platform_height), ] # Main game loop running = True while running: clock.tick(FPS) # Draw background screen.blit(background, (0, 0)) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Key presses keys = pygame.key.get_pressed() mario_vel_x = 0 if keys[pygame.K_LEFT]: mario_vel_x = -5 if keys[pygame.K_RIGHT]: mario_vel_x = 5 if (keys[pygame.K_SPACE] or keys[pygame.K_UP]) and on_ground: mario_vel_y = JUMP_POWER on_ground = False # Apply gravity mario_vel_y += GRAVITY mario_y += mario_vel_y mario_x += mario_vel_x # Mario rectangle for collision mario_rect = pygame.Rect(mario_x, mario_y, mario_width, mario_height) # Check platform collision (only if falling) on_ground = False for plat in platforms: if mario_vel_y >= 0 and mario_rect.colliderect(plat): # Check if Mario is above the platform and falling onto it if mario_rect.bottom <= plat.top + mario_vel_y: mario_y = plat.top - mario_height mario_vel_y = 0 on_ground = True # Ground collision if mario_y >= GROUND_LEVEL: mario_y = GROUND_LEVEL mario_vel_y = 0 on_ground = True # Screen boundaries if mario_x < 0: mario_x = 0 if mario_x > WIDTH - mario_width: mario_x = WIDTH - mario_width # Draw platforms for plat in platforms: screen.blit(platform_image, (plat.x, plat.y)) # Draw Mario using sprite screen.blit(character_image, (mario_x, mario_y)) # Draw ground pygame.draw.rect(screen, BLUE, (0, HEIGHT - 50, WIDTH, 50)) pygame.display.flip() pygame.quit() sys.exit()