Created
December 29, 2013 16:31
-
-
Save imfromsweden/8172070 to your computer and use it in GitHub Desktop.
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
from tkinter import * | |
import tkinter | |
import time | |
import json | |
class Tamaguchi(object): | |
def __init__(self,size=1): | |
self.size=size | |
def increasesize(self): | |
self.size+=1 | |
GoodBadChange.config(text="Good work. You increased in size!") | |
def decreasesize(self): | |
self.size-=1 | |
GoodBadChange.config(text="Bad work. You decreased in size!") | |
def halfsize(self): | |
self.size=self.size/2 | |
GoodBadChange.config(text="Really bad work. Size halfed!") | |
def samesize(self): | |
GoodBadChange.config(text="Not very effective!") | |
tamaguchin=Tamaguchi() | |
def HämtaLista(sökväg): | |
namn=[] | |
tmp=[] | |
for i, line in enumerate(open(sökväg)): | |
if i % 4 == 0 and tmp !=[]: | |
namn.append(tmp) | |
tmp = [] | |
if len(line.strip())>3: | |
tmp.append(line.strip()) | |
if (len(namn))>1: | |
return namn | |
else: | |
namn=[] | |
lista=[] | |
global lista | |
a = open(sökväg,'r') | |
b = [line.strip() for line in a.readlines()] | |
namn = b | |
lista=namn | |
return lista | |
class Application(Frame): | |
def __init__(self,master): | |
super(Application,self).__init__(master) | |
self.grid() | |
if delta > 72: | |
self.label1 = tkinter.Label(self,text = "Unfortunally it's been 3 days since you played with me. I'm about to burst into tears from anger.\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 you played with me. I'm very pissed.\n",font=('Helvetica', 12)) | |
self.label2.grid() | |
tamaguchin.size-=10 | |
elif delta > 24: | |
self.label3 = tkinter.Label(self,text = "Unfortunally it's been one day since you played with me. I'm angry now.\n",font=('Helvetica', 12)) | |
self.label3.grid() | |
tamaguchin.size-=5 | |
self.btn0 = tkinter.Button(self, text = "Continue last round", command = lambda:self.continue1('ja')) | |
self.btn0.grid() | |
self.btn10 = tkinter.Button(self,text = "Start new round", command = lambda:self.continue1('nej')) | |
self.btn10.grid() | |
def continue1(self,value1): | |
if value1 == "ja": | |
self.btn0.destroy() | |
self.btn10.destroy() | |
if delta > 72: | |
self.label1.destroy() | |
elif delta > 48: | |
self.label2.destroy() | |
elif delta > 24: | |
self.label3.destroy() | |
self.create_widgets() | |
elif value1 == "nej": | |
lista=['sova','sova','sova'] | |
tamaguchin.size=1 | |
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.create_widgets() | |
global lista | |
updateDisplay() | |
def create_widgets(self): | |
self.btn1 = tkinter.Button(self, width=20, height=6, text = "plugga", command = lambda:self.update_text('plugga')) | |
self.btn1.grid(row=2,column=2) | |
self.btn2 = tkinter.Button(self, width=20, height=6, text = "festa", command = lambda:self.update_text('festa')) | |
self.btn2.grid(row=3,column=2) | |
self.btn3 = tkinter.Button(self, width=20, height=6, text = "tenta", command = lambda:self.update_text('tenta')) | |
self.btn3.grid(row=3,column=3) | |
self.btn4 = tkinter.Button(self, width=20, height=6, text = "sova", 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(): | |
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 therefore: '+lista[0]+"-"+lista[1]+"-"+lista[2]+"\n") | |
def getLists(): | |
PositivLista = HämtaLista('PositivLista.txt') | |
global PositivLista | |
NegativLista = HämtaLista('NegativLista.txt') | |
global NegativLista | |
HalveringsLista = HämtaLista('HalveringsLista.txt') | |
global HalveringsLista | |
lista = HämtaLista('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 lastGame(): | |
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 = then - now | |
delta = (delta.total_seconds()/3600) | |
global delta | |
file.close() | |
def main(): | |
root = tkinter.Tk() | |
global root | |
root.title("Tamaguchi-game") | |
root.geometry("900x900") | |
getLists() | |
getPhotos() | |
lastGame() | |
Picture = tkinter.Label(root, image=normal) | |
Picture.pack() | |
global Picture | |
ScoreBoard = tkinter.Label(root, text="Score " + 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() | |
updateDisplay() | |
LatestAction.config(text="Hello and welcome to the tamaguchi-game!\nThe tamaguchi starts the day with "+str(tamaguchin.size)+"!\nThe tamaguchi starts the day with "+lista[0]+"-"+lista[1]+"-"+lista[2]+"\n") | |
root.mainloop() | |
main() | |
print('Your tamaguchi reached a size of',tamaguchin.size) | |
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() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment