import pygame
import sys
# Initialize Pygame
[Link]()
# Screen setup
width, height = 800, 600
screen = [Link].set_mode((width, height))
[Link].set_caption("2D Movement with Jumping")
# Colors
white = (255, 255, 255)
blue = (0, 0, 255)
# Player setup
player_size = 50
player_x = width // 2
player_y = height - player_size
player_speed = 5
velocity_y = 0
gravity = 1
jump_force = -15
on_ground = True
# Game loop
clock = [Link]()
running = True
while running:
[Link](white)
for event in [Link]():
if [Link] == [Link]:
running = False
# Key press handling
keys = [Link].get_pressed()
if keys[pygame.K_LEFT]:
player_x -= player_speed
if keys[pygame.K_RIGHT]:
player_x += player_speed
# Jumping
if keys[pygame.K_SPACE] and on_ground:
velocity_y = jump_force
on_ground = False
# Apply gravity
velocity_y += gravity
player_y += velocity_y
# Ground collision
if player_y >= height - player_size