Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
1st Game
#1
Hi All,

I am very new to python (and programming in general!) and have been following an online class. A task there was to create the old "Camel" game.

As I have no experience with functions yet, I created the game using a for loop (code below)

I'm just wondering if the code would be classed as "clean" as is, and also if there is a way to do this using functions? I'm not asking for anyone to write the function for me :) I'll never learn if that happens. Only looking for some pointers on clean code writing and how a function would help in this situation


import random

camel_tired = 0
thirst = 0
miles_travelled_total =20
miles_travelled_trip = 0
natives = 19
canteen = 5
done = False
oasis = 20
oasis_roll = 0

print("Welcome to the camel game!")
print("You have stoeln a camel from the local natives, and need to ride 300 miles to the border and to saftey.")
print("You have managed to get ahead by 20 miles, but now the natives are chasing you!")


while done == False:
    
    #Game Over settings
    if thirst > 6: 
        print("You have died of thirst. Game Over!")
        done == True
        replay = input("Would you like to play again?: ")
        replay_char = replay[:1]
        replay_char = replay_char.upper()
        if replay_char == "Y":
            done == False
        else:
            break

    if camel_tired > 30:
        print("Your camel has dies of exhaustion! Game over!")
        done == True
        replay = input("Would you like to play again?: ")
        replay_char = replay[:1]
        replay_char = replay_char.upper()
        if replay_char == "Y":
            done == False
        else:
            break

    if natives < 0:
        natives == 0
        print("The natives have caught and killed you. Game over!")
        replay = input("Would you like to play again?: ")
        replay_char = replay[:1]
        replay_char = replay_char.upper()
        if replay_char == "Y":
            done == False
        elif natives == 0:
            print("The natives have caught and killed you. Game over!")
            replay = input("Would you like to play again?: ")
            replay_char = replay[:1]
            replay_char = replay_char.upper()
        if replay_char == "Y":
            done == False
        else:
            break
        #Determines if player reaches Oasis
    if oasis_roll == oasis:
        print("You have reached an oasis!")
        print("You have rested your camel and refilled your canteen")
        print("The natives are ", natives, " miles behind you!")
        canteen = 5
        camel_tired = 0
        thirst = 0
        natives = miles_travelled_trip - random.randint(7, 12)




    choice = input("Select an option :  \n"
                   "A. Drink from your canteen \n"
                   "B. Ahead normal speed \n"
                   "C. Ahead Full Speed\n"
                   "D. Stop for the night\n"
                   "E. Check Status\n"
                   "Q. Quit \n"
                   "Enter your choice:  ")
    choice = choice.upper()
    if choice == "Q":
        done = True
    elif choice == "A":
        if thirst == 0:
            print("You are not thirsty, choose again")
        elif canteen == 0:
            print("There are no more drinks left. You have one more chance to escape or find an oasis!")
        else:
            thirst -= 1
            canteen -= 1
            natives -= random.randrange(3,6)
    elif choice == "B":
        miles_travelled_trip =  random.randrange(7, 15)
        miles_travelled_total  += miles_travelled_trip
        camel_tired += 5
        thirst += 1
        natives = miles_travelled_trip - random.randrange(1, 4)
        print("You have chosen to travel at normal speed. You have travelled ", miles_travelled_trip, " miles")
        oasis_roll = random.randrange(1, 21)
    elif choice == "C":
        miles_travelled_trip = random.randrange(10, 30)
        miles_travelled_total += miles_travelled_trip
        camel_tired += 10
        thirst += 1
        natives = miles_travelled_trip - random.randrange(5, 12)
        print("You have chosen to travel at full speed. You have travelled ", miles_travelled_trip, " miles.")
        oasis_roll = random.randrange(1, 21)
    elif choice == "D":
        camel_tired -= 10
        if camel_tired <0:
            camel_tired = 0
        natives  -= random.randrange(5, 13)
    elif choice == "E":
        print("Your camel's tiredness is ",  camel_tired)
        print("your thirst is ", thirst)
        print("the natives are ", natives, " miles behind you")
        print("You have travelled ", miles_travelled_total, " miles.")
Reply
#2
(Jun-26-2018, 08:21 AM)taflad Wrote: As I have no experience with functions yet, I created the game using a for loop (code below)

Note that you used a while loop, not a for loop. Two different animals.

The first thing you could do with functions is checking for replay. You keep repeating that code over and over again. If you want to change it, you have to change it in each place. If the code is in a function, you just have to change it in one place.

Functions can also help organized your code. I don't like having a block of code bigger than about 50 lines. If it is under 50 lines, I can see the whole block on my screen at once. You have a whole bunch of if statements. Each of those could be a function, and then the main block of code would be much smaller. If you use descriptive function names, like your variable names (nice to see from a new programmer), it's still clear what is going on in your code.

What I would do is put all the variables defined at the top of the code into a dictionary. Call it game_state. Then pass that dictionary to the functions as a parameter. While not strictly necessary, I would return it from the function and reassign it each time (for clarity). So you function calls would look something like this:

game_state = full_speed(game_state)
And your function calls would look something like this:

def full_speed(game_state):
    ... # code here
    return game_state
A few comments about the game: You don't need the done variable. Everywhere you would set done to True, you break instead, except the user choosing 'Q'. You could just break on 'Q', make the loop while True:, and simplify your if statements:

if replay_char != 'Y':
    break
Or, if you do the replay function like I suggest, and have it return True for replaying:

if not replay_check():
    break
You don't reset the game tracking variables when the player wants to replay. So if they kill their camel, they start the next game with a dead camel. This could be another function, to return a fresh game_state dictionary. The way you track the natives has no memory. If I go full speed twice and normal speed once, going normal speed wipes out any "gains" I got from going full speed. I would keep track of where the natives are, not how close they are, and have them move the same each turn (maybe a range between normal speed and full speed). Then check if their distance is greater than the player's to see if they've caught up.

There is a functions tutorial on this site. There is a link to it in my signature (below).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Brilliant, thanks very much for the reply and advice. I'll work to incorporate this and see if I can get my head around it :)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020