Python Forum
misactivation of functions - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Game Development (https://python-forum.io/forum-11.html)
+--- Thread: misactivation of functions (/thread-16878.html)



misactivation of functions - SheeppOSU - Mar-18-2019

I have this script for my game. The problem is when I click start and character select pops up. It prints "Shoton" twice and then prints "Got to the end" before I even click on anything, besides the start button. The start button launches the functions on lines 45 and 22. Neither of those launch the "CharacterSelection" though. So I don't understand why it does that. Also, another thing is, if it is running through, why doesn't it destroy the button. It destroys the "ShotonLabel" but not the "sgLabel", the background, nor the two buttons. Thankyou for ur time.

from tkinter import *


#Variables
tk = Tk()
money = 0
score = 0
startvariable = 0
ShotonButton = Button()
Shoton = Button()
Something = Button()
findAssist = 0
CharacterVariable = 0
CharacterVariable2 = 0

#Images
BGImage = PhotoImage(file='C:/Users/Chadd/Downloads/Background.gif')
ShotonImage = PhotoImage(file='C:/Users/Chadd/Pictures/Converted/Converted/Official Shoton.gif')
SomethingImage = PhotoImage(file='C:/Users/Chadd/Pictures/Converted/Character 1.gif')

#Classes
def StartUpSprites():
    class StartingSpritesBut():
        class Shoton():
            ShotonButton = Button(tk, text="", command=CharacterSelection(1), bg='red')
            Shoton = ShotonButton
            Shoton.config(image=ShotonImage, width=130, height=200 )
            Shoton.place(relx=.1, rely=.1)
            ShotonLabel = canvas.create_text(145, 310, text='Shoton', fill='red', font=('Times', 30))
        class Something():
            Something = Button(tk, text="", command=CharacterSelection(2), bg='Brown')
            sg = Something
            sg.config(image=SomethingImage, width=130, height=200)
            sg.place(relx=.3, rely=.1)
            sgLabel = canvas.create_text(310, 310, text='PoopHead', fill='Brown', font=('Times', 25))

def StartingSprites():
    print('Got to the end')
            

def StartUpItems():
    class StartingItems():
        pass

def StartUpBG():
    class FightingBG():
        canvas.create_image(0, 0, anchor=NW, image=BGImage)

#Canvas Creator
canvas = Canvas(tk, width=800, height=800, bg='white')
canvas.pack()
StartUpBG()

#Functions
def startscreen():
    canvas.delete('all')
    startbtn.destroy()
    StartUpBG()
    StartUpSprites()
    startvariable = 1

def CharacterSelection(character):
    ShotonButton.destroy()
    Something.destroy()
    canvas.delete('all')
    StartUpBG()
    CharacterVariable = character
    findCharacter(0, character)

def findCharacter(variable2, character):
    if character == 1:
        print("Shoton")
        if variable2 == 1:
            StartingSprites()
        else:
            CharacterSelection2(character)
    elif character == 2:
        print("Something")
        if variable2 == 1:
            StartingSprites()
        else:
            CharacterSelection2(character)

def CharacterSelection2(character):
    Shoton.destroy()
    Something.destroy()
    canvas.delete('all')
    StartUpBG()
    findCharacter(1, character)
    CharacterVariable2 = character
    

#Buttons
startbtn = Button(tk, text="Start Game", command=startscreen, font=('Times', 50), bg='red', foreground='blue')
startbtn.pack()
startbtn.place(relx=.5, rely=.8, anchor="c")

#Canvas Text
GunnerText = canvas.create_text(400, 300, text='The Gunner', fill='red', font=('Times', 100))



RE: misactivation of functions - metulburr - Mar-18-2019

Why are you defining classes in classes in a global function???

Quote:
def StartUpSprites():
    class StartingSpritesBut():
        class Shoton():
            ShotonButton = Button(tk, text="", command=CharacterSelection(1), bg='red')
            Shoton = ShotonButton
            Shoton.config(image=ShotonImage, width=130, height=200 )
            Shoton.place(relx=.1, rely=.1)
            ShotonLabel = canvas.create_text(145, 310, text='Shoton', fill='red', font=('Times', 30))
        class Something():
            Something = Button(tk, text="", command=CharacterSelection(2), bg='Brown')
            sg = Something
            sg.config(image=SomethingImage, width=130, height=200)
            sg.place(relx=.3, rely=.1)
            sgLabel = canvas.create_text(310, 310, text='PoopHead', fill='Brown', font=('Times', 25))



RE: misactivation of functions - SheeppOSU - Mar-18-2019

I thought it would be easiest to have a function start up my sprites and whatnot so and to make it organized and easy I classed them within the function. Is that not very efficient? Is there an easier way?


RE: misactivation of functions - metulburr - Mar-19-2019

I believe you would benefit from reading a class tutorial especially with it used in conjunction of tkinter usage. Trouble following control flow in spaghetti code means it needs to be reorganized.