NMG11

Untitled

Sep 21st, 2020
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.25 KB | None | 0 0
  1. import sys
  2. from PyQt5.QtCore import QSize, Qt, QTime, QTimer
  3. from PyQt5.QtWidgets import (QWidget, QPushButton,
  4. QHBoxLayout, QVBoxLayout, QApplication,
  5. QLabel, QLineEdit, QMessageBox, QDesktopWidget,
  6. QMainWindow, QAction, qApp, QGridLayout, QDialog, QToolButton, QCheckBox, QComboBox,
  7. QSlider)
  8. from PyQt5.QtGui import QFont, QIcon, QPixmap, QPainter, QImage
  9. from PyQt5.uic.properties import QtCore
  10. import random
  11.  
  12. identifikator_dict = {}
  13. record_dict = {}
  14. min_rand_number = -10
  15. max_rand_number = 10
  16.  
  17. # main class
  18. class Example(QWidget):
  19. def __init__(self):
  20. super().__init__()
  21. self.initUI()
  22.  
  23. def initUI(self):
  24. # VARIABLE
  25. x = open('user list.txt')
  26. for line in x:
  27. user = line.strip().split(':')
  28. identifikator_dict[ user[ 0 ] ] = user[ 1 ]
  29. x.close()
  30. x = open('record list.txt')
  31. for line in x:
  32. user = line.strip().split(':')
  33. record_dict[ user[ 0 ] ] = user[ 1 ]
  34. x.close()
  35. self.username = ''
  36. login_label = QLabel('Login')
  37. self.login_edit = QLineEdit()
  38. self.push_button = QPushButton("LOG IN!")
  39. self.push_button_regisration = QPushButton('REGISTRATION')
  40. password_label = QLabel('Password')
  41. self.password_edit = QLineEdit()
  42. self.password_edit.setEchoMode(QLineEdit.Password)
  43. self.button_user = QPushButton('all users')
  44. self.button_record = QPushButton('Records')
  45. # true image
  46. image_label = QLabel()
  47. image = QPixmap('1.jpg')
  48. image_label.setPixmap(image)
  49. image_label.heightForWidth(20)
  50.  
  51. # stile
  52. stile_font = QFont('SanSerif', 12)
  53. login_label.setFont(stile_font)
  54. self.push_button.setFont(stile_font)
  55. password_label.setFont(stile_font)
  56.  
  57. # test image
  58. self.button = QPushButton(self)
  59. self.button.setIconSize(QSize(200, 200))
  60. self.button.setGeometry(0, 0, 200, 200)
  61. self.button.setIcon(QIcon(QPixmap("exit.png")))
  62.  
  63. # LOGIN elements
  64. horizontal_layout_login = QVBoxLayout()
  65. horizontal_layout_login.addStretch(1)
  66. horizontal_layout_login.addWidget(login_label)
  67. horizontal_layout_login.addWidget(self.login_edit)
  68. horizontal_layout_login.addStretch(1)
  69. # PASSWORD elements
  70. horizontal_layout_password = QVBoxLayout()
  71. horizontal_layout_password.addStretch(1)
  72. horizontal_layout_password.addWidget(password_label)
  73. horizontal_layout_password.addWidget(self.password_edit)
  74. horizontal_layout_password.addStretch(1)
  75. # BUTTON elements
  76. horizontal_layout_button = QHBoxLayout()
  77. horizontal_layout_button.addWidget(self.push_button)
  78. horizontal_layout_button.addWidget(self.button_user)
  79. horizontal_layout_button.addWidget(self.button_record)
  80. horizontal_layout_button.addWidget(self.push_button_regisration)
  81. # VERTICAL LAYOUT elements
  82. vertical_layout = QVBoxLayout()
  83. vertical_layout.addWidget(self.button)
  84. vertical_layout.addLayout(horizontal_layout_login)
  85. vertical_layout.addLayout(horizontal_layout_password)
  86. vertical_layout.addLayout(horizontal_layout_button)
  87. # HORIZONTAL LAYOUT MAIN elements
  88. horizontal_layout_main = QHBoxLayout()
  89. horizontal_layout_main.addStretch(1)
  90. horizontal_layout_main.addLayout(vertical_layout)
  91. horizontal_layout_main.addStretch(1)
  92. self.setLayout(horizontal_layout_main)
  93. # LOGIC_VAR
  94. self.push_button.pressed.connect(self.login)
  95. self.push_button_regisration.pressed.connect(self.registration)
  96. self.button_user.pressed.connect(self.show_user)
  97. self.button_record.pressed.connect(self.record)
  98. # show
  99. self.setWindowTitle('MyProgramm PyQt')
  100. self.show()
  101.  
  102. # LOGIC_DEF
  103. def login(self):
  104. msb = QMessageBox()
  105. msb.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
  106. if self.login_edit.text() in identifikator_dict:
  107. if identifikator_dict[ self.login_edit.text() ] == self.password_edit.text():
  108. username = self.login_edit.text()
  109. start = StartGame(self, username)
  110. else:
  111. msb.setDetailedText("login or password is not valid")
  112. msb.setText('you a NOT TRUE')
  113. msb.exec()
  114. else:
  115. msb.setDetailedText("login or password is not valid")
  116. msb.setText('you a NOT TRUE')
  117. msb.exec()
  118.  
  119. def record(self):
  120. self.rec = record(self)
  121. self.rec.show()
  122.  
  123. def registration(self):
  124. self.reg = registration_user(self)
  125. self.reg.show()
  126.  
  127.  
  128. def show_user(self):
  129. self.ul = user_list(self)
  130.  
  131. class record(QWidget):
  132. def __init__(self, parent=None):
  133. super().__init__(parent, Qt.Window)
  134. self.init_record()
  135. def init_record(self):
  136. grid_user = QGridLayout()
  137. numb = 0
  138. for i in record_dict:
  139. grid_user.addWidget(QLabel(i), numb, 0)
  140. grid_user.addWidget(QLabel(record_dict[ i ]), numb, 1)
  141. numb += 1
  142. self.setLayout(grid_user)
  143. self.show()
  144.  
  145. class user_list(QWidget):
  146. def __init__(self, parent=None):
  147. super().__init__(parent, Qt.Window)
  148. self.init_user_list()
  149. def init_user_list(self):
  150. grid_user = QGridLayout()
  151. numb = 0
  152. for i in identifikator_dict:
  153. grid_user.addWidget(QLabel(i), numb, 0)
  154. grid_user.addWidget(QLabel(identifikator_dict[ i ]), numb, 1)
  155. numb += 1
  156. self.setLayout(grid_user)
  157. self.show()
  158.  
  159.  
  160. class registration_user(QWidget):
  161. def __init__(self, parent=None, ):
  162. super().__init__(parent, Qt.Window)
  163. self.init_reg()
  164.  
  165. def init_reg(self):
  166. box = QVBoxLayout()
  167. hor_box = QHBoxLayout()
  168. # var
  169. name_label = QLabel('enter your name')
  170. self.name_edit = QLineEdit()
  171. pass_label = QLabel('your password')
  172. self.pass_edit = QLineEdit()
  173. self.regg = QPushButton('Registration')
  174.  
  175. # build
  176. box.addWidget(name_label)
  177. box.addWidget(self.name_edit)
  178. box.addWidget(pass_label)
  179. box.addWidget(self.pass_edit)
  180. box.addWidget(self.regg)
  181.  
  182. hor_box.addStretch(1)
  183. hor_box.addLayout(box)
  184. hor_box.addStretch(1)
  185. # logic var
  186. self.regg.pressed.connect(self.registr)
  187. #
  188. self.setLayout(hor_box)
  189. self.show()
  190.  
  191. # Logic def
  192. def registr(self):
  193. push = QMessageBox()
  194. if self.name_edit.text() not in identifikator_dict:
  195. identifikator_dict[ self.name_edit.text() ] = self.pass_edit.text()
  196. push.setText('registration complited!')
  197. x = open('user list.txt', 'a')
  198. user = '\n' + self.name_edit.text() + ':' + self.pass_edit.text()
  199. x.write(user)
  200. x.close()
  201. else:
  202. push.setText('this name is already in use')
  203. push.setStandardButtons(QMessageBox.Ok)
  204. push.exec()
  205.  
  206. #enter area game
  207. class StartGame(QWidget):
  208. def __init__(self, parent = None, username = 'player1'):
  209. super().__init__(parent, Qt.Window)
  210. self.username = username
  211. self.init_start_game()
  212.  
  213.  
  214. def init_start_game(self):
  215. # start widget
  216. level_label = QLabel('enter a quantity block')
  217. enemy_label = QLabel('enter your enemy')
  218. self.enemy_current_label = QLabel('easy')
  219. timer_label = QLabel('enter Timer')
  220. self.GAME = QPushButton('Start game!')
  221. #enter area layout
  222. grid_buttons = QGridLayout()
  223. enemy_layout = QHBoxLayout()
  224. timer_layout = QHBoxLayout()
  225. for i in range(3,7):
  226. setattr(self, 'tool_button_%s' %i, QToolButton())
  227. getattr(self,'tool_button_%s'%i).setText(str(str(i) +' x '+str(i)))
  228. getattr(self, 'tool_button_%s' % i).setCheckable(True)
  229.  
  230.  
  231. grid_buttons.addWidget(self.tool_button_3, 0, 0)
  232. grid_buttons.addWidget(self.tool_button_4, 0, 1)
  233. grid_buttons.addWidget(self.tool_button_5, 1, 0)
  234. grid_buttons.addWidget(self.tool_button_6, 1, 1)
  235.  
  236. #enter enemy layout
  237. QTB = QToolButton()
  238. QTB.setText('vs. Friend')
  239. QTB.setCheckable(True)
  240. Combo_box_enemy = QComboBox()
  241. Combo_box_enemy.addItems(['easy', 'medium', 'hard'])
  242. Combo_box_enemy.activated[str].connect(self.onActivated)
  243.  
  244. enemy_layout.addWidget(QTB)
  245. enemy_layout.addWidget(Combo_box_enemy)
  246.  
  247. #enter timer layout
  248. chb = QCheckBox('without timer', self)
  249. chb.toggle()
  250. chb.stateChanged.connect(self.check_timer)
  251.  
  252. slider = QSlider(Qt.Horizontal,self)
  253. slider.setGeometry(30, 40, 100, 30)
  254. slider.setRange(0, 30)
  255. slider.setPageStep(6)
  256. slider.setTickPosition(QSlider.TicksAbove)
  257. slider.valueChanged.connect(self.changeValue)
  258. self.label_timer = QLabel('0')
  259.  
  260. timer_layout.addWidget(chb)
  261. timer_layout.addStretch(1)
  262. timer_layout.addWidget(slider)
  263. timer_layout.addWidget(self.label_timer)
  264.  
  265.  
  266. vertical_lay = QVBoxLayout()
  267. vertical_lay.addStretch(1)
  268. vertical_lay.addWidget(level_label)
  269. vertical_lay.addStretch(1)
  270. vertical_lay.addLayout(grid_buttons)
  271. vertical_lay.addStretch(1)
  272. vertical_lay.addWidget(enemy_label)
  273. vertical_lay.addLayout(enemy_layout)
  274. vertical_lay.addStretch(1)
  275. vertical_lay.addWidget(timer_label)
  276. vertical_lay.addLayout(timer_layout)
  277. vertical_lay.addWidget(self.GAME)
  278.  
  279. self.widget = QWidget()
  280. self.horizontal_lay = QHBoxLayout(self.widget)
  281. self.horizontal_lay.addStretch(1)
  282. self.horizontal_lay.addLayout(vertical_lay)
  283. self.horizontal_lay.addStretch(1)
  284.  
  285. for n in range(3, 7):
  286. getattr(self, 'tool_button_%s' % n).pressed.connect(lambda v=n: self.play_game(v))
  287. n = 4
  288. self.tool_button_3.pressed.connect(lambda v=n: self.play_game(v))
  289. self.GAME.pressed.connect(self.play_game(n))
  290. self.widget.show()
  291.  
  292. def changeValue(self, value):
  293. self.label_timer.setText(str(value))
  294.  
  295. def onActivated(self, text):
  296. self.enemy_current_label.setText(text)
  297. self.enemy_current_label.adjustSize()
  298.  
  299. def check_timer(self, state):
  300. if state == Qt.Checked:
  301. self.setWindowTitle("QCheckBox")
  302. else:
  303. self.setWindowTitle('')
  304.  
  305. def play_game(self, number):
  306. self.widget.hide()
  307.  
  308. #settings mode
  309. self.number = number # number x number area
  310. # var
  311. self.step = 0#step all game
  312. self.timer = QTimer(self) #timer to end step
  313. self.timer.setInterval(10)
  314. self.time_last = QLabel('0')
  315. self.player_1 = QLabel('player 1')
  316. self.player_1.setText(str(self.username))
  317. self.player_2 = QLabel('player 2')
  318. self.steps_over = QLabel('steps over')
  319. self.steps_everywhing = QLabel(str(number ** 2 - self.step))
  320. self.timer_label = QLabel('Time')
  321. self.timer_label.setStyleSheet('QLabel {background-color: white; color: black;}')
  322. self.player_1.setStyleSheet('background-color: white')
  323. self.player_2.setStyleSheet('background-color: white')
  324. self.timer_label.setText('step player 1')
  325.  
  326. self.count_1 = QLabel('0')
  327. self.count_2 = QLabel('0')
  328. self.player_move = 1
  329. self.matrix_play = [ [ 1 for i in range(self.number) ] for j in range(self.number) ]
  330. self.matrix_play2 = [ list(x) for x in zip(*self.matrix_play) ]
  331.  
  332. # layout grid button
  333. self.grid_play = QGridLayout()
  334. for i in range(number):
  335. for j in range(number):
  336. rand_number = str(random.randint(min_rand_number, max_rand_number))
  337. x = 'pushbuttongrid_%s' % i + '_%s' % j
  338. setattr(self, x, QPushButton(rand_number))
  339. getattr(self, x).setEnabled(False)
  340. getattr(self, x).setStyleSheet('QPushButton {background-color: white; color: black;}')
  341. self.grid_play.addWidget(getattr(self, x), i, j)
  342. for i in range(number):
  343. x = 'pushbuttongrid_%s' % i + '_0'
  344. getattr(self, x).setEnabled(True)
  345. if int(getattr(self, x).text()) > 0:
  346. getattr(self, x).setStyleSheet('QPushButton {background-color: green; color: black;}')
  347. else:
  348. getattr(self, x).setStyleSheet('QPushButton {background-color: orange; color: black;}')
  349.  
  350. #logic button
  351. for i in range(number):
  352. for j in range(number):
  353. x = 'pushbuttongrid_%s' % i + '_%s' % j
  354. getattr(self, x).pressed.connect(lambda v=i, w=j: self.player_step(v, w))
  355. #layout game area
  356. horizontal_layout_label = QHBoxLayout()
  357. horizontal_layout_edit = QHBoxLayout()
  358. horizontal_layout_timer = QHBoxLayout()
  359. vertical_layout = QVBoxLayout()
  360. horizontal_layout_main = QHBoxLayout()
  361.  
  362. horizontal_layout_label.addStretch(4)
  363. horizontal_layout_label.addWidget(self.player_1)
  364. horizontal_layout_label.addStretch(1)
  365. horizontal_layout_label.addWidget(self.steps_over)
  366. horizontal_layout_label.addStretch(1)
  367. horizontal_layout_label.addWidget(self.player_2)
  368. horizontal_layout_label.addStretch(4)
  369.  
  370. horizontal_layout_edit.addStretch(1)
  371. horizontal_layout_edit.addWidget(self.count_1)
  372. horizontal_layout_edit.addStretch(1)
  373. horizontal_layout_edit.addWidget(self.steps_everywhing)
  374. horizontal_layout_edit.addStretch(1)
  375. horizontal_layout_edit.addWidget(self.count_2)
  376. horizontal_layout_edit.addStretch(1)
  377.  
  378. horizontal_layout_timer.addStretch(4)
  379. horizontal_layout_timer.addWidget(self.timer_label)
  380. horizontal_layout_timer.addStretch(2)
  381. horizontal_layout_timer.addWidget(self.time_last)
  382. horizontal_layout_timer.addStretch(4)
  383.  
  384. vertical_layout.addStretch(1)
  385. vertical_layout.addLayout(horizontal_layout_label)
  386. vertical_layout.addStretch(1)
  387. vertical_layout.addLayout(horizontal_layout_edit)
  388. vertical_layout.addStretch(1)
  389. vertical_layout.addLayout(self.grid_play)
  390. vertical_layout.addStretch(1)
  391. vertical_layout.addLayout(horizontal_layout_timer)
  392.  
  393. horizontal_layout_main.addStretch(1)
  394. horizontal_layout_main.addLayout(vertical_layout)
  395. horizontal_layout_main.addStretch(1)
  396. self.setLayout(horizontal_layout_main)
  397. self.show()
  398.  
  399. def player_step(self, height=1, width=1):
  400. # delete abd hide button
  401. self.matrix_play[ height ][ width ] = 0
  402. self.matrix_play2[ width ][ height ] = 0
  403. self.steps_everywhing.setText(str(int(self.steps_everywhing.text()) - 1))
  404. x = 'pushbuttongrid_%s' % height + '_%s' % width
  405. getattr(self, x).setEnabled(False)
  406. self.grid_play.addWidget(QLabel(getattr(self, x).text()), height, width)
  407. # getattr(self, x).hide()
  408. getattr(self, x).deleteLater()
  409. #step player1
  410. if self.player_move == 2:
  411. self.count_2.setText(str(int(self.count_2.text()) + int(getattr(self, x).text())))
  412. for i in range(self.number):
  413. y = 'pushbuttongrid_%s' % height + '_%s' % i
  414. try:
  415. getattr(self, y).setEnabled(False)
  416. except RuntimeError:
  417. pass
  418. else:
  419. getattr(self, y).setStyleSheet('QPushButton {background-color: white; color: black;}')
  420. if 1 in self.matrix_play2[ width ]:
  421. for i in range(self.number):
  422. x = 'pushbuttongrid_%s' % i + '_%s' % width
  423. if 1 in self.matrix_play2[ width ]:
  424. try:
  425. getattr(self, x).setEnabled(True)
  426. except RuntimeError:
  427. pass
  428. else:
  429. if int(getattr(self, x).text()) > 0:
  430. getattr(self, x).setStyleSheet('QPushButton {background-color: green; color: black;}')
  431. else:
  432. getattr(self, x).setStyleSheet('QPushButton {background-color: orange; color: black;}')
  433. self.player_1.setStyleSheet('QLabel {background-color: green; color: black;}')
  434. self.player_2.setStyleSheet('QLabel {background-color: white; color: black;}')
  435. self.timer_label.setText('step player 1')
  436. self.player_move = 1
  437. #end_game
  438. else:
  439. self.end_game = QMessageBox()
  440. self.end_game.setStandardButtons(QMessageBox.Ok)
  441. if int(self.count_1.text()) > int(self.count_2.text()):
  442. winner = 'player 1'
  443. self.record()
  444. else:
  445. winner = 'player 2'
  446. winner += ' wingame'
  447. self.end_game.setText(winner)
  448. self.end_game.exec()
  449. # step player 2
  450. else:
  451. self.count_1.setText(str(int(self.count_1.text()) + int(getattr(self, x).text())))
  452. for i in range(self.number):
  453. y = 'pushbuttongrid_%s' % i + '_%s' % width
  454. try:
  455. getattr(self, y).setEnabled(False)
  456. except RuntimeError:
  457. print('this button was deleted!')
  458. else:
  459. getattr(self, y).setStyleSheet('QPushButton {background-color: white; color: black;}')
  460. if 1 in self.matrix_play[ height ]:
  461. for i in range(self.number):
  462.  
  463. y = 'pushbuttongrid_%s' % i + '_%s' % width
  464. x = 'pushbuttongrid_%s' % height + '_%s' % i
  465. try:
  466. getattr(self, x).setEnabled(True)
  467. except RuntimeError:
  468. pass
  469. else:
  470. if int(getattr(self, x).text()) > 0:
  471. getattr(self, x).setStyleSheet('QPushButton {background-color: green; color: black;}')
  472. else:
  473. getattr(self, x).setStyleSheet('QPushButton {background-color: orange; color: black;}')
  474. try:
  475. getattr(self, y).setEnabled(False)
  476. except RuntimeError:
  477. pass
  478. else:
  479. getattr(self, y).setStyleSheet('QPushButton {background-color: white; color: black;}')
  480. self.player_2.setStyleSheet('QLabel {background-color: green; color: black;}')
  481. self.player_1.setStyleSheet('QLabel {background-color: white; color: black;}')
  482. self.timer_label.setText('step player 2')
  483. self.player_move = 2
  484. #end game
  485. else:
  486. self.end_game = QMessageBox()
  487. self.end_game.setStandardButtons(QMessageBox.Ok)
  488. if int(self.count_1.text()) > int(self.count_2.text()):
  489. winner = 'player 1'
  490. self.record()
  491. else:
  492. winner = 'player 2'
  493. winner += ' wingame'
  494. self.end_game.setText(winner)
  495. self.end_game.exec()
  496. self.timer.stop()
  497. self.over = int(1500)
  498. self.timer.setInterval(10)
  499. self.timer.start()
  500. self.timer.timeout.connect(self.time_reload)
  501. self.timer.stop()
  502.  
  503. def time_reload(self):
  504. sec = self.over // 100
  505. milsec = self.over % 100
  506. if sec == 0:
  507. sec == '00'
  508. self.time_last.setText(str(sec) + ':' + str(milsec))
  509. self.over -=1
  510. if self.over == 0:
  511. if self.player_move == 2:
  512. min = min_rand_number
  513. for numb in self.number:
  514. pass
  515. pass
  516. else:
  517. pass
  518.  
  519.  
  520.  
  521. def record(self):
  522. #new record
  523. if self.username not in record_dict.keys():
  524. record_dict[self.username] = self.count_1.text()
  525. with open('record list.txt', 'a') as file:
  526. file.write(str('\n'+self.username + ':' + self.count_1.text()))
  527. #new user record
  528. else:
  529. if int(record_dict[self.username]) < int(self.count_1.text()):
  530. record_dict[self.username] = self.count_1.text()
  531. with open('record list.txt', 'w') as file:
  532. for line in record_dict:
  533. file.write(str('\n' + line + ':' + record_dict[line]))
  534.  
  535. if __name__ == '__main__':
  536. app = QApplication(sys.argv)
  537. ex = Example()
  538. sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment