瀏覽代碼

integrate very simple cocos2d terminal

Bastien Sevajol 7 年之前
父節點
當前提交
4937d75fda
共有 4 個文件被更改,包括 89 次插入2 次删除
  1. 3 0
      requirements.txt
  2. 60 0
      sandbox/life_game/gui.py
  3. 24 2
      sandbox/life_game/run.py
  4. 2 0
      synergine2/terminals.py

+ 3 - 0
requirements.txt 查看文件

@@ -1,3 +1,6 @@
1
+cocos2d==0.6.4
1 2
 pkg-resources==0.0.0
2 3
 py==1.4.31
4
+pyglet==1.2.4
3 5
 pytest==3.0.4
6
+six==1.10.0

+ 60 - 0
sandbox/life_game/gui.py 查看文件

@@ -0,0 +1,60 @@
1
+import cocos
2
+import pyglet
3
+from cocos.actions import Repeat, ScaleBy, Reverse
4
+
5
+from sandbox.life_game.simulation import CellDieEvent
6
+from sandbox.life_game.simulation import CellBornEvent
7
+from synergine2.simulation import Event
8
+from synergine2.terminals import TerminalPackage
9
+from synergine2.terminals import Terminal
10
+
11
+
12
+class HelloWorld(cocos.layer.Layer):
13
+    def __init__(self):
14
+        super().__init__()
15
+
16
+        self.label = cocos.text.Label(
17
+            '...',
18
+            font_name='Times New Roman',
19
+            font_size=21,
20
+            anchor_x='center', anchor_y='center'
21
+        )
22
+        self.label.position = 320, 240
23
+        scale = ScaleBy(3, duration=5)
24
+        self.label.do(Repeat(scale + Reverse(scale)))
25
+        self.add(self.label)
26
+
27
+
28
+class Gui(object):
29
+    def __init__(self, terminal: Terminal):
30
+        self.terminal = terminal
31
+        self.terminal.register_event_handler(CellDieEvent, self.on_cell_die)
32
+        self.terminal.register_event_handler(CellBornEvent, self.on_cell_born)
33
+
34
+        cocos.director.director.init()
35
+        self.hello_layer = HelloWorld()
36
+        self.main_scene = cocos.scene.Scene(self.hello_layer)
37
+
38
+        self.born = 0
39
+        self.die = 0
40
+
41
+        pyglet.clock.schedule_interval(lambda *_, **__: self.terminal.read(), 1 / 60.0)
42
+
43
+    def run(self):
44
+        cocos.director.director.run(self.main_scene)
45
+
46
+    def before_received(self, package: TerminalPackage):
47
+        self.born = 0
48
+        self.die = 0
49
+
50
+    def after_received(self, package: TerminalPackage):
51
+        self.hello_layer.label.element.text = 'This cycle: {0} born, {1} dead'.format(
52
+            self.born,
53
+            self.die,
54
+        )
55
+
56
+    def on_cell_die(self, event: Event):
57
+        self.die += 1
58
+
59
+    def on_cell_born(self, event: Event):
60
+        self.born += 1

+ 24 - 2
sandbox/life_game/run.py 查看文件

@@ -1,9 +1,9 @@
1 1
 import collections
2
+
2 3
 from sandbox.life_game.simulation import Cell
3 4
 from sandbox.life_game.simulation import Empty
4 5
 from sandbox.life_game.simulation import CellDieEvent
5 6
 from sandbox.life_game.simulation import CellBornEvent
6
-
7 7
 from sandbox.life_game.utils import get_subjects_from_str_representation
8 8
 from synergine2.core import Core
9 9
 from synergine2.cycle import CycleManager
@@ -62,6 +62,28 @@ class SimplePrintTerminal(Terminal):
62 62
         print()
63 63
 
64 64
 
65
+class CocosTerminal(Terminal):
66
+    subscribed_events = [
67
+        CellDieEvent,
68
+        CellBornEvent,
69
+    ]
70
+
71
+    def __init__(self):
72
+        super().__init__()
73
+        self.subjects = None
74
+        self.gui = None
75
+
76
+    def receive(self, package: TerminalPackage):
77
+        self.gui.before_received(package)
78
+        super().receive(package)
79
+        self.gui.after_received(package)
80
+
81
+    def run(self):
82
+        from sandbox.life_game import gui
83
+        self.gui = gui.Gui(self)
84
+        self.gui.run()
85
+
86
+
65 87
 def main():
66 88
     start_str_representation = """
67 89
         0 0 0 0 0 0 0 0 0 0 0
@@ -86,7 +108,7 @@ def main():
86 108
     core = Core(
87 109
         simulation=simulation,
88 110
         cycle_manager=CycleManager(subjects=subjects),
89
-        terminal_manager=TerminalManager([SimplePrintTerminal()]),
111
+        terminal_manager=TerminalManager([CocosTerminal(), SimplePrintTerminal()]),
90 112
     )
91 113
     core.run()
92 114
 

+ 2 - 0
synergine2/terminals.py 查看文件

@@ -19,6 +19,7 @@ class TerminalPackage(object):
19 19
             add_subjects: [Subject]=None,
20 20
             remove_subjects: [Subject]=None,
21 21
             events: [Event]=None,
22
+            is_cycle: bool=True,
22 23
             *args,
23 24
             **kwargs
24 25
     ):
@@ -26,6 +27,7 @@ class TerminalPackage(object):
26 27
         self.add_subjects = add_subjects or []
27 28
         self.remove_subjects = remove_subjects or []
28 29
         self.events = events or []
30
+        self.is_cycle = is_cycle
29 31
 
30 32
 
31 33
 class Terminal(object):