-
-
Save anonymous/367b2b344221c9d08d5e to your computer and use it in GitHub Desktop.
Bryan H's "The Shooting Game" V2, for Treehouse Forum Contest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import getpass | |
import sys | |
# Player info | |
class Player: | |
def __init__(self): | |
self.hearts = 3 | |
self.bullets = 0 | |
self.score = 0 | |
def loadBullet(self): | |
self.bullets += 1 | |
def fireBullet(self): | |
self.bullets -= 1 | |
def loseHeart(self): | |
self.hearts -= 1 | |
def addScore(self): | |
self.score += 1 | |
player1 = Player() | |
player2 = Player() | |
def getPlayerNames(): | |
print("\nUsernames must be between 3 - 25 characters long. Type 'quit' to quit the game.\n") | |
player1.name = input("Player 1, please choose your username: ") | |
if (player1.name == 'quit' or player1.name == 'Quit'): | |
sys.exit(0) | |
while (len(player1.name) < 3 or len(player1.name) > 25): | |
player1.name = input("Player 1, usernames must be 3-25 characters long. Try again: ") | |
if (player1.name == 'quit' or player1.name == 'Quit'): | |
sys.exit(0) | |
player1.name = "[" + player1.name + "]" | |
player2.name = input("\nPlayer 2, please choose your username: ") | |
if (player2.name == 'quit' or player2.name == 'Quit'): | |
sys.exit(0) | |
while (len(player2.name) < 3 or len(player2.name) > 25): | |
player2.name = input("Player 2, usernames must be 3-25 characters long. Try again: ") | |
if (player2.name == 'quit' or player2.name == 'Quit'): | |
sys.exit(0) | |
player2.name = "[" + player2.name + "]" | |
def actionSorter(option1, option2): | |
# If player1 fires a bullet | |
if (option1 == '1' and player1.bullets > 0): | |
# If player2 also fires a bullet | |
if (option2 == '1' and player2.bullets > 0): | |
player1.fireBullet() | |
player2.loseHeart() | |
player2.fireBullet() | |
player1.loseHeart() | |
print("\n\t{0} and {1} shot each other!".format(player1.name, player2.name)) | |
# If player2 tries to shoot with no bullets | |
elif (option2 == '1' and player2.bullets == 0): | |
player1.fireBullet() | |
player2.loseHeart() | |
print("\n\t{0} shot {1} as he was attempting to shoot. Sadly, [{1}] had no bullets.".format(player1.name, player2.name)) | |
# If player2 loads a bullet | |
elif (option2 == '2'): | |
player1.fireBullet() | |
player2.loseHeart() | |
player2.loadBullet() | |
print("\n\t{0} shot {1} and he was loading his gun!".format(player1.name, player2.name)) | |
# If player2 blocks | |
elif (option2 == '3'): | |
player1.fireBullet() | |
print("\n\t{0} shot at {1}, but it was blocked!".format(player1.name, player2.name)) | |
# If player1 tries to shoot with no bullets | |
elif (option1 == '1' and player1.bullets == 0): | |
#If player2 fires a bullet | |
if (option2 == '1' and player2.bullets > 0): | |
player2.fireBullet() | |
player1.loseHeart() | |
print("\n\t{0} tried to shoot with no bullets, but {1} landed a shot!".format(player1.name, player2.name)) | |
# If player2 tries to shoot with no bullets | |
elif (option2 == '1' and player2.bullets == 0): | |
print("\n\tBoth {0} and {1} tried to shoot with no bullets... awkward.".format(player1.name, player2.name)) | |
# If player2 loads a bullet | |
elif (option2 == '2'): | |
player2.loadBullet() | |
print("\n\t{0} tried to shoot with no bullets, and {1} loads a bullet.".format(player1.name, player2.name)) | |
# If player2 blocks | |
elif (option2 == '3'): | |
print("\n\t{0} tried to shoot with no bullets. Too bad {1} blocked!".format(player1.name, player2.name)) | |
# If player1 loads a bullet | |
elif (option1 == '2'): | |
#If player2 fires a bullet | |
if (option2 == '1' and player2.bullets > 0): | |
player1.loadBullet() | |
player2.fireBullet() | |
player1.loseHeart() | |
print("\n\t{0} loaded a bullet as {1} landed a shot!".format(player1.name, player2.name)) | |
# If player2 tries to shoot with no bullets | |
elif (option2 == '1' and player2.bullets == 0): | |
player1.loadBullet() | |
print("\n\t{0} loaded a bullet as {1} tried to shoot. Good thing {1} had no bullets".format(player1.name, player2.name)) | |
# If player2 loads a bullet | |
elif (option2 == '2'): | |
player1.loadBullet() | |
player2.loadBullet() | |
print("\n\t{0} and {1} both loaded bullets.".format(player1.name, player2.name)) | |
# If player2 blocks | |
elif (option2 == '3'): | |
player1.loadBullet() | |
print("\n\t{0} loaded a bullet as {1} blocked.".format(player1.name, player2.name)) | |
# If player1 blocks | |
elif (option1 == '3'): | |
#If player2 fires a bullet | |
if (option2 == '1' and player2.bullets > 0): | |
player2.fireBullet() | |
print("\n\t{0} blocked a shot from {1}.".format(player1.name, player2.name)) | |
# If player2 tries to shoot with no bullets | |
elif (option2 == '1' and player2.bullets == 0): | |
print("\n\t{0} blocked as {1} tried to shoot with no bullets.".format(player1.name, player2.name)) | |
# If player2 loads a bullet | |
elif (option2 == '2'): | |
player2.loadBullet() | |
print("\n\t{0} blocked as {1} loaded a bullet".format(player1.name, player2.name)) | |
# If player2 blocks | |
elif (option2 == '3'): | |
print("\n\tBoth {0} and {1} blocked. How boring.".format(player1.name, player2.name)) | |
def theGame(): | |
print("\n\nTo avoid detection, the keys '1', '2', and '3' are used. At any point during the game, you can type 'quit' to leave. The options won't show up as you type, but they are there. Have fun!") | |
while (player1.hearts != 0 and player2.hearts != 0): | |
print("\n\t{0} has: {1} hearts, and {2} bullets".format(player1.name, player1.hearts, player1.bullets)) | |
print("\n\t{0} has: {1} hearts, and {2} bullets".format(player2.name, player2.hearts, player2.bullets)) | |
# Prompt player1 for their action. 'getpass' hides choices from opponent | |
option1 = getpass.getpass("\n{0}, Shoot(1), Reload(2), Block(3), or 'quit': ".format(player1.name)) | |
while (option1 != '1' and option1 != '2' and option1 != '3' and option1 != 'quit' and option1 != 'Quit'): | |
option1 = getpass.getpass("\n{0}, That's not an option! Shoot(1), Reload(2), Block(3), or 'quit': ".format(player1.name)) | |
if (option1 == 'quit' or option1 == 'Quit'): | |
sys.exit(0) | |
# Prompt player2 for their action. 'getpass' hides choices from opponent | |
option2 = getpass.getpass("\n{0}, Shoot(1), Reload(2), Block(3), or 'quit': ".format(player2.name)) | |
while (option2 != '1' and option2 != '2' and option2 != '3' and option2 != 'quit' and option2 != 'Quit'): | |
option2 = getpass.getpass("\n{0}, That's not an option! Shoot(1), Reload(2), Block(3), or 'quit': ".format(player2.name)) | |
if (option2 == 'quit' or option2 == 'Quit'): | |
sys.exit(0) | |
print("\n\t##############################################################") | |
actionSorter(option1, option2) | |
print("\n\t##############################################################\n") | |
# Let players know who won, and add scores in case they play again | |
if (player1.hearts == 0 and player2.hearts > 0): | |
print("\n{0} Won!\n".format(player2.name)) | |
player2.addScore() | |
elif (player2.hearts == 0 and player1.hearts > 0): | |
print("\n{0} Won!\n".format(player1.name)) | |
player1.addScore() | |
elif (player1.hearts == 0 and player2.hearts == 0): | |
print("\nBoth {0} and {1} have died. How exciting.\n".format(player1.name, player2.name)) | |
print("{0} Wins: {1} \t\t {2} Wins: {3}".format(player1.name, player1.score, player2.name, player2.score)) | |
# Check if player wants to play again | |
restart = input("Would you like to play again? (Y/N): ") | |
while (restart != 'Y' and restart != 'y' and restart != 'N' and restart != 'n'): | |
restart = input("Please only type 'Y' for yes, or 'N' for no. Caps don't matter: ") | |
if (restart == 'Y' or restart == 'y'): | |
# Check to see if new people will be playing | |
restartNames = input("Would you also like to start with new players? This will reset names and scores. (Y/N): ") | |
while (restartNames != 'Y' and restartNames != 'y' and restartNames != 'N' and restartNames != 'n'): | |
restartNames = input("Please only type 'Y' for yes, or 'N' for no. Caps don't matter: ") | |
if (restartNames == 'Y' or restartNames == 'y'): | |
player1.hearts = 3 | |
player1.bullets = 0 | |
player1.score = 0 | |
player2.hearts = 3 | |
player2.bullets = 0 | |
player2.score = 0 | |
getPlayerNames() | |
theGame() | |
elif (restartNames == 'N' or restartNames == 'n'): | |
player1.hearts = 3 | |
player1.bullets = 0 | |
player2.hearts = 3 | |
player2.bullets = 0 | |
theGame() | |
elif (restart == 'N' or restart == 'n'): | |
print("\nThanks for playing! Goodbye.") | |
print("""\n\nWelcome to The Shooting Game! You're about to have a shootout with a friend.\nYou both start with 3 hearts, and have 3 options each turn: Shoot, Reload, or Block.\nYou start with 0 bullets, and must Reload to collect more. If you someone Shoots, and their opponent is doing anything but Blocking, they'll lose a heart! Be careful not to run out of bullets! Have fun!\n\n""") | |
getPlayerNames() | |
theGame() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment