3
\$\begingroup\$

I am currently working on a small python project for an undergraduate course in python. The task is to create a Tamagotchi-like program with a GUI. My question is whether my code is Object oriented or not? Here are my classes:

Tamagotchi class

class Tamagotchi(Canvas):

    def __init__(self, radie, x,y):
        super().__init__()
        self.radie = radie
        self.x=x
        self.y=y

        self.createCircle(x, y, radie, fill="black")

    def createCircle(self, x, y, radie, **kwargs):
        circle = self.create_oval(x-radie,y-radie,x+radie,y+radie,**kwargs)
        return circle

    def updateCircle(self,radie, **kwargs):
        self.delete("all")
        self.radie=radie
        self.createCircle(self.x,self.y,self.radie, fill="black")

GUI class

class GUI(Frame):
    def __init__(self):
        super().__init__()
        tamagotchi = Tamagotchi(60,140,125)
        tamagotchi.pack(side=TOP)

        activityList = []

        Style().configure("TButton", padding=(20, 20, 20, 20), font='serif 10')

        sizeLbl = Label(self, text="Storlek: ")
        sizeLbl.grid(row=0, column=3, sticky=N)

        size = Label(self, text = tamagotchi.radie, foreground="red", font=(None, 15))
        size.grid(row=0,column=3, sticky = S)

        sovaButton = Button(self, text="Sova", command = lambda: self.tamagotchiLogic("sova", activityList, tamagotchi, size))
        sovaButton.grid(row=0, column=0)

        tentaButton = Button(self, text="Tenta", command = lambda: self.tamagotchiLogic("tenta", activityList, tamagotchi, size))
        tentaButton.grid(row=0, column=1)

        festaButton = Button(self, text="Festa", command = lambda: self.tamagotchiLogic("festa", activityList, tamagotchi, size))
        festaButton.grid(row=1, column=0)

        pluggaButton = Button(self, text="Plugga", command = lambda: self.tamagotchiLogic("plugga", activityList, tamagotchi, size))
        pluggaButton.grid(row=1, column=1)

        self.centerWindow()

    def centerWindow(self):
        w = 300
        h = 393

        sw = self.master.winfo_screenwidth()
        sh = self.master.winfo_screenheight()

        x = (sw - w) / 2
        y = (sh - h) / 2
        self.master.geometry('%dx%d+%d+%d' % (w, h, x, y))

    def tamagotchiLogic(self, activity, activitylist, tamagotchi, sizeLabel):
        activitylist.append(activity)

        end = len(activitylist)
        actString = ''.join(activitylist[end-3:end])

        print(actString)

        growthCombinations = ["pluggasovatenta", "sovatentafesta"]
        declineCombinations = ["festasovatenta","tentapluggaplugga"]
        boredCombinations = ["festafestafetsa","pluggapluggaplugga","sovasovasova","tentatentatenta"]

        if actString in growthCombinations:
            tamagotchi.updateCircle(tamagotchi.radie*1.05, fill="black")

        elif actString in declineCombinations:
            tamagotchi.updateCircle(tamagotchi.radie*0.95, fill="black")

        elif actString in boredCombinations:
            tamagotchi.updateCircle(tamagotchi.radie*(1/sqrt(2)), fill="black")

        sizeLabel.config(text=round(tamagotchi.radie))

Main

def main():
    root = Tk()

    gui = GUI()
    gui.pack()

    root.mainloop()

if __name__ == '__main__':
    main()
\$\endgroup\$
3
\$\begingroup\$

Your code is built by using classes and consequently by creating objects(instances) of those classes. In addition you are also utilizing the concept of inheritance to associate multiple classes to each other. So yes, your code is OOP.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.