lundi 13 juin 2016

How to correctly use a timer and how to get it to reset for a new game

I am currently learning Python3 and just finished my first trimester at University and thought I would try making a game while I am still on holidays. I am still very new to programming so there are probably a bunch of problems with my code, but I was hoping someone might be able to help me with one specific problem I am having in relation to using a timer for my game.

The game is a simple anagram game where you have to find as many words as possible within a random sample of letters in a set amount of time. The problem arises when the clock runs down and I start a new game; the second time around, it does not count down in increments of 1 but rather increments of 3. I can't seem to figure out why it is doing this and was hoping someone might be able to point me in the right direction. Here's my code:

#!/usr/bin/env python3

from tkinter import*
from tkinter import ttk
import random


root = Tk()
root.title("Agraman")


frame = ttk.Frame(root, padding='3 3 3 3')
frame.grid(column=0, row=0, sticky=(N, S, E, W))
frame.columnconfigure(0, weight=1)
frame.rowconfigure(0, weight=1)

t = True
count = 0

sec = 10
anagram = []


def start():
    anagram = []
    global sec
    sec = 10
    global t
    t = True
    frame2 = ttk.Frame(root, padding='3 3 3 3')
    frame2.grid(column=0, row=0, sticky=(N, S, E, W))
    frame2.columnconfigure(0, weight=1)
    frame2.rowconfigure(0, weight=1)
    """This stores the shuffled consonants and vowels in part_3 which    is the anagram"""
    vowels = "aeiou"
    consonants = 'bbccddffgghhjjkkllmmnnppqrrssttvvwwxyz'
    part_1 = random.sample(vowels, 4)
    part_2 = random.sample(consonants, 5)
    part_3 = part_1 + part_2
    random.shuffle(part_3)
    anagram_label = ttk.Label(frame2, text=part_3)
    anagram_label.grid(column=0, row=2, sticky=(W, E))
    for letter in part_3:
        anagram.append(letter)
    answer = StringVar()
    guessed_words = []


    def gobutton(*args):
        """This is assigned to the return key and starts the timer the     first time the 'GO' button is pressed
    It also calls possible_words to check user input."""
        global t
        if t is True:
            timer()
            t = False
            possible_words(anagram, guessed_words)
            answer_entry.delete(0, END)
        else:
            possible_words(anagram, guessed_words)
            answer_entry.delete(0, END)

    def score():

        #time_label.configure(state=DISABLED)
        time_label.grid_remove()
        frame3 = ttk.Frame(root, padding='3 3 3 3')
        frame3.grid(column=0, row=0, sticky=(N, S, E, W))
        frame3.columnconfigure(0, weight=1)
        frame3.rowconfigure(0, weight=1)
        score_label = ttk.Label(frame3, text="Score: ")
        score_label.grid(column=0, row=0)
        points2_label = ttk.Label(frame3, text=count)
        points2_label.grid(column=1, row=0)
        new_game = ttk.Button(frame3, text='New Game', command=start)
        new_game.grid(column=0, row=1)
        misc_label2 = ttk.Label(frame3, text='Out of time!')
        misc_label2.grid(column=0, row=2)
        for child in frame3.winfo_children():
            child.grid_configure(padx=5, pady=5)

    def timer():
         """This counts time and ends game after certain point"""
        #time_label.configure(state=NORMAL)
        global sec
        sec -= 1
        time_label['text'] = sec
        time_label.after(1000, timer)
        if sec == 0:

            sec = 10
            #time_label.configure(state=DISABLED)
            score()
            return


    def possible_words(anagram, guessed_words):
        """This checks user input against words in dictionary and against the letters in the anagram
    as well as words already guessed. It also assigns points depending on the length of the word"""
        words_list = open('words.txt')
        anagram2 = anagram
        answer2 = answer.get()

        for l in answer2:
            if l not in anagram2:
                misc_label['text'] = "Not in anagram"
                break
            else:
                pass

            for i in words_list:
                wordz = i.strip()
                word = str(wordz)
                if answer.get() not in guessed_words:
                    if word == answer.get():
                        misc_label['text'] += (answer2 + " ")
                        points = len(answer2)
                        global count
                        count += points
                        points_label['text'] = count
                        guessed_words.append(answer2)
                        break



    """All my labels"""
    title_label = ttk.Label(frame2, text="Agra-Man")
    title_label.grid(column=0, row=1, sticky=N)

    time_label = ttk.Label(frame2, text=sec, justify=CENTER)
    time_label.grid(column=0, row=7)
    time_label.pack()

    count_label = ttk.Label(frame2, text=("Points: "))
    count_label.grid(column=0, row=3, sticky=(W, E))

    points_label = ttk.Label(frame2, text=count, justify=LEFT)
    points_label.grid(column=1, row=3)

    answer_entry = ttk.Entry(frame2, width=7, textvariable=answer)
    answer_entry.grid(column=0, row=4, sticky=(W, E))

    misc_label = ttk.Label(frame2, text="")
    misc_label.grid(column=0, row=6, sticky=(W, E))

    go_button = ttk.Button(frame2, width=7, text="Go", command=(gobutton))
    go_button.grid(column=0, row=5, sticky=(W, E))

    root.bind('<Return>', gobutton)

    answer_entry.focus()
    for child in frame2.winfo_children():
        child.grid_configure(padx=5, pady=5)


start_game = ttk.Button(frame, text='Start Game', command=start)
start_game.grid(column=0, row=3)

welcome_label = ttk.Label(frame, text='AGRAMANnThe LettersnandnNumbers Game', justify=CENTER)
welcome_label.grid(column=0, row=0)


for child in frame.winfo_children():
    child.grid_configure(padx=5, pady=5)


root.mainloop()

Aucun commentaire:

Enregistrer un commentaire