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

Pygame Phython

This document is a Python script that implements a simple platformer game using the Pygame library. The game features a player character that can move left, right, and jump, while avoiding obstacles that spawn over time. The player's score increases as they survive longer, and a game over screen displays the final score when the player collides with an obstacle.
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)
37 views2 pages

Pygame Phython

This document is a Python script that implements a simple platformer game using the Pygame library. The game features a player character that can move left, right, and jump, while avoiding obstacles that spawn over time. The player's score increases as they survive longer, and a game over screen displays the final score when the player collides with an obstacle.
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

import pygame

import sys

pygame.init()

WIDTH = 800
HEIGHT = 400
clock = pygame.time.Clock()
pygame.display.set_caption("simple platformer")
font = pygame.font.Font(None, 40)
big_font = pygame.font.Font(None, 60)
player_vel_y = 0
gravity = 1
jump_speed = -15
ground_y = 300
on_ground = True
speed =5
screen = pygame.display.set_mode((WIDTH , HEIGHT))
# player
dino=pygame.Rect(50,ground_y-40,40,60)

# obstacles
obstacles = []
obstacle_width = 30
obstacle_height = 40
spawn_event = pygame.USEREVENT + 1
pygame.time.set_timer(spawn_event, 1500)
#score
score=0
def draw_dino():
pygame.draw.rect(screen, (255, 0, 0), dino)

def draw_obstacles():
for obs in obstacles:
pygame.draw.rect(screen, (123, 0, 0), obs)
def draw_ground():
pygame.draw.rect(screen, (0, 244 , 0), (0, ground_y ,WIDTH,40))#, HEIGHT -
ground_y ))

def display_score():
text = font.render(f"Score: {score}", True, (0, 0, 0))
screen.blit(text, (10, 10))

def game_over_function(final_score):
screen.fill((255, 255, 255))
game_over_text = big_font.render(f"Game Over! Final Score: {final_score}",
True, (0, 0, 125))
score_text = font.render(f"final_score: {final_score}", True, (0, 0, 0))
msg_text = font.render("thanks for playing", True, (0, 0, 0))
screen.blit(game_over_text,(WIDTH// 2 - game_over_text.get_width() // 2, 120))
screen.blit(score_text, (WIDTH // 2 - score_text.get_width() // 2, 200))
screen.blit(msg_text, (WIDTH // 2 - msg_text.get_width() // 2, 250))
pygame.display.flip()
pygame.time.delay(3000) # wait for 3 seconds

running = True
while running:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == spawn_event:
obstacles.append(pygame.Rect(WIDTH, ground_y - obstacle_height,
obstacle_width, obstacle_height))

# key input
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and dino.left > 0:
dino.x -= speed
if keys[pygame.K_RIGHT] and dino.right < 600:
dino.x += speed
if keys[pygame.K_SPACE] and on_ground:
player_vel_y = jump_speed
on_ground = False

player_vel_y += gravity
dino.y += player_vel_y

if dino.y >= ground_y - dino.height:


dino.y = ground_y - dino.height
player_vel_y = 0
on_ground = True

for obs in obstacles:


obs.x -= 6
obstacles = [ obs for obs in obstacles if obs.x > -obstacle_width]
# collision detection
for obs in obstacles:
if dino.colliderect(obs):
game_over_function(score // 10)
pygame.quit()
sys.exit()
# score
score += 1

# draw everything
screen.fill((153, 220, 253)) # sky blue background
draw_dino()
draw_obstacles()
draw_ground()
display_score()

pygame.display.flip()

pygame.quit()

You might also like