Here is s simple video game in python you can expand further
Below is an example of a simple text-based video game in Python. It’s a basic adventure game where the player moves through rooms, picks up items, and has a simple goal (like finding a treasure). You can copy this code into a Python script and run it in a terminal.
# Adventure Game
# Define the rooms and their connections
rooms = {
'Hall': {'south': 'Kitchen', 'east': 'Library', 'item': None},
'Kitchen': {'north': 'Hall', 'item': 'knife'},
'Library': {'west': 'Hall', 'south': 'Garden', 'item': None},
'Garden': {'north': 'Library', 'item': 'treasure'},
}
# Player starts in the Hall
current_room = 'Hall'
inventory = []
def show_status():
"""Displays the player's current status: room, inventory, and room details."""
print(f"\nYou are in the {current_room}.")
if rooms[current_room]['item']:
print(f"You see a {rooms[current_room]['item']} here.")
else:
print("There's nothing of interest here.")
print(f"Inventory: {inventory}")
def move(direction):
"""Moves the player in the specified direction if possible."""
global current_room
if direction in rooms[current_room]:
current_room = rooms[current_room][direction]
print(f"\nYou moved {direction} to the {current_room}.")
else:
print("\nYou can't go that way!")
def pickup_item():
"""Handles the player picking up an item if available in the current room."""
item = rooms[current_room]['item']
if item:
inventory.append(item)
print(f"\nYou picked up the {item}!")
rooms[current_room]['item'] = None
else:
print("\nThere's nothing to pick up.")
def check_win():
"""Checks if the player has won the game (by finding the treasure)."""
if 'treasure' in inventory:
print("\nCongratulations! You found the treasure and won the game!")
return True
return False
def game_loop():
"""Main game loop to interact with the player."""
print("Welcome to the Adventure Game!")
print("Commands: go [north/south/east/west], pickup, quit")
while True:
show_status()
command = input("\nEnter your command: ").strip().lower()
if command == 'quit':
print("\nThanks for playing!")
break
if command.startswith('go '):
direction = command.split()[1]
move(direction)
elif command == 'pickup':
pickup_item()
if check_win():
break
# Run the game
game_loop()
How to play:
- The player starts in the “Hall” and can navigate through rooms using the command
go [north/south/east/west]
. - They can pick up items using the
pickup
command. - The goal is to find the “treasure” in the Garden and pick it up.
- The player can check their inventory and see their location at any time.
- The game ends once the treasure is found or the player types
quit
.
You can expand this game further by adding more rooms, items, and interactions.