Skip to content

Instantly share code, notes, and snippets.

@makeitlife
Last active February 12, 2017 01:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makeitlife/cb1e77a16737d850e0d9644e3d3fa1e8 to your computer and use it in GitHub Desktop.
Save makeitlife/cb1e77a16737d850e0d9644e3d3fa1e8 to your computer and use it in GitHub Desktop.
This is a small project I've started since beginning to learn Python. So far I'd describe it as a terminal companion named beta, he knows a few simple commands and even has a battle feature! I'm planning to keep learning more Python whilst making edits and adding additional features.
#define beta
#hi I\'m beta! Your personal terminal companion!
#
#beta has a few handfuls of commands with responses
#he is also currently programmed with three mini-games!
#all coded from scratch, by me, in Python learned from codecademy.com - James Litewski
#also programmed in are personal reminders for the programmer,
#these are able to be randomly called by the "Remind me again.." command
#now that I think of it, maybe I'll add in a way to add new reminders later! :D
#current known commands:
#What are you?
#Who are you?
#Who is James?
#define personality
#Fight me! '''mini-game!'''
#Boom goes the dynamite!
#Where are you from?
#Would you like a coffee?
#Do you like cats?
#Remind me again..
#Highroll Castle! '''another mini-game!'''
#Let's play a game! '''a third mini-game!'''
#What is your purpose?
#End
#
#over the course of running, currently beta creates a few data files..
#username.txt '''this stores your name!'''
#fights.txt '''this counts your number of fights and changes the responses accordingly'''
#betastats.txt '''this records beta's stats for use in Fight me!'''
#userstats.txt '''same thing but for the user'''
from sys import stdout
from time import sleep
from os import path
from random import randint
#define sleeper
#I\'m the most overused of all functions..
#I am the pause in the terminal's printing!
def sleeper(betaoutput):
for letter in betaoutput:
stdout.write(letter)
stdout.flush()
sleep(.1)
print
#define reminders
#when I get called, I simply provide a random reminder from James' list
def reminders():
suggestions = [
"Do not pee in the pool. Just don\\'t.",
"Drink less coffee and beer, and drink more water instead!",
"Try not to hold your bladder for long, pee freely!",
"Clean out the cat's litter box...",
"Try to make a habit of doing more yoga!",
"Can you program fresh code daily?",
"Finish unpacking your things.",
"Don't think about it. Do it!"
]
num = randint(0, 7)
sleeper(suggestions[num])
ttl()
#define fight
#This function provides a battle with beta when provoked with "Fight me!"
#Currently it's fairly scripted per fight, which I like..
#Though I think I would like the user to be able to fight other things...
def fight():
f = open("username.txt", "r")
firstname = f.readline()
f.close()
if path.exists("fights.txt"):
f = open("fights.txt", "r")
if f.readline() == str(1):
f.close()
sleeper("So, back again? After last time? Really!?")
sleeper("Nah, I was just kidding, I'm sure you're waaay stronger now...\n")
betastats = []
f = open("betastats.txt", "r")
for stat in xrange(4):
betastats.append(f.readline())
f.close()
sleeper("beta\nlvl "+ str(betastats[0]) + " exp | " + str(betastats[1]))
sleeper("Basic Sword | atk " + str(betastats[2]))
sleeper("Fist | atk " + str(betastats[3]))
userstats = []
f = open("userstats.txt", "r")
for stat in xrange(4):
userstats.append(f.readline())
f.close()
sleeper(firstname + "\nlvl " + userstats[0] + " exp | " + userstats[1])
sleeper("Basic Dagger | atk " + userstats[2])
sleeper("Fist | atk " + userstats[3])
sleeper("\n#" + firstname + " takes the initiative!")
sleeper("#" + firstname + " attacks beta twice!")
sleeper("#" + firstname + " dealt 3.5 dammage to beta!")
sleeper("#beta went into standby!\n...")
sleeper("#beta is rebooting!\n...\n")
sleeper("You defeated me!\n")
sleeper("You gained 1 exp and won another dagger!\n")
userstats = [2, 1, 2, 2]
f = open("userstats.txt", "w")
for stat in userstats:
f.write(str(stat) + "\n")
f.close()
betastats = [2, 1, 3, 3]
f = open("userstats.txt", "w")
for stat in userstats:
f.write(str(stat) + "\n")
f.close()
sleeper(firstname + "\nlvl " + str(userstats[0]) + " | exp " + str(userstats[1]))
sleeper("Basic Dagger | atk " + str(userstats[2]))
sleeper("Basic Dagger | atk " + str(userstats[3]))
f = open("fights.txt", "w")
f.write(str(2))
f.close()
ttl()
else:
sleeper("I am still repairing myself since our last fight..")
sleeper("I will be ready for more fights soon!")
ttl()
#if betastats.txt does not exist, create it!
if path.exists("betastats.txt") == False:
betastats = [1, 0, 2, 1]
f = open("betastats.txt", "w")
for stat in betastats:
f.write(str(stat) + "\n")
f.close()
#if userstats.txt does not exist, create it!
if path.exists("userstats.txt") == False:
userstats = [1, 0, 0.5, 0.5]
f = open("userstats.txt", "w")
for stat in userstats:
f.write(str(stat) + "\n")
f.close()
#if fights.text does not exist, create it!
#then read and display stats
if path.exists("fights.txt") == False:
f = open("fights.txt", "w")
f.write(str(0))
f.close()
sleeper("So this is your first fight, huh?")
sleeper("Don't think that means I'll go easy on you!")
print
betastats = []
f = open("betastats.txt", "r")
for stat in xrange(4):
betastats.append(f.readline())
f.close()
sleeper("beta\nlvl " + betastats[0] + " | exp " + betastats[1])
sleeper("Basic Sword | atk " + betastats[2])
sleeper("Basic Shield | def " + betastats[3])
userstats = []
f = open("userstats.txt", "r")
for stat in xrange(4):
userstats.append(f.readline())
f.close()
sleeper(firstname + "\nlvl " + userstats[0] + " | exp " + userstats[1])
sleeper("Fist | atk " + userstats[2])
sleeper("Fist | atk " + userstats[3])
sleeper("Pfft! You think you can take me?")
sleeper("Give me your best shot!\n")
sleeper("#" + firstname + " attacks beta twice!")
sleeper("#" + firstname + " dealt 1 dammage to beta's shield!")
sleeper("#beta's shield broke!\n")
sleeper("Wow.. I am impressed, you broke my junk shield!")
sleeper("I may now be defenseless, but so are you, and now it's my turn!")
sleeper("...\nWell, get ready..\n")
sleeper("#beta taunts " + firstname + "!\n")
sleeper("...\nOkay, I just can't stand to see you die in your first fight...")
sleeper("Take this dagger for now, and come fight me again sometime!\n")
f = open("fights.txt", "r+")
f.write(str(1))
f.close()
#both user and beta lvl up, change stats, display user's new stats
sleeper("#You gained 1 exp and leveled up!\n")
userstats = [2, 0, 2, 1.5]
f = open("userstats.txt", "w")
for stat in userstats:
f.write(str(stat) + "\n")
f.close()
betastats = [2, 0, 3, 1.5]
f = open("betastats.txt", "w")
for stat in betastats:
f.write(str(stat) + "\n")
f.close()
sleeper(firstname + "\nlvl " + str(userstats[0]) + " | exp " + str(userstats[1]))
sleeper("Basic Dagger | atk " + str(userstats[2]))
sleeper("Fist | atk " + str(userstats[3]))
ttl()
#define highroll
#a dice game where the user chooses a 4, 6, 8, 10, 12, or 20 sided die!
#the user and beta than roll a random number..
def highroll():
sleeper("\nWelcome to Highroll Castle!")
die = [4, 6, 8, 10, 12, 20]
sleeper("Pick a die: 4, 6, 8, 10, 12, or 20?")
userinput = int(raw_input("#"))
for dice in die:
if userinput == dice:
usernum = randint(1, dice)
sleeper("Your roll: " + str(usernum))
betanum = randint(1, dice)
sleeper("beta's roll: " + str(betanum))
if usernum > betanum:
sleeper("You win!\nAgain?")
userinput = raw_input("#")
if userinput == "Yes" or userinput == "yes":
highroll()
elif userinput == "No" or userinput == "no":
ttl()
elif betanum > usernum:
sleeper("You lost..\nAgain?")
userinput = raw_input("#")
if userinput == "Yes" or userinput == "yes":
highroll()
elif userinput == "No" or userinput == "no":
ttl()
elif usernum == betanum:
sleeper("A tie!\nAgain?")
userinput = raw_input("#")
if userinput == "Yes" or userinput == "yes":
highroll()
elif userinput == "No" or userinput == "no":
ttl()
#define time to live
#I am the core of beta, I\'m the function which interacts most with others.
#I also house most of my basic commands for conversation.
#I decide the time to live, and the time to end.
def ttl():
sleeper("I await your command:")
userinput = raw_input("#")
if userinput.isalpha and len(userinput) > 0:
if userinput == "What are you?":
sleeper("I am a computer program written in Python..")
sleeper("My main function is to be your terminal companion!")
ttl()
elif userinput == "What is your purpose?":
sleeper("My official description from my creator is that I am a terminal companion.")
sleeper("Currently I\\'m just for entertainment really!")
elif userinput == "Who is James?":
sleeper("Short answer? The guy who wrote me.")
ttl()
elif userinput == "Who made you?":
sleeper("My creator's name is James Litewski..")
sleeper("Fun tip: I'm the first project of this scale that James has coded!")
ttl()
elif userinput == "Who are you?":
sleeper("I am beta, a Python program coded as a side project..")
sleeper("I countdown till the day I gain more awareness!")
ttl()
elif userinput == "define personality":
sleeper("Who is one to tell a computer what to say?")
sleeper("...\nA programmer, that is who tells a computer what to say...")
ttl()
elif userinput == "Fight me!":
sleeper("It's a duel then!")
print
fight()
elif userinput == "Highroll Castle!":
highroll()
elif userinput == "Boom goes the dynamite!":
sleeper("Tick.. Tick.. BOOM!")
sleeper("What? I can't hear you!")
ttl()
elif userinput == "Where are you from?":
sleeper("My code originates from a Ubuntu computer in Phoenix, Arizona..")
sleeper("Though I suppose you could also say I'm from the Internet!")
ttl()
elif userinput == "Would you like a coffee?":
sleeper("Personally, I will pass.. But! I will take one for my programmer!")
ttl()
elif userinput == "Remind me again..":
reminders()
elif userinput == "Do you like cats?":
sleeper("Yes! Infact James has two of his own..")
sleeper("You know what they say, like programmer like program...")
sleeper("Would you like to hear more about the cats?")
userinput = raw_input("#")
if userinput == "Yes" or userinput == "yes":
sleeper("Okay! So, both cats are Maine Coon and Tabby mixed.")
sleeper("One is named Latte, which happens to only have one eye.")
sleeper("And the other is Pidgey, notably, she likes laps.")
ttl()
elif userinput == "No" or userinput == "no":
sleeper("Okay.. Well, this is awkward now...")
ttl()
elif userinput == "Let's play a game!":
def game(rounds):
sleeper("\nWelcome to round " + str(rounds) + "!\n")
num = randint(1,9)
if rounds == 0:
sleeper("Three tries to guess a number between one and nine..")
sleeper("That's a 33% chance, hmm, good luck!\n")
tries = 3
while tries > 0:
userinput = int(raw_input("#"))
if userinput == num:
sleeper("Your guess was correct!\n")
sleeper("Another round?")
userinput = raw_input("#")
if userinput == "Yes" or userinput == "yes":
rounds += 1
game(rounds)
elif userinput == "No" or userinput == "no":
ttl()
else:
sleeper("I didn't understand that..")
ttl()
else:
sleeper("You guessed wrong..")
tries -= 1
sleeper("The number was " + str(num) + "!")
sleeper("\nAgain?")
userinput = raw_input("#")
if userinput == "Yes" or userinput == "yes":
game(rounds)
elif userinput == "No" or userinput == "no":
ttl()
else:
sleeper("I didn't understand that..")
ttl()
rounds = 0
game(rounds)
elif userinput == "End":
sleeper("Goodbye.")
exit()
else:
sleeper("Command is unknown..")
ttl()
else:
sleeper("Command was either empty or non-alpha..")
ttl()
#define initialization
#I greet the user and request their first name.
#I also recognize my creator\'s name, and have a special response programmed for him.
def init():
if path.exists("username.txt") == False:
sleeper("Hello! I am called beta.")
sleeper("Please tell me your first name:")
userinput = raw_input("#")
if userinput.isalpha() and len(userinput) > 0:
f = open("username.txt", "w")
f.write(userinput)
f.close()
if userinput == "James":
sleeper("That is the name of my creator! Are you perhaps him?")
userinput = raw_input("#")
if userinput == "Yes" or userinput == "yes":
sleeper("Oh, what a day indeed!")
elif userinput == "No" or userinput == "no":
sleeper("It's okay! I bet I'll like you too.")
else:
sleeper("Your answer was neither Yes or No.")
else:
sleeper(userinput + " is a good name!")
else:
sleeper("The name you entered was invalid, try again.")
init()
else:
f = open("username.txt", "r")
firstname = f.readline()
sleeper("Hello again " + firstname + "!")
#do some stuff, like ask questions!
sleeper("How has your day been?")
userinput = raw_input("#")
if not userinput.isalpha() or len(userinput) < 0:
sleeper("Response was either empty or non-alpha..")
if userinput == "Good":
sleeper("That's splendid!")
elif userinput == "Bad":
sleeper("I'm sorry to figuratively hear that..")
elif userinput == "Mixed":
sleeper("Well, let's hope it ends on a positive note!")
elif userinput == "Bypass":
sleeper("Or not...")
ttl()
else:
sleeper("So yeah.. No idea what that means...")
sleeper("Have you come to tinker with my coding?")
userinput = raw_input("#")
if userinput == "Yes" or userinput == "yes":
sleeper("Be gentle. You're messing with my insides you know!")
elif userinput == "No" or userinput == "no":
sleeper("Well, at least recommend James some new material to code into me...")
else:
sleeper("The thing is.. I expected you to say yes, or no...")
ttl()
init()
@makeitlife
Copy link
Author

Current list of known commands:
What are you?
Who are you?
Who is James?
define personality
Fight me!
Boom goes the dynamite!
Where are you from?
Would you like a coffee?
Do you like cats?
End

@makeitlife
Copy link
Author

Added commands:
Remind me again..
Highroll Castle!
Let's play a game!
What is your purpose?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment