瀏覽代碼

engulf: grass density

Bastien Sevajol 9 年之前
父節點
當前提交
c422c0b848
共有 6 個文件被更改,包括 39 次插入15 次删除
  1. 7 2
      sandbox/engulf/gui.py
  2. 二進制
      sandbox/engulf/resources/grass.png
  3. 9 9
      sandbox/engulf/run.py
  4. 17 0
      sandbox/engulf/subject.py
  5. 1 4
      synergine2/xyz.py
  6. 5 0
      synergine2/xyz_utils.py

+ 7 - 2
sandbox/engulf/gui.py 查看文件

27
         super().__init__(*args, **kwargs)
27
         super().__init__(*args, **kwargs)
28
         self.cells = {}
28
         self.cells = {}
29
 
29
 
30
-    def born(self, grid_position):
30
+    def born(self, grid_position, opacity=100):
31
         grass = Sprite('resources/grass.png')
31
         grass = Sprite('resources/grass.png')
32
+        grass.rotation = randint(0, 360)
33
+        grass.opacity = opacity
32
         self.grid_manager.scale_sprite(grass)
34
         self.grid_manager.scale_sprite(grass)
33
         self.grid_manager.position_sprite(grass, grid_position)
35
         self.grid_manager.position_sprite(grass, grid_position)
34
         self.cells[grid_position] = grass
36
         self.cells[grid_position] = grass
65
                     self.main_layer.cells.born(subject.position)
67
                     self.main_layer.cells.born(subject.position)
66
                 if isinstance(subject, Grass):
68
                 if isinstance(subject, Grass):
67
                     self.positions[subject.id] = subject.position
69
                     self.positions[subject.id] = subject.position
68
-                    self.main_layer.grasses.born(subject.position)
70
+                    self.main_layer.grasses.born(
71
+                        subject.position,
72
+                        subject.density,
73
+                    )

二進制
sandbox/engulf/resources/grass.png 查看文件


+ 9 - 9
sandbox/engulf/run.py 查看文件

18
       * alone/not alone: - be alone + not alone
18
       * alone/not alone: - be alone + not alone
19
 
19
 
20
 """
20
 """
21
-from random import randint
21
+from random import randint, seed
22
 
22
 
23
 from sandbox.engulf.subject import Cell, Grass, COLLECTION_GRASS
23
 from sandbox.engulf.subject import Cell, Grass, COLLECTION_GRASS
24
 from synergine2.core import Core
24
 from synergine2.core import Core
26
 from synergine2.terminals import TerminalManager, Terminal, TerminalPackage
26
 from synergine2.terminals import TerminalManager, Terminal, TerminalPackage
27
 from synergine2.xyz import Simulation
27
 from synergine2.xyz import Simulation
28
 from sandbox.engulf.simulation import EngulfSubjects
28
 from sandbox.engulf.simulation import EngulfSubjects
29
-from synergine2.xyz_utils import get_around_positions_of
29
+from synergine2.xyz_utils import get_around_positions_of, get_distance_between_points
30
 
30
 
31
 
31
 
32
 class Engulf(Simulation):
32
 class Engulf(Simulation):
98
             grasses.append(grass)
98
             grasses.append(grass)
99
             subjects.append(grass)
99
             subjects.append(grass)
100
 
100
 
101
-    for grass in subjects.simulation.collections[COLLECTION_GRASS][:]:
102
-        a = 1
101
+    for grass in grasses:
103
         for around in get_around_positions_of(grass.position, distance=density):
102
         for around in get_around_positions_of(grass.position, distance=density):
104
             if around not in subjects.grass_xyz:
103
             if around not in subjects.grass_xyz:
105
-                grass = Grass(
104
+                new_grass = Grass(
106
                     simulation=subjects.simulation,
105
                     simulation=subjects.simulation,
107
                     position=around,
106
                     position=around,
108
                 )
107
                 )
109
-                grasses.append(grass)
110
-                subjects.append(grass)
111
-                # TODO: valeur nutritive regressive avec eloignement
108
+                distance = get_distance_between_points(around, grass.position)
109
+                new_grass.density = 100 - round((distance * 100) / 7)
110
+                subjects.append(new_grass)
112
 
111
 
113
 
112
 
114
 def main():
113
 def main():
114
+    seed(0)
115
     simulation = Engulf()
115
     simulation = Engulf()
116
     subjects = EngulfSubjects(simulation=simulation)
116
     subjects = EngulfSubjects(simulation=simulation)
117
     fill_with_random_cells(
117
     fill_with_random_cells(
122
     )
122
     )
123
     fill_with_random_grass(
123
     fill_with_random_grass(
124
         subjects,
124
         subjects,
125
-        15,
125
+        5,
126
         (-34, -34, 0),
126
         (-34, -34, 0),
127
         (34, 34, 0),
127
         (34, 34, 0),
128
     )
128
     )

+ 17 - 0
sandbox/engulf/subject.py 查看文件

20
         COLLECTION_EATABLE,
20
         COLLECTION_EATABLE,
21
         COLLECTION_GRASS,
21
         COLLECTION_GRASS,
22
     ]
22
     ]
23
+
24
+    def __init__(self, *args, **kwargs):
25
+        super().__init__(*args, **kwargs)
26
+        self._density = 100.0
27
+
28
+    @property
29
+    def density(self) -> float:
30
+        return self._density
31
+
32
+    @density.setter
33
+    def density(self, value: float) -> None:
34
+        if value > 100:
35
+            self._density = 100
36
+        elif value < 0:
37
+            self._density = 0
38
+        else:
39
+            self._density = value

+ 1 - 4
synergine2/xyz.py 查看文件

4
 
4
 
5
 from synergine2.simulation import SubjectMechanism, Subjects, Subject
5
 from synergine2.simulation import SubjectMechanism, Subjects, Subject
6
 from synergine2.simulation import Simulation as BaseSimulation
6
 from synergine2.simulation import Simulation as BaseSimulation
7
+from synergine2.xyz_utils import get_distance_between_points
7
 
8
 
8
 
9
 
9
 """
10
 """
25
 COLLECTION_XYZ = 'COLLECTION_XYZ'
26
 COLLECTION_XYZ = 'COLLECTION_XYZ'
26
 
27
 
27
 
28
 
28
-def get_distance_between_points(a: tuple, b: tuple) -> float:
29
-    return abs(sqrt((b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2))
30
-
31
-
32
 def get_degree_from_north(a, b):
29
 def get_degree_from_north(a, b):
33
     if a == b:
30
     if a == b:
34
         return 0
31
         return 0

+ 5 - 0
synergine2/xyz_utils.py 查看文件

1
+from math import sqrt
1
 import collections
2
 import collections
2
 
3
 
3
 
4
 
198
         positions.remove(position)
199
         positions.remove(position)
199
 
200
 
200
     return positions
201
     return positions
202
+
203
+
204
+def get_distance_between_points(a: tuple, b: tuple) -> float:
205
+    return abs(sqrt((b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2))