Advertisement
Chl_Snt

№18 Homework

Apr 25th, 2023 (edited)
944
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.27 KB | None | 0 0
  1. from PyQt5 import QtCore, QtWidgets
  2. from PyQt5.QtWidgets import *
  3. from PyQt5.QtCore import Qt
  4.  
  5.  
  6. class Gate(QMainWindow):
  7.     def __init__(self):
  8.         super().__init__()
  9.         self.setupUi(self)
  10.  
  11.     def setupUi(self, MainWindow):
  12.         MainWindow.setObjectName("MainWindow")
  13.         MainWindow.resize(320, 320)
  14.         MainWindow.setStyleSheet("image: url(C:/Users/Queen/Downloads/gates.jpg)")
  15.  
  16.         self.retranslateUi(MainWindow)
  17.         QtCore.QMetaObject.connectSlotsByName(MainWindow)
  18.  
  19.     def retranslateUi(self, MainWindow):
  20.         _translate = QtCore.QCoreApplication.translate
  21.         MainWindow.setWindowTitle(_translate("MainWindow", "Ворота"))
  22.  
  23.  
  24. class Ball(object):
  25.     def setupUi(self, MainWindow):
  26.         MainWindow.setObjectName("MainWindow")
  27.         MainWindow.resize(121, 121)
  28.         MainWindow.setStyleSheet("image: url(C:/Users/Queen/Downloads/football.png);")
  29.  
  30.         self.retranslateUi(MainWindow)
  31.         QtCore.QMetaObject.connectSlotsByName(MainWindow)
  32.  
  33.     def retranslateUi(self, MainWindow):
  34.         _translate = QtCore.QCoreApplication.translate
  35.         MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
  36.  
  37.  
  38. class Main(QMainWindow, Ball):
  39.     def __init__(self):
  40.         super().__init__()
  41.         self.setupUi(self)
  42.         self.gate = Gate()
  43.         self.gate.setGeometry(0, 0, 320, 320)
  44.         self.gate.show()
  45.  
  46.     def keyPressEvent(self, e):
  47.         pos = [self.geometry().x(), self.geometry().y()]
  48.         if e.key() == Qt.Key_W:
  49.             self.setGeometry(pos[0], pos[1] - 10, 121, 121)
  50.         if e.key() == Qt.Key_S:
  51.             self.setGeometry(pos[0], pos[1] + 10, 121, 121)
  52.         if e.key() == Qt.Key_A:
  53.             self.setGeometry(pos[0] - 10, pos[1], 121, 121)
  54.         if e.key() == Qt.Key_D:
  55.             self.setGeometry(pos[0] + 10, pos[1], 121, 121)
  56.  
  57.         gate = [self.gate.geometry().x(), self.gate.geometry().y(), self.gate.width(), self.gate.height()]
  58.  
  59.         if gate[0] <= pos[0] <= gate[0] + gate[2]:
  60.             if gate[1] <= pos[1] <= gate[1] + gate[3]:
  61.                 self.close()
  62.                 self.gate.close()
  63.  
  64.  
  65. if __name__ == "__main__":
  66.     import sys
  67.     app = QtWidgets.QApplication(sys.argv)
  68.     g = Main()
  69.     g.show()
  70.     sys.exit(app.exec_())
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement