write a character-mode python game to simulate driving through a chasm or narrow road, with undrivable terrain on each side. the player character stays on the same line, controlling their character to left or right, trying to stay in the drivable area, as the environment scrolls automatically. if they drive off the drivable center, the game ends.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a simple character-mode Python game that simulates driving through a chasm:
python
import random
import time
# Set up the game constants
CHASM_WIDTH = 20
SCREEN_WIDTH = 50
DRIVEABLE_WIDTH = 10
CHARACTER = '🚗'
END_GAME_MSG = '💥 Game Over! You drove off the road. 💥'
SPEED = 0.1
CURSOR_VISIBLE = False
def draw_screen(chasm midpoint):
screen = ['_' * SCREEN_WIDTH for _ in range(SCREEN_WIDTH)]
screen[midpoint:midpoint + DRIVEABLE_WIDTH] = ['-' * DRIVEABLE_WIDTH]
screen[0] = '|'
screen[-1] = '|'
return '\n'.join(screen)
def game_loop():
midpoint = random.randint(DRIVEABLE_WIDTH, SCREEN_WIDTH - DRIVEABLE_WIDTH - 1)
player_pos = midpoint // 2
game_over = False
while not game_over:
# Draw the screen
print(draw_screen(midpoint), end='\r')
time.sleep(SPEED)
# Move the chasm
midpoint -= 1
if midpoint < -DRIVEABLE_WIDTH:
midpoint = SCREEN_WIDTH + DRIVEABLE_WIDTH
# Handle user input
user_input = input("\033[{};{}H{}".format(player_pos + 1, 1, CHARACTER))
if user_input == 'LEFT' and player_pos > 0:
player_pos -= 1
elif user_input == 'RIGHT' and player_pos < DRIVEABLE_WIDTH - 1:
player_pos += 1
# Check if game is over
if player_pos < 0 or player_pos >= DRIVEABLE_WIDTH:
game_over = True
print(END_GAME_MSG)
# Start the game
print("\033[2J") # Clear the terminal
print("Drive through the chasm! Use LEFT and RIGHT arrows.\n")
print(draw_screen(random.randint(DRIVEABLE_WIDTH, SCREEN_WIDTH - DRIVEABLE_WIDTH - 1)))
while True:
try:
game_loop()
except KeyboardInterrupt:
print("\nExiting the game...")
break
This game uses the terminal to display a simple ASCII representation of the chasm. The player's car is represented by '🚗', and the drivable area is marked by '-'. The chasm automatically scrolls to the left, and the player must use the LEFT and RIGHT arrow keys to stay on the road. If the car goes off the road, the game ends with a Game Over message.
To run the game, save it as a .py file and execute it in a terminal. Note that the terminal should support ANSI escape codes for proper cursor control and clearing the screen.
User
I got an error immediately after trying to run it.
Assistant
Apologies for the inconvenience. The error might be due to the terminal not supporting ANSI escape codes or the input method used. I'll modify the code to use a more terminal-friendly approach.
Here's the updated version:
python
import random
import time
# Set up the game constants
CHASM_WIDTH = 20
SCREEN_WIDTH = 50
DRIVEABLE_WIDTH = 10
CHARACTER = '🚗'
END_GAME_MSG = '💥 Game Over! You drove off the road. 💥'
SPEED = 0.1
CURSOR_VISIBLE = False
def draw_screen(chasm midpoint, player_pos):
screen = ['_' * SCREEN_WIDTH for _ in range(SCREEN_WIDTH)]
screen[midpoint:midpoint + DRIVEABLE_WIDTH] = ['-' * DRIVEABLE_WIDTH]
screen[0] = '|'
screen[-1] = '|'
screen[player_pos] = CHARACTER
return '\n'.join(screen)
def game_loop():
midpoint = random.randint(DRIVEABLE_WIDTH, SCREEN_WIDTH - DRIVEABLE_WIDTH - 1)
player_pos = midpoint // 2
game_over = False
while not game_over:
# Draw the screen
print(draw_screen(midpoint, player_pos), end='\r')
time.sleep(SPEED)
# Move the chasm
midpoint -= 1
if midpoint < -DRIVEABLE_WIDTH:
midpoint = SCREEN_WIDTH + DRIVEABLE_WIDTH
# Handle user input
try:
user_input = input()
if user_input == 'LEFT':
player_pos -= 1
elif user_input == 'RIGHT':
player_pos += 1
# Check if game is over
if player_pos < 0 or player_pos >= DRIVEABLE_WIDTH:
game_over = True
print(END_GAME_MSG)
except KeyboardInterrupt:
game_over = True
print(END_GAME_MSG)
# Start the game
print("\033[2J") # Clear the terminal
print("Drive through the chasm! Use LEFT and RIGHT to move.\n")
print(draw_screen(random.randint(DRIVEABLE_WIDTH, SCREEN_WIDTH - DRIVEABLE_WIDTH - 1), 0))
game_loop()
In this version, I've removed the cursor positioning and made the game loop wait for user input. The user should now press 'LEFT' or 'RIGHT' to move the car. This should work in most terminals. Press 'Ctrl+C' to quit the game.