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


Messages In This Thread
1st Game - by taflad - Jun-26-2018, 08:21 AM
RE: 1st Game - by ichabod801 - Jun-26-2018, 12:52 PM
RE: 1st Game - by taflad - Jun-27-2018, 10:29 AM

Forum Jump:

User Panel Messages

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