-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevolution-spiel.py
More file actions
508 lines (431 loc) · 17.9 KB
/
Copy pathevolution-spiel.py
File metadata and controls
508 lines (431 loc) · 17.9 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
import pygame
import random
import math
import logging
# ===============================
# KONSTANTEN UND EINSTELLUNGEN
# ===============================
# Fenstergröße
WINDOW_WIDTH = 1600
WINDOW_HEIGHT = 1000
# Spielparameter
FPS = 30
# Farben
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
ORANGE = ( 214, 151, 69)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
GRAY = (128, 128, 128)
BLACK = (0, 0, 0)
TRANSPARENT_GRAY = (128, 128, 128, 150)
# Größe der Sprites
PLANT_SIZE = 20
PREY_SIZE = 28
PREDATOR_SIZE = 60
OBSTACLE_SIZE = 100
# Anzahl der Anfangsobjekte
NUM_PLANTS = 100
NUM_PREYS = 20
MIN_PREDATORS = 5
NUM_OBSTACLES = 30
# Maximale Anzahl an Pflanzen
MAX_PLANTS = 1000
# Lebenszeiten in Millisekunden
PREY_LIFETIME = 100000
PREDATOR_LIFETIME = 20000
# Fortpflanzungsbedingungen
PLANTS_EATEN_TO_REPRODUCE = 3
PREYS_EATEN_TO_REPRODUCE = 5
# Sichtfeld
SIGHT_RANGE = 150
# Minimale Anzahl an Beute
MIN_PREYS = 1
# Wachstumsrate
PLANT_GROWTH_INTERVAL = 0.01 # Zeitintervall für das Wachstum in Millisekunden
# Bewegungsparameter
PREY_SPEED = 1
PREDATOR_SPEED = 1.5
RANDOM_MOVEMENT_INTERVAL = 2000
# Ausdauerparameter
SPRINT_SPEED_MULTIPLIER = 5
SPRINT_DURATION = 2000 # Dauer des Sprints in Millisekunden
COOLDOWN_DURATION = 6000 # Cooldown-Zeit in Millisekunden
# ===============================
# RESTART BUTTON
# ===============================
BUTTON_WIDTH = 80
BUTTON_HEIGHT = 30
BUTTON_COLOR = WHITE
BUTTON_HOVER_COLOR = (200, 200, 200)
BUTTON_TEXT_COLOR = BLACK
BUTTON_FONT = 'Arial'
# ===============================
# LOGGING EINRICHTEN
# ===============================
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
# ===============================
# SPIELKLASSEN
# ===============================
class Creature(pygame.sprite.Sprite):
def __init__(self, x, y, image_path, size, sight_range, life_time, speed):
super().__init__()
self.image_path = image_path
self.image = pygame.image.load(image_path).convert_alpha()
self.image = pygame.transform.scale(self.image, (size, size))
self.rect = self.image.get_rect(topleft=(x, y))
self.size = size
self.sight_range = sight_range
self.life_time = life_time
self.base_speed = speed
self.speed = speed
self.timer = pygame.time.get_ticks()
self.last_food_time = pygame.time.get_ticks()
self.last_random_movement_time = pygame.time.get_ticks()
self.direction = pygame.Vector2(random.choice([-1, 1]), random.choice([-1, 1])).normalize()
# Ausdauerparameter
self.sprint_start_time = None
self.cooldown_end_time = 0
self.is_sprinting = False
def update(self):
if pygame.time.get_ticks() - self.timer > self.life_time:
logging.info(f"{self.__class__.__name__} hat seine Lebenszeit überschritten und wird entfernt.")
self.kill()
self.handle_sprint()
self.wrap_around_screen()
self.random_movement()
self.rotate_image()
def handle_sprint(self):
current_time = pygame.time.get_ticks()
if self.is_sprinting:
if current_time - self.sprint_start_time > SPRINT_DURATION:
self.is_sprinting = False
self.cooldown_end_time = current_time + COOLDOWN_DURATION
self.speed = self.base_speed * SPRINT_SPEED_MULTIPLIER
else:
if current_time < self.cooldown_end_time:
self.speed = self.base_speed
else:
self.speed = self.base_speed
def start_sprint(self):
if not self.is_sprinting and pygame.time.get_ticks() > self.cooldown_end_time:
self.is_sprinting = True
self.sprint_start_time = pygame.time.get_ticks()
def detect_objects(self, objects):
detected = []
for obj in objects:
if self.get_distance(obj) < self.sight_range:
detected.append(obj)
return detected
def get_distance(self, obj):
return math.hypot(self.rect.centerx - obj.rect.centerx, self.rect.centery - obj.rect.centery)
def wrap_around_screen(self):
if self.rect.left > WINDOW_WIDTH:
self.rect.right = 0
elif self.rect.right < 0:
self.rect.left = WINDOW_WIDTH
if self.rect.top > WINDOW_HEIGHT:
self.rect.bottom = 0
elif self.rect.bottom < 0:
self.rect.top = WINDOW_HEIGHT
def random_movement(self):
current_time = pygame.time.get_ticks()
if current_time - self.last_random_movement_time > RANDOM_MOVEMENT_INTERVAL:
self.last_random_movement_time = current_time
self.direction = pygame.Vector2(random.choice([-1, 1]), random.choice([-1, 1])).normalize()
self.rect.x += self.direction.x * self.speed
self.rect.y += self.direction.y * self.speed
def rotate_image(self):
angle = self.direction.angle_to(pygame.Vector2(0, -1))
self.image = pygame.transform.rotate(pygame.image.load(self.image_path).convert_alpha(), angle)
self.image = pygame.transform.scale(self.image, (self.size, self.size))
class Prey(Creature):
def __init__(self, x, y):
super().__init__(x, y, 'images/prey.png', PREY_SIZE, SIGHT_RANGE, PREY_LIFETIME, PREY_SPEED)
self.plants_eaten = 0
def update(self):
super().update()
self.move()
self.search_for_food()
self.avoid_obstacles()
def move(self):
predators_in_range = self.detect_objects(predators)
if predators_in_range:
self.start_sprint() # Beginne Sprinten, wenn ein Jäger in der Nähe ist
closest_predator = min(predators_in_range, key=lambda p: self.get_distance(p))
direction_x = self.rect.centerx - closest_predator.rect.centerx
direction_y = self.rect.centery - closest_predator.rect.centery
distance = math.hypot(direction_x, direction_y)
if distance > 0:
direction_x /= distance
direction_y /= distance
self.direction = pygame.Vector2(direction_x, direction_y)
self.rect.x += self.direction.x * self.speed
self.rect.y += self.direction.y * self.speed
else:
self.speed = PREY_SPEED # Zurück zur normalen Geschwindigkeit, wenn kein Jäger in der Nähe ist
plants_in_range = self.detect_objects(plants)
if plants_in_range:
closest_plant = min(plants_in_range, key=lambda p: self.get_distance(p))
direction_x = closest_plant.rect.centerx - self.rect.centerx
direction_y = closest_plant.rect.centery - self.rect.centery
distance = math.hypot(direction_x, direction_y)
if distance > 0:
direction_x /= distance
direction_y /= distance
self.direction = pygame.Vector2(direction_x, direction_y)
self.rect.x += self.direction.x * self.speed
self.rect.y += self.direction.y * self.speed
self.avoid_overlap(preys)
def search_for_food(self):
if plants:
plants_in_range = self.detect_objects(plants)
if plants_in_range:
closest_plant = min(plants_in_range, key=lambda p: self.get_distance(p))
if self.get_distance(closest_plant) < self.size:
plants.remove(closest_plant)
all_sprites.remove(closest_plant)
self.plants_eaten += 1
self.last_food_time = pygame.time.get_ticks()
if self.plants_eaten >= PLANTS_EATEN_TO_REPRODUCE:
self.plants_eaten = 0
self.reproduce()
def reproduce(self):
new_prey = Prey(self.rect.x + random.randint(-20, 20), self.rect.y + random.randint(-20, 20))
preys.add(new_prey)
all_sprites.add(new_prey)
logging.info(f"Neue Beute bei ({new_prey.rect.x}, {new_prey.rect.y}) erzeugt.")
def avoid_obstacles(self):
obstacles_in_range = self.detect_objects(obstacles)
if obstacles_in_range:
closest_obstacle = min(obstacles_in_range, key=lambda o: self.get_distance(o))
direction_x = self.rect.centerx - closest_obstacle.rect.centerx
direction_y = self.rect.centery - closest_obstacle.rect.centery
distance = math.hypot(direction_x, direction_y)
if distance > 0:
direction_x /= distance
direction_y /= distance
self.rect.x += direction_x * PREY_SPEED
self.rect.y += direction_y * PREY_SPEED
def avoid_overlap(self, group):
for sprite in group:
if sprite != self and pygame.sprite.collide_rect(self, sprite):
direction_x = self.rect.centerx - sprite.rect.centerx
direction_y = self.rect.centery - sprite.rect.centery
distance = math.hypot(direction_x, direction_y)
if distance > 0:
direction_x /= distance
direction_y /= distance
self.rect.x += direction_x * PREY_SPEED
self.rect.y += direction_y * PREY_SPEED
class Predator(Creature):
def __init__(self, x, y):
super().__init__(x, y, 'images/predator.png', PREDATOR_SIZE, SIGHT_RANGE, PREDATOR_LIFETIME, PREDATOR_SPEED)
self.preys_eaten = 0
def update(self):
super().update()
self.move()
self.search_for_food()
self.avoid_obstacles()
def move(self):
preys_in_range = self.detect_objects(preys)
if preys_in_range:
self.start_sprint() # Beginne Sprinten, wenn Beute in der Nähe ist
closest_prey = min(preys_in_range, key=lambda p: self.get_distance(p))
direction_x = closest_prey.rect.centerx - self.rect.centerx
direction_y = closest_prey.rect.centery - self.rect.centery
distance = math.hypot(direction_x, direction_y)
if distance > 0:
direction_x /= distance
direction_y /= distance
self.direction = pygame.Vector2(direction_x, direction_y)
self.rect.x += self.direction.x * self.speed
self.rect.y += self.direction.y * self.speed
else:
self.speed = PREDATOR_SPEED # Zurück zur normalen Geschwindigkeit, wenn keine Beute in der Nähe ist
self.avoid_overlap(predators)
def search_for_food(self):
if preys:
preys_in_range = self.detect_objects(preys)
if preys_in_range:
closest_prey = min(preys_in_range, key=lambda p: self.get_distance(p))
if self.get_distance(closest_prey) < self.size:
preys.remove(closest_prey)
all_sprites.remove(closest_prey)
self.preys_eaten += 1
self.last_food_time = pygame.time.get_ticks()
if self.preys_eaten >= PREYS_EATEN_TO_REPRODUCE:
self.preys_eaten = 0
self.reproduce()
def reproduce(self):
new_predator = Predator(self.rect.x + random.randint(-20, 20), self.rect.y + random.randint(-20, 20))
predators.add(new_predator)
all_sprites.add(new_predator)
logging.info(f"Neuer Jäger bei ({new_predator.rect.x}, {new_predator.rect.y}) erzeugt.")
def avoid_obstacles(self):
obstacles_in_range = self.detect_objects(obstacles)
if obstacles_in_range:
closest_obstacle = min(obstacles_in_range, key=lambda o: self.get_distance(o))
direction_x = self.rect.centerx - closest_obstacle.rect.centerx
direction_y = self.rect.centery - closest_obstacle.rect.centery
distance = math.hypot(direction_x, direction_y)
if distance > 0:
direction_x /= distance
direction_y /= distance
self.rect.x += direction_x * PREDATOR_SPEED
self.rect.y += direction_y * PREDATOR_SPEED
def avoid_overlap(self, group):
for sprite in group:
if sprite != self and pygame.sprite.collide_rect(self, sprite):
direction_x = self.rect.centerx - sprite.rect.centerx
direction_y = self.rect.centery - sprite.rect.centery
distance = math.hypot(direction_x, direction_y)
if distance > 0:
direction_x /= distance
direction_y /= distance
self.rect.x += direction_x * PREDATOR_SPEED
self.rect.y += direction_y * PREDATOR_SPEED
class Plant(pygame.sprite.Sprite):
def __init__(self, x, y, plant_image):
super().__init__()
self.image = pygame.image.load(plant_image).convert_alpha()
self.rect = self.image.get_rect(topleft=(x, y))
class Obstacle(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.image.load('images/obstacle.png').convert_alpha() # Bild für das Hindernis laden
self.image = pygame.transform.scale(self.image, (OBSTACLE_SIZE, OBSTACLE_SIZE)) # Größe des Hindernisses anpassen
self.rect = self.image.get_rect(topleft=(x, y))
# ===============================
# SPIEL INIT
# ===============================
pygame.init()
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("Ökosystem Simulation")
clock = pygame.time.Clock()
# Sprite-Gruppen
all_sprites = pygame.sprite.Group()
plants = pygame.sprite.Group()
preys = pygame.sprite.Group()
predators = pygame.sprite.Group()
obstacles = pygame.sprite.Group()
# Pflanzenbilder
plant_images = [
'images/plant1.png',
'images/plant2.png',
'images/plant3.png'
]
# Initialisierung der Pflanzen
def create_plant():
x = random.randint(0, WINDOW_WIDTH - PLANT_SIZE)
y = random.randint(0, WINDOW_HEIGHT - PLANT_SIZE)
plant_image = random.choice(plant_images)
plant = Plant(x, y, plant_image)
plants.add(plant)
all_sprites.add(plant)
for _ in range(NUM_PLANTS):
create_plant()
# Initialisierung der Beute
for _ in range(NUM_PREYS):
x = random.randint(0, WINDOW_WIDTH - PREY_SIZE)
y = random.randint(0, WINDOW_HEIGHT - PREY_SIZE)
prey = Prey(x, y)
preys.add(prey)
all_sprites.add(prey)
# Initialisierung der Jäger
for _ in range(MIN_PREDATORS):
x = random.randint(0, WINDOW_WIDTH - PREDATOR_SIZE)
y = random.randint(0, WINDOW_HEIGHT - PREDATOR_SIZE)
predator = Predator(x, y)
predators.add(predator)
all_sprites.add(predator)
# Initialisierung der Hindernisse
for _ in range(NUM_OBSTACLES):
x = random.randint(0, WINDOW_WIDTH - OBSTACLE_SIZE)
y = random.randint(0, WINDOW_HEIGHT - OBSTACLE_SIZE)
obstacle = Obstacle(x, y)
obstacles.add(obstacle)
all_sprites.add(obstacle)
# ===============================
# FUNKTIONEN ZUM ZEICHNEN DER ÜBERSICHT
# ===============================
def draw_stats(screen, start_time):
font = pygame.font.SysFont('Arial', 30)
elapsed_time = pygame.time.get_ticks() - start_time
seconds = elapsed_time // 1000
minutes = seconds // 60
seconds %= 60
overlay = pygame.Surface((160, 140), pygame.SRCALPHA)
overlay.fill(TRANSPARENT_GRAY)
screen.blit(overlay, (10, 10))
text = [
f"Zeit: {minutes:02}:{seconds:02}",
f"Pflanzen: {len(plants)}",
f"Beute: {len(preys)}",
f"Jäger: {len(predators)}"
]
for i, line in enumerate(text):
label = font.render(line, True, BLACK)
screen.blit(label, (20, 20 + i * 30))
# Funktion zum Zeichnen des Neustart-Buttons
def draw_button(screen, text, x, y, width, height, color, hover_color, text_color, font_name, action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + width > mouse[0] > x and y + height > mouse[1] > y:
pygame.draw.rect(screen, hover_color, (x, y, width, height))
if click[0] == 1 and action is not None:
action()
else:
pygame.draw.rect(screen, color, (x, y, width, height))
font = pygame.font.SysFont(font_name, 30)
text_surface = font.render(text, True, text_color)
text_rect = text_surface.get_rect(center=(x + width // 2, y + height // 2))
screen.blit(text_surface, text_rect)
def restart_game():
global all_sprites, plants, preys, predators, obstacles, start_time
all_sprites.empty()
plants.empty()
preys.empty()
predators.empty()
obstacles.empty()
for _ in range(NUM_PLANTS):
create_plant()
for _ in range(NUM_PREYS):
x = random.randint(0, WINDOW_WIDTH - PREY_SIZE)
y = random.randint(0, WINDOW_HEIGHT - PREY_SIZE)
prey = Prey(x, y)
preys.add(prey)
all_sprites.add(prey)
for _ in range(MIN_PREDATORS):
x = random.randint(0, WINDOW_WIDTH - PREDATOR_SIZE)
y = random.randint(0, WINDOW_HEIGHT - PREDATOR_SIZE)
predator = Predator(x, y)
predators.add(predator)
all_sprites.add(predator)
for _ in range(NUM_OBSTACLES):
x = random.randint(0, WINDOW_WIDTH - OBSTACLE_SIZE)
y = random.randint(0, WINDOW_HEIGHT - OBSTACLE_SIZE)
obstacle = Obstacle(x, y)
obstacles.add(obstacle)
start_time = pygame.time.get_ticks()
# ===============================
# SPIELSCHLEIFE
# ===============================
running = True
start_time = pygame.time.get_ticks()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
all_sprites.update()
# Erzeuge neue Pflanzen, falls nötig
if len(plants) < MAX_PLANTS:
if pygame.time.get_ticks() % PLANT_GROWTH_INTERVAL < FPS:
create_plant()
screen.fill(ORANGE)
all_sprites.draw(screen)
draw_stats(screen, start_time)
draw_button(screen, "Restart", WINDOW_WIDTH - BUTTON_WIDTH - 10, 10, BUTTON_WIDTH, BUTTON_HEIGHT, BUTTON_COLOR, BUTTON_HOVER_COLOR, BUTTON_TEXT_COLOR, BUTTON_FONT, restart_game)
pygame.display.flip()
clock.tick(FPS)
pygame.quit()