Skip to content

Instantly share code, notes, and snippets.

@sjoness
Created August 8, 2013 09:13
Show Gist options
  • Save sjoness/6183033 to your computer and use it in GitHub Desktop.
Save sjoness/6183033 to your computer and use it in GitHub Desktop.
# sys module lets you do sys.exit() - quit a program early
# random module lets you use the random functions
import random
import sys
# just to keep things simple and help you focus on the if...elif...else structure
# if you run within IDLE don't worry about the traceback on sys.exit() - won't happen when you run
# through DOS
# procedures go here
def gameoverearly():
print("""
Oh dear you lost.
Well, you're actually dead.
Never mind.
GAME OVER!!!""")
waitforinput = input("")
sys.exit()
# procedures end here
# main program starts here
print("""
Welcome to Galactic Adventure!
Your guide to the If...Elif...Else mystery...
""")
# initialise variables - are these locals/globals and why?
lives = 3
userinput = ""
# on the bridge of your space ship... #
print("""
You are on the bridge.
Through the viewport you see the enemy gun ports open...
""")
userinput = input("Would you like to shoot first ? (Yes/No): ")
print("")
if (userinput == "Yes") or (userinput == "yes"):
print(" Your laser disables their engines.")
print(" You move your ship across and start boarding.")
# else if userinput is "No" or "no"
if (userinput == "No") or (userinput == "no"):
print(" The enemy hits you with a MegaMissile.")
print(" You blow up - and therefore, are quite dead.")
# call gameoverearly
gameoverearly()
# entering the strange alien ship... #
print("")
print("""
You enter the strange alien ship. Everything is pink and blue.
1000 fluffy droids rush towards you, bristling with large hoover attachments.
You decide on the multi-spread grenade.
How many explosive tips do you want to arm the grenade with?""")
# read in noexplosivetips from keyboard
noexplosivetips = int(input(" No of tips: "))
print("")
if (noexplosivetips >= 1) and (noexplosivetips <=5):
print(" You destroy about 10 of the droids.")
print(" The others overpower you and throw you out the airlook.")
print("")
gameoverearly()
elif (noexplosivetips >=6) and (noexplosivetips <= 100):
print(" 100 droids explode against each other.")
print(" You take a couple of hits and lose 2 lives.")
print(" The other droids turn and run away.")
lives = lives - 2
# check for between 101 and 200 tips
elif (noexplosivetips >= 101) and (noexplosivetips <= 200):
print(" About half of the droids are destroyed, sending pink fluff all over the walls.")
print(" You take one hit an lose a life.")
print(" The other droids turn around and run away.")
lives = lives - 1
# check for 201 to 250 tips
elif (noexplosivetips >= 201) and (noexplosivetips < 250):
print(" 400 droids dissolve in front of you.")
print(" The other droids turn and run away.")
# check for more than 250 tips
elif (noexplosivetips > 250) :
print(" 500 droids explode in front of you.")
print(" The explosions blow out a hole in the side of the ship.")
print(" You are sucked out into the void.")
gameoverearly()
# check for everything else (wrong input, 0 etc)
else:
print(" You fumble with your grenades.")
print(" The droids raise their guns and you shut your eyes.")
print("")
gameoverearly()
# and now to defeat the master alien .... #
print(" You walk cautiously to the bridge. You see the big green master alien.")
print(" You wave at him - but he's not happy to see you.")
print("")
print(" Do you: ")
userinput = input(" A) attempt first contact, or B) shoot to kill: ")
# test if userinput is "A" or "a"
if (userinput == "A" or userinput == "a") :
print(" The Master Alien opens his mouth wide.")
print(" You stand frozen to the spot while you are eaten alive")
gameoverearly()
# this shows you a nested if statement!
if (userinput == "B") or (userinput == "b"):
print(" You shoulder your AutoZap gun. Which setting do you want to use?")
gunsetting = int(input(" (1 to 4) 4 = vaporize."))
if gunsetting == 1:
print(" A light red beam hits the alien in the chest and bounces off.")
print(" He opens his mouth...")
print(" ...you stand frozen to the spot while you are eaten alive")
elif gunsetting == 2:
print(" A red beam hits the alien and puts a hole through his middle.")
print(" He lunges for you and bites...you lose 1 life.")
# take one life away
lives = lives - 1
# test for 0 or fewer lives
if (lives <= 0):
print(" You've used up all your lives!")
gameoverearly()
elif gunsetting == 3:
print(" A bright red beam hits the alien and blows away half his head.")
print(" He lunges for you...you lose 2 lives.")
# take two lives away
lives = lives - 2
# test for 0 or fewer lives
if (lives <=0) :
print(" You've used up all your lives!")
gameoverearly()
elif gunsetting == 4:
print(" A powerful superZap hits the alien - he dissolves to dust.")
print(" But did you overdo the power setting...")
# get a random number between 1 and 2 (flip a coin)
random.seed()
randomno = random.randrange(1,2)
if randomno == 0:
print(" It's ok - the gun cools down...")
elif randomno == 1:
print(" The beam hits the walls which dissolve in a spray of white-hot metal.")
print(" You lost 1 life.")
lives = lives - 1
if lives <= 0:
print(" You've used up all your lives!")
gameoverearly()
# and if you get through all that...
print("""
You've survived!! You take both ships back to Earth and crown yourself Emperor.")
Game over!!""")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment