2016-07-05 3 views
0

私は簡単な数学ゲームを作ろうとしています。今、私はインターネット上で研究し、何時間も作業しましたが、このシンプルなゲームを機能させることはできません。どのようにゲームが動作しますボタンを使ってアルゴリズムを再起動する

:ゲームはユーザーに質問をすると、ユーザが入力バーに答えを与える 、ユーザーがボタンをクリックすると、ユーザーは別の質問を受けます。

問題: は、どのように私はそれをループさせることができますので、毎回ユーザーが質問に答える、ループ/アルゴリズムをやり直します。したがって、ボタンをクリックするとアルゴリズムが進むだけです。

追加情報: 私はGUIとしてTkinterを使用しています。そして、ここで私のフローチャートである。 https://s31.postimg.org/pzs2m43kb/The_Math_Game_algorithm.png

私の問題はしばらく< 10ループの下にクラスGamePageです。 Tkinterので

import Tkinter as tk 
import ttk as tkk 
import random 

TITLE_FONT = ("Helvetica", 18, "bold") 

""" Functions """ 


""" GUI """ 
class App(tk.Tk): 

    def __init__(self, *args, **kwargs): 
     tk.Tk.__init__(self, *args, **kwargs) 

     container = tk.Frame(self) 
     container.pack(side="top", fill="both", expand=True) 
     container.grid_rowconfigure(0, weight=1) 
     container.grid_columnconfigure(0, weight=1) 

     self.frames = {} 

     for F in (StartPage, GamePage, ValidationPage): 
      page_name = F.__name__ 
      frame = F(parent=container, controller=self) 
      self.frames[page_name] = frame 
      frame.grid(row=0, column=0, sticky="nsew") 

     self.show_frame("StartPage") 

    def show_frame(self, page_name): 
     frame = self.frames[page_name] 
     frame.tkraise() 

class StartPage(tk.Frame): 

    def __init__(self, parent, controller): 

     tk.Frame.__init__(self, parent) 
     self.controller = controller 

     label = tk.Label(self, text="TheMathGame", font=TITLE_FONT) 
     label.pack(side="top", fill="x", pady=10) 

     labelStart = tk.Label(self, text="Click start to continue",) 
     labelStart.pack(side="top", fill="x", pady=10) 

     button1 = tkk.Button(self, text="Start", command = lambda: controller.show_frame("GamePage")) 
     button1.pack() 

class GamePage(tk.Frame): 

    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     self.count = 0 
     self.price = 0 

     self.label = tk.Label(self, text='Answer the question', font=TITLE_FONT) 
     self.label.pack(side="top", fill="x", pady=10) 




     while self.count < 10: 
      MathQuestion, MathAnswer = self.mathGenerator() 
      self.label2 = tk.Label(self, text=MathQuestion) 
      self.label2.pack(side="top", fill="x", pady=10) 

      userInputField = tk.Entry(self) 
      userInputField.pack() 


      buttonNext = tk.Button(self, text="OK", command=lambda: self.validationManager(MathAnswer, userInputField)) 
      buttonNext.pack() 


    def mathGenerator(self): 
     def answerMath(number1, number2, function): 
      if function == "+": 
       return number1 + number2 
      if function == "-": 
       return number1 - number2 
      if function == "*": 
       return number1 * number2 
      if function == "/": 
       return number1/number2 

     def numberGenerator(): 
      number1 = random.randint(1, 100) 
      number2 = random.randint(1, 100) 
      return (number1, number2) 

     def subtractFunction(math_function): 
      number1, number2 = numberGenerator() 

      while number1 < number2: 
       number1, number2 = numberGenerator() 
      else: 
       question = ("%d %s %d" % (number1, math_function, number2)) 
       answer = answerMath(number1, number2, math_function) 
       return (question), (answer) 

     def divideFunction(math_function): 
      number1, number2 = numberGenerator() 

      while number1 % number2 != 0 or number1 < number2: 
       number1, number2 = numberGenerator() 
      else: 
       question = ("%d %s %d" % (number1, math_function, number2)) 
       answer = answerMath(number1, number2, math_function) 
       return (question), (answer) 

     math_functions = ['+', '-', '*', '/'] 
     math_function = (random.choice(math_functions)) 

     if math_function == '/': 
      (question), (answer) = divideFunction(math_function) 
     elif math_function == '-': 
      (question), (answer) = subtractFunction(math_function) 
     else: 
      number1, number2 = numberGenerator() 
      if math_function == '*': 
       math_function_string = 'x' 
      else: 
       math_function_string = math_function 
      question = ("%d %s %d" % (number1, math_function_string, number2)) 
      answer = answerMath(number1, number2, math_function) 
     return question, answer 

    def validationManager(self, questionAnswer, userAnswer): 
     questionAnswer = questionAnswer 
     userAnswerd = userAnswer.get() 

     if int(userAnswerd) == questionAnswer: 
      return "Correct" 
     else: 
      return "Wrong" 

class ValidationPage(tk.Frame): 

    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     self.labelText = 'Wrong..' 
     self.ValidationLabel = tk.Label(self, text=self.labelText, font=TITLE_FONT) 
     self.ValidationLabel.pack(side="top", fill="x", pady=10) 


if __name__ == "__main__": 

    root = App() 
    root.iconbitmap('Tesla.ico') 
    root.resizable(width=False, height=False) 
    root.geometry('{}x{}'.format(350, 250)) 
    root.title("TheMathGame") 

    photo = tk.PhotoImage(file="monkey.gif") 
    label = tk.Label(root, image=photo) 
    label.pack() 

    root.mainloop() 

答えて

0

大きな問題は、ユーザーの相互作用を必要とするループが終了している間、それを殺すことです。

ここに私の調整があります。

class GamePage(tk.Frame): 
    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     self.count = 0 
     self.price = 0 

     self.label = tk.Label(self, text='Answer the question', font=TITLE_FONT) 
     self.label.pack(side="top", fill="x", pady=10) 

     self.label2 = tk.Label(self) 
     self.label2.pack(side="top", fill="x", pady=10) 

     self.userInputField = tk.Entry(self) 
     self.userInputField.pack() 

     buttonNext = tk.Button(self, text="OK", command=self.validationManager) 
     buttonNext.pack() 

     self.askQuestion() 

    def askQuestion(self): 
     self.userInputField.delete(0, tk.END) 
     MathQuestion, self.MathAnswer = self.mathGenerator() 
     self.label2.config(text = MathQuestion) 

    def validationManager(self): 
     if int(self.userInputField.get()) == self.MathAnswer: 
      self.price += 1 
     self.count += 1 

     print(self.count, self.price) 

     if self.count == 10: 
      print("To the price page") 
     else: 
      self.askQuestion() 

また、ボタンクリックで呼び出された関数から値を返そうとしていましたが、これは実際には機能しません。ボタンを押したときに起こるべきすべてのことを意味する代わりに、代わりにアクションを実行するという機能を持っていることをお勧めします。

何が不完全であるかは、「修正する」、「間違っている」などを表示するために再生する必要があります。しかし、これは何が間違っているかを軽視するべきです。

関連する問題