if been using python's tkinter for awhile to write programs when I caused an issue that I'm not sure what is my code is long even after I gutted it.

from Tkinter import *
import random
import operator

info_dict = {}
info_dict["area"] = 1
info_dict["slime_spot"] = random.randint(1,36)
info_dict["slime spotter"] = list()
for x in range(1,37):
    info_dict["slime spotter"].append(x)
info_dict["areas"] = list()
info_dict["player"] = 50
info_dict["mana"] = 10
info_dict["resting"] = 0

class App:

    def __init__(self, master):

        frame = Frame(master)
        frame.pack()
        
        def slime_move():
            for x in info_dict["slime spotter"]:
                if info_dict["slime_spot"] == x:
                    info_dict["areas"].append(x)
            data = list()
            if info_dict["slime_spot"] == 1:
                data = "east","east"
            if info_dict["slime_spot"] == 2:
                data = "west","south"
            if info_dict["slime_spot"] == 3:
                data = "east","south"
            if info_dict["slime_spot"] == 4:
                data = "west","west"
            if info_dict["slime_spot"] == 5:
                data = "east","south"
            if info_dict["slime_spot"] == 6:
                data = "west","west"
            if info_dict["slime_spot"] == 7:
                data = "east","south"
            if info_dict["slime_spot"] == 8:
                data = "north","west"
            if info_dict["slime_spot"] == 9:
                data = "north","east","south"
            if info_dict["slime_spot"] == 10:
                data = "west","east"
            if info_dict["slime_spot"] == 11:
                data = "north","west"
            if info_dict["slime_spot"] == 12:
                data = "south","south"
            if info_dict["slime_spot"] == 13:
                data = "south","south"
            if info_dict["slime_spot"] == 14:
                data = "east","south"
            if info_dict["slime_spot"] == 15:
                data = "north","west",
            if info_dict["slime_spot"] == 16:
                data = "east","south"
            if info_dict["slime_spot"] == 17:
                data = "west","east"
            if info_dict["slime_spot"] == 18:
                data = "north","west"
            if info_dict["slime_spot"] == 19:
                data = "north","south"
            if info_dict["slime_spot"] == 20:
                data = "north","east"
            if info_dict["slime_spot"] == 21:
                data = "west","south"
            if info_dict["slime_spot"] == 22:
                data = "north","south"
            if info_dict["slime_spot"] == 23:
                data = "east","east"
            if info_dict["slime_spot"] == 24:
                data = "west","south"
            if info_dict["slime_spot"] == 25:
                data = "north","east","south"
            if info_dict["slime_spot"] == 26:
                data = "west","south"
            if info_dict["slime_spot"] == 27:
                data = "north","east"
            if info_dict["slime_spot"] == 28:
                data = "north","west","east","south"
            if info_dict["slime_spot"] == 29:
                data = "west","west"
            if info_dict["slime_spot"] == 30:
                data = "north","south"
            if info_dict["slime_spot"] == 31:
                data = "north","east"
            if info_dict["slime_spot"] == 32:
                data = "north","west","east"
            if info_dict["slime_spot"] == 33:
                data = "west","east"
            if info_dict["slime_spot"] == 34:
                data = "north","west","east"
            if info_dict["slime_spot"] == 35:
                data = "west","east"
            if info_dict["slime_spot"] == 36:
                data = "north","west"
            pos = random.randrange( len(data) )
            elem = data[pos]
            if elem == "north" and info_dict["area"] != info_dict["slime_spot"] - 6:
                info_dict["slime_spot"] = info_dict["slime_spot"] - 6
            if elem == "south" and info_dict["area"] != info_dict["slime_spot"] + 6:
                info_dict["slime_spot"] = info_dict["slime_spot"] + 6
            if elem == "west" and info_dict["area"] != info_dict["slime_spot"] - 1:
                info_dict["slime_spot"] = info_dict["slime_spot"] - 1
            if elem == "east" and info_dict["area"] != info_dict["slime_spot"] + 1:
                info_dict["slime_spot"] = info_dict["slime_spot"] + 1
            if elem == "north" and info_dict["area"] == info_dict["slime_spot"] - 6:
                strike = random.randint(1,6)
                info_dict["player"] = info_dict["player"] - strike
            if elem == "south" and info_dict["area"] == info_dict["slime_spot"] + 6:
                strike = random.randint(1,6)
                info_dict["player"] = info_dict["player"] - strike
            if elem == "west" and info_dict["area"] == info_dict["slime_spot"] - 1:
                strike = random.randint(1,6)
                info_dict["player"] = info_dict["player"] - strike
            if elem == "east" and info_dict["area"] == info_dict["slime_spot"] + 1:
                strike = random.randint(1,6)
                info_dict["player"] = info_dict["player"] - strike
            print elem, info_dict["slime_spot"]
            for x in info_dict["slime spotter"]:
                if info_dict["slime_spot"] == x:
                    info_dict["areas"].append(x)
            if info_dict["resting"] == 0:
                info_dict["counter"] = 0
                info_dict["resting"] = 0
                health.after(100, slime_move_ready)
            if info_dict["resting"] > 0:
                info_dict["counter"] = 0
                if info_dict["player"] < 50:
                    info_dict["player"] = info_dict["player"] + random.randint(1,6)
                if info_dict["mana"] < 10:
                    info_dict["mana"] = info_dict["mana"] + random.randint(1,3)
                if info_dict["mana"] > 10:
                    info_dict["mana"] = 10
                if info_dict["player"] > 50:
                    info_dict["player"] = 50
                info_dict["resting"] = info_dict["resting"] - 1
                health.after(100, slime_move)

        def slime_move_ready():
            if info_dict["resting"] > 0:
                health.after(100, slime_move)
            info_dict["counter"] = info_dict["counter"]+1
            if info_dict["counter"] == 19:
                info_dict["counter"] = 0
                health.after(100, slime_move)
            else:
                health.after(100, slime_move_ready)

        health = Label(master, text="")
        health.after(0, slime_move)

        self.Rest = Button(frame, text="rest",command=self.rest)
        self.Rest.pack(side=LEFT)

    def rest(self):
        info_dict["resting"] = 5

root = Tk()

d = App(root)

root.wait_window()

when you click the rest button it is supposed to make 5 really fast prints then return to normal but instead it begins to speed up every time you click rest

Recommended Answers

All 5 Replies

Why you have nested definition inside __init__?

If I can read the 166 lines of code correctly, what happens when "resting" == 1

info_dict["resting"] = info_dict["resting"] - 1
## it now equals zero, so the following would never execute
        def slime_move_ready():
            ## also add a print statement to see if this is executing
            print 'info_dict["resting"]', info_dict["resting"]
            if info_dict["resting"] > 0:
                health.after(100, slime_move)

Also, use a tuple or another dictionary to replace all of the if() statements.

if info_dict["slime_spot"] == 1:
                data = "east","east"
            if info_dict["slime_spot"] == 2:
                data = "west","south"
            if info_dict["slime_spot"] == 3:
                data = "east","south"
##   etc, etc.
##   becomes
info_tuple = ( ("", ""), ("east","east"), ("west","south"), ("east","south"))
info_dict = {"slime_spot":2 }    ## test it
num = info_dict["slime_spot"]
data = info_tuple[num]   ## or info_tuple[info_dict["slime_spot"]]
print data

that's just the way I've always done it...mainly because I occasionally redefine the same definition such as

def forward():
    print "run away"

might be changed to

def forward():
    info_dict["area"] = info_dict["area"] + 1

and to Woeee that snip-it is incase you click rest before the slime is ready to move

I figured it out...apparently if you click rest the

def slime_move_ready():
            if info_dict["resting"] > 0:
                health.after(100, slime_move)
            info_dict["counter"] = info_dict["counter"]+1
            if info_dict["counter"] == 19:
                info_dict["counter"] = 0
                health.after(100, slime_move)
            else:
                health.after(100, slime_move_ready)

command began to run multiple times. By changing that to

def slime_move_ready():
                    info_dict["counter"] = info_dict["counter"]+1
                    if info_dict["resting"] > 0:
                        health.after(100, slime_move)
                    elif info_dict["counter"] == 19:
                        info_dict["counter"] = 0
                        health.after(100, slime_move)
                    else:
                        health.after(100, slime_move_ready)

the code will no longer repeat on resting

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.