Przeglądaj źródła

lifegame better render

Bastien Sevajol 8 lat temu
rodzic
commit
a5e35ffd6b

+ 29 - 12
sandbox/life_game/gui.py Wyświetl plik

1
 import cocos
1
 import cocos
2
+from cocos.actions import ScaleBy, Repeat, Reverse, RotateBy
3
+from cocos.director import director
2
 from cocos.layer import ScrollableLayer, Layer
4
 from cocos.layer import ScrollableLayer, Layer
3
 from cocos.sprite import Sprite
5
 from cocos.sprite import Sprite
4
 from pyglet.window import key as wkey
6
 from pyglet.window import key as wkey
7
+from random import randint
5
 
8
 
6
 from sandbox.life_game.simulation import CellDieEvent, Cell
9
 from sandbox.life_game.simulation import CellDieEvent, Cell
7
 from sandbox.life_game.simulation import CellBornEvent
10
 from sandbox.life_game.simulation import CellBornEvent
8
 from synergine2.gui import Gui
11
 from synergine2.gui import Gui
9
-from synergine2.simulation import Event
10
 from synergine2.terminals import TerminalPackage
12
 from synergine2.terminals import TerminalPackage
11
 from synergine2.terminals import Terminal
13
 from synergine2.terminals import Terminal
12
 
14
 
15
+cell_scale = ScaleBy(1.1, duration=0.25)
16
+cell_rotate = RotateBy(360, duration=30)
17
+
13
 
18
 
14
 class Cells(Layer):
19
 class Cells(Layer):
15
     def __init__(self):
20
     def __init__(self):
18
 
23
 
19
     def born(self, grid_position):
24
     def born(self, grid_position):
20
         cell = Sprite('resources/cells_l.png')
25
         cell = Sprite('resources/cells_l.png')
21
-        cell.position = grid_position[0] * 30 + 300, grid_position[1] * 30 + 300
26
+        cell.position = grid_position[0] * 30, grid_position[1] * 30
22
         cell.scale = 0.50
27
         cell.scale = 0.50
28
+        cell.do(Repeat(cell_scale + Reverse(cell_scale)))
29
+        cell.do(Repeat(cell_rotate + Reverse(cell_rotate)))
23
         self.cells[grid_position] = cell
30
         self.cells[grid_position] = cell
24
         self.add(cell)
31
         self.add(cell)
25
 
32
 
28
         del self.cells[grid_position]
35
         del self.cells[grid_position]
29
 
36
 
30
 
37
 
31
-class MainScene(ScrollableLayer):
38
+class MainLayer(ScrollableLayer):
32
     is_event_handler = True
39
     is_event_handler = True
33
 
40
 
34
     def __init__(self):
41
     def __init__(self):
36
 
43
 
37
         self.scroll_step = 20
44
         self.scroll_step = 20
38
 
45
 
39
-        self.sprite = Sprite('resources/banner-1711735_640.jpg')
40
-        self.sprite.position = 320, 240
41
-        self.sprite.opacity = 70
42
-        self.add(self.sprite, z=1)
46
+        self.background = Sprite('resources/banner-1711735_640.jpg')
47
+        self.background.position = 0, 0
48
+        self.background.opacity = 70
49
+        self.background.scale = 5
50
+        self.add(self.background, z=1)
43
 
51
 
44
         self.cells = Cells()
52
         self.cells = Cells()
45
         self.add(self.cells)
53
         self.add(self.cells)
46
 
54
 
55
+        self.cross = Sprite('resources/cross31x31.png')
56
+        self.cross.position = 0, 0
57
+        self.cross.opacity = 50
58
+        self.add(self.cross)
59
+
60
+        # Set scene center on center of screen
61
+        window_size = director.get_window_size()
62
+        self.position = window_size[0] // 2, window_size[1] // 2
63
+
47
     def on_key_press(self, key, modifiers):
64
     def on_key_press(self, key, modifiers):
48
         if key == wkey.LEFT:
65
         if key == wkey.LEFT:
49
             self.position = (self.position[0] + self.scroll_step, self.position[1])
66
             self.position = (self.position[0] + self.scroll_step, self.position[1])
66
     ):
83
     ):
67
         super().__init__(terminal, read_queue_interval)
84
         super().__init__(terminal, read_queue_interval)
68
 
85
 
69
-        self.background = MainScene()
70
-        self.main_scene = cocos.scene.Scene(self.background)
86
+        self.main_layer = MainLayer()
87
+        self.main_scene = cocos.scene.Scene(self.main_layer)
71
         self.positions = {}
88
         self.positions = {}
72
 
89
 
73
         self.terminal.register_event_handler(CellDieEvent, self.on_cell_die)
90
         self.terminal.register_event_handler(CellDieEvent, self.on_cell_die)
81
             for subject in package.subjects:
98
             for subject in package.subjects:
82
                 if isinstance(subject, Cell):
99
                 if isinstance(subject, Cell):
83
                     self.positions[subject.id] = subject.position
100
                     self.positions[subject.id] = subject.position
84
-                    self.background.cells.born(subject.position)
101
+                    self.main_layer.cells.born(subject.position)
85
 
102
 
86
     def on_cell_die(self, event: CellDieEvent):
103
     def on_cell_die(self, event: CellDieEvent):
87
-        self.background.cells.die(self.positions[event.subject_id])
104
+        self.main_layer.cells.die(self.positions[event.subject_id])
88
 
105
 
89
     def on_cell_born(self, event: CellBornEvent):
106
     def on_cell_born(self, event: CellBornEvent):
90
         # TODO: La position peut evoluer dans un autre programme
107
         # TODO: La position peut evoluer dans un autre programme
91
         # resoudre cette problematique de données subjects qui évolue
108
         # resoudre cette problematique de données subjects qui évolue
92
         subject = self.terminal.subjects.get(event.subject_id)
109
         subject = self.terminal.subjects.get(event.subject_id)
93
         self.positions[event.subject_id] = subject.position
110
         self.positions[event.subject_id] = subject.position
94
-        self.background.cells.born(subject.position)
111
+        self.main_layer.cells.born(subject.position)

BIN
sandbox/life_game/resources/cross31x31.png Wyświetl plik