HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Small text-based RPG

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
textsmallbasedrpg

Problem

This is my first text-based game I made. It's very small because I just learned Python and wanted to start small and then code bigger programs. Please comment on this.

```
import random
import time
import sys

def fight_enemy(enemy_name, min_enemy_damage, max_enemy_damage, min_player_damage, max_player_damage):
enemy_damage_dealt = random.randint(min_enemy_damage, max_enemy_damage)
player_damage_dealt = random.randint(min_player_damage, max_player_damage)

if enemy_damage_dealt > player_damage_dealt:
print("Uh-oh! You died!")
game_over()
elif enemy_damage_dealt player_damage_dealt:
print("Uh-oh! You died!")
game_over()
elif enemy_damage_dealt < player_damage_dealt:
print("You killed the {enemy_name}".format(enemy_name=enemy_name))
game_end()
else:
print("You walk away unscathed, but the {enemy_name} still lives.".format(enemy_name=enemy_name))
game_end()

def intro():
name = input("Enter you name: ")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("Welcome to the Wildlife %s" %(name))
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
time.sleep(1)
desert()

def desert():
print("You are in the middle of a Desert in Africa")
time.sleep(2)
print("All you see is Sand. EVERYWHERE!!!")
time.sleep(2)
print("You start walking")
time.sleep(2)
print("You see a Snake")
ch1 = str(input("Do you wanna attck the Snake? [y/n]: "))

if ch1 in ['y', 'Y', 'YES', 'Yes', 'yes']:
time.sleep(2)
fight_enemy('Snake', 1, 7, 1, 7)
elif ch1 in ['n', 'N', 'No', 'NO', 'no']:
print("You slowly back away")
time.sleep(1)
print("You managed to get away from the Snake")
print("But you fell in a sinkhole and died")
game_over()

def game_end():
print("Congratulations you won!!!")
ch4 = str(input("Do you wanna play again? [y/n] "))

if ch4 in ['y', 'Y', 'YES', 'Yes', 'yes']:
intr

Solution

You could simplify the yes/no answer checks:

if answer.lower() in ('y', 'yes'):
    # ...


And instead of writing these repeatedly, you could create a helper function to encapsulate this logic:

def is_answer_yes(answer):
    return answer.lower() in ('y', 'yes')

Code Snippets

if answer.lower() in ('y', 'yes'):
    # ...
def is_answer_yes(answer):
    return answer.lower() in ('y', 'yes')

Context

StackExchange Code Review Q#100554, answer score: 4

Revisions (0)

No revisions yet.