/
main.py
281 lines (221 loc) · 7.88 KB
/
main.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
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
import getopt
import random
import sys
from itertools import cycle
import serial
from PyQt5 import QtCore
from PyQt5.QtCore import pyqtSlot, QTimer
from PyQt5.QtWidgets import (QWidget, QApplication, QMainWindow, QStackedWidget)
import signal
import Chrono
import ScreenGame
import Setup
import Simon
import ui
from ScreenGame import guess, paintButton, eraseAll, win
from gameVar import GameVar
from WebServer import WebServer
import Fils
import StressMode
import Symbole
from Bouton import checkBouton
signal.signal(signal.SIGINT, signal.SIG_DFL)
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setStyleSheet(open('assets/style.qss').read())
self.setWindowTitle("Bombe")
self.game = Game(self)
ui.initUi(self.game)
self.setup = Setup.Setup(self)
self.setupSymbole = Setup.SetupSymbole(self)
self.bios = QWidget()
self.desamorcage = QWidget()
self.win = StressMode.winEscape(self)
self.stackSetup = QStackedWidget()
self.stackSetup.addWidget(self.setup)
self.stackSetup.addWidget(self.setupSymbole)
self.stackSetup.addWidget(self.game)
self.stackSetup.addWidget(self.bios)
self.stackSetup.addWidget(self.win)
self.stackSetup.addWidget(self.desamorcage)
self.bios.setStyleSheet("border-image: url(assets/Images/biosurgence.png) 0 0 0 0 stretch stretch");
self.desamorcage.setStyleSheet("border-image: url(assets/Images/desamorcage.png) 0 0 0 0 stretch stretch");
self.setGeometry(0, 0, 480, 720)
self.setCentralWidget(self.stackSetup)
class Game(QWidget):
def __init__(self, m):
QWidget.__init__(self)
self.gameVar = GameVar() # Contient tout les variables du jeu
self.MW = m
self.on_click
try:
self.initSerial()
except serial.SerialException as e:
print("Erreur connexion Arduino")
self.gameVar.error += str(e)
print(e)
self.ws = WebServer(self.gameVar, self)
self.ws.start()
def checkSensor(self):
gv = self.gameVar
try:
self.ser.write(gv.sendArduino().encode())
line = self.ser.readline()
gv.interprete(line.decode('utf-8').rstrip())
except Exception as e:
self.gameVar.error = str(e)
if not gv.defaite and not gv.pause and not gv.stressMode:
Simon.checkSimon(self)
Fils.checkFils(self)
Symbole.checkSymbole(self)
checkBouton(self)
if gv.stressMode and gv.stressModeStarted:
StressMode.checkStressMode(self)
def erreurGlobal(self, module):
self.gameVar.moduleErr[module] += 1
self.gameVar.nbErreur = sum(self.gameVar.moduleErr)
self.gameVar.sounds["error"].play()
if self.gameVar.nbErreur > self.gameVar.nbErreurMax:
self.defaite()
else:
for i in range(4):
if i < self.gameVar.nbErreur :
self.gameVar.chronoLedErr[i] = 1
else:
self.gameVar.chronoLedErr[i] = 0
if self.gameVar.moduleErr[module] == 1 and module != 5:
Chrono.removeXmMin(self, 3)
elif self.gameVar.moduleErr[module] > 1 and module != 5:
Chrono.removeXmMin(self, 1)
def bip(self):
if self.gameVar.pause:
return
if self.gameVar.stressMode:
self.gameVar.sounds["bip2"].play()
elif self.gameVar.chronoBoutonSon:
self.gameVar.sounds["bip"].play()
Chrono.chrono(self)
def initSerial(self):
self.ser = ""
try:
self.ser = serial.Serial('/dev/ttyUSB0', 19200, timeout=1)
except:
try:
self.ser = serial.Serial('/dev/ttyUSB1', 19200, timeout=1)
except Exception as e:
raise e
print(self.ser.write("initialisation\n".encode()))
line = self.ser.readline()
self.gameVar.serialOK = 1
def start(self):
Fils.initFils(self.gameVar)
Symbole.initSymbole(self.gameVar)
Simon.initSimon(self.gameVar)
ScreenGame.paintButton(self)
self.timerBip = QtCore.QTimer()
self.timerBip.timeout.connect(self.bip)
self.timerBip.start(1000)
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.checkSensor)
self.timer.start(50)
self.timerLumiere = QtCore.QTimer()
self.timerLumiere.timeout.connect(lambda: Simon.lumiereSimon(self.gameVar))
self.timerLumiere.start(500)
@pyqtSlot()
def on_click(self):
print("lala")
btn = self.sender()
guess(self, btn)
def victoire(self):
self.gameVar.pause = 1
self.gameVar.sounds["victoire"].play()
self.timerBip.stop()
self.gameVar.moduleWin = [1, 1, 1, 1, 1]
self.gameVar.lumSimon = [0, 0, 0, 0]
self.gameVar.symboleLum = [0, 0, 0, 0]
self.gameVar.boutonBande = [0, 1, 0]
self.gameVar.chronoLum = 1
self.gameVar.chronoBlink = 0
if self.gameVar.escapeGame:
self.timerStress.stop()
self.gameVar.stressModeStarted = 0
self.MW.stackSetup.setCurrentIndex(4)
self.MW.win.reloadChrono()
def defaite(self):
if self.gameVar.defaite : # defaite() déjà appelé
return
print("defaite")
if self.gameVar.stressModeStarted == 1 :
try :
self.gameVar.sounds["stress"].stop()
except :
pass
self.gameVar.chronoBlink = 1
self.gameVar.chronoLum = 2
self.gameVar.defaite = True
self.timerLumiere.stop()
self.timerBip.stop()
self.gameVar.sounds["defaite"].play()
QTimer().singleShot(14000, self.retourMenu)
QTimer().singleShot(3200, self.callbackDefaite)
self.gameVar.moduleWin = [0, 0, 0, 0, 0]
self.blink = 30
def callbackDefaite(self):
self.gameVar.chronoLum = 0
self.gameVar.chronoBlink = 0
self.gameVar.chronoLedErr = [1,1,1,1]
self.gameVar.simonErreur = 1
self.gameVar.calcErreur = 1
self.gameVar.symboleErreur = 1
self.gameVar.boutonErreur = 1
self.gameVar.filErreur = 1
self.gameVar.lumSimon = [1, 1, 1, 1]
self.gameVar.symboleLum = [1, 1, 1, 1]
self.gameVar.boutonBande = [1, 1, 1]
QTimer().singleShot(50, self.callbackDefaiteBlink)
def callbackDefaiteBlink(self):
self.gameVar.chronoLedErr = [0, 0, 0, 0]
self.gameVar.simonErreur = 0
self.gameVar.calcErreur = 0
self.gameVar.symboleErreur = 0
self.gameVar.boutonErreur = 0
self.gameVar.filErreur = 0
self.gameVar.moduleErr = [1, 1, 1, 1, 1]
self.gameVar.lumSimon = [0, 0, 0, 0]
self.gameVar.symboleLum = [0, 0, 0, 0]
self.gameVar.boutonBande = [0, 0, 0]
self.blink -= 1
if self.blink > 0:
QTimer().singleShot(50, self.callbackDefaite)
def retourMenu(self):
self.gameVar.reset()
self.MW.stackSetup.setCurrentIndex(0)
self.MW.setup.mPrincipal()
def slot_repaint(self):
eraseAll(self)
paintButton(self)
def slot_win_screenGame(self):
win(self)
def exit(self):
self.ws.exit()
if __name__ == "__main__":
fullScreen = False
try:
opts, args = getopt.getopt(sys.argv[1:], "f")
except getopt.GetoptError:
print("Option inconnu")
sys.exit(2)
for opt, arg in opts:
print(opt)
if opt == '-f':
fullScreen = True
app = QApplication.instance()
if not app:
app = QApplication(sys.argv)
m = MainWindow()
if fullScreen :
m.showFullScreen()
else:
m.show()
app.exec_()