0% found this document useful (0 votes)
26 views2 pages

python mario coad

Uploaded by

dusha4303
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views2 pages

python mario coad

Uploaded by

dusha4303
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import pygame

import sys

# Initialize Pygame
pygame.init()

# Set up display
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Mario-Like Platformer")

# Colors
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)

# Player settings
player_width = 50
player_height = 50
player_x = 100
player_y = 500
player_velocity = 5
player_jump = -15
gravity = 0.8
y_velocity = 0
is_jumping = False

# Platform settings
platform_width = 800
platform_height = 50
platform_y = 550

# Set up the clock


clock = pygame.time.Clock()

# Game loop
while True:
screen.fill(WHITE) # Fill screen with white

# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

# Get the state of keys


keys = pygame.key.get_pressed()

# Move the player left and right


if keys[pygame.K_LEFT]:
player_x -= player_velocity
if keys[pygame.K_RIGHT]:
player_x += player_velocity

# Jumping mechanic
if not is_jumping:
if keys[pygame.K_SPACE]:
y_velocity = player_jump
is_jumping = True
else:
y_velocity += gravity
player_y += y_velocity

# Collision with the ground (platform)


if player_y >= platform_y - player_height:
player_y = platform_y - player_height
y_velocity = 0
is_jumping = False

# Draw the player and platform


pygame.draw.rect(screen, BLUE, (player_x, player_y, player_width,
player_height))
pygame.draw.rect(screen, GREEN, (0, platform_y, platform_width,
platform_height))

# Update the display


pygame.display.update()

# Set frame rate


clock.tick(60)
v

You might also like