-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrocket_ai.py
57 lines (41 loc) · 1.83 KB
/
rocket_ai.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
import os
import time
from ppci import wasm
from rocket_qt import QtRocketGame
# Load the wasm ai modules
filename1 = os.path.join(os.path.dirname(__file__), 'wasm', 'ai1.wat')
ai_data1 = open(filename1, 'rt').read()
ai_module1 = wasm.Module(ai_data1)
#
filename2 = os.path.join(os.path.dirname(__file__), 'wasm', 'ai2.wasm')
ai_data2 = open(filename2, 'rb').read()
ai_module2 = wasm.Module(ai_data2)
class AiRocketGame(QtRocketGame):
def __init__(self, ai_module=ai_module1):
super().__init__()
self.ai = wasm.instantiate(ai_module, self.imports, target='native')
def paintEvent(self, event):
super().paintEvent(event)
self.ai.exports.update()
def wasm_debug(self, a:float, b:float) -> None:
print('debug', a, b)
## Imported methods of the AI module, which are exports to the game module
def wasm_toggle_shoot(self, b:int) -> None:
self.game.exports.toggle_shoot(bool(b))
def wasm_toggle_turn_left(self, b:int) -> None:
self.game.exports.toggle_turn_left(bool(b))
def wasm_toggle_turn_right(self, b:int) -> None:
self.game.exports.toggle_turn_right(bool(b))
## Imported methods of the game module, which are exports to the AI module
def wasm_clear_screen(self) -> None: # [] -> []
super().wasm_clear_screen()
self.ai.exports.clear_screen()
def wasm_draw_enemy(self, x: float, y: float) -> None: # [(0, 'f64'), (1, 'f64')] -> []
super().wasm_draw_enemy(x, y)
self.ai.exports.draw_enemy(x, y)
def wasm_draw_player(self, x: float, y: float, a: float) -> None: # [(0, 'f64'), (1, 'f64'), (2, 'f64')] -> []
super().wasm_draw_player(x, y, a)
self.ai.exports.draw_player(x, y, a)
if __name__ == '__main__':
game = AiRocketGame(ai_module2)
game.run()