在Python中将计时器实现到Math游戏中

时间:2017-11-30 17:50:28

标签: python

import random
from tkinter import *
import tkinter as tk
import time

scores = []
score = 0

#introduction
def start():
    print("               Welcome to Maths Smash                    \n")
    print("Complete the questions the quickest to get a better score\n")

    username = True #this makes sure the user can only enter characters a-z
    while username:
        name = input("What is your name? ")
        if not name.isalpha():
            print("Please enter letters only")
            username = True
        else:
            print("Ok,", name, "let's begin!")
            main()

#menu
def main():
    choice = None
    while choice !="0":
        print(
        """
        Maths Smash

        0 - Exit
        1 - Easy
        2 - Medium
        3 - Hard
        4 - Extreme
        5 - Dashboard
        """
        )

        choice = input("Choice: ")
        print()

    #exit
        if choice == "0":
            print("Good-bye!")
            quit()
    #easy
        elif choice == "1":
            print("Easy Level")
            easy_level()
    #medium
        elif choice == "2":
            print("Medium Level")
            medium_level()
    #hard
        elif choice == "3":
            print("Hard Level")
            hard_level()
    #extreme
        elif choice == "4":
            print("Extreme Level")
            extreme_level()
    #teacher login
        elif choice == "5":
            print("Dashboard")
            from GUI import dashboard
            dashboard()     
        else:
            print("Sorry but", choice, "isn't a vaild choice.")

def easy_level():
    global score
    #easy level
    for i in range(10):
        operator_sign =""
        answer = 0
        num1 = random.randint(1,5)
        num2 = random.randint(1,5)
        operator = random.randint(1,4)
        #choosing the operator randomly
        if operator == 1:
            operator_sign = " + "
            answer = num1 + num2
        elif operator == 2:
            operator_sign = " - "
            answer = num1 - num2
        elif operator == 3:
            operator_sign = " * "
            answer = num1 * num2
        elif operator == 4:
            operator_sign = " / "
            num1 = random.randint(1,5) * num2
            num2 = random.randint(1,5) 
            answer = num1 // num2
        else:
            print("That is not a vaild entry!\n")
        #outputting the questions along with working out if it's correct        
        question = str(num1) + operator_sign + str(num2) + "\n"
        user_input = int(input(question))
        if user_input == answer:
            score = score + 1
    #total score
    print("You scored:", str(score), "out of 10")

试图在这个游戏中添加计时器一段时间但是每次我进入第一级时它都不会同时使用游戏,因为我会加载第一级而不是问题将弹出,直到计时器完成。想知道是否有人可以帮助我或知道修复此问题,我想这样做,如果用户在一定时间内完成测试,他们会得到额外的10分加入总分。这是我最初想出的:

def timer():
    countdown=120
    while countdown >0:
        time.sleep(1)
        score = score + 10

0 个答案:

没有答案