cellsim 0.0.0__tar.gz → 1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
cellsim-1.0/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2018 The Python Packaging Authority
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
cellsim-1.0/PKG-INFO ADDED
@@ -0,0 +1,19 @@
1
+ Metadata-Version: 2.1
2
+ Name: cellsim
3
+ Version: 1.0
4
+ Summary: first version of a cell simulation pakage using pygame.
5
+ Author-email: Sam Fertig <sfertig007@gmail.com>
6
+ Project-URL: Homepage, https://github.com/pypa/sampleproject
7
+ Project-URL: Issues, https://github.com/pypa/sampleproject/issues
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.8
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+
15
+ # Example Package
16
+
17
+ This is a simple example package. You can use
18
+ [GitHub-flavored Markdown](https://guides.github.com/features/mastering-markdown/)
19
+ to write your content.
cellsim-1.0/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # Example Package
2
+
3
+ This is a simple example package. You can use
4
+ [GitHub-flavored Markdown](https://guides.github.com/features/mastering-markdown/)
5
+ to write your content.
@@ -0,0 +1,18 @@
1
+ [project]
2
+ name = "cellsim"
3
+ version = "1.0"
4
+ authors = [
5
+ { name="Sam Fertig", email="sfertig007@gmail.com" },
6
+ ]
7
+ description = "first version of a cell simulation pakage using pygame."
8
+ readme = "README.md"
9
+ requires-python = ">=3.8"
10
+ classifiers = [
11
+ "Programming Language :: Python :: 3",
12
+ "License :: OSI Approved :: MIT License",
13
+ "Operating System :: OS Independent",
14
+ ]
15
+
16
+ [project.urls]
17
+ Homepage = "https://github.com/pypa/sampleproject"
18
+ Issues = "https://github.com/pypa/sampleproject/issues"
@@ -0,0 +1,327 @@
1
+ import pygame # type: ignore
2
+ import pyautogui # type: ignore
3
+ import random
4
+ import os
5
+ import math
6
+ import time
7
+ from moviepy.editor import ImageSequenceClip # type: ignore
8
+
9
+ class GameWindow:
10
+ def __init__(self, WIDTH: int, HEIGHT: int, color: tuple):
11
+ pygame.init()
12
+ self.WIDTH = WIDTH
13
+ self.HEIGHT = HEIGHT
14
+ self.color = color
15
+ self.WIN = pygame.display.set_mode((self.WIDTH, self.HEIGHT))
16
+ pygame.display.set_caption("Application")
17
+ self.objects = {}
18
+ self.objcolor = {}
19
+ self.images = {}
20
+ self.image_pos = {}
21
+ self.text = None
22
+ self.file_operations = self.FileOperations()
23
+
24
+ def screen(self):
25
+ self.WIN.fill(self.color)
26
+ for item in self.objects:
27
+ pygame.draw.rect(self.WIN, self.objcolor[item + ' color'], self.objects[item])
28
+ for item in self.images:
29
+ self.WIN.blit(self.images[item], self.image_pos[item + ' pos'])
30
+ if self.text:
31
+ self.WIN.blit(self.text[0], self.text[1])
32
+ pygame.display.update()
33
+
34
+ def create_object(self, name: str, x: int, y: int, width: int, height: int, color: tuple):
35
+ self.objects[name] = pygame.Rect(x, y, width, height)
36
+ self.objcolor[name + ' color'] = color
37
+
38
+ def key_move(self, name: str, speed: int):
39
+ keys = pygame.key.get_pressed()
40
+ if keys[pygame.K_LEFT] or keys[pygame.K_a]:
41
+ self.objects[name].x -= speed
42
+ if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
43
+ self.objects[name].x += speed
44
+ if keys[pygame.K_UP] or keys[pygame.K_w]:
45
+ self.objects[name].y -= speed
46
+ if keys[pygame.K_DOWN] or keys[pygame.K_s]:
47
+ self.objects[name].y += speed
48
+
49
+ def move(self, name: str, x: int, y: int):
50
+ self.objects[name].x = x
51
+ self.objects[name].y = y
52
+
53
+ def create_image(self, name: str, image_file: str, x: int, y: int):
54
+ self.images[name] = pygame.image.load(image_file)
55
+ self.image_pos[name + ' pos'] = (x, y)
56
+
57
+ def collide(self, object1: str, object2: str) -> bool:
58
+ return pygame.Rect.colliderect(self.objects[object1], self.objects[object2])
59
+
60
+ def position(self, object: str) -> tuple:
61
+ return self.objects[object].x, self.objects[object].y
62
+
63
+ def mouse_X(self) -> int:
64
+ return pygame.mouse.get_pos()[0]
65
+
66
+ def mouse_Y(self) -> int:
67
+ return pygame.mouse.get_pos()[1]
68
+
69
+ def mouse_pos(self) -> tuple:
70
+ return pygame.mouse.get_pos()
71
+
72
+ def image_position(self, name: str) -> tuple:
73
+ return self.image_pos[name + ' pos']
74
+
75
+ def click(self) -> bool:
76
+ for event in pygame.event.get():
77
+ if event.type == pygame.MOUSEBUTTONDOWN:
78
+ return True
79
+ return False
80
+
81
+ def remove_object(self, name: str):
82
+ del self.objects[name]
83
+ del self.objcolor[name + ' color']
84
+
85
+ def clear_objects(self, key: str):
86
+ if key == "YES":
87
+ self.objects = {}
88
+ self.objcolor = {}
89
+ self.text = None
90
+
91
+ def render_text(self, text: str, x: int, y: int, color: tuple):
92
+ font_size = 30
93
+ pygame.font.init()
94
+ font = pygame.font.Font(None, font_size)
95
+ text_surface = font.render(text, True, color)
96
+ self.text = [text_surface, (x, y)]
97
+
98
+ def pressed_keys(self):
99
+ return pygame.key.get_pressed()
100
+
101
+ class FileOperations:
102
+ def read(self, file_name: str) -> list:
103
+ try:
104
+ with open(file_name, 'r') as file:
105
+ return [line.strip() for line in file.readlines()]
106
+ except FileNotFoundError:
107
+ print(f"File {file_name} not found.")
108
+ return []
109
+
110
+ def write(self, file_name: str, text: str):
111
+ with open(file_name, 'w') as file:
112
+ file.write(text)
113
+
114
+ def append(self, file_name: str, text: str):
115
+ with open(file_name, 'a') as file:
116
+ file.write(text)
117
+
118
+
119
+ class CellOut:
120
+ def __init__(self, width: int, height: int, size: int, starting_pop: int, colors=None, fps=10):
121
+ self.width = width
122
+ self.height = height
123
+ self.size = size
124
+ self.colors = colors if colors else ['red', 'green']
125
+ self.pop = starting_pop
126
+ self.cells = []
127
+ self.win = GameWindow(width, height, (0, 0, 0))
128
+ self.death_conditions = False
129
+ self.paused = False
130
+ self.shots = 0
131
+ self.fps = fps
132
+
133
+ def init(self, rules=None, spawning_rules=False, death_conditions=False):
134
+ self.init_rules(rules)
135
+ self.init_cells()
136
+ self.init_spawning(spawning_rules)
137
+ self.init_text()
138
+ self.init_death(death_conditions)
139
+
140
+ def init_death(self, death_conditions):
141
+ self.death_conditions = death_conditions
142
+
143
+ def init_text(self):
144
+ self.win.render_text(f'Population: {self.pop}', 10, 10, (255, 255, 255))
145
+
146
+ def init_rules(self, rules):
147
+ if rules is None:
148
+ self.rules = [[('red', 'green'), 5, 200], [('green', 'red'), -3, 200]]
149
+ else:
150
+ self.rules = rules
151
+
152
+ def init_spawning(self, spawning):
153
+ if spawning is not False:
154
+ self.sp = spawning
155
+
156
+ def init_cells(self):
157
+ for _ in range(self.pop):
158
+ x = random.randint(0, self.width)
159
+ y = random.randint(0, self.height)
160
+ self.cells.append([random.choice(self.colors), (x, y)])
161
+ print(self.cells)
162
+
163
+ def run(self, exit):
164
+ """
165
+ exit = [population, shots, videoLength]
166
+ else: exit = False
167
+ """
168
+ clock = pygame.time.Clock()
169
+ while True:
170
+ self.gather_screen()
171
+ if not self.paused:
172
+ self.exit_check(exit)
173
+ self.execute()
174
+ self.spawning()
175
+ self.capture_screenshots(self.shots)
176
+ self.shots += 1
177
+ clock.tick(60)
178
+
179
+ def exit_check(self, exit):
180
+ if exit is not False:
181
+ shots = exit[1]
182
+ vid = exit[2]
183
+ pop = exit[0]
184
+
185
+ # Check if the number of shots is reached
186
+ if shots is not None:
187
+ if self.shots >= shots:
188
+ pygame.quit()
189
+ self.create_video_from_screenshots('cellFolder', self.shots, 'output_video.mp4', self.fps)
190
+ quit()
191
+
192
+ # Check if the video length is reached
193
+ elif vid is not None:
194
+ if self.shots // self.fps >= vid:
195
+ pygame.quit()
196
+ self.create_video_from_screenshots('cellFolder', self.shots, 'output_video.mp4', self.fps)
197
+ quit()
198
+
199
+ # Check if the population condition is met
200
+ elif pop is not None:
201
+ if len(self.cells) >= pop:
202
+ pygame.quit()
203
+ self.create_video_from_screenshots('cellFolder', self.shots, 'output_video.mp4', self.fps)
204
+ quit()
205
+
206
+
207
+ def capture_screenshots(self, number):
208
+ screenshot = pyautogui.screenshot()
209
+ screenshot.save(f'cellFolder/screenshot_{number}.png')
210
+
211
+ def create_video_from_screenshots(self, image_folder, num_screenshots, output_file='output_video.mp4', fps=10):
212
+ # Generate image filenames with zero-padding and start from 1
213
+ image_files = [f'{image_folder}/screenshot_{i}.png' for i in range(0, num_screenshots)]
214
+
215
+ # Create video clip from the sequence of images
216
+ clip = ImageSequenceClip(image_files, fps=fps)
217
+ clip.write_videofile(output_file)
218
+
219
+ # Remove screenshots after creating the video
220
+ print('Removing screenshots...')
221
+ for image_file in image_files:
222
+ if os.path.exists(image_file): # Check if file exists before trying to remove
223
+ os.remove(image_file)
224
+
225
+ print('Done')
226
+
227
+ def apply_death(self):
228
+ Holder = []
229
+ value = self.death_conditions[0]
230
+ for cell in self.cells:
231
+ if random.randint(0, value[0]) != value[1]:
232
+ Holder.append(cell)
233
+ self.cells = Holder
234
+
235
+ def gather_screen(self):
236
+ self.win.clear_objects("YES")
237
+ self.win.render_text(f'Population: {len(self.cells)} | shots: {self.shots}, | time: {self.shots // self.fps}', 10, 10, (255, 255, 255))
238
+ for event in pygame.event.get():
239
+ if event.type == pygame.QUIT:
240
+ pygame.quit()
241
+ self.create_video_from_screenshots('cellFolder', self.shots, 'output_video.mp4', self.fps)
242
+ quit() # Exit the loop
243
+ keys = self.win.pressed_keys()
244
+ if keys[pygame.K_TAB]:
245
+ pygame.quit()
246
+ self.create_video_from_screenshots('cellFolder', self.shots, 'output_video.mp4', self.fps)
247
+ quit() # Exit the loop
248
+ if keys[pygame.K_SPACE]:
249
+ time.sleep(2)
250
+ self.paused = not self.paused
251
+ for cell in self.cells:
252
+ pos = cell[1]
253
+ dim = self.size
254
+ self.win.create_object(str(cell), pos[0], pos[1], dim, dim, pygame.Color(cell[0]))
255
+ self.win.screen()
256
+
257
+ def execute(self):
258
+ holder = []
259
+ for cell in self.cells:
260
+ color = cell[0]
261
+ pos = cell[1]
262
+ new_pos = pos
263
+ for rule in self.rules:
264
+ if rule[0][0] == color:
265
+ Rcolors = rule[0]
266
+ Rvalue = rule[1]
267
+ Rrange = rule[2]
268
+ points = self.find_positions(Rcolors[1], new_pos, Rrange)
269
+ new_pos = self.move_to_points(points, Rvalue, new_pos, color)
270
+ holder.append([color, new_pos])
271
+ self.cells = holder
272
+
273
+ def find_positions(self, color: str, position: tuple, range: int) -> list:
274
+ points = []
275
+ for cell in self.cells:
276
+ if cell[0] == color:
277
+ dist = math.sqrt((cell[1][0] - position[0]) ** 2 + (cell[1][1] - position[1]) ** 2)
278
+ if dist <= range:
279
+ points.append(cell[1])
280
+ return points
281
+
282
+ def spawning(self):
283
+ if not hasattr(self, 'sp') or not self.sp:
284
+ return
285
+ for rule in self.sp:
286
+ colors = rule[0]
287
+ chance = rule[1]
288
+ mutate = rule[2]
289
+ if random.randint(0, chance) == 1:
290
+ if random.randint(0, mutate) == 1:
291
+ color = random.choice(colors)
292
+ else:
293
+ color = colors[0]
294
+ x = random.randint(0, self.width)
295
+ y = random.randint(0, self.height)
296
+ self.cells.append([color, (x, y)])
297
+
298
+ def overlaping(self, colors):
299
+ combinations = []
300
+ for color1 in colors:
301
+ for color2 in colors:
302
+ combinations.append([(color1, color2), color1 == color2])
303
+ return combinations
304
+
305
+ def move_to_points(self, points: list, speed: int, position: tuple, current_color: str) -> tuple:
306
+ if not points:
307
+ return position
308
+ total_x, total_y = 0, 0
309
+ for x, y in points:
310
+ total_x += x - position[0]
311
+ total_y += y - position[1]
312
+ magnitude = math.sqrt(total_x ** 2 + total_y ** 2)
313
+ if magnitude == 0:
314
+ return position
315
+ direction_x = total_x / magnitude
316
+ direction_y = total_y / magnitude
317
+ new_x = position[0] + direction_x * speed
318
+ new_y = position[1] + direction_y * speed
319
+ if current_color == self.colors[0]:
320
+ new_x = position[0] - direction_x * speed
321
+ new_y = position[1] - direction_y * speed
322
+ if 0 <= new_x <= self.width - self.size and 0 <= new_y <= self.height - self.size:
323
+ return new_x, new_y
324
+ else:
325
+ return position
326
+
327
+
@@ -0,0 +1,19 @@
1
+ Metadata-Version: 2.1
2
+ Name: cellsim
3
+ Version: 1.0
4
+ Summary: first version of a cell simulation pakage using pygame.
5
+ Author-email: Sam Fertig <sfertig007@gmail.com>
6
+ Project-URL: Homepage, https://github.com/pypa/sampleproject
7
+ Project-URL: Issues, https://github.com/pypa/sampleproject/issues
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.8
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+
15
+ # Example Package
16
+
17
+ This is a simple example package. You can use
18
+ [GitHub-flavored Markdown](https://guides.github.com/features/mastering-markdown/)
19
+ to write your content.
cellsim-0.0.0/LICENSE DELETED
File without changes
cellsim-0.0.0/PKG-INFO DELETED
@@ -1,4 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: cellsim
3
- Version: 0.0.0
4
- License-File: LICENSE
cellsim-0.0.0/README.md DELETED
File without changes
File without changes
File without changes
@@ -1,4 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: cellsim
3
- Version: 0.0.0
4
- License-File: LICENSE
File without changes
File without changes