forked from SydneyTechnologies/riddleGame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameMechanics.py
More file actions
82 lines (72 loc) · 2.65 KB
/
GameMechanics.py
File metadata and controls
82 lines (72 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import random
import time
from tkinter import Entry, Label, Text
from tkinter.constants import END
from turtle import RawTurtle
from datamuse import datamuse
import pyttsx3
engine = pyttsx3.init()
score = 50
def InitializeRiddleList():
riddle_bank = open("output.txt")
riddle_library = []
for line in riddle_bank:
riddle = line.split("?")
riddle_library.append(riddle)
_init_voice()
return riddle_library
def RandomRiddleItem(riddle_library: list):
index = random.randint(0, len(riddle_library) - 1)
riddle = riddle_library[index]
# after a riddle is picked we remove it from the list
riddle_library.remove(riddle)
riddle_question = riddle[0].replace(".", "", 1)
riddle_answer = riddle[1].replace(",", "", 1)
riddle_answer = riddle_answer.replace(".", "", 1)
riddle_answer = riddle_answer.replace("\n", "", 1)
# here we format the riddle question and the answer and then return a dictionary
hint_dictionary = _generateHint(riddle_answer)
return {"question": riddle_question, "answer": riddle_answer, "shortened_answer": hint_dictionary[0], "hints": hint_dictionary[1]}
def _generateHint(answer: str):
api = datamuse.Datamuse()
# try:
# if "/" or "\\" in answer:
# print('weird symbol in answer')
# except:
# pass
if "/" or "\\" in answer:
print('weird symbol in answer')
x = answer.replace("/", " ", 1)
x = x.replace("\\", " ", 1)
answer = x
split_answer = answer.split()
answer_head = split_answer[len(split_answer) - 1]
hint = api.words(rel_rhy= answer_head, max = 5)
return [answer_head, hint]
def verify_answer(user_input: Entry, shortened_answer: str, pen: RawTurtle, read_aloud:bool):
userInput = user_input.get().lower()
shortenedAnswer = shortened_answer.lower()
result_text = ''
if userInput == shortenedAnswer:
global score
score += 10
user_input.delete(0, END)
# score_label.configure(text="Score: " + str(score))
# time.sleep(2)
result_text = "THAT IS CORRECT"
pen.write(result_text, move=False, align="center")
if score >= 100:
result_text = "CONGRATULATIONS YOU HAVE COMPLETED THE GAME"
pen.write(result_text, move=False, align="center")
else:
result_text = "THAT IS INCORRECT"
pen.write(result_text, move=False, align="center")
if read_aloud:
readAloud(result_text)
def readAloud(text:str):
engine.say(text)
engine.runAndWait()
def _init_voice():
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)
engine.setProperty('rate', 175)