0% found this document useful (0 votes)
23 views5 pages

Untitled

This document contains a Python script for a simple game called 'Danger Dash' using the Pygame library. The game involves a player dodging obstacles while keeping score, with controls for moving left and right. It includes features such as displaying the score, handling game over conditions, and allowing the player to restart or quit the game.

Uploaded by

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

Untitled

This document contains a Python script for a simple game called 'Danger Dash' using the Pygame library. The game involves a player dodging obstacles while keeping score, with controls for moving left and right. It includes features such as displaying the score, handling game over conditions, and allowing the player to restart or quit the game.

Uploaded by

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

import pygame

import random

import time

# Initialize pygame

pygame.init()

# Set up the game screen

screen_width = 800

screen_height = 600

screen = pygame.display.set_mode((screen_width, screen_height))

pygame.display.set_caption(”Danger Dash”)

# Define colors

WHITE = (255, 255, 255)

BLACK = (0, 0, 0)

RED = (255, 0, 0)

# Player settings

player_width = 50

player_height = 50

player_x = screen_width // 4

player_y = screen_height - player_height - 10

player_speed = 10

# Obstacle settings

obstacle_width = 50
obstacle_height = 50

obstacle_speed = 10

obstacles = []

# Set up the clock for FPS control

clock = pygame.time.Clock()

# Font for score and text

font = pygame.font.SysFont(None, 30)

# Function to display score

def display_score(score):

value = font.render(f”Score: {score}”, True, WHITE)

screen.blit(value, (10, 10))

# Function to display message

def display_message(message):

text = font.render(message, True, RED)

screen.blit(text, (screen_width // 2 - 100, screen_height // 2))

# Main game loop

def game_loop():

game_over = False

score = 0

player_x = screen_width // 4

player_y = screen_height - player_height - 10


# Game loop

while not game_over:

screen.fill(BLACK)

for event in pygame.event.get():

if event.type == pygame.QUIT:

game_over = True

# 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 < screen_width - player_width:

player_x += player_speed

# Generate obstacles

if random.randint(1, 100) <= 5:

obstacle_x = random.randint(screen_width, screen_width + 100)

obstacle_y = screen_height - obstacle_height - 10

obstacles.append([obstacle_x, obstacle_y])

# Move obstacles

for obstacle in obstacles:

obstacle[0] -= obstacle_speed

if obstacle[0] < 0:

obstacles.remove(obstacle)

score += 1
# Check for collision with player

if (player_x < obstacle[0] + obstacle_width and

player_x + player_width > obstacle[0] and

player_y < obstacle[1] + obstacle_height and

player_y + player_height > obstacle[1]):

game_over = True

# Draw player

pygame.draw.rect(screen, WHITE, (player_x, player_y, player_width, player_height))

# Draw obstacles

for obstacle in obstacles:

pygame.draw.rect(screen, RED, (obstacle[0], obstacle[1], obstacle_width, obstacle_height))

# Display score

display_score(score)

# Check if the player is game-over

if game_over:

display_message(”Game Over! Press Q to Quit or C to Play Again”)

pygame.display.update()

# Control FPS

clock.tick(30)
# Restart or quit game

if game_over:

keys = pygame.key.get_pressed()

if keys[pygame.K_q]:

game_over = True

if keys[pygame.K_c]:

game_loop() # Restart the game

# Start the game

game_loop()

# Quit pygame

pygame.quit()

You might also like