-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrocket_qt.py
105 lines (79 loc) · 3.62 KB
/
rocket_qt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
""" Run the rocket game on Qt.
"""
import time
import math
from qtpy import QtWidgets, QtCore, QtGui
from rocket import BaseRocketGame
REL_SIZE = 2.5
class QtRocketGame(BaseRocketGame, QtWidgets.QWidget):
""" Rocket game with Qt providing a drawing canvas and user input.
"""
def __init__(self):
if not hasattr(QtWidgets, '_app'):
QtWidgets._app = QtWidgets.QApplication([])
QtWidgets.QWidget.__init__(self, None)
BaseRocketGame.__init__(self)
self.setWindowTitle("Rocket, written in Rust, compiled to WASM, running in Python, with Qt")
self.resize(900, 700)
self._lasttime = time.time()
self._highscore = 0
def run(self):
self.show()
QtWidgets.qApp.exec_()
def paintEvent(self, event):
self._painter = QtGui.QPainter()
self._painter.begin(self)
self._painter.setRenderHint(QtGui.QPainter.Antialiasing)
progress = time.time() - self._lasttime
self._lasttime = time.time()
self.game.exports.update(progress)
self.game.exports.draw()
self._painter.end()
self.update() # Request a new paint event
## Events going into the WASM module
def resizeEvent(self, event):
self.game.exports.resize(self.width(), self.height())
def keyPressEvent(self, event):
self._toggleKey(event, True)
def keyReleaseEvent(self, event):
self._toggleKey(event, False)
def _toggleKey(self, event, b):
if event.key() == QtCore.Qt.Key_Space:
self.game.exports.toggle_shoot(b)
elif event.key() == QtCore.Qt.Key_Left:
self.game.exports.toggle_turn_left(b)
elif event.key() == QtCore.Qt.Key_Right:
self.game.exports.toggle_turn_right(b)
elif event.key() == QtCore.Qt.Key_Up:
self.game.exports.toggle_boost(b)
## Events generated by WASM module
def wasm_clear_screen(self) -> None: # [] -> []
pass # not needed, we start with a blanc canvas each iteration
def wasm_draw_bullet(self, x: float, y: float) -> None: # [(0, 'f64'), (1, 'f64')] -> []
self._painter.setBrush(QtGui.QColor('#0f0'))
self._painter.drawEllipse(x, y, REL_SIZE*3, REL_SIZE*3)
def wasm_draw_enemy(self, x: float, y: float) -> None: # [(0, 'f64'), (1, 'f64')] -> []
self._painter.setBrush(QtGui.QColor('#ff0'))
self._painter.drawEllipse(x, y, REL_SIZE*14, REL_SIZE*14)
def wasm_draw_particle(self, x: float, y: float, a: float) -> None: # [(0, 'f64'), (1, 'f64'), (2, 'f64')] -> []
self._painter.setBrush(QtGui.QColor('#f04'))
self._painter.drawEllipse(x, y, REL_SIZE, REL_SIZE)
def wasm_draw_player(self, x: float, y: float, a: float) -> None: # [(0, 'f64'), (1, 'f64'), (2, 'f64')] -> []
p = QtGui.QPainterPath()
self._painter.save()
self._painter.translate(x, y)
self._painter.rotate(a*180/math.pi-90)
self._painter.translate(-x, -y)
p.moveTo(x, y + 12*REL_SIZE)
p.lineTo(x - 6*REL_SIZE, y - 6*REL_SIZE)
p.lineTo(x + 6*REL_SIZE, y - 6*REL_SIZE)
p.lineTo(x, y + 12*REL_SIZE)
self._painter.fillPath(p, QtGui.QBrush(QtGui.QColor("#00f")))
self._painter.restore()
def wasm_draw_score(self, score: float) -> None: # env.draw_score: [(0, 'f64')] -> []
score = int(score)
self._highscore = max(self._highscore, score)
self._painter.drawText(0, 20, f'Score: {score}, HighScore: {self._highscore}')
if __name__ == '__main__':
game = QtRocketGame()
game.run()