Untitled
unknown
plain_text
a year ago
6.0 kB
12
Indexable
import pygame
# Initialize Pygame
pygame.init()
# Screen dimensions
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("2D Platformer")
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
# Game clock
clock = pygame.time.Clock()
# Player settings
player_width = 50
player_height = 60
player_x = screen_width // 2
player_y = screen_height - player_height - 10
player_velocity = 5
is_jumping = False
jump_count = 10
# Gravity
gravity = 0.8
# Main game loop
running = True
while running:
screen.fill(WHITE) # Fill the screen with white
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Key presses for player movement
keys = pygame.key.get_pressed()
# Move left and right
if keys[pygame.K_LEFT]:
player_x -= player_velocity
if keys[pygame.K_RIGHT]:
player_x += player_velocity
# Jumping logic
if not is_jumping:
if keys[pygame.K_SPACE]:
is_jumping = True
else:
if jump_count >= -10:
neg = 1
if jump_count < 0:
neg = -1
player_y -= (jump_count ** 2) * 0.5 * neg
jump_count -= 1
else:
is_jumping = False
jump_count = 10
# Draw the player (a simple rectangle)
pygame.draw.rect(screen, GREEN, (player_x, player_y, player_width, player_height))
# Update the screen
pygame.display.update()
# Set the FPS (frames per second)
clock.tick(60)
# Quit Pygame
pygame.quit()
Editor is loading...
Leave a Comment