-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman_app.py
113 lines (99 loc) · 4.14 KB
/
hangman_app.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from tkinter import *
import pymysql
import configparser
config = configparser.ConfigParser()
config.read('config.ini')
host = config['MySQL']['host']
username = config['MySQL']['username']
password = config['MySQL']['password']
database = config['MySQL']['database']
def main(char, wrongAttempts, array, data, guessedWord, level):
w.delete(ALL)
while (wrongAttempts != 0 and "-" in guessedWord):
for index in range(len(data)):
if char == data[index]:
guessedWord[index] = char
if char not in data:
wrongAttempts = wrongAttempts - 1
guesssedWord = ''.join(guessedWord)
drawCount = 7 - wrongAttempts
for index in range(drawCount):
array[index] = 1
w.create_text(250, 50, font=("times roman", 30),
text="HANGMAN", fill="#B0171F")
w.create_text(750, 150, text="wrong attempts remaining : ")
text = Label(master, text=wrongAttempts)
text.place(x=835, y=142)
w.create_text(750, 170, text="guess a character : ")
if(array[0]):
w.create_line(150, 450, 150, 150, fill="#000000", width=4)
w.create_line(300, 150, 150, 150, fill="#000000", width=4)
w.create_line(100, 450, 200, 450, fill="#000000", width=4)
w.create_line(300, 150, 300, 200, fill="#000000", width=4)
w.create_line(150, 400, 175, 450, fill="#000000", width=4)
w.create_line(150, 400, 125, 450, fill="#000000", width=4)
if(array[1]):
img = PhotoImage(file="resources/sadface.png")
w.create_image(268, 192, anchor=NW, image=img)
if(array[2]):
w.create_line(300, 250, 300, 350, fill="#000000", width=4)
if(array[3]):
w.create_line(300, 300, 250, 250, fill="#000000", width=4)
if(array[4]):
w.create_line(300, 300, 350, 250, fill="#000000", width=4)
if(array[5]):
w.create_line(300, 350, 250, 400, fill="#000000", width=4)
if(array[6]):
w.create_line(300, 350, 350, 400, fill="#000000", width=4)
w.create_text(750, 200, text=guessedWord)
textBox = Entry(master)
textBox.place(x=750, y=250)
Button(master, text='enter', command=lambda: main(textBox.get(),
wrongAttempts, array, data, guessedWord, level)).place(x=750, y=300)
if(wrongAttempts == 0):
w.create_text(750, 350, text="the word was : "+str(data))
w.create_text(750, 370, text="you lost")
if "-" not in guessedWord:
w.create_text(750, 350, text="the word was : "+str(data))
w.create_text(750, 370, text="Congratulation!!!! YOU WON!!!!")
Button(master, text='newgame', command=lambda: init(
master, w, level)).place(x=830, y=300)
mainloop()
def init(master, w, level):
conn = pymysql.connect(host=host, user=username,
passwd=password, db=database)
cur = conn.cursor()
if level == 1:
cur.execute("SELECT word FROM easy ORDER BY RAND() LIMIT 1")
data = cur.fetchone()
data = data[0]
elif level == 2:
cur.execute("SELECT word FROM moderate ORDER BY RAND() LIMIT 1")
data = cur.fetchone()
data = data[0]
elif level == 3:
cur.execute("SELECT word FROM difficult ORDER BY RAND() LIMIT 1")
data = cur.fetchone()
data = data[0]
else:
print('that level doesnt exist!!')
guessedWord = []
for letter in data:
guessedWord.append("-")
array = []
for index in range(7):
array.append(0)
main('-', 8, array, data, guessedWord, level)
if __name__ == "__main__":
master = Tk()
w = Canvas(master, width=1000, height=500)
w.pack()
w.create_text(250, 50, font=("times roman", 30),
text="HANGMAN", fill="#B0171F")
Button(master, text='simple', command=lambda: init(
master, w, 1)).place(x=700, y=50)
Button(master, text='moderate', command=lambda: init(
master, w, 2)).place(x=800, y=50)
Button(master, text='difficult', command=lambda: init(
master, w, 3)).place(x=920, y=50)
mainloop()