I decided I would attempt my very first TKinter in py 3.4.3 and i think it turned out pretty well! As before. link is below, as is the plain text code.
Download for python 3.4.3 (needs extracting):
For the plain text version, see below:
import tkinter as tk
import datetime
import math
import random
import time
import os
toon_list = [] #Python Lang
player_deck = []
cpu_deck = []
class character:
def __init__(self, name, health, stamina, morality, strength, height, will, image):
self.name = name
self.health = health
self.stamina = stamina
self.morality = morality
self.strength = strength
self.height = height
self.will = will
self.image = image
toon_list.append(self)
########################################################################Character definitions
vader = character('Darth Vader', 85, 30, 40, 75, 80, 40, "Top_Trumps_Cards\Darth_Vader.gif") #1
finn = character('Finn', 80, 80, 95, 90, 60, 80, "Top_Trumps_Cards\Finn.gif") #2
gandalf = character('Gandalf', 75, 50, 100, 65, 90, 70, "Top_Trumps_Cards\Gandalf.gif") #3
tony_stark = character('Iron Man', 85, 90, 90, 85, 60, 10, "Top_Trumps_Cards\Iron_Man.gif") #4
batman = character('Batman', 90, 90, 90, 80, 80, 90, "Top_Trumps_Cards\Batman.gif") #5
joker = character('The Joker', 70, 80, 30, 40, 80, 50, "Top_Trumps_Cards\The_Joker.gif") #6
two_face = character('Two Face', 70, 70, 50, 70, 80, 60, "Top_Trumps_Cards\Two-Face.gif") #7
superman = character('Superman', 95, 90, 90, 90, 90, 90, "Top_Trumps_Cards\Superman.gif") #8
wonder_woman = character('Wonder Woman', 80, 75, 90, 80, 70, 95, "Top_Trumps_Cards\Wonder_Woman.gif") #9
spider_man = character('Spider Man', 70, 80, 70, 70, 80, 90, "Top_Trumps_Cards\Spiderman.gif") #10
deadpool = character('Deadpool', 85, 90, 50, 83, 90, 85, "Top_Trumps_Cards\Deadpool.gif") #11
harley_quinn = character('Harley Quinn', 70, 90, 40, 60, 70, 60, "Top_Trumps_Cards\Harley_Quinn.gif") #12
loki = character('Loki', 100, 90, 10, 90, 85, 70, "Top_Trumps_Cards\Loki.gif") #13
thor = character('Thor', 100, 90, 85, 90, 90, 80, "Top_Trumps_Cards\Thor.gif") #14
penguin = character('Penguin', 60, 40, 40, 50, 50, 30, "Top_Trumps_Cards\Penguin.gif") #15
picard = character('Cpt. Picard', 60, 60, 90, 70, 80, 100, "Top_Trumps_Cards\Picard.gif") #16
groot = character('Groot', 80, 60, 90, 90, 100, 40, "Top_Trumps_Cards\Groot.gif") #17
green_arrow = character('Green Arrow', 80, 85, 85, 85, 70, 85, "Top_Trumps_Cards\Green_Arrow.gif") #18
flash = character('The Flash', 70, 100, 85, 80, 80, 90, "Top_Trumps_Cards\The_Flash.gif") #19
aqua_man = character('Aqua Man', 85, 90, 85, 90, 90, 85, "Top_Trumps_Cards\Aquaman.gif") #20
########################################################################Print character stats
cpu_index = ['health', 'stamina', 'morality', 'strength', 'height', 'will']
def print_all_toon_stats(deck):
print ("*****************************************************************************************************************************************")
print ("* Name ********* Health ********* Stamina ********* Morality ********* Strength ********* Height ********* Will**")
print ("-----------------------------------------------------------------------------------------------------------------------------------------")
for toon in deck:
col_1 = "*************"
col_1 = col_1.replace("*", toon.name + " ",1)
col_1 = col_1[:14]
col_2 = "**************"
col_2 = col_2.replace("*", str(toon.health) + " ",1)
col_2 = col_2[:16]
col_3 = "*******************************************"
col_3 = col_3.replace("*", str(toon.stamina) + " ",1)
col_3 = col_3[:17]
col_4 = "**************************"
col_4 = col_4.replace("*", str(toon.morality) + " ",1)
col_4 = col_4[:18]
col_5 = "****************"
col_5 = col_5.replace("*", str(toon.strength) + " ",1)
col_5 = col_5[:18]
col_6 = "****************************"
col_6 = col_6.replace("*", str(toon.height) + " ",1)
col_6 = col_6[:16]
col_7 = "**************"
col_7 = col_7.replace("*", str(toon.will) + " ",1)
col_7 = col_7[:10]
data_length = (len(toon.name) + len(str(toon.health)) + len(str(toon.stamina)) + len(str(toon.morality)) + len(str(toon.strength)) + len(str(toon.height)) + len(str(toon.will)))
print ("*", col_1 + "|" + col_2, col_3, col_4, col_5, col_6, col_7)
print (" ")
#######################################################################Randomly assign cards to deck
def random_asign(deck):
if len(toon_list) == 0:
print ("Pack out of cards")
else:
while len(deck) < 10:
temp = random.choice(toon_list)
if temp in deck:
pass
else:
deck.append(temp)
toon_list.remove(temp)
def cards_back(deck):
while len(deck) > 0:
for x in deck:
toon_list.append(x)
deck.remove(x)
#######################################################################Card manipulation
def draw_card(deck):
try:
return deck[0]
except:
pass
def card_back(deck):
deck.insert(-1, deck.pop(0))
#######################################################################Button Presses
player_win = 0
times_played = 0
player_won_last = 0
def reset_card():
winning_text = (str(current_card.name)+ " WINS")
if str(current_card.name) == "Wonder Woman" or str(current_card.name) == "Harley Quinn":
label4 = tk.Label(root, text=(winning_text), font=("Helvetica", (win_varw * win_varh * 50)))
label4.grid(row=0, column=0, columnspan=6)
else:
label4 = tk.Label(root, text=(winning_text), font=("Helvetica", (win_varw * win_varh * 60)))
label4.grid(row=0, column=0, columnspan=6, padx=(win_varw * win_varh * (win_width-700)/2))
label4.after(2000, label4.grid_forget)
def reset_cpu_card():
winning_text = (str(cpu_current_card.name)+ " WINS")
if str(cpu_current_card.name) == "Wonder Woman" or str(cpu_current_card.name) == "Harley Quinn":
label4 = tk.Label(root, text=(winning_text), font=("Helvetica", (win_varw * win_varh * 50)))
label4.grid(row=0, column=0, columnspan=6)
else:
label4 = tk.Label(root, text=(winning_text), font=("Helvetica", (win_varw * win_varh * 60)))
label4.grid(row=0, column=0, columnspan=6, padx=(win_varw * win_varh * (win_width-700)/2))
label4.after(2000, label4.grid_forget)
def it_a_draw():
winning_text = "It's a draw!"
label4 = tk.Label(root, text=(winning_text), font=("Helvetica", (win_varw * win_varh * 60)))
label4.grid(row=0, column=0, columnspan=6, padx=(win_varw * win_varh * (win_width-700)/2))
label4.after(2000, label4.grid_forget)
def cpu_choose(cpu_current_card):
cpu_current_battle_stat = 0
cpu_current_class = 0
print("TestIsBest")
for x in cpu_index:
if x == 'health':
if cpu_current_card.health > cpu_current_battle_stat:
cpu_current_battle_stat = cpu_current_card.health
current_battle_stat = current_card.health
current_class = x
if x == 'stamina':
if cpu_current_card.stamina > cpu_current_battle_stat:
cpu_current_battle_stat = cpu_current_card.stamina
current_battle_stat = current_card.stamina
current_class = x
if x == 'morality':
if cpu_current_card.morality > cpu_current_battle_stat:
cpu_current_battle_stat = cpu_current_card.morality
current_battle_stat = current_card.morality
current_class = x
if x == 'strength':
if cpu_current_card.strength > cpu_current_battle_stat:
cpu_current_battle_stat = cpu_current_card.strength
current_battle_stat = current_card.strength
current_class = x
if x == 'height':
if cpu_current_card.height > cpu_current_battle_stat:
cpu_current_battle_stat = cpu_current_card.height
current_battle_stat = current_card.height
current_class = x
if x == 'will':
if cpu_current_card.will > cpu_current_battle_stat:
cpu_current_battle_stat = cpu_current_card.will
current_battle_stat = current_card.will
current_class = x
print ("cpu current " + str(cpu_current_battle_stat))
game_calc(current_battle_stat, cpu_current_battle_stat)
def game_calc(current_battle_stat, cpu_current_battle_stat):
print("E")
global player_won_last
global current_card
global cpu_current_card
global label1
global label2
global player_win
if current_battle_stat > cpu_current_battle_stat:
reset_card()
player_turn = True
player_deck.append(cpu_current_card)
try:
cpu_deck.remove(cpu_current_card)
card_back(player_deck)
player_won_last = 0
except:
player_win = 1
if len(player_deck) == 20:
player_win = 1
label5.config(text="20")
label6.config(text="0")
print("F")
print(player_win)
print(len(cpu_deck))
elif current_battle_stat < cpu_current_battle_stat:
reset_cpu_card()
player_turn = False
cpu_deck.append(current_card)
try:
player_deck.remove(current_card)
card_back(cpu_deck)
player_won_last = 1
except:
player_win = -1
if len(cpu_deck) == 20:
player_win = -1
label5.config(text="0")
label6.config(text="20")
print("G")
else:
it_a_draw()
card_back(player_deck)
card_back(cpu_deck)
print("H")
print(player_win)
print(player_won_last)
if player_win == 0:
current_card = draw_card(player_deck)
print("len player " + str(len(player_deck)))
print("len comp " + str(len(cpu_deck)))
label5.config(text=str(len(player_deck)))
label6.config(text=str(len(cpu_deck)))
photo = tk.PhotoImage(file=current_card.image)
print("YESH")
print(current_card.image)
label1.config(image = "")
label1.grid_forget
label1.config(image = photo)
label1.image = photo
print (label1.image)
label1.grid(row=0, column=0, columnspan=2)
cpu_current_card = draw_card(cpu_deck)
photo1 = tk.PhotoImage(file=((str(cpu_current_card.image)[:-4]) + "1" + ".gif"))
label2.config(image = "")
label2.grid_forget
label2.config(image = photo1)
label2.image = photo1
label2.grid(row=0, column=4, columnspan=2)
if player_won_last == 1:
healthb.config(state=tk.DISABLED, relief=tk.SUNKEN)
staminab.config(state=tk.DISABLED, relief=tk.SUNKEN)
moralityb.config(state=tk.DISABLED, relief=tk.SUNKEN)
strengthb.config(state=tk.DISABLED, relief=tk.SUNKEN)
staminab.config(state=tk.DISABLED, relief=tk.SUNKEN)
heightb.config(state=tk.DISABLED, relief=tk.SUNKEN)
willb.config(state=tk.DISABLED, relief=tk.SUNKEN)
cpu_choose(cpu_current_card)
if player_won_last != 1:
healthb.config(state='normal', relief=tk.RAISED)
staminab.config(state='normal', relief=tk.RAISED)
moralityb.config(state='normal', relief=tk.RAISED)
strengthb.config(state='normal', relief=tk.RAISED)
staminab.config(state='normal', relief=tk.RAISED)
heightb.config(state='normal', relief=tk.RAISED)
willb.config(state='normal', relief=tk.RAISED)
print("I")
print("J")
if player_win != 0:
healthb.config(state=tk.DISABLED, relief=tk.SUNKEN)
staminab.config(state=tk.DISABLED, relief=tk.SUNKEN)
moralityb.config(state=tk.DISABLED, relief=tk.SUNKEN)
strengthb.config(state=tk.DISABLED, relief=tk.SUNKEN)
staminab.config(state=tk.DISABLED, relief=tk.SUNKEN)
heightb.config(state=tk.DISABLED, relief=tk.SUNKEN)
willb.config(state=tk.DISABLED, relief=tk.SUNKEN)
if player_win == 1:
label4 = tk.Label(root, text="You WON!!!", font=("Helvetica", (win_varw * win_varh * 60)))
label4.grid(row=0, column=0, columnspan=6, padx=(win_varw * win_varh * (win_width-700)/2))
else:
label4 = tk.Label(root, text="You Lost", font=("Helvetica", (win_varw * win_varh * 60)))
label4.grid(row=0, column=0, columnspan=6, padx=(win_varw * win_varh * (win_width-700)/2))
label4.after(7000, label4.grid_forget)
play_again = tk.Button(root, text="Play again?", font=("Helvetica"), command= play).grid(row=6, column=2)
exit = tk.Button(root, text="Exit?", font=("Helvetica"), command= root.destroy).grid(row=6, column=3)
def cpu_press(current_class, current_battle_stat):
print("C")
cpu_class_selected = False
while cpu_class_selected == False:
cpu_current_class = current_class
if cpu_current_class == 'health' or cpu_current_class == 'Health':
cpu_current_battle_stat = cpu_current_card.health
cpu_class_selected = True
elif cpu_current_class == 'stamina' or cpu_current_class == 'Stamina':
cpu_current_battle_stat = cpu_current_card.stamina
cpu_class_selected = True
elif cpu_current_class == 'morality' or cpu_current_class == 'Morality':
cpu_current_battle_stat = cpu_current_card.morality
cpu_class_selected = True
elif cpu_current_class == 'strength' or cpu_current_class == 'Strength':
cpu_current_battle_stat = cpu_current_card.strength
cpu_class_selected = True
elif cpu_current_class == 'stamina' or cpu_current_class == 'Stamina':
cpu_current_battle_stat = cpu_current_card.stamina
cpu_class_selected = True
elif cpu_current_class == 'height' or cpu_current_class == 'Height':
cpu_current_battle_stat = cpu_current_card.height
cpu_class_selected = True
elif cpu_current_class == 'will' or cpu_current_class == 'Will':
cpu_current_battle_stat = cpu_current_card.will
cpu_class_selected = True
else:
pass
print (cpu_current_battle_stat)
print("D")
game_calc(current_battle_stat, cpu_current_battle_stat)
def current_press(button_pressed):
print("a")
current_battle_stat = getattr(current_card,button_pressed)
class_selected = True
print (current_battle_stat)
print("b")
cpu_press(button_pressed, current_battle_stat)
#######################################################################Game initialisation
print("J")
game_started = False
player_win = 0
turn_count = 0
times_played = 0
cards_back(player_deck)
cards_back(cpu_deck)
random_asign(cpu_deck)
random_asign(player_deck)
current_card = draw_card(player_deck)
cpu_current_card = draw_card(cpu_deck)
root = tk.Tk()
root.columnconfigure('all', minsize = 200)
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.overrideredirect(1)
root.geometry("%dx%d+0+0" % (w, h))
print("L")
win_width = root.winfo_screenwidth()
win_height = root.winfo_screenheight()
win_varw = int(win_width/1600)
win_varh = int(win_height/900)
root.columnconfigure('all', minsize = (win_width/6))
'''space = tk.PhotoImage(file="crab_nebula.gif")
Background = tk.Label(root, image=space)
Background.image = space
Background.grid(row=0, column=0, columnspan=6,rowspan=7,pady=(0), padx=(0))'''
def play():
global player_won_last
global player_win
global turn_count
global current_card
global cpu_current_card
global times_played
healthb = tk.Button(root, text="Health", font=("Helvetica"), command= lambda: current_press('health'), state=tk.DISABLED)
staminab = tk.Button(root, text="Stamina", font=("Helvetica"), command= lambda: current_press('stamina'), state=tk.DISABLED)
moralityb = tk.Button(root, text="Morality", font=("Helvetica"), command= lambda: current_press('morality'), state=tk.DISABLED)
strengthb = tk.Button(root, text="Strength", font=("Helvetica"), command= lambda: current_press('strength'), state=tk.DISABLED)
heightb = tk.Button(root, text="Height", font=("Helvetica"), command= lambda: current_press('height'), state=tk.DISABLED)
willb = tk.Button(root, text="Will", font=("Helvetica"), command= lambda: current_press('will'), state=tk.DISABLED)
times_played += 1
player_won_last = 0
player_win = 0
turn_count = 0
print("Cards being put into pack")
cards_back(player_deck)
cards_back(cpu_deck)
print(len(toon_list))
print(len(player_deck))
print(len(cpu_deck))
print("assigning to cpu")
random_asign(cpu_deck)
print(len(toon_list))
print(len(player_deck))
print(len(cpu_deck))
print("assigning to player")
random_asign(player_deck)
print(len(toon_list))
print(len(player_deck))
print(len(cpu_deck))
print("123")
current_card = draw_card(player_deck)
print("cur card = " + current_card.name)
print("432")
cpu_current_card = draw_card(cpu_deck)
print("543")
if times_played != 0:
healthb.config(state='normal', relief=tk.RAISED)
staminab.config(state='normal', relief=tk.RAISED)
moralityb.config(state='normal', relief=tk.RAISED)
strengthb.config(state='normal', relief=tk.RAISED)
staminab.config(state='normal', relief=tk.RAISED)
heightb.config(state='normal', relief=tk.RAISED)
willb.config(state='normal', relief=tk.RAISED)
print("srguedrbjkgn")
print(current_card.image)
player = tk.PhotoImage(file=current_card.image)
computer = tk.PhotoImage(file=((str(cpu_current_card.image)[:-4]) + "1" + ".gif"))
print("M")
print(player)
label1.config(image = "")
label1.grid_forget
label1.config(image = player)
label1.image = player
label1.grid(row=0, column=0, columnspan=2, padx=(win_varw * win_varh * ((win_width/3)-400)/2))
label2.config(image = "")
label2.grid_forget
label2.config(image = computer)
label2.image = computer
label2.grid(row=0, column=4, columnspan=2, padx=(win_varw * win_varh * ((win_width/3)-400)/2))
label3 = tk.Label(root, text="VS", font=("Helvetica", (win_varw * win_varh * 70)))
label3.grid(row=0, column=2, columnspan=2, padx=(win_varw * win_varh * ((win_width/3)-140)/2))
print("Tester1")
print(len(player_deck))
label7 = tk.Label(root, text="Your Deck", font=("Helvetica", (win_varw * win_varh * 60)))
label7.grid(row=5, column=0, columnspan=2)
label8 = tk.Label(root, text="CPU's Deck", font=("Helvetica", (win_varw * win_varh * 60)))
label8.grid(row=5, column=4, columnspan=2)
label5.config(text = "")
label5.grid_forget
label5.config(text = str(len(player_deck)))
label5.text = str(len(player_deck))
label5.grid(row=6, column=0, columnspan=2)
label6.config(text = "")
label6.grid_forget
label6.config(text = str(len(cpu_deck)))
label6.text = str(len(cpu_deck))
label6.grid(row=6, column=4, columnspan=2)
healthb = tk.Button(root, text="Health", font=("Helvetica"), command= lambda: current_press('health'))
healthb.grid(row=1, column=0)
staminab = tk.Button(root, text="Stamina", font=("Helvetica"), command= lambda: current_press('stamina'))
staminab.grid(row=1, column=1)
moralityb = tk.Button(root, text="Morality", font=("Helvetica"), command= lambda: current_press('morality'))
moralityb.grid(row=1, column=2)
strengthb = tk.Button(root, text="Strength", font=("Helvetica"), command= lambda: current_press('strength'))
strengthb.grid(row=1, column=3)
heightb = tk.Button(root, text="Height", font=("Helvetica"), command= lambda: current_press('height'))
heightb.grid(row=1, column=4)
willb = tk.Button(root, text="Will", font=("Helvetica"), command= lambda: current_press('will'))
willb.grid(row=1, column=5)
escape = tk.Button(root, text="Escape", font=("Helvetica"), command= root.destroy)
escape.grid(row=2, column=2, columnspan=2)
print("K")
player = tk.PhotoImage(file=current_card.image)
computer = tk.PhotoImage(file=((str(cpu_current_card.image)[:-4]) + "1" + ".gif"))
print("M")
label1 = tk.Label(root, image=player)
label1.image = player
label1.grid(row=0, column=0, columnspan=2, padx=(win_varw * win_varh * ((win_width/3)-400)/2))
label2 = tk.Label(root, image=computer)
label2.image = computer
label2.grid(row=0, column=4, columnspan=2, padx=(win_varw * win_varh * ((win_width/3)-400)/2))
label3 = tk.Label(root, text="VS", font=("Helvetica", (win_varw * win_varh * 70)))
label3.grid(row=0, column=2, columnspan=2, padx=(win_varw * win_varh * ((win_width/3)-140)/2))
label7 = tk.Label(root, text="Your Deck", font=("Helvetica", (win_varw * win_varh * 60)))
label7.grid(row=5, column=0, columnspan=2)
label8 = tk.Label(root, text="CPU's Deck", font=("Helvetica", (win_varw * win_varh * 60)))
label8.grid(row=5, column=4, columnspan=2)
label5 = tk.Label(root, text=str(len(player_deck)), font=("Helvetica", (win_varw * win_varh * 70)))
label5.grid(row=6, column=0, columnspan=2)
label6 = tk.Label(root, text=str(len(cpu_deck)), font=("Helvetica", (win_varw * win_varh * 70)))
label6.grid(row=6, column=4, columnspan=2)
label4 = tk.Label(root, text="", font=("Helvetica", (win_varw * win_varh * 60)))
label4.grid(row=0, column=0, columnspan=6, padx=(win_varw * win_varh * (win_width-700)/2))
healthb = tk.Button(root, text="Health", font=("Helvetica"), command= lambda: current_press('health'))
healthb.grid(row=1, column=0)
staminab = tk.Button(root, text="Stamina", font=("Helvetica"), command= lambda: current_press('stamina'))
staminab.grid(row=1, column=1)
moralityb = tk.Button(root, text="Morality", font=("Helvetica"), command= lambda: current_press('morality'))
moralityb.grid(row=1, column=2)
strengthb = tk.Button(root, text="Strength", font=("Helvetica"), command= lambda: current_press('strength'))
strengthb.grid(row=1, column=3)
heightb = tk.Button(root, text="Height", font=("Helvetica"), command= lambda: current_press('height'))
heightb.grid(row=1, column=4)
willb = tk.Button(root, text="Will", font=("Helvetica"), command= lambda: current_press('will'))
willb.grid(row=1, column=5)
escape = tk.Button(root, text="Escape", font=("Helvetica"), command= root.destroy)
escape.grid(row=2, column=2, columnspan=2)
print("N")
root.mainloop()