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

Untitled Document

The document is a Python script that implements a simple game using the Pygame library called 'Forest Run'. In the game, a player controls a green square that moves around the screen while avoiding a red monster that chases it. The game ends when the player collides with the monster, displaying a 'Game Over' message.

Uploaded by

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

Untitled Document

The document is a Python script that implements a simple game using the Pygame library called 'Forest Run'. In the game, a player controls a green square that moves around the screen while avoiding a red monster that chases it. The game ends when the player collides with the monster, displaying a 'Game Over' message.

Uploaded by

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

import pygame

import random

# Initialize pygame
pygame.init()

# Screen settings
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Forest Run")

# Colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)

# Game settings
player_size = 50
monster_size = 60
player_pos = [WIDTH // 4, HEIGHT // 2]
monster_pos = [WIDTH * 3 // 4, HEIGHT // 2]
player_speed = 5
monster_speed = 3

# Clock
clock = pygame.time.Clock()
font = pygame.font.Font(None, 74)

# Function to check collision


def check_collision(player_pos, monster_pos):
p_x, p_y = player_pos
m_x, m_y = monster_pos
return abs(p_x - m_x) < player_size and abs(p_y - m_y) < player_size

# Game loop
game_over = False
while not game_over:
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]:
player_pos[0] -= player_speed
if keys[pygame.K_RIGHT]:
player_pos[0] += player_speed
if keys[pygame.K_UP]:
player_pos[1] -= player_speed
if keys[pygame.K_DOWN]:
player_pos[1] += player_speed

# Loop player and monster positions if they go off-screen


player_pos[0] %= WIDTH
player_pos[1] %= HEIGHT
monster_pos[0] %= WIDTH
monster_pos[1] %= HEIGHT

# Monster movement: Moves towards the player


if monster_pos[0] < player_pos[0]:
monster_pos[0] += monster_speed
elif monster_pos[0] > player_pos[0]:
monster_pos[0] -= monster_speed
if monster_pos[1] < player_pos[1]:
monster_pos[1] += monster_speed
elif monster_pos[1] > player_pos[1]:
monster_pos[1] -= monster_speed

# Check for collision


if check_collision(player_pos, monster_pos):
game_over = True
text = font.render("Game Over!", True, RED)
screen.blit(text, (WIDTH // 2 - 100, HEIGHT // 2 - 50))
pygame.display.flip()
pygame.time.delay(2000)
break

# Drawing
screen.fill(WHITE)
pygame.draw.rect(screen, GREEN, (*player_pos, player_size, player_size))
pygame.draw.rect(screen, RED, (*monster_pos, monster_size, monster_size))
pygame.display.flip()

# Frame rate
clock.tick(30)

pygame.quit()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy