Basic Money Game

Basic Money Game

Postby grim of doom » Thu Jul 10, 2014 3:21 am

Hello! I actually came here looking for some help on my game. It is a very basic money game using tkinter and python 2.7. I have a longer version(got ahead of myself and, made a but load of functions that are repeating, so im fixing it) of my game that I am shortening because of a minor mistake. Now that i did shorten it, it does not exactly run the right way. Could someone please help me, ive only been using python for a couple weeks and tkinter this past 2 weeks.

[Problem Code,most updated]
Code: Select all
from Tkinter import *
win=Tk()

#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
#||||||||||||||||||||||||||text Vars|||||||||||||||||||||||||||||||||||||||||||||
#////////////////////////////////////////////////////////////////////////////////
daysc=StringVar()
tc=StringVar()
negm=StringVar()
j1=StringVar()
j2=StringVar()
j3=StringVar()
j4=StringVar()
j5=StringVar()
j6=StringVar()
j7=StringVar()
j8=StringVar()
j9=StringVar()
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
#||||||||||||||||||||||||||Basic Vars||||||||||||||||||||||||||||||||||||||||||||
#////////////////////////////////////////////////////////////////////////////////
ad=0#actual number of days
ac=0#actual cash
hw=0#tacked hours before subtracting days
chw=1#hours worked at time
moneymulti=1#money multiplyer ex: x2,x3-perks
neg=0#amount of money taken per day
dbg=win.cget('bg')#default bg color

jo1=1
jo2=0#Job owned
jo3=0
jo4=0
jo5=0
jo6=0
jo7=0
jo8=0
jo9=0

jm1=1#job starts with x/this much
jm2=2
jm3=5
jm4=10
jm5=20
jm6=50
jm7=100
jm8=200
jm9=300

aj1=1#currently job makes
aj2=0
aj3=0
aj4=0
aj5=0
aj6=0
aj7=0
aj8=0
aj9=0
mpc=((aj1+aj2+aj3+aj4+aj5+aj6+aj7+aj8+aj9)*chw)*moneymulti
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
#||||||||||||||||||||||||||Basic Defs||||||||||||||||||||||||||||||||||||||||||||
#////////////////////////////////////////////////////////////////////////////////
def sadf():#ignore
    print'hello'
def cw():#check win
    global ac
    global jo2
    global jo3
    global jo4
    global jo5
    global jo6
    global jo7
    global jo8
    global jo9
    checkwin=jo2+jo3+jo4+jo5+jo6+jo7+jo8+jo9
    if checkwin>=8:
        win.destroy()
        gw=Tk()
        gw.overrideredirect(1)
        x = (gw.winfo_screenwidth() - gw.winfo_reqwidth()) / 2
        y = (gw.winfo_screenheight() - gw.winfo_reqheight()) / 2
        gw.geometry("+%d+%d" % (x, y))
        gw.configure(background='green')
        logol=Label(gw,text='WINNER!',font=('System',64,'bold'),fg='white',bg='green')
        logol.pack(fill='both',expand=True)
        qui=Button(gw,text='Close',font=('System',16,'bold'),bd=1,command=lambda:qu(),bg='green')
        qui.pack(fill='both',expand=True)
        def qu():
            gw.destroy()
def cgo():#check lose
    global ac
    if ac<0:
        win.destroy()
        end=Tk()
        def endd():
            end.destroy()
        end.configure(background='blue')
        endl=Label(end,text='Game Over',fg='white',bg='blue',font=('System',64,'bold'))
        endl.pack(side='top',expand=True,fill='both')
        close=Button(end,text='Close',bg='blue',fg='white',font=('System',16,'bold'),bd=2,command=lambda:endd())
        close.pack(side='top',expand=True,fill='both')
        end.overrideredirect(1)
        x = (end.winfo_screenwidth() - end.winfo_reqwidth()) / 2
        y = (end.winfo_screenheight() - end.winfo_reqheight()) / 2
        end.geometry("+%d+%d" % (x, y))
       
def workshift():#work *chw* hours
    global ad
    global chw
    global hw
    global mpc
    global ac
    global neg
    hw+=chw
    ac+=mpc*chw
    tc.set(ac)
    if hw>=48:
        cgo()
        hw-=48
        ad+=2
        daysc.set(ad)
        ac-=neg
        ac-=neg
        tc.set(ac)
        cgo()
    elif hw>=24:
        hw-=24
        ad+=1
        daysc.set(ad)
        ac-=neg
        tc.set(ac)
        cgo()
def pod():#pay off dept
    global ac
    global neg
    if ac>=neg:
        ac-=neg
        neg=0
        tc.set(ac)
        negm.set(neg)
    else:
        p20.flash()
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
#||||||||||||||||||||||||||buy Hours|||||||||||||||||||||||||||||||||||||||||||||
#////////////////////////////////////////////////////////////////////////////////
def hu(a,b,c):
    global mpc
    global chw
    global ac
    global neg
    if ac>=b:
        ac-=b
        tc.set(ac)
        neg+=b
        negm.set(neg)
        chw+=c
        a.config(state=DISABLED)
    else:
        a.flash()
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
#||||||||||||||||||||||||||buy jobs||||||||||||||||||||||||||||||||||||||||||||||
#////////////////////////////////////////////////////////////////////////////////
def bj(aj,jo,upg1,jm,jobb,jobm,c,j):
    global ac
    global mpc
    if ac>=c:
        upg1.config(state=DISABLED)
        jo=1
        aj+=jm
        ac-=c
        tc.set(ac)
        jobb.config(bg='lightgrey')
        jobm.config(bg='lightgrey')
        mpc+=jm
        j.set(aj)
        cw()
    else:
        upg1.flash()
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
#||||||||||||||||||||||||||x2 job Defs|||||||||||||||||||||||||||||||||||||||||||
#////////////////////////////////////////////////////////////////////////////////
def x2(aj,j,upg2,jm,c,jo):
    global ac
    global mpc
    global hw
    global neg
    if jo>=1:
        if ac>=c:
            aj+=jm
            j.set(aj)
            ac-=c
            tc.set(ac)
            neg+=c
            negm.set(neg)
            mpc+=jm
    else:
        upg2.flash()
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
#||||||||||||||||||||||||||Main Window Structure|||||||||||||||||||||||||||||||||
#////////////////////////////////////////////////////////////////////////////////
p4=Frame(win,relief=SUNKEN,bd=0)#TITLE-holds the top frames
p4.pack(side='top')
p5=Frame(win,relief=SUNKEN,bd=0)#CONTENT-holds the bottom frames
p5.pack(side='bottom',expand=True,fill='both')
p1=Frame(win,relief=SUNKEN,bd=2)#CONTENT/PERKS & UPGRADES-holds the two right side frames
p1.pack(side='left',fill='both',expand=True)
p9=Frame(p1,relief=SUNKEN,bd=1)
p9.pack(side='left',expand=True,fill='both')
p10=Frame(p1,relief=SUNKEN,bd=1)
p10.pack(side='left',expand=True,fill='both')
p11=Frame(p1,relief=SUNKEN,bd=1)
p11.pack(side='left',expand=True,fill='both')
p3=Frame(p5,relief=SUNKEN,bd=0)#JOBS-bottom
p3.pack(side='left',expand=True,fill='both')
p7=Frame(p3,relief=SUNKEN,bd=2)#JOBS click
p7.pack(side='left')
p8=Frame(p3,relief=SUNKEN,bd=2)#JOBS make
p8.pack(side='left')
p20=Button(p3,text='Pay Off Dept',command=lambda:pod(),font=("Helvetica", 10, "bold"),fg='red')
p20.pack(side='left')
p21=Button(p3,text='Become World Leader',command=lambda:cw(),font=("Helvetica", 10, "bold"),fg='green')
p21.pack(side='left')
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
#||||||||||||||||||||||||||Main Content||||||||||||||||||||||||||||||||||||||||||
#////////////////////////////////////////////////////////////////////////////////
##MAIN##
logo=Label(p4,text='Money Game!',font=("Helvetica", 32, "bold italic"))
logo.pack(side='top')
dayc=Label(p4,textvariable=daysc,font=("Helvetica", 16, "bold italic"),bg='grey')
dayc.pack(side='right')
daysc.set(ad)
days=Label(p4,text='Total Days:',font=("Helvetica", 16, "bold italic"))
days.pack(side='right')
workd=Button(p4,text='Work Shift',font=("Helvetica", 10, "bold italic"),command=lambda:workshift())
workd.pack(side='right')
ttlc=Label(p4,text='Total Cash:',font=("Helvetica", 16, "bold italic"))
ttlc.pack(side='left')
ttcc=Label(p4,textvariable=tc,font=("Helvetica", 16, "bold italic"),bg='grey')
ttcc.pack(side='left')
tc.set(ac)
##Jobs##
jobl1=Label(p7,text='Jobs',font=("Helvetica", 16, "bold italic"))
jobl1.pack(side='top')
jobl2=Label(p8,text='Makes',font=("Helvetica", 16, "bold italic"))
jobl2.pack(side='top')
##Upgrades and Perks##
perks2=Label(p1,textvariable=negm,font=("Helvetica", 16, "bold"),fg='red')
perks2.pack(side='top')
negm.set(neg)
upgradel=Label(p1,text='Daily Dept',font=("Helvetica", 16, "bold italic"))
upgradel.pack(side='top')
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
#||||||||||||||||||||||||||JOBS NAMES||||||||||||||||||||||||||||||||||||||||||||
#////////////////////////////////////////////////////////////////////////////////
jobb1=Label(p7,text='Grocery Bagger',font=("Helvetica", 8, "bold"))
jobb1.pack(side='top',fill='both',expand=True)
jobb2=Label(p7,text='Lemonade Cart',font=("Helvetica", 8, "bold"),bg='black')
jobb2.pack(side='top',fill='both',expand=True)
jobb3=Label(p7,text='Hotdog Cart',font=("Helvetica", 8, "bold"),bg='black')
jobb3.pack(side='top',fill='both',expand=True)
jobb4=Label(p7,text='Small Hamburger Joint',font=("Helvetica", 8, "bold"),bg='black')
jobb4.pack(side='top',fill='both',expand=True)
jobb5=Label(p7,text='Small Corner Store',font=("Helvetica", 8, "bold"),bg='black')
jobb5.pack(side='top',fill='both',expand=True)
jobb6=Label(p7,text='Family Resturant',font=("Helvetica", 8, "bold"),bg='black')
jobb6.pack(side='top',fill='both',expand=True)
jobb7=Label(p7,text='Electronics Store',font=("Helvetica", 8, "bold"),bg='black')
jobb7.pack(side='top',fill='both',expand=True)
jobb8=Label(p7,text='Mayor',font=("Helvetica", 8, "bold"),bg='black')
jobb8.pack(side='top',fill='both',expand=True)
jobb9=Label(p7,text='President',font=("Helvetica", 8, "bold"),bg='black')
jobb9.pack(side='top',fill='both',expand=True)
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
#||||||||||||||||||||||||||JOBS MAKES||||||||||||||||||||||||||||||||||||||||||||
#////////////////////////////////////////////////////////////////////////////////
jobm1=Label(p8,textvariable=j1,font=("Helvetica", 8, "bold"))
jobm1.pack(side='top',fill='both',expand=True)
j1.set(aj1)
jobm2=Label(p8,textvariable=j2,font=("Helvetica", 8, "bold"),bg='black')
jobm2.pack(side='top',fill='both',expand=True)
j2.set(aj2)
jobm3=Label(p8,textvariable=j3,font=("Helvetica", 8, "bold"),bg='black')
jobm3.pack(side='top',fill='both',expand=True)
j3.set(aj3)
jobm4=Label(p8,textvariable=j4,font=("Helvetica", 8, "bold"),bg='black')
jobm4.pack(side='top',fill='both',expand=True)
j4.set(aj4)
jobm5=Label(p8,textvariable=j5,font=("Helvetica", 8, "bold"),bg='black')
jobm5.pack(side='top',fill='both',expand=True)
j5.set(aj5)
jobm6=Label(p8,textvariable=j6,font=("Helvetica", 8, "bold"),bg='black')
jobm6.pack(side='top',fill='both',expand=True)
j6.set(aj6)
jobm7=Label(p8,textvariable=j7,font=("Helvetica", 8, "bold"),bg='black')
jobm7.pack(side='top',fill='both',expand=True)
j7.set(aj7)
jobm8=Label(p8,textvariable=j8,font=("Helvetica", 8, "bold"),bg='black')
jobm8.pack(side='top',fill='both',expand=True)
j8.set(aj8)
jobm9=Label(p8,textvariable=j9,font=("Helvetica", 8, "bold"),bg='black')
jobm9.pack(side='top',fill='both',expand=True)
j9.set(aj9)
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
#||||||||||||||||||||||||||Upgrades & Perks||||||||||||||||||||||||||||||||||||||
#////////////////////////////////////////////////////////////////////////////////
upgradel1=Label(p9,text='Upgrade Job',font=("Helvetica", 8, "bold"))
upgradel1.pack(side='top')
upg12=Button(p9,text='[Costs:200]   BUY Lemonade Cart',font=("Helvetica", 10,'bold'),fg='red',command=lambda:bj(aj2,jo2,upg12,jm2,jobb2,jobm2,200,j2))
upg12.pack(fill='both',expand=True)
upg13=Button(p9,text='[Costs:500]   BUY Hotdog Cart',font=("Helvetica", 10,'bold'),fg='red',command=lambda:bj(aj3,jo3,upg13,jm3,jobb3,jobm3,500,j3))
upg13.pack(fill='both',expand=True)
upg14=Button(p9,text='[Costs:1000]   BUY Hamburger Joint',font=("Helvetica", 10,'bold'),fg='red',command=lambda:bj(aj4,jo4,upg14,jm4,jobb4,jobm4,1000,j4))
upg14.pack(fill='both',expand=True)
upg15=Button(p9,text='[Costs:2000]   BUY Corner Store',font=("Helvetica", 10,'bold'),fg='red',command=lambda:bj(aj5,jo5,upg15,jm5,jobb5,jobm5,2000,j5))
upg15.pack(fill='both',expand=True)
upg16=Button(p9,text='[Costs:5000]   BUY Family Resturant',font=("Helvetica", 10,'bold'),fg='red',command=lambda:bj(aj6,jo6,upg16,jm6,jobb6,jobm6,5000,j6))
upg16.pack(fill='both',expand=True)
upg17=Button(p9,text='[Costs:10000]   BUY Electronics Store',font=("Helvetica", 10,'bold'),fg='red',command=lambda:bj(aj7,jo7,upg17,jm7,jobb7,jobm7,10000,j7))
upg17.pack(fill='both',expand=True)
upg18=Button(p9,text='[Costs:20000]   BUY Mayor',font=("Helvetica", 10,'bold'),fg='red',command=lambda:bj(aj8,jo8,upg18,jm8,jobb8,jobm8,20000,j8))
upg18.pack(fill='both',expand=True)
upg19=Button(p9,text='[Costs:50000]   BUY President',font=("Helvetica", 10,'bold'),fg='red',command=lambda:bj(aj9,jo9,upg19,jm9,jobb9,jobm9,50000,j9))
upg19.pack(fill='both',expand=True)
upgradel2=Label(p10,text='Promotions',font=("Helvetica", 8, "bold"))
upgradel2.pack(side='top')
upg21=Button(p10,text='[Costs:10]   x2 Grocery Bagger',font=("Helvetica", 10,'bold'),fg='red',command=lambda:x2(aj1,j1,upg21,jm1,10,jo1))
upg21.pack(fill='both',expand=True)
upg22=Button(p10,text='[Costs:20]   x2 Lemonade Cart',font=("Helvetica", 10,'bold'),fg='red',command=lambda:x2(aj2,j2,upg22,jm2,20,jo2))
upg22.pack(fill='both',expand=True)
upg23=Button(p10,text='[Costs:50]   x2 Hotdog Cart',font=("Helvetica", 10,'bold'),fg='red',command=lambda:x2(aj3,j3,upg23,jm3,50,jo3))
upg23.pack(fill='both',expand=True)
upg24=Button(p10,text='[Costs:100]   x2 Hamburger Joint',font=("Helvetica", 10,'bold'),fg='red',command=lambda:x2(aj4,j4,upg24,jm4,100,jo4))
upg24.pack(fill='both',expand=True)
upg25=Button(p10,text='[Costs:200]   x2 Corner Store',font=("Helvetica", 10,'bold'),fg='red',command=lambda:x2(aj5,j5,upg25,jm5,200,jo5))
upg25.pack(fill='both',expand=True)
upg26=Button(p10,text='[Costs:500]   x2 Family Resturant',font=("Helvetica", 10,'bold'),fg='red',command=lambda:x2(aj6,j6,upg26,jm6,500,jo6))
upg26.pack(fill='both',expand=True)
upg27=Button(p10,text='[Costs:1000]   x2 Electronics Store',font=("Helvetica", 10,'bold'),fg='red',command=lambda:x2(aj7,j7,upg27,jm7,1000,jo7))
upg27.pack(fill='both',expand=True)
upg28=Button(p10,text='[Costs:2000]   x2 Mayor',font=("Helvetica", 10,'bold'),fg='red',command=lambda:x2(aj8,j8,upg28,jm8,2000,jo8))
upg28.pack(fill='both',expand=True)
upg29=Button(p10,text='[Costs:5000]   x2 President',font=("Helvetica", 10,'bold'),fg='red',command=lambda:x2(aj9,j9,upg29,jm9,5000,jo9))
upg29.pack(fill='both',expand=True)
upgradel3=Label(p11,text='Longer Shifts',font=("Helvetica", 8, "bold"))
upgradel3.pack(side='top')
upg31=Button(p11,text='[Costs:100]   +1 Hour',font=("Helvetica", 10,'bold'),fg='red',command=lambda:hu(upg31,100,1))
upg31.pack(fill='both',expand=True)
upg32=Button(p11,text='[Costs:200]   +2 Hours',font=("Helvetica", 10,'bold'),fg='red',command=lambda:hu(upg32,200,2))
upg32.pack(fill='both',expand=True)
upg33=Button(p11,text='[Costs:500]   +3 Hours',font=("Helvetica", 10,'bold'),fg='red',command=lambda:hu(upg33,500,3))
upg33.pack(fill='both',expand=True)
upg34=Button(p11,text='[Costs:1000]   +4 Hours',font=("Helvetica", 10,'bold'),fg='red',command=lambda:hu(upg34,1000,4))
upg34.pack(fill='both',expand=True)
upg35=Button(p11,text='[Costs:2000]   +5 Hours',font=("Helvetica", 10,'bold'),fg='red',command=lambda:hu(upg35,2000,5))
upg35.pack(fill='both',expand=True)
upg36=Button(p11,text='[Costs:5000]   +6 Hours',font=("Helvetica", 10,'bold'),fg='red',command=lambda:hu(upg36,5000,6))
upg36.pack(fill='both',expand=True)
upg37=Button(p11,text='[Costs:10000]   +8 Hours',font=("Helvetica", 10,'bold'),fg='red',command=lambda:hu(upg37,10000,7))
upg37.pack(fill='both',expand=True)
upg38=Button(p11,text='[Costs:20000]   +9 Hours',font=("Helvetica", 10,'bold'),fg='red',command=lambda:hu(upg38,20000,8))
upg38.pack(fill='both',expand=True)
upg39=Button(p11,text='[Costs:50000]   +10 Hours',font=("Helvetica", 10,'bold'),fg='red',command=lambda:hu(upg39,50000,9))
upg39.pack(fill='both',expand=True)

#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
#||||||||||||||||||||||||||Configure Window||||||||||||||||||||||||||||||||||||||
#////////////////////////////////////////////////////////////////////////////////
win.mainloop()

(i tried making it as easy/readable as possible when making it)
Last edited by micseydel on Thu Jul 10, 2014 3:43 am, edited 2 times in total.
Reason: First post lock.
grim of doom
 
Posts: 20
Joined: Thu Jul 10, 2014 3:16 am

Re: Basic Money Game

Postby grim of doom » Thu Jul 10, 2014 4:00 am

Couldn't figure out how to edit, but my problem (90% sure) is my in functions for 'x2' and 'bj'(thise don't act as they should or think they should as I wrote them)
grim of doom
 
Posts: 20
Joined: Thu Jul 10, 2014 3:16 am

Re: Basic Money Game

Postby Mekire » Thu Jul 10, 2014 4:27 am

I'm afraid, as it stands, no one is going to debug that for you.

I'll give you a few pointers instead.
Firstly, don't do this:
Code: Select all
from Tkinter import *
instead do this:
Code: Select all
import Tkinter as tk
and then prefix all Tkinter specific items appropriately.
Ex:
Code: Select all
win = tk.Tk()


Secondly, no numbered variables. Any time you think you need numbered variables, what you actually want is a list (or other container type).
So this:
Code: Select all
jo1=1
jo2=0#Job owned
jo3=0
jo4=0
jo5=0
jo6=0
jo7=0
jo8=0
jo9=0
Should probably be this:
Code: Select all
job_owned = [0, 0, 0, 0, 0, 0, 0, 0, 0]
Or maybe this:
Code: Select all
job_owned = [0]*9


This brings us to the next thing; meaningful variable names. Don't do this:
Code: Select all
jo = 0 #Job owned
Do this instead:
Code: Select all
job_owned = 0
Code should, wherever possible, be self documenting.

And final advice for now; global variables and the global keyword. No.
Until you are much more experienced you should consider the global keyword off limits.
Learn how to correctly pass and return variables. Globals are never the answer.
Global constants are fine, but none of yours qualify as such.

Once you have worked on the above issues, try to reduce your problem down to a reproducable example. No one is going to debug 400 lines of spaghetti for you, but we might be able to assist you in untangling 50 lines that represent your problem.

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Basic Money Game

Postby grim of doom » Thu Jul 10, 2014 5:06 am

Thank you for the information on what I have been doing wrong. Most of what I learn, I learn as I needed. I don't really find any useful tutorials that run strait forward that I understand. Though, about the 'import tkinter as tk' and it's prefix, is there a difference at all? I have seen it a few times when searching around the internet but never understood why so I stuck with what I did.
grim of doom
 
Posts: 20
Joined: Thu Jul 10, 2014 3:16 am

Re: Basic Money Game

Postby Mekire » Thu Jul 10, 2014 6:42 am

This touches on some of the reasons not to star import:
http://python-forum.org/viewtopic.php?f=25&t=4822

Star importing does something called namespace flooding. Namespace flooding is bad. The small convenience of not needing to type a few characters is far out weighed by the potential problems that can arise.

If you still can't see the logical rationale behind it, then at least know that doing so makes you look like an amateur to your peers. This alone should be a good enough reason to avoid it.

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Basic Money Game

Postby grim of doom » Thu Jul 10, 2014 7:48 am

I understand it, and will fix it once i re-write all my functions and variables, and get them working. Kinda wish i didn't learn and get a habit of using
Code: Select all
from tkinter import *

thanks for all the help you've given a 'newb' like me.

___EDIT___
so I have updated my script and it works great now, variables are eaasier to read. used import Tkinter as tk, might have global wrong still(only way i can get function to work), and adjusted all the buttons/labels/frames for better looks.

Code: Select all
import Tkinter as tk
win=tk.Tk()

cash=tk.StringVar()
days=tk.StringVar()
loss=tk.StringVar()
j1=tk.StringVar()
j2=tk.StringVar()
j3=tk.StringVar()
j4=tk.StringVar()
j5=tk.StringVar()
j6=tk.StringVar()
j7=tk.StringVar()
j8=tk.StringVar()
j9=tk.StringVar()
#==============================================main variables
default_bg='#40E0D0'
tcash=0
tdays=0
thours=0
tloss=0
hours_worked=1

job_makes=[0,1,0,0,0,0,0,0,0,0]
job_truly_makes=[0,1,2,5,10,20,50,100,200,500]
job_owned=[0,1,0,0,0,0,0,0,0,0]


#===============================================main functions
def work():
    global tcash,tdays,tloss,thours,job_makes
    checkwin()
    tcash+=sum(job_makes)
    thours+=hours_worked
    if thours>=48:
        tcash-=tloss*2
        tdays+=2
        thours-=48
        checklose()
    elif thours>=24:
        tcash-=tloss
        tdays+=1
        thours-=24
        checklose()
    days.set(tdays)
    cash.set(tcash)
    loss.set(tloss)
   
def pay_dept():
    global tcash,tloss
    if tcash>=tloss:
        tcash-=tloss
        tloss=0
        loss.set(tloss)
        cash.set(tcash)
   
def buy_job(jobn,cost,button,jobb,jobm,j):
    global tcash,tloss,job_makes,job_owned,job_truly_makes
    if tcash>=cost:
        button.config(state=tk.DISABLED)
        tcash-=cost
        cash.set(tcash)
        tloss+=cost
        loss.set(tloss)
        jobb.config(bg=default_bg)
        jobm.config(bg=default_bg)
        job_owned[jobn]=1
        job_makes[jobn]+=job_truly_makes[jobn]
        j.set(job_makes[jobn])
        checkwin()
    else:
        button.flash()
       
def add_job(jobn,cost,j,button):
    global tcash,tloss,job_makes,job_truly_makes,job_owned
    if tcash>=cost and job_owned[jobn]==1:
        tcash-=cost
        cash.set(tcash)
        job_makes[jobn]+=job_truly_makes[jobn]
        j.set(job_makes[jobn])
        tloss+=cost
        loss.set(tloss)
    else:
        button.flash()

