5
\$\begingroup\$

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("You killed the {enemy_name}".format(enemy_name=enemy_name))
        its_getting_late()
    else:
        print("You walk away unscathed, but the {enemy_name} still lives.".format(enemy_name=enemy_name))
        its_getting_late()

def fight_enemy_2(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("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']:
        intro()
    elif ch4 in ['n', 'N', 'No', 'NO', 'no']:
        print("Thank you for playing")

def its_getting_late():
    time.sleep(1)
    print("Its starting to get late")
    ch2 = str(input("Do you wanna go to sleep? [y/n]: "))

    if ch2 in ['y', 'Y', 'YES', 'Yes', 'yes']:
        print("You were shot dead last Night")
    elif ch2 in ['n', 'N', 'No', 'NO', 'no']:
        print("You stay up")
        print("You see a person")
        ch3 = str(input("Do you wanna attack the person? [y/n]: "))

        if ch3 in ['y', 'Y', 'YES', 'Yes', 'yes']:
            time.sleep(1)
            fight_enemy_2('Enemy', 4, 9, 1, 7)
        elif ch3 in ['n', 'N', 'No', 'NO', 'no']:
            print("You try to run away")
            print("But the Enemy is faster than you and stabs you")
            game_over()

def game_over():
    print("You lost")
    ch5 = str(input("Do you wanna play again? [y/n] "))

    if ch5 in ['y', 'Y', 'YES', 'Yes', 'yes']:
        intro()
    elif ch5 in ['n', 'N', 'No', 'NO', 'no']:
        print("Thank you for playing")


intro()
\$\endgroup\$
2
  • \$\begingroup\$ See codereview.stackexchange.com/help/how-to-ask - ask specific questions and maybe also explain your reasoning, even for a simple program your current question is off-topic as way too vague. Maybe describe your approach a little, then explain why you chose it, and concerns or thoughts you have that you would appreciate some input on. \$\endgroup\$ Commented Aug 11, 2015 at 5:27
  • \$\begingroup\$ I edited the question is it ok now? @DanielBrose \$\endgroup\$ Commented Aug 11, 2015 at 5:33

3 Answers 3

4
\$\begingroup\$

I believe it's a good initial attempt.

However some advice, ideas for you consideration:


Main function

May I point out that it's common to use the construct:

#define you main function
def main():

#when the script is executed it will call the main function.
if __name__ == "__main__":
    main()

this because else if the script is imported inside another script it will automatically execute your code. The use of the main construct will protect your code against this, in a way that it will only execute the main() method in case the script is ran directly.


Cascading

I'm not a big fan of constructs where a intro function calls another function, which will call another function. This often gets messy very quickly. (What function called the fighting scene again?). It also requires you to dig deep in your function structure to alter the main story of the RPG.

In my opinion it's better to use the main function described above and use it as main entry point of your storyline. Let it call intro() and afterwards let it call desert(). Put the main logic inside the main function and perhaps even consider to use a gameloop to keep your game going and even have multiple areas/levels.


OOP

This might be a good time to start considering delving into object oriented programming. You can consider creating player objects, enemy objects and a game object. These will hold state information about the player and enemy (things like hp/dmg/items/etc..) in a clean and readable way. I won't go deep into this for this review, but I believe this type of programs are an excellent starting point to get acquainted with OOP.


lastly there is the part that was covered already by Janos, which can also clean things up a bit.

\$\endgroup\$
4
\$\begingroup\$

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')
\$\endgroup\$
3
\$\begingroup\$

Structure

  • I would second @DJannssens's point about using a main() function and checking for __name__=="__main__" You should think of this as mandatory in Python.
  • You should put related functions near each other. For example, it's confusing that game_over and game_end are not declared back-to-back. Many readers of your code would want to compare these two
  • Docstrings are helpful to readers and yourself.

Brevity/clarity

  • In its_getting_late you can shorten your check for yes/no from:

    if ch4 in ['y', 'Y', 'YES', 'Yes', 'yes']:
        intro()
    elif ch4 in ['n', 'N', 'No', 'NO', 'no']:
        print("Thank you for playing")
    

    to something like this:

    if ch4[0].lower() == "y":
        ...
    elif ch4[0].lower() == "n":
        ...
    
  • As @janos said of the yes/no checking, it would be best to stick this in a function since you often check for yes/no answers.

  • You don't use sys so you can skip the import.

Maintainability

  • It's usually a sign of trouble if you have many numbers, rather than constants, in your code. I see time.sleep(1) and time.sleep(2) all over. Why not do something like:

    LONG_SLEEP = 1
    SHORT_SLEEP = 2 
    time.sleep(LONG_SLEEP)
    

    If you ever want to change the sleeping period, this will make it an easy fix. It also shows your reader there is a logic to the amounts of time you're choosing. And given that you'll want to optimize your game based on user feedback, you want these parameters, like waiting time, to be easy to tweak repeatedly.

Helpful additions/future learning challenges

  • If you want to keep playing with this, think about how you would internationalize it. You could create a class for the strings you print to the user. Figure out how to make a class where you can set the language with just one parameter and have all the strings change the language they print.

  • If you want to add a pause feature look into the pickle module to see how you can save game information for a later continuation.

\$\endgroup\$
2
  • \$\begingroup\$ I like the maintainiblity idea. I will definitely add that in the game and I will look into adding the pickle module. Thank you for your answer. \$\endgroup\$ Commented Aug 11, 2015 at 18:00
  • \$\begingroup\$ Have my up vote for the maintainability idea! Nice one. \$\endgroup\$
    – DJanssens
    Commented Aug 11, 2015 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.