Skip to content

Instantly share code, notes, and snippets.

@imfromsweden
Created January 28, 2014 16:08
Show Gist options
  • Save imfromsweden/8670608 to your computer and use it in GitHub Desktop.
Save imfromsweden/8670608 to your computer and use it in GitHub Desktop.
from tkinter import *
import tkinter
import time
import json
#The Tamaguchi game is a game designed to let the user choose what he/she wants the tamaguchi to do, and then get a score depending on what choices the person does. The actions are saved in a list, and are
#matched against three diferent lists, a good list, a bad list, and a list that makes the tamaguchi half in size (only if you do the same action thrice). Depending on which list the three last actions are matched
#against, the Tamaguchi either increases or decreases in size. It stays the same size if the last three actions can't be matched in any list. The lists are predetermined by me.
class Tamaguchi(object):
def __init__(self,size=1):
self.size=size
def increasesize(self):
self.size+=1
GoodBadChange.config(text="Good job! You increased in size!")
def decreasesize(self):
self.size-=1
GoodBadChange.config(text="Bad job! You decreased in size!")
def halfsize(self):
self.size=self.size/2
GoodBadChange.config(text="Terrible job! You halfed in size!")
def samesize(self):
GoodBadChange.config(text="Not very effective. Nothing happened.")
tamaguchin=Tamaguchi()
def getList(sökväg):
#This function creates the lists from the file.
name=[]
tmp=[]
for i, line in enumerate(open(sökväg)):
if i % 4 == 0 and tmp !=[]:
name.append(tmp)
tmp = []
if len(line.strip())>3:
tmp.append(line.strip())
if (len(name))>1:
#som lists are different than others, to separate them I decided to differ them judging by length.
return name
else:
name=[]
lista=[]
global lista
a = open(sökväg,'r')
b = [line.strip() for line in a.readlines()]
name = b
lista=name
return lista
class Application(Frame):
def __init__(self,master):
super(Application,self).__init__(master)
self.grid()
#delta is how long time it was since I played with the tamaguchi (in hours)
if delta > 72:
self.label1 = tkinter.Label(self,text = "Unfortunally it's been more than 3 days since you played with me. I'm pissed.\n",font=('Helvetica', 12))
self.label1.grid()
tamaguchin.size-=20
elif delta > 48:
self.label2 = tkinter.Label(self,text = "Unfortunally it's been 2 days since u played with me. I'm kinda mad.\n",font=('Helvetica', 12))
self.label2.grid()
tamaguchin.size-=10
elif delta > 24:
self.label3 = tkinter.Label(self,text = "Unfortunally it's been 1 day since you played with me. I'm irritated.\n",font=('Helvetica', 12))
self.label3.grid()
tamaguchin.size-=5
#the User gets to choose if he or she wants to continue with the last round or start a new one.
self.btn0 = tkinter.Button(self, text = "Continue with last round", command = lambda:self.continue1('continue'))
self.btn0.grid()
self.btn10 = tkinter.Button(self,text = "Start New round", command = lambda:self.continue1('newRound'))
self.btn10.grid()
def continue1(self,value1):
if value1 == "continue":
self.create_widgets()
elif value1 == "newRound":
#If user wants to create a new round the score is set to 0.
lista=['sova','sova','sova']
tamaguchin.size=1
self.create_widgets()
updateDisplay()
def create_widgets(self):
if delta > 72:
self.label1.destroy()
elif delta > 48:
self.label2.destroy()
elif delta > 24:
self.label3.destroy()
self.btn0.destroy()
self.btn10.destroy()
self.btn1 = tkinter.Button(self, width=20, height=6, text = "study", command = lambda:self.update_text('plugga'))
self.btn1.grid(row=2,column=2)
self.btn2 = tkinter.Button(self, width=20, height=6, text = "party", command = lambda:self.update_text('festa'))
self.btn2.grid(row=3,column=2)
self.btn3 = tkinter.Button(self, width=20, height=6, text = "finals", command = lambda:self.update_text('tenta'))
self.btn3.grid(row=3,column=3)
self.btn4 = tkinter.Button(self, width=20, height=6, text = "sleep", command = lambda:self.update_text('sova'))
self.btn4.grid(row=2,column=3)
self.btn5 = tkinter.Button(self, width=20, height=6, text = "exit", command = self.exit)
self.btn5.grid(row=5,column=4)
def update_text(self,value):
lista.append(value)
lista.remove(lista[0])
if lista in PositivLista:
tamaguchin.increasesize()
elif lista in NegativLista:
tamaguchin.decreasesize()
elif lista in HalveringsLista:
tamaguchin.halfsize()
else:
tamaguchin.samesize()
updateDisplay()
return lista
def exit(self):
print('You have chosen to exit the program')
root.destroy()
def updateDisplay():
#Denna funktion updaterar bilden så att den passar med poängen, updaterar score-boarden och tamaguchins tre senaste val.
if tamaguchin.size >= 8:
Picture.config(image=mostHappy)
elif tamaguchin.size >= 5:
Picture.config(image=MOREHAPPY)
elif tamaguchin.size >= 2:
Picture.config(image=happy)
elif tamaguchin.size <= -8:
Picture.config(image=mostUnhappy)
elif tamaguchin.size <= -6:
Picture.config(image=evenmoreunhappy)
elif tamaguchin.size <= -4:
Picture.config(image=moreUnhappy)
elif tamaguchin.size <= -2:
Picture.config(image=unhappy)
else:
Picture.config(image=normal)
ScoreBoard.config(text="Poäng: " + str(tamaguchin.size))
LatestAction.config(text="Your tamaguchi chose to: " + lista[2]+'\nIts last 3 actions is thereby: '+lista[0]+"-"+lista[1]+"-"+lista[2]+"\n")
def getAllLists():
PositivLista = getList('PositivLista.txt')
global PositivLista
NegativLista = getList('NegativLista.txt')
global NegativLista
HalveringsLista = getList('HalveringsLista.txt')
global HalveringsLista
lista = getList('lista.txt')
global lista
def getPhotos():
normal = tkinter.PhotoImage(file="normal.gif")
global normal
happy = tkinter.PhotoImage(file="happy.gif")
global happy
MOREHAPPY= tkinter.PhotoImage(file="MOREHAPPY.gif")
global MOREHAPPY
mostHappy = tkinter.PhotoImage(file="mostHappy.gif")
global mostHappy
unhappy = tkinter.PhotoImage(file="unhappy.gif")
global unhappy
moreUnhappy = tkinter.PhotoImage(file="moreUnhappy.gif")
global moreUnhappy
evenmoreunhappy = tkinter.PhotoImage(file="evenmoreunhappy.gif")
global evenmoreunhappy
mostUnhappy = tkinter.PhotoImage(file="mostUnhappy.gif")
global mostUnhappy
def openLastGame():
#The function openLastGame reads in the data from the last time the game was played.
#We also introduce date time, to figure out how long ago it was since we last played with the tamaguchi.
with open("Tamaguchis.txt","r") as file:
dictionary = json.loads(file.read())
lista = dictionary["order"]
global lista
tamaguchin.size=dictionary["size"]
from datetime import datetime
date_format = "%m/%d/%y %H:%M:%S"
då=(dictionary["date"])
then = datetime.strptime(str(då), date_format)
now = datetime.now()
delta = now - then
delta = (delta.total_seconds()/3600)
global delta
file.close()
def saveToFile():
#Saves the date/condition/last actions to a file.
with open("Tamaguchis.txt","w") as file:
date = time.strftime("%c")
dictionary = {"size":tamaguchin.size,"date":date,"order":lista}
file.write(json.dumps(dictionary))
file.close()
def background():
Picture = tkinter.Label(root, image=normal)
Picture.pack()
global Picture
ScoreBoard = tkinter.Label(root, text="Poäng " + str(tamaguchin.size), font=('Helvetica', 15))
ScoreBoard.pack()
global ScoreBoard
GoodBadChange = tkinter.Label(root, text="", font=('Helvetica', 12))
GoodBadChange.pack()
global GoodBadChange
LatestAction = tkinter.Label(root, text="", font=('Helvetica', 12))
LatestAction.pack()
global LatestAction
app = Application(root)
app.pack()
def main():
root = tkinter.Tk()
global root
root.title("Tamaguchi-spelet")
root.geometry("900x900")
getAllLists()
getPhotos()
openLastGame()
background()
updateDisplay()
LatestAction.config(text="Hello and welcome!\nThe Tamaguchi starts the day with the size "+str(tamaguchin.size)+"!\nThe Tamaguchi starts the day with the last 3 actions of "+lista[0]+"-"+lista[1]+"-"+lista[2]+"\n")
root.mainloop()
saveToFile()
print('Your tamaguchi reached a size of ',tamaguchin.size)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment