Here's a simple text-based Python game where you control Yogi Bear, who needs to steal 5 picnic baskets while avoiding Ranger Smith. After stealing 3 baskets, Ranger Smith will start driving a jeep, making him move faster. If you collect all 5 baskets, Ranger Smith gets fired by Chief Short.
import random
# Define the game map size
MAP_WIDTH = 10
MAP_HEIGHT = 10
# Game symbols
YOGI = 'Y'
RANGER = 'R'
BASKET = 'B'
EMPTY = '.'
# Game state
class GameState:
def __init__(self):
# Initialize positions
self.yogi_pos = [0, 0]
self.ranger_pos = [MAP_WIDTH - 1, MAP_HEIGHT - 1]
self.baskets = []
self.baskets_collected = 0
self.ranger_in_jeep = False
self.game_over = False
self.win = False
# Place baskets randomly
self.place_baskets()
def place_baskets(self):
while len(self.baskets) < 5:
x = random.randint(0, MAP_WIDTH - 1)
y = random.randint(0, MAP_HEIGHT - 1)
if [x, y] != self.yogi_pos and [x, y] != self.ranger_pos and [x, y] not in self.baskets:
self.baskets.append([x, y])
def draw_map(self):
for y in range(MAP_HEIGHT):
row = ''
for x in range(MAP_WIDTH):
if self.yogi_pos == [x, y]:
row += YOGI
elif self.ranger_pos == [x, y]:
row += RANGER
elif [x, y] in self.baskets:
row += BASKET
else:
row += EMPTY
print(row)
print(f"Baskets Collected: {self.baskets_collected}/5")
def move_yogi(self, direction):
x, y = self.yogi_pos
if direction == 'w' and y > 0:
y -= 1
elif direction == 's' and y < MAP_HEIGHT - 1:
y += 1
elif direction == 'a' and x > 0:
x -= 1
elif direction == 'd' and x < MAP_WIDTH - 1:
x += 1
else:
print("Can't move in that direction!")
return
self.yogi_pos = [x, y]
self.check_basket()
def check_basket(self):
if self.yogi_pos in self.baskets:
self.baskets.remove(self.yogi_pos)
self.baskets_collected += 1
print("Yogi collected a picnic basket!")
if self.baskets_collected == 3 and not self.ranger_in_jeep:
self.ranger_in_jeep = True
print("Ranger Smith is now in a jeep and moves faster!")
if self.baskets_collected == 5:
self.game_over = True
self.win = True
print("Yogi collected all baskets!")
print("Ranger Smith is fired by Chief Short!")
def move_ranger(self):
# Ranger moves towards Yogi
x, y = self.ranger_pos
yx, yy = self.yogi_pos
dx = 1 if yx > x else -1 if yx < x else 0
dy = 1 if yy > y else -1 if yy < y else 0
speed = 2 if self.ranger_in_jeep else 1
for _ in range(speed):
if x != yx:
x += dx
elif y != yy:
y += dy
self.ranger_pos = [x, y]
if self.yogi_pos == self.ranger_pos:
self.game_over = True
print("Ranger Smith caught Yogi!")
break
def main():
game = GameState()
print("Welcome to Yogi Bear's Picnic Panic!")
print("Controls: w - up, s - down, a - left, d - right")
print("Collect 5 picnic baskets while avoiding Ranger Smith.")
print("After collecting 3 baskets, Ranger Smith will drive a jeep!")
while not game.game_over:
game.draw_map()
move = input("Your move (w/a/s/d): ").lower()
if move in ['w', 'a', 's', 'd']:
game.move_yogi(move)
if game.game_over:
break
game.move_ranger()
else:
print("Invalid input!")
if game.win:
print("Congratulations! You win!")
else:
print("Game Over. Better luck next time!")
if __name__ == "__main__":
main()
Instructions to run the game:
- Make sure you have Python installed on your computer.
- Copy the code into a file named
yogi_bear_game.py.
- Open a terminal or command prompt.
- Navigate to the directory containing the
yogi_bear_game.py file.
- Run the game with the command:
python yogi_bear_game.py
How to play:
- Use the following keys to move Yogi Bear:
w: Move up
s: Move down
a: Move left
d: Move right
- After each move, Ranger Smith will also move towards Yogi.
- Collect all 5 picnic baskets (
B) to win.
- Avoid Ranger Smith (
R).
- The game map will display the following symbols:
Y: Yogi Bear
R: Ranger Smith
B: Picnic Basket
.: Empty space
Game Features:
- Dynamic Map: Each game has a randomly generated map with picnic baskets placed in different locations.
- Increasing Difficulty: After collecting 3 baskets, Ranger Smith moves faster (simulating the jeep).
- Win Condition: Collect all 5 baskets to win the game and see Ranger Smith get fired.
- Lose Condition: If Ranger Smith catches Yogi Bear, the game is over.
Enjoy the game and help Yogi Bear collect those picnic baskets while outsmarting Ranger Smith!