-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
132 lines (102 loc) · 4.03 KB
/
run.py
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
import random
import numpy as np
from typing import Optional, List
import arcade
from arcade.gui import UIManager
from arcade.gui.widgets import UITextArea
from src.being_sprite import BeingSprite
from src.world import World
WORLD_SIZE = 800
SIDEBAR_WIDTH = 200
SCREEN_WIDTH = WORLD_SIZE + SIDEBAR_WIDTH
SCREEN_HEIGHT = WORLD_SIZE
SCREEN_TITLE = "Evolving Beings"
DEBUG_PADDING = 10
FPS_LIMIT = 120
INITIAL_POPULATION = 1000
FOOD_SCALE = 0.25
TILE_SCALE = 1
TILE_SIZE = 64
class EvolvingBeings(arcade.Window):
def __init__(self):
super().__init__(
width=SCREEN_WIDTH,
height=SCREEN_HEIGHT,
title=SCREEN_TITLE,
update_rate=1/FPS_LIMIT,
)
self.background_color = arcade.color.DARK_BROWN
self.manager: Optional[UIManager] = None
self.debug_text: Optional[UITextArea] = None
self.world: Optional[World] = None
self.grid_sprite_list: Optional[arcade.SpriteList] = None
self.food_sprite_list: Optional[arcade.SpriteList] = None
self.bg_sprite_list: Optional[arcade.SpriteList] = None
self.fps: List[float] = []
self.available_sprites: List[int] = []
def setup(self):
"""Sets up the world for the current simulation"""
self.world = World(WORLD_SIZE, WORLD_SIZE)
self.manager = UIManager()
self.manager.enable()
self.debug_text = UITextArea(
x=DEBUG_PADDING,
y=-DEBUG_PADDING,
width=SCREEN_WIDTH - SCREEN_HEIGHT - DEBUG_PADDING,
height=SCREEN_HEIGHT - DEBUG_PADDING,
text='',
text_color=(255, 255, 255, 255),
)
self.manager.add(self.debug_text)
# cell rendering
self.grid_sprite_list = arcade.SpriteList()
self.food_sprite_list = arcade.SpriteList()
# create beings
for _ in range(INITIAL_POPULATION):
idx = len(self.grid_sprite_list)
location, _ = self.world.spawn(idx)
being_sprite = BeingSprite()
being_sprite.center_x = location[0] + SIDEBAR_WIDTH
being_sprite.center_y = location[1]
self.grid_sprite_list.append(being_sprite)
self.bg_sprite_list = arcade.SpriteList(use_spatial_hash=True)
for x in range(SIDEBAR_WIDTH, SCREEN_WIDTH, TILE_SIZE):
for y in range(0, SCREEN_HEIGHT, TILE_SIZE):
bg_tile_idx = random.randint(1, 2)
bg = arcade.Sprite(f":resources:images/topdown_tanks/tileSand{bg_tile_idx}.png", TILE_SCALE)
bg.center_x = x + TILE_SIZE / 2
bg.center_y = y + TILE_SIZE / 2
self.bg_sprite_list.append(bg)
def on_update(self, delta_time: float):
"""Updates the position of all game objects
Arguments:
delta_time {float} -- How much time since the last call
"""
self.fps.append(1 / delta_time)
if len(self.fps) > 100:
self.fps.pop(0)
self.debug_text.text = f'FPS: {round(np.mean(self.fps))}/{FPS_LIMIT}\n' \
f'Beings alive: {self.world.alive}\n' \
f'Sprite buffer: {len(self.available_sprites)}\n' \
idx = 0
for location, being in self.world.locations.items():
self.grid_sprite_list[idx].angle = being.angle
if being.speed > 0:
self.grid_sprite_list[idx].center_x = location[0] + SIDEBAR_WIDTH
self.grid_sprite_list[idx].center_y = location[1]
idx += 1
dead_sprites = self.world.step()
for sprite_index in dead_sprites:
self.grid_sprite_list[sprite_index].color = arcade.color.BLACK
self.available_sprites.append(sprite_index)
def on_draw(self):
self.clear()
self.manager.draw()
self.bg_sprite_list.draw()
# self.food_sprite_list.draw()
self.grid_sprite_list.draw()
if __name__ == "__main__":
print('Init Evolving Beings')
window = EvolvingBeings()
window.setup()
arcade.run()