def add_hours(upg3,cost,added_hours):
    global tcash,tloss,hours_worked
    if tcash>=cost:
        upg3.config(state=tk.DISABLED)
        tcash-=cost
        cash.set(tcash)
        tloss+=cost
        loss.set(tloss)
        hours_worked+=added_hours
    else:
        upg3.flash()
       
def checklose():
    global tcash
    if tcash<0:
        win.destroy()
        losewin=tk.Tk()
        losewin.overrideredirect(1)
        w,h=losewin.winfo_screenwidth(),losewin.winfo_screenheight()
        losewin.geometry("%dx%d+0+0" % (w, h))
        losewin.configure(background='red')
        label=tk.Label(losewin,text='Ran Out Of Money',bg='red',fg='white',font=('System',64,'bold'))
        close=tk.Button(losewin,text='Close',font=('System',32,'bold'),bd=2,fg='white',bg='red',command=lambda:loserclose())
        label.pack()
        close.pack()
        def loserclose():
            losewin.destroy()

def checkwin():
    global job_owned
    if sum(job_owned)==9:
        win.destroy()
        winnerwin=tk.Tk()
        winnerwin.overrideredirect(1)
        w,h=winnerwin.winfo_screenwidth(),winnerwin.winfo_screenheight()
        winnerwin.geometry("%dx%d+0+0" % (w, h))
        winnerwin.configure(background='lightgreen')
        label=tk.Label(winnerwin,text='WINNER!',bg='lightgreen',fg='white',font=('System',64,'bold'))
        close=tk.Button(winnerwin,text='Close',font=('System',32,'bold'),bd=2,fg='white',bg='lightgreen',command=lambda:winnerclose())
        label.pack()
        close.pack()
        def winnerclose():
            winnerwin.destroy()
