0% found this document useful (0 votes)
40 views3 pages

Running Game Aym

This document provides a simple running game implemented using the pygame library, where a player controls a rectangle character to avoid incoming obstacles. The game includes features such as score tracking, player movement, and obstacle generation, with increasing difficulty as the score rises. To run the game, users need to install pygame and execute the provided code.

Uploaded by

ayanislam0973
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)
40 views3 pages

Running Game Aym

This document provides a simple running game implemented using the pygame library, where a player controls a rectangle character to avoid incoming obstacles. The game includes features such as score tracking, player movement, and obstacle generation, with increasing difficulty as the score rises. To run the game, users need to install pygame and execute the provided code.

Uploaded by

ayanislam0973
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

Here's a simple "running" game using the pygame library, where the player controls

a character (represented by a rectangle) that must avoid incoming obstacles. The


character moves left or right, and the game continues until the character collides
with an obstacle.

To play this game, you'll need the pygame library. If you don't have it installed
yet, use the following command:

bash
Copy
pip install pygame
Now, here's the code for the running game:

python
Copy
import pygame
import random

# Initialize pygame
pygame.init()

# Define colors
white = (255, 255, 255)
black = (0, 0, 0)
red = (213, 50, 80)
green = (0, 255, 0)
blue = (50, 153, 213)

# Set up the display


width = 600
height = 400
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Running Game')

# Define clock and font


clock = pygame.time.Clock()
player_width = 50
player_height = 50
player_x = width // 2
player_y = height - player_height - 10
player_speed = 5
obstacle_width = 50
obstacle_height = 50
obstacle_speed = 7

font_style = pygame.font.SysFont("bahnschrift", 25)

# Function to display score


def Your_score(score):
value = font_style.render("Score: " + str(score), True, black)
screen.blit(value, [10, 10])

# Function to create the player


def player(x, y):
pygame.draw.rect(screen, green, [x, y, player_width, player_height])

# Function to create obstacles


def create_obstacle():
x = random.randint(0, width - obstacle_width)
y = -obstacle_height
return x, y

# Main function for the game


def gameLoop():
game_over = False
game_close = False

# Initial position and speed of the player


player_x = width // 2
player_y = height - player_height - 10
player_speed = 5

# Initial obstacle settings


obstacle_x, obstacle_y = create_obstacle()
obstacle_speed = 7

# Game variables
score = 0

while not game_over:

while game_close:
screen.fill(blue)
message = font_style.render("You Lost! Press Q-Quit or C-Play Again",
True, red)
screen.blit(message, [width // 6, height // 3])
Your_score(score)
pygame.display.update()

for event in pygame.event.get():


if event.type == pygame.QUIT:
game_over = True
game_close = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_c:
gameLoop()

for event in pygame.event.get():


if event.type == pygame.QUIT:
game_over = True

# Control player movement


keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player_x > 0:
player_x -= player_speed
if keys[pygame.K_RIGHT] and player_x < width - player_width:
player_x += player_speed

# Move the obstacle down the screen


obstacle_y += obstacle_speed

# If obstacle goes off the screen, create a new one and increase the score
if obstacle_y > height:
obstacle_x, obstacle_y = create_obstacle()
score += 1
obstacle_speed += 0.5 # Increase speed as score goes up

# Check for collision


if player_x < obstacle_x + obstacle_width and player_x + player_width >
obstacle_x:
if player_y < obstacle_y + obstacle_height and player_y + player_height
> obstacle_y:
game_close = True

# Fill screen with background color and draw player and obstacle
screen.fill(blue)
player(player_x, player_y)
pygame.draw.rect(screen, red, [obstacle_x, obstacle_y, obstacle_width,
obstacle_height])
Your_score(score)

pygame.display.update()

# Set the game frame rate


clock.tick(30)

pygame.quit()
quit()

# Run the game


gameLoop()

You might also like