Code the Oregon Trail game in Pygame
- User
Code the Oregon Trail game in Pygame
- Assistant
Code the Oregon Trail game in Pygame
Sure! I'd be happy to help you create a simplified version of the Oregon Trail game using Pygame. The Oregon Trail is a classic game that simulates a journey across the 19th-century American frontier, focusing on resource management and survival.
We'll create a basic version that captures key elements:
Let's get started!
Ensure you have Python and Pygame installed. You can install Pygame using:
pip install pygame
We'll structure the game into several parts:
import pygame
import sys
import random
pygame.init()
# Screen dimensions
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Oregon Trail Simplified")
clock = pygame.time.Clock()
The Player class will manage player attributes.
class Player:
def __init__(self):
self.name = "Pioneer"
self.health = 100
self.food = 500
self.money = 300
self.distance_traveled = 0
self.total_distance = 2000 # Total distance to Oregon
self.pace = 10 # How fast they travel per turn
self.status = "Good"
def update_status(self):
if self.health > 70:
self.status = "Good"
elif self.health > 40:
self.status = "Fair"
elif self.health > 0:
self.status = "Poor"
else:
self.status = "Dead"
The Game class will manage the game state and events.
class Game:
def __init__(self):
self.player = Player()
self.running = True
self.font = pygame.font.SysFont(None, 32)
self.large_font = pygame.font.SysFont(None, 64)
self.event_message = ""
self.choice_made = False
def process_events(self):
# Handle player input and window events
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
def main():
game = Game()
while game.running:
game.process_events()
game_logic(game)
draw_screen(game)
pygame.display.flip()
clock.tick(60) # Limit to 60 frames per second
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()
def game_logic(game):
# Update the player's status
game.player.update_status()
# Check if the player is still alive
if game.player.status == "Dead":
game.running = False
return
# Handle random events
if not game.choice_made:
chance = random.randint(0, 100)
if chance < 10:
random_event(game)
else:
travel(game)
else:
game.choice_made = False
def travel(game):
# Reduce food based on pace
food_consumed = game.player.pace * 2
game.player.food -= food_consumed
game.player.distance_traveled += game.player.pace
# Random health decrease
health_loss = random.randint(0, 5)
game.player.health -= health_loss
# Check for victory condition
if game.player.distance_traveled >= game.player.total_distance:
game.running = False
game.victory = True
def random_event(game):
events = [
"Nothing happens",
"You find wild berries",
"A thief steals some of your food",
"You encounter a trading post",
"A member falls ill",
"Your wagon wheel breaks",
]
event = random.choice(events)
game.event_message = event
if event == "You find wild berries":
game.player.food += 50
elif event == "A thief steals some of your food":
stolen = min(100, game.player.food)
game.player.food -= stolen
elif event == "You encounter a trading post":
trade(game)
elif event == "A member falls ill":
game.player.health -= 20
elif event == "Your wagon wheel breaks":
game.player.pace -= 2
if game.player.pace < 5:
game.player.pace = 5
def trade(game):
# Simple trade interaction
trade_complete = False
while not trade_complete:
# Display trade options
# (In an actual game, you would capture player input)
game.event_message = "You traded $50 for 100 food."
game.player.food += 100
game.player.money -= 50
trade_complete = True
game.choice_made = True
def draw_screen(game):
screen.fill((135, 206, 235)) # Sky blue background
# Draw the ground
pygame.draw.rect(screen, (34, 139, 34), (0, SCREEN_HEIGHT - 100, SCREEN_WIDTH, 100))
# Display player stats
draw_text(screen, f"Health: {game.player.health}", 20, 20)
draw_text(screen, f"Food: {game.player.food}", 20, 60)
draw_text(screen, f"Money: ${game.player.money}", 20, 100)
draw_text(screen, f"Distance Traveled: {game.player.distance_traveled}/{game.player.total_distance}", 20, 140)
draw_text(screen, f"Status: {game.player.status}", 20, 180)
# Display event message
if game.event_message:
draw_text_center(screen, game.event_message, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
# Reset event message after displaying
game.event_message = ""
# Display victory or game over
if not game.running:
if hasattr(game, 'victory') and game.victory:
draw_text_center(screen, "You have reached Oregon! You Win!", SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, font_size=48)
else:
draw_text_center(screen, "Game Over", SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, font_size=48)
def draw_text(surface, text, x, y, font_size=32, color=(0, 0, 0)):
font = pygame.font.SysFont(None, font_size)
img = font.render(text, True, color)
surface.blit(img, (x, y))
def draw_text_center(surface, text, x, y, font_size=32, color=(0, 0, 0)):
font = pygame.font.SysFont(None, font_size)
img = font.render(text, True, color)
rect = img.get_rect(center=(x, y))
surface.blit(img, rect)
This is a basic framework for the Oregon Trail game. Here are some enhancements you can implement:
Here's the complete code combining all sections:
import pygame
import sys
import random
# Initialize Pygame
pygame.init()
# Screen dimensions
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Oregon Trail Simplified")
# Clock
clock = pygame.time.Clock()
class Player:
def __init__(self):
self.name = "Pioneer"
self.health = 100
self.food = 500
self.money = 300
self.distance_traveled = 0
self.total_distance = 2000 # Total distance to Oregon
self.pace = 10 # How fast they travel per turn
self.status = "Good"
def update_status(self):
if self.health > 70:
self.status = "Good"
elif self.health > 40:
self.status = "Fair"
elif self.health > 0:
self.status = "Poor"
else:
self.status = "Dead"
class Game:
def __init__(self):
self.player = Player()
self.running = True
self.font = pygame.font.SysFont(None, 32)
self.large_font = pygame.font.SysFont(None, 64)
self.event_message = ""
self.choice_made = False
def process_events(self):
# Handle player input and window events
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
def game_logic(game):
# Update the player's status
game.player.update_status()
# Check if the player is still alive
if game.player.status == "Dead":
game.running = False
return
# Handle random events
if not game.choice_made:
chance = random.randint(0, 100)
if chance < 10:
random_event(game)
else:
travel(game)
else:
game.choice_made = False
def travel(game):
# Reduce food based on pace
food_consumed = game.player.pace * 2
game.player.food -= food_consumed
game.player.distance_traveled += game.player.pace
# Random health decrease
health_loss = random.randint(0, 5)
game.player.health -= health_loss
# Check for victory condition
if game.player.distance_traveled >= game.player.total_distance:
game.running = False
game.victory = True
def random_event(game):
events = [
"Nothing happens",
"You find wild berries",
"A thief steals some of your food",
"You encounter a trading post",
"A member falls ill",
"Your wagon wheel breaks",
]
event = random.choice(events)
game.event_message = event
if event == "You find wild berries":
game.player.food += 50
elif event == "A thief steals some of your food":
stolen = min(100, game.player.food)
game.player.food -= stolen
elif event == "You encounter a trading post":
trade(game)
elif event == "A member falls ill":
game.player.health -= 20
elif event == "Your wagon wheel breaks":
game.player.pace -= 2
if game.player.pace < 5:
game.player.pace = 5
def trade(game):
# Simple trade interaction
trade_complete = False
while not trade_complete:
# Display trade options
# (In an actual game, you would capture player input)
game.event_message = "You traded $50 for 100 food."
game.player.food += 100
game.player.money -= 50
trade_complete = True
game.choice_made = True
def draw_screen(game):
screen.fill((135, 206, 235)) # Sky blue background
# Draw the ground
pygame.draw.rect(screen, (34, 139, 34), (0, SCREEN_HEIGHT - 100, SCREEN_WIDTH, 100))
# Display player stats
draw_text(screen, f"Health: {game.player.health}", 20, 20)
draw_text(screen, f"Food: {game.player.food}", 20, 60)
draw_text(screen, f"Money: ${game.player.money}", 20, 100)
draw_text(screen, f"Distance Traveled: {game.player.distance_traveled}/{game.player.total_distance}", 20, 140)
draw_text(screen, f"Status: {game.player.status}", 20, 180)
# Display event message
if game.event_message:
draw_text_center(screen, game.event_message, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
# Reset event message after displaying
game.event_message = ""
# Display victory or game over
if not game.running:
if hasattr(game, 'victory') and game.victory:
draw_text_center(screen, "You have reached Oregon! You Win!", SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, font_size=48)
else:
draw_text_center(screen, "Game Over", SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, font_size=48)
def draw_text(surface, text, x, y, font_size=32, color=(0, 0, 0)):
font = pygame.font.SysFont(None, font_size)
img = font.render(text, True, color)
surface.blit(img, (x, y))
def draw_text_center(surface, text, x, y, font_size=32, color=(0, 0, 0)):
font = pygame.font.SysFont(None, font_size)
img = font.render(text, True, color)
rect = img.get_rect(center=(x, y))
surface.blit(img, rect)
def main():
game = Game()
while game.running:
game.process_events()
game_logic(game)
draw_screen(game)
pygame.display.flip()
clock.tick(60) # Limit to 60 frames per second
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()
Save the code to a file named oregon_trail.py and run it using:
python oregon_trail.py
You should see a window displaying your stats and occasional event messages. Since user input isn't implemented yet, the game runs automatically. Enhancing it with input handling will make it interactive.
Feel free to ask if you need assistance implementing specific features or have questions about the code!
Not yet included in a published release.