-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgame.py
More file actions
290 lines (251 loc) · 11 KB
/
game.py
File metadata and controls
290 lines (251 loc) · 11 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
from PyQt5 import QtWidgets, uic, QtGui, QtCore,QtTest
from PyQt5.QtCore import QTime, QTimer
import menu
import sys
import psycopg2
from datetime import datetime
class Game(QtWidgets.QDialog):
def __init__(self, name, level, count_t, select_value):
self.settime = select_value
self.name = name
self.level = level
self.count_t = count_t
self.count_s = '00:00:00'
self.count = int(self.settime)
super(Game, self).__init__()
uic.loadUi('ui/game_screen.ui', self)
# when click beck button call func. back
self.quitButton.clicked.connect(self.back)
self.exitButton.clicked.connect(self.exit)
self.levelNumber.setText(str(self.level))
# disable window close button
self.setWindowFlag(QtCore.Qt.WindowCloseButtonHint, False)
# game time
self.total_time()
self.gameTime.setText(self.count_s)
self.timer1 = QTimer(self)
self.timer1.timeout.connect(self.total_time)
self.timer1.start(1000)
# when the game begin true and false button are disable
self.pushButton.setEnabled(False)
self.pushButton.toggle()
self.pushButton_2.setEnabled(False)
self.pushButton_2.toggle()
# in game when click true & false button call methode
self.pushButton.clicked.connect(self.true)
self.pushButton_2.clicked.connect(self.false)
self.show()
# ----------QTimer------
self.sleeptime.setText(str(self.count))
self.timer2 = QTimer(self)
self.timer2.timeout.connect(self.timer_TimeOut)
self.timer2.start(1000)
# -----call the game methode
self.load_words()
def total_time(self):
# methode for game time
self.count_t += 1
self.time = self.count_t
self.time = int(self.time)
self.day = self.time // (24 * 3600)
self.time = self.time % (24 * 3600)
self.hour = self.time // 3600
self.time %= 3600
self.minutes = self.time // 60
self.time %= 60
self.seconds = self.time
if self.day != 0:
self.count_s = "%02d:%02d:%02d:%02d" % (
self.day, self.hour, self.minutes, self.seconds)
elif self.day == 0:
self.count_s = "%02d:%02d:%02d" % (
self.hour, self.minutes, self.seconds)
self.gameTime.setText(self.count_s)
def timer_TimeOut(self):
# methode for sleep time
self.count -= 1
if self.count == 0:
# show the english word
self.words.setText(self.word_list[self.index][1])
self.language.setText('English')
# let sleep time stop en wait for click
self.timer2.stop()
self.pushButton.setEnabled(True)
self.pushButton_2.setEnabled(True)
self.sleeptime.setText(str(self.count))
def back(self):
self.level_update() # call methode to level update -A
self.cams = menu.Menu(self.name)
self.cams.show()
self.close()
def exit(self):
self.level_update()
self.close()
def level_update(self): # level and game time update -A
# -------------------------------------------
conn = psycopg2.connect(database="FlashCards",
user="postgres",
password="1234",
host="localhost",
port="5432")
cur = conn.cursor()
cur.execute(f"""
SELECT player_level from PLAYERS where player_name ='{self.name}'
""")
level_x = cur.fetchone()
if level_x[0] < self.level:
cur.execute(
f"UPDATE PLAYERS set player_level = {self.level} where player_name ='{self.name}' ")
cur.execute(
f"UPDATE PLAYERS set player_time = {self.count_t} where player_name ='{self.name}' ")
cur.execute(f"""SELECT player_name,CAST(AVG(level_percent) AS INTEGER) avg_percent
FROM levels
WHERE player_name = '{self.name}'
GROUP BY player_name;""")
my_avg = cur.fetchone()
cur.execute(
f"""UPDATE PLAYERS set player_level = {self.level},average_percent = {my_avg[1]},player_time = {self.count_t}
where player_name ='{self.name}' """)
else:
cur.execute(
f"UPDATE PLAYERS set player_time = {self.count_t} where player_name ='{self.name}' ")
conn.commit()
conn.close()
def true(self): # click True +1 to c_true
# set button disable
self.pushButton.setEnabled(False)
self.pushButton_2.setEnabled(False)
# start sleep time
self.timer2.start(1000)
self.count = int(self.settime)
# update sleep time
self.sleeptime.setText(str(self.count))
self.index += 1
self.c_true += 1
if self.index > 20:
self.c_false -= 1
if self.c_true == 20: # if c_true = 20 level +1 and begin new level
self.percentupdate()
#self.level += 1
self.tebrik()#yeni eklendi
self.c_true = 0
self.c_false = 0
self.point2.setText(str(self.c_false))
self.point1.setText(str(self.c_true))
self.levelNumber.setText(str(self.level))
self.progressBar_ingame.setProperty("value", self.c_true)
self.index = 0
self.word_list = []
if self.level == 251:
self.timer2.stop()
self.level = 1
self.index = 0
self.back()
self.close()
else:
# call methode for begin new level
self.load_words()
else:
# show dutch word
self.words.setText(self.word_list[self.index][0])
self.point1.setText(str(self.c_true))
self.point2.setText(str(self.c_false))
self.language.setText('Nederlands')
# updat progresbar
self.progressBar_ingame.setProperty("value", self.c_true)
def false(self): # click false +1 to c_false and append this word to word_list -A
# set button disable
self.pushButton_2.setEnabled(False)
self.pushButton.setEnabled(False)
self.timer2.start(1000)
self.count = int(self.settime)
self.sleeptime.setText(str(self.count))
# append words to last of list
self.word_list.append(self.word_list[self.index])
if self.index < 20:
self.c_false += 1
self.point2.setText(str(self.c_false))
self.index += 1
self.words.setText(self.word_list[self.index][0])
self.language.setText('Nederlands')
def load_words(self):
self.index = 0
self.levelNumber.setText(str(self.level))
self.word_list = []
# connect to database & load words
conn = psycopg2.connect(database="FlashCards",
user="postgres",
password="1234",
host="localhost",
port="5432")
cur = conn.cursor()
cur.execute(f"""
SELECT dutch_word,english_word from "words" where word_level ={self.level} ORDER BY word_id LIMIT 20;
""")
self.word_list = cur.fetchall() # Select all rows from words table
conn.commit()
conn.close()
self.c_true = 0
self.c_false = 0
self.point1.setText(str(self.c_true))
self.point2.setText(str(self.c_false))
self.words.setText(self.word_list[self.index][0])
self.language.setText('Nederlands')
def percentupdate(self):
conn = psycopg2.connect(database="FlashCards",
user="postgres",
password="1234",
host="localhost",
port="5432")
cur = conn.cursor()
cur.execute(f"""
SELECT player_level from PLAYERS where player_name ='{self.name}'
""")
level_x = cur.fetchone()
if level_x[0] == self.level:
# 1 updat levels table -click_count and percentage
# 2 insert new row to levels met level+=1 and nulls(click & percentage)
# 3 update players table player_level and time and AVG_percentage
cur.execute(
f"UPDATE levels set click_count = {self.index} where case when player_name ='{self.name}' and level_id = {self.level} then 0 else 1 end=0; ")
p = int(20/self.index*100)
cur.execute(
f"UPDATE levels set level_percent = {p} where case when player_name ='{self.name}' and level_id = {self.level} then 0 else 1 end=0; ")
self.level += 1
cur.execute(f"INSERT INTO levels (player_name,level_id) \
VALUES ('{self.name}', {self.level} )")
else:
# 1 select click_count,if index < click_count
# 2 update click_count and percentage
# 3 level+=1
cur.execute(f"""
select click_count from levels where case when level_id ={self.level} and player_name ='{self.name}' then 0 else 1 end=0;
""")
click_x = cur.fetchone()
if self.index < click_x[0]:
p = int(20/self.index*100)
cur.execute(
f"""UPDATE LEVELS set click_count = {self.index}, level_percent = {p}
where case when player_name ='{self.name}' and level_id = {self.level} then 0 else 1 end=0; """)
cur.execute(f"""SELECT player_name,CAST(AVG(level_percent) AS INTEGER) avg_percent
FROM levels
WHERE player_name = '{self.name}'
GROUP BY player_name;""")
my_avg = cur.fetchone()
cur.execute(
f"""UPDATE PLAYERS set player_level = {self.level},average_percent = {my_avg[1]},player_time = {self.count_t}
where player_name ='{self.name}' """)
self.level += 1
else:
self.level += 1
conn.commit()
conn.close()
def tebrik(self):
self.pushButton_2.setEnabled(False)
self.pushButton.setEnabled(False)
self.timer2.start(1000)
self.count = int(self.settime)
self.sleeptime.setText(str(self.count))
# append words to last of list
self.point2.setText(str(0))
self.language.setText('Next')