##==========================================================================================================structure starts here
#creating basic structure
p4=tk.Frame(win,relief=tk.SUNKEN,bd=0)#TITLE-holds the top frames
p5=tk.Frame(win,relief=tk.SUNKEN,bd=0)#CONTENT-holds the bottom frames
p1=tk.Frame(win,relief=tk.SUNKEN,bd=2)#CONTENT/PERKS & UPGRADES-holds the two right side frames
p9=tk.Frame(p1,relief=tk.SUNKEN,bd=1)
p10=tk.Frame(p1,relief=tk.SUNKEN,bd=1)
p11=tk.Frame(p1,relief=tk.SUNKEN,bd=1)
p3=tk.Frame(p5,relief=tk.SUNKEN,bd=0)#JOBS-bottom
p7=tk.Frame(p3,relief=tk.SUNKEN,bd=2)#JOBS click
p8=tk.Frame(p3,relief=tk.SUNKEN,bd=2)#JOBS make
p20=tk.Button(p3,text='Pay Off Dept',font=("Helvetica", 10, "bold"),fg='red',command=lambda:pay_dept())
p21=tk.Button(p3,text='Become World Leader',font=("Helvetica", 10, "bold"),fg='green')
#pack basic structure
p4.pack(side='top')
p5.pack(side='bottom',expand=True,fill='both')
p1.pack(side='left',fill='both',expand=True)
p9.pack(side='left',expand=True,fill='both')
p10.pack(side='left',expand=True,fill='both')
p11.pack(side='left',expand=True,fill='both')
p3.pack(side='left',expand=True,fill='both')
p7.pack(side='left')
p8.pack(side='left')
p20.pack(side='left')
p21.pack(side='left')
#TOP Contents
logo=tk.Label(p4,text='Money Game!',font=("Helvetica", 32, "bold italic"))
dayc=tk.Label(p4,textvariable=days,font=("Helvetica", 16, "bold italic"),bg='grey')
daysp=tk.Label(p4,text='Total Days:',font=("Helvetica", 16, "bold italic"))
workd=tk.Button(p4,text='Work Shift',font=("Helvetica", 10, "bold italic"),command=lambda:work())
ttlc=tk.Label(p4,text='Total Cash:',font=("Helvetica", 16, "bold italic"))
ttcc=tk.Label(p4,textvariable=cash,font=("Helvetica", 16, "bold italic"),bg='grey')
logo.pack(side='top')
dayc.pack(side='right')
days.set(tdays)
daysp.pack(side='right')
workd.pack(side='right')
ttlc.pack(side='left')
ttcc.pack(side='left')
cash.set(tcash)
#Jobs Contents
jobl1=tk.Label(p7,text='Jobs',font=("Helvetica", 16, "bold italic"))
jobl2=tk.Label(p8,text='Makes',font=("Helvetica", 16, "bold italic"))
jobl1.pack(side='top')
jobl2.pack(side='top')
#Upgrades and Perks Contents
perks2=tk.Label(p1,textvariable=loss,font=("Helvetica", 16, "bold"),fg='red')
upgradel=tk.Label(p1,text='Daily Dept',font=("Helvetica", 16, "bold italic"))
perks2.pack(side='top')
loss.set(tloss)
upgradel.pack(side='top')
#=========================================================================================================main content
#job names
jobb1=tk.Label(p7,text='Grocery Bagger',font=("Helvetica", 8, "bold"))
jobb2=tk.Label(p7,text='Lemonade Cart',font=("Helvetica", 8, "bold"),bg='black')
jobb3=tk.Label(p7,text='Hotdog Cart',font=("Helvetica", 8, "bold"),bg='black')
jobb4=tk.Label(p7,text='Small Hamburger Joint',font=("Helvetica", 8, "bold"),bg='black')
jobb5=tk.Label(p7,text='Small Corner Store',font=("Helvetica", 8, "bold"),bg='black')
jobb6=tk.Label(p7,text='Family Resturant',font=("Helvetica", 8, "bold"),bg='black')
jobb7=tk.Label(p7,text='Electronics Store',font=("Helvetica", 8, "bold"),bg='black')
jobb8=tk.Label(p7,text='Mayor',font=("Helvetica", 8, "bold"),bg='black')
jobb9=tk.Label(p7,text='President',font=("Helvetica", 8, "bold"),bg='black')
#job names pack
jobb1.pack(side='top',fill='both',expand=True)
jobb2.pack(side='top',fill='both',expand=True)
jobb3.pack(side='top',fill='both',expand=True)
jobb4.pack(side='top',fill='both',expand=True)
jobb5.pack(side='top',fill='both',expand=True)
jobb6.pack(side='top',fill='both',expand=True)
jobb7.pack(side='top',fill='both',expand=True)
jobb8.pack(side='top',fill='both',expand=True)
jobb9.pack(side='top',fill='both',expand=True)
#job makes
jobm1=tk.Label(p8,textvariable=j1,font=("Helvetica", 8, "bold"))
jobm2=tk.Label(p8,textvariable=j2,font=("Helvetica", 8, "bold"),bg='black')
jobm3=tk.Label(p8,textvariable=j3,font=("Helvetica", 8, "bold"),bg='black')
jobm4=tk.Label(p8,textvariable=j4,font=("Helvetica", 8, "bold"),bg='black')
jobm5=tk.Label(p8,textvariable=j5,font=("Helvetica", 8, "bold"),bg='black')
jobm6=tk.Label(p8,textvariable=j6,font=("Helvetica", 8, "bold"),bg='black')
jobm7=tk.Label(p8,textvariable=j7,font=("Helvetica", 8, "bold"),bg='black')
jobm8=tk.Label(p8,textvariable=j8,font=("Helvetica", 8, "bold"),bg='black')
jobm9=tk.Label(p8,textvariable=j9,font=("Helvetica", 8, "bold"),bg='black')
#job makes pack
jobm1.pack(side='top',fill='both',expand=True)
j1.set(job_makes[1])
jobm2.pack(side='top',fill='both',expand=True)
j2.set(job_makes[2])
jobm3.pack(side='top',fill='both',expand=True)
j3.set(job_makes[3])
jobm4.pack(side='top',fill='both',expand=True)
j4.set(job_makes[4])
jobm5.pack(side='top',fill='both',expand=True)
j5.set(job_makes[5])
jobm6.pack(side='top',fill='both',expand=True)
j6.set(job_makes[6])
jobm7.pack(side='top',fill='both',expand=True)
j7.set(job_makes[7])
jobm8.pack(side='top',fill='both',expand=True)
j8.set(job_makes[8])
jobm9.pack(side='top',fill='both',expand=True)
j9.set(job_makes[9])
#=====================================================================================upgrades & perks buttons
#job upgrades
upgradel1=tk.Label(p9,text='Upgrade Job',font=("Helvetica", 8, "bold"))
upg12=tk.Button(p9,text='[Costs:200]   BUY Lemonade Cart',font=("Helvetica", 10,'bold'),fg='red',command=lambda:buy_job(2,200,upg12,jobb2,jobm2,j2))
upg13=tk.Button(p9,text='[Costs:500]   BUY Hotdog Cart',font=("Helvetica", 10,'bold'),fg='red',command=lambda:buy_job(3,500,upg13,jobb3,jobm3,j3))
upg14=tk.Button(p9,text='[Costs:1000]   BUY Hamburger Joint',font=("Helvetica", 10,'bold'),fg='red',command=lambda:buy_job(4,1000,upg14,jobb4,jobm4,j4))
upg15=tk.Button(p9,text='[Costs:2000]   BUY Corner Store',font=("Helvetica", 10,'bold'),fg='red',command=lambda:buy_job(5,2000,upg15,jobb5,jobm5,j5))
upg16=tk.Button(p9,text='[Costs:5000]   BUY Family Resturant',font=("Helvetica", 10,'bold'),fg='red',command=lambda:buy_job(6,5000,upg16,jobb6,jobm6,j6))
upg17=tk.Button(p9,text='[Costs:10000]   BUY Electronics Store',font=("Helvetica", 10,'bold'),fg='red',command=lambda:buy_job(7,10000,upg17,jobb7,jobm7,j7))
upg18=tk.Button(p9,text='[Costs:20000]   BUY Mayor',font=("Helvetica", 10,'bold'),fg='red',command=lambda:buy_job(8,20000,upg18,jobb8,jobm8,j8))
upg19=tk.Button(p9,text='[Costs:50000]   BUY President',font=("Helvetica", 10,'bold'),fg='red',command=lambda:buy_job(9,50000,upg19,jobb9,jobm9,j9))
#pack job upgrades
upgradel1.pack(side='top')
upg12.pack(fill='both',expand=True)
upg13.pack(fill='both',expand=True)
upg14.pack(fill='both',expand=True)
upg15.pack(fill='both',expand=True)
upg16.pack(fill='both',expand=True)
upg17.pack(fill='both',expand=True)
upg18.pack(fill='both',expand=True)
upg19.pack(fill='both',expand=True)
#job promotions
upgradel2=tk.Label(p10,text='Promotions',font=("Helvetica", 8, "bold"))
upg21=tk.Button(p10,text='[Costs:10]   +1 Grocery Bagger',font=("Helvetica", 10,'bold'),fg='red',command=lambda:add_job(1,10,j1,upg21))
upg22=tk.Button(p10,text='[Costs:20]   +1 Lemonade Cart',font=("Helvetica", 10,'bold'),fg='red',command=lambda:add_job(2,20,j2,upg22))
upg23=tk.Button(p10,text='[Costs:50]   +1 Hotdog Cart',font=("Helvetica", 10,'bold'),fg='red',command=lambda:add_job(3,50,j3,upg23))
upg24=tk.Button(p10,text='[Costs:100]   +1 Hamburger Joint',font=("Helvetica", 10,'bold'),fg='red',command=lambda:add_job(4,100,j4,upg24))
upg25=tk.Button(p10,text='[Costs:200]   +1 Corner Store',font=("Helvetica", 10,'bold'),fg='red',command=lambda:add_job(5,200,j5,upg25))
upg26=tk.Button(p10,text='[Costs:500]   +1 Family Resturant',font=("Helvetica", 10,'bold'),fg='red',command=lambda:add_job(6,500,j6,upg26))
upg27=tk.Button(p10,text='[Costs:1000]   +1 Electronics Store',font=("Helvetica", 10,'bold'),fg='red',command=lambda:add_job(7,1000,j7,upg27))
upg28=tk.Button(p10,text='[Costs:2000]   +1 Mayor',font=("Helvetica", 10,'bold'),fg='red',command=lambda:add_job(8,2000,j8,upg28))
upg29=tk.Button(p10,text='[Costs:5000]   +1 President',font=("Helvetica", 10,'bold'),fg='red',command=lambda:add_job(9,5000,j9,upg29))
#pack job promototions
upgradel2.pack(side='top')
upg21.pack(fill='both',expand=True)
upg22.pack(fill='both',expand=True)
upg23.pack(fill='both',expand=True)
upg24.pack(fill='both',expand=True)
upg25.pack(fill='both',expand=True)
upg26.pack(fill='both',expand=True)
upg27.pack(fill='both',expand=True)
upg28.pack(fill='both',expand=True)
upg29.pack(fill='both',expand=True)
#shift upgrades
upgradel3=tk.Label(p11,text='Longer Shifts',font=("Helvetica", 8, "bold"))
upg31=tk.Button(p11,text='[Costs:100]   +1 Hour',font=("Helvetica", 10,'bold'),fg='red',command=lambda:add_hours(upg31,100,1))
upg32=tk.Button(p11,text='[Costs:200]   +2 Hours',font=("Helvetica", 10,'bold'),fg='red',command=lambda:add_hours(upg32,200,2))
upg33=tk.Button(p11,text='[Costs:500]   +3 Hours',font=("Helvetica", 10,'bold'),fg='red',command=lambda:add_hours(upg33,500,3))
upg34=tk.Button(p11,text='[Costs:1000]   +4 Hours',font=("Helvetica", 10,'bold'),fg='red',command=lambda:add_hours(upg34,1000,4))
upg35=tk.Button(p11,text='[Costs:2000]   +5 Hours',font=("Helvetica", 10,'bold'),fg='red',command=lambda:add_hours(upg35,2000,5))
upg36=tk.Button(p11,text='[Costs:5000]   +6 Hours',font=("Helvetica", 10,'bold'),fg='red',command=lambda:add_hours(upg36,5000,6))
upg37=tk.Button(p11,text='[Costs:10000]   +8 Hours',font=("Helvetica", 10,'bold'),fg='red',command=lambda:add_hours(upg37,10000,7))
upg38=tk.Button(p11,text='[Costs:20000]   +9 Hours',font=("Helvetica", 10,'bold'),fg='red',command=lambda:add_hours(upg38,20000,8))
upg39=tk.Button(p11,text='[Costs:50000]   +10 Hours',font=("Helvetica", 10,'bold'),fg='red',command=lambda:add_hours(upg39,50000,9))
#shift upgrades pack
upgradel3.pack(side='top')
upg31.pack(fill='both',expand=True)
upg32.pack(fill='both',expand=True)
upg33.pack(fill='both',expand=True)
upg34.pack(fill='both',expand=True)
upg35.pack(fill='both',expand=True)
upg36.pack(fill='both',expand=True)
upg37.pack(fill='both',expand=True)
upg38.pack(fill='both',expand=True)
upg39.pack(fill='both',expand=True)
#==================================================================================================configure windows
win.mainloop()

(at 296 lines, I could most likely have a function to remove a good chunk of adding buttons and text in functions. But i do not know how to exactly do that yet)
grim of doom
 
Posts: 20
Joined: Thu Jul 10, 2014 3:16 am


Return to Game Development

Who is online

Users browsing this forum: No registered users and 2 guests