top of page
Screenshot 2025-02-12 at 8.31_edited.png

Pygame Platformer

Python Code

import pygame

 

pygame.init()

 

SCREEN_WIDTH = 800

SCREEN_HEIGHT = 600

GRAVITY = 0.5

 

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

 

def reset_game():

global player, player_speed_y, on_ground, falling_through

player = pygame.Rect((100, 450, 50, 50))

player_speed_y = 0

on_ground = False

falling_through = False

 

reset_game()

 

platforms = [

pygame.Rect(50, 500, 700, 20),

pygame.Rect(200, 400, 200, 20),

pygame.Rect(400, 300, 200, 20),

pygame.Rect(250, 200, 200, 20),

pygame.Rect(500, 100, 200, 20)

]

 

run = True

clock = pygame.time.Clock()

while run:

clock.tick(30)

screen.fill((0, 0, 0))

pygame.draw.rect(screen, (255, 255, 255), player)

for platform in platforms:

pygame.draw.rect(screen, (0, 100, 0), platform)

key = pygame.key.get_pressed()

if key[pygame.K_a]:

player.x -= 5

if key[pygame.K_d]:

player.x += 5

if key[pygame.K_w] and on_ground:

player_speed_y = -10

on_ground = False

if key[pygame.K_r]:

reset_game()

if key[pygame.K_s] and on_ground:

falling_through = True

player_speed_y += GRAVITY

player.y += player_speed_y

on_ground = False

for platform in platforms:

if player.colliderect(platform) and player_speed_y > 0 and not falling_through:

player.y = platform.y - player.height

player_speed_y = 0

on_ground = True

if not key[pygame.K_s]:

falling_through = False

for event in pygame.event.get():

if event.type == pygame.QUIT:

run = False

 

pygame.display.update()

 

pygame.quit()

Project Name

This is your Project description. A brief summary can help visitors understand the context of your work. Click on "Edit Text" or double click on the text box to start.

Project Name

This is your Project description. Provide a brief summary to help visitors understand the context and background of your work. Click on "Edit Text" or double click on the text box to start.

Project Name

This is your Project description. Click on "Edit Text" or double click on the text box to start.

Project Name

This is your Project description. Provide a brief summary to help visitors understand the context and background of your work. Click on "Edit Text" or double click on the text box to start.

Project Name

This is your Project description. A brief summary can help visitors understand the context of your work. Click on "Edit Text" or double click on the text box to start.

bottom of page