Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
85
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.QtCore import Qt, QTimer
  2. from PyQt5.QtGui import QPainter
  3. from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QWidget
  4. from random import randint
  5.  
  6. shelf_size = 100
  7.  
  8.  
  9.  
  10. class drow(QWidget):
  11.     def paintEvent(self, event):
  12.         global shelf_number
  13.         painter = QPainter(self)
  14.         h = self.height()
  15.         w = self.width()
  16.         y = 0
  17.         while y < h:
  18.             y += shelf_size
  19.             shelf_number+=1
  20.             painter.drawLine(0, y, w, y)
  21.  
  22.  
  23. class Box(QLabel):
  24.     pass
  25.  
  26.  
  27. speed = 10
  28.  
  29.  
  30. def move_hero(window, key):
  31.     global speed
  32.     x = window.hero.x()
  33.     y = window.hero.y()
  34.     hero = window.hero
  35.     if key == Qt.Key_Left:
  36.         hero.move(x - speed, y)
  37.     elif key == Qt.Key_Up:
  38.         hero.move(x, y - shelf_size)
  39.     elif key == Qt.Key_Right:
  40.         hero.move(x + speed, y)
  41.  
  42.  
  43. class HeroWindow(QMainWindow):
  44.     def keyPressEvent(self, event):
  45.         hero = self.hero
  46.         key = event.key()
  47.         if key != Qt.Key_Down:
  48.             hero.direction = key
  49.  
  50.  
  51. def make_hero(window):
  52.     hero = Box()
  53.     hero.setFixedSize(100, 100)
  54.     hero.move(int((window.width() - hero.width()) / 2), int((window.height() - hero.height())))
  55.     hero.direction = Qt.Key_Up
  56.     hero.setStyleSheet("background-color:  black")
  57.     window.layout().addWidget(hero)
  58.     window.hero = hero
  59.     HeroWindow.hero = hero
  60.  
  61.  
  62. def make_monster(window):
  63.     monst = Box()
  64.     mnst_size = [randint(3, 10) * 10, randint(1, 10) * 10]
  65.     monst.setFixedSize(mnst_size[0], mnst_size[1])
  66.  
  67.     monst.move(int((randint(3, 5) * shelf_size - monst.width()) / 2),
  68.                int((randint(3, 5) * shelf_size - monst.height())))
  69.     monst.direction = Qt.Key_Up
  70.     monst.setStyleSheet("background-color:  red")
  71.     window.layout().addWidget(monst)
  72.     window.monst = monst
  73.     HeroWindow.monst = monst
  74.  
  75.  
  76. root = QApplication([])
  77. window = HeroWindow()
  78. window.resize(900, 600)
  79. window.setStyleSheet("background-color:  #FF9E73")
  80.  
  81. shelf_number = window.height()//100
  82.  
  83. pole = drow()
  84. window.layout().addWidget(pole)
  85.  
  86. make_hero(window)
  87. for i in range(15):
  88.     make_monster(window)
  89.  
  90.  
  91. timer = QTimer()
  92. timer.timeout.connect(lambda: move_hero(window, HeroWindow.hero.direction))
  93. timer.start(266)
  94.  
  95. window.show()
  96. root.exec()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement