Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. from PyQt5.QtCore import Qt, QTimer
  2. from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
  3.  
  4.  
  5. class Box(QLabel):
  6. pass
  7.  
  8.  
  9. def make_monster(window):
  10. monst = Box()
  11. monst.setFixedSize(300, 100)
  12. monst.move((window.width()-300)/2, window.height()/2)
  13. monst.setStyleSheet("background-color: brown")
  14. window.layout().addWidget(monst)
  15. HeroWindow.monst = monst
  16.  
  17.  
  18. def make_button(window, x, y):
  19. hero = Box()
  20. hero.setFixedSize(200, 100)
  21. hero.move(x, y)
  22. hero.direction = Qt.Key_Up
  23. hero.setStyleSheet("background-color: black")
  24. window.layout().addWidget(hero)
  25. window.hero = hero
  26. HeroWindow.hero = hero
  27.  
  28.  
  29. def check_colision(window):
  30. hero = window.hero
  31. x_b = hero.x()
  32. y_b = hero.y()
  33. x1_b = hero.x() + hero.width()
  34. y1_b = hero.y() + hero.height()
  35.  
  36. monst = window.monst
  37. x_m = monst.x()
  38. y_m = monst.y()
  39. x1_m = monst.x() + monst.width()
  40. y1_m = monst.y() + monst.height()
  41.  
  42. s1 = (x_b > x_m and x_b < x1_m) or (x1_b > x_m and x1_b < x1_m)
  43. s2 = (y_b > y_m and y_b < y1_m) or (y1_b > y_m and y1_b < y1_m)
  44. s3 = (x_m > x_b and x_m < x1_b) or (x1_m > x_b and x1_m < x1_b)
  45. s4 = (y_m > y_b and y_m < y1_b) or (y1_m > y_b and y1_m < y1_b)
  46.  
  47. if ((s1 and s2) or (s3 and s4)) or ((s1 and s4) or (s3 and s2)):
  48. monst.setStyleSheet("background-color: red")
  49. else:
  50. monst.setStyleSheet("background-color: brown")
  51.  
  52.  
  53. speed = 10
  54. def move_button(window, key):
  55. global speed
  56. x = window.hero.x()
  57. y = window.hero.y()
  58. hero = window.hero
  59. if key == Qt.Key_Left:
  60. hero.move(x - speed, y)
  61. elif key == Qt.Key_Up:
  62. hero.move(x, y - speed)
  63. elif key == Qt.Key_Right:
  64. hero.move(x + speed, y)
  65. elif key == Qt.Key_Down:
  66. hero.move(x, y + speed)
  67. check_colision(window)
  68.  
  69.  
  70. class HeroWindow(QMainWindow):
  71. def keyPressEvent(self, event):
  72. key = event.key()
  73. move_button(window, key)
  74.  
  75.  
  76. root = QApplication([])
  77. window = HeroWindow()
  78. window.resize(900, 600)
  79. window.setStyleSheet("background-color: #FF9E73")
  80.  
  81. make_button(window, 300, 310)
  82. make_monster(window)
  83.  
  84. move_button(window, HeroWindow.hero.direction)
  85.  
  86. window.show()
  87. root.exec()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement