Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PyQt5.QtCore import Qt, QTimer
- from PyQt5.QtGui import QPainter
- from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QWidget
- from random import randint
- shelf_size = 100
- class drow(QWidget):
- def paintEvent(self, event):
- global shelf_number
- painter = QPainter(self)
- h = self.height()
- w = self.width()
- y = 0
- while y < h:
- y += shelf_size
- shelf_number+=1
- painter.drawLine(0, y, w, y)
- class Box(QLabel):
- pass
- speed = 10
- def move_hero(window, key):
- global speed
- x = window.hero.x()
- y = window.hero.y()
- hero = window.hero
- if key == Qt.Key_Left:
- hero.move(x - speed, y)
- elif key == Qt.Key_Up:
- hero.move(x, y - shelf_size)
- elif key == Qt.Key_Right:
- hero.move(x + speed, y)
- class HeroWindow(QMainWindow):
- def keyPressEvent(self, event):
- hero = self.hero
- key = event.key()
- if key != Qt.Key_Down:
- hero.direction = key
- def make_hero(window):
- hero = Box()
- hero.setFixedSize(100, 100)
- hero.move(int((window.width() - hero.width()) / 2), int((window.height() - hero.height())))
- hero.direction = Qt.Key_Up
- hero.setStyleSheet("background-color: black")
- window.layout().addWidget(hero)
- window.hero = hero
- HeroWindow.hero = hero
- def make_monster(window):
- monst = Box()
- mnst_size = [randint(3, 10) * 10, randint(1, 10) * 10]
- monst.setFixedSize(mnst_size[0], mnst_size[1])
- monst.move(int((randint(3, 5) * shelf_size - monst.width()) / 2),
- int((randint(3, 5) * shelf_size - monst.height())))
- monst.direction = Qt.Key_Up
- monst.setStyleSheet("background-color: red")
- window.layout().addWidget(monst)
- window.monst = monst
- HeroWindow.monst = monst
- root = QApplication([])
- window = HeroWindow()
- window.resize(900, 600)
- window.setStyleSheet("background-color: #FF9E73")
- shelf_number = window.height()//100
- pole = drow()
- window.layout().addWidget(pole)
- make_hero(window)
- for i in range(15):
- make_monster(window)
- timer = QTimer()
- timer.timeout.connect(lambda: move_hero(window, HeroWindow.hero.direction))
- timer.start(266)
- window.show()
- root.exec()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement