Python Forum
Problems with this little game - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Problems with this little game (/thread-2108.html)



Problems with this little game - Flooo - Feb-19-2017

Hello! I'm supposed to program a little game for school: two random numbers are shown and you have to multiply them, but you have only 15 seconds. You get 1 point if you succeed, and your score is reset to 0 if the times up or you answer wrong.
I'm supposed to work with object-oriented programming. One problem I have for instance is that I have a defined function inside of another defined function, but I don't know how to make a button activate the inner function. Also, I'm sure there are lots of other mistakes, so I'm already sorry for that but thanks in advance to everyone helping me! :) (sorry for any language mistakes, German python forums were not really helpful...)

from tkinter import *
from random import randint
tkWindow = Tk()
tkWindow.title('Spiel')
tkWindow.geometry('400x350')
picbg=PhotoImage(file="tafel.gif")
canvasbg=Canvas(master=tkWindow)
canvasbg.place(x=0, y=0, width=410, height=510)
canvasbg.create_image(0, 0, image=picbg, anchor='nw')

solution=None
usersolution=None
feedback=None
score=0
timelimitactivated=None
random1= ' '
random2=' '
    



def game():

    
    timeractivated=0
    random1=randint(2,20)
    random2=randint(2,20)
    solution = (random1*random2)
    calc1 = Label(master=canvasbg, text= random1, bg='#4B76CD', font=('Arial', 90))
    calc1.place(x=20, y=90, width=130, height=80)
    calc3 = Label(master=canvasbg, text= zufall2, bg='#4B76CD', font=('Arial', 90))
    calc3.place(x=195, y=90, width=100, height=80)


    def checkUserCalculation():
        usersolution = entrysolution.get()
        if usersolution==solution:
            feedback='Right!'
            feedbackLabel=Label(master=canvasbg, text=feedback, bg='#1FD97F', font=('Arial', 90))
            feedbackLabel.place(x=20, y=90, width=365, height=80)
            score=score+1
            timeractivated=1
            tkWindow.after(3000, game)
            
            
        elif usersolution!=solution:
            feedback='Wrong!'
            feedbackLabel=Label(master=canvasbg, text=feedback, bg='#D91F1F', font=('Arial', 90))
            feedbackLabel.place(x=20, y=90, width=365, height=80)
            score=0
            timeractivated=1
            tkWindow.after(3000, game)

        if timeractivated!=1:
            tkWindow.after(15000, timesUp)

        
    def timesUp():
         feedback='Too slow!'
         feedbackLabel=Label(master=canvasbg, text=feedback, bg='#D91F1F', font=('Arial', 90))
         feedbackLabel.place(x=20, y=90, width=365, height=80)
         score=0
         tkWindow.after(3000, game)
 

   
tkWindow.after(0, game)

    
start = Button(master=canvasbg, text='Start!', command=game)
start.place(x=130, y=250)
heading = Label(master=canvasbg, text='Speed-Calculating', fg='#A9282D', bg= '#4F9F4B', font=('Arial', 42))
heading.place(x=40, y=5, width=320, height=50)

calc2 = Label(master=canvasbg, text= 'x', bg='#4B76CD', font=('Arial', 90))
calc2.place(x=145, y=90, width=50, height=80)

calc4 = Label(master=canvasbg, text= '=', bg='#4B76CD', font=('Arial', 90))
calc4.place(x=295, y=90, width=90, height=80)
highscore= Label(master=canvasbg, text='Score:', bg='#F4E800')
highscore.place(x=20, y=250, width=70, height=25)
scoreLabel=Label(master=canvasbg, text=score, bg='#F4E800')
scoreLabel.place(x=90, y=250, height=25)
buttonok = Button(master=canvasbg, text='Okay', command = checkUserCalculation)
buttonok.place(x=300, y=250)
entrysolution = Entry(master=canvasbg)
entrysolution.place(x=230, y=250, width=70)

 


tkWindow.mainloop()



RE: Problems with this little game - nilamo - Feb-20-2017

(Feb-19-2017, 06:17 PM)Flooo Wrote: I'm supposed to work with object-oriented programming. One problem I have for instance is that I have a defined function inside of another defined function...

The first step would be to use a class, and define methods for that class, instead of nested functions.  That is, after all, your task as you've described it.  Here's some docs about classes.


RE: Problems with this little game - Ofnuts - Feb-20-2017

A simple example of how classes, windows, and their callbacks work together: http://www.python-forum.org/viewtopic.php?f=11&t=20304