-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation.py
84 lines (74 loc) · 3.32 KB
/
simulation.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
import itertools
import pygame
import color
from settings import Settings
class Simulation:
"""
Simulation object exposing methods to control the simulation.
The drawing on a pygame screen is done internally.
"""
def __init__(self, window_width, window_height, grid):
self.grid = grid
# Pygame setup
pygame.init()
self.clock = pygame.time.Clock()
self.pygame_screen = pygame.display.set_mode((window_width,
window_height))
self.pygame_screen.fill(color.BLACK)
self._draw_grid()
pygame.display.update()
def _draw_grid(self):
bs = Settings.BLOCKSIZE
font = pygame.font.SysFont(pygame.font.get_fonts()[0], bs // 2)
for tile in self.grid:
# humidity rect
rect_h = pygame.Rect(tile.x_coordinate_in_grid * bs,
tile.y_coordinate_in_grid * bs, bs, bs)
# temperature rect
rect_t = pygame.Rect((tile.x_coordinate_in_grid + Settings.WINDOW_WIDTH // 2) * bs,
tile.y_coordinate_in_grid * bs, bs, bs)
# resources rect
rect_r = pygame.Rect(tile.x_coordinate_in_grid * bs,
(tile.y_coordinate_in_grid + Settings.WINDOW_HEIGHT // 2) * bs, bs, bs)
# pheromones and grasshoppers rect
rect_p = pygame.Rect((tile.x_coordinate_in_grid + Settings.WINDOW_WIDTH // 2) * bs,
(tile.y_coordinate_in_grid + Settings.WINDOW_HEIGHT // 2) * bs, bs, bs)
# Fill
pygame.draw.rect(self.pygame_screen, (0, 0, tile.humidity),
rect_h, 0)
pygame.draw.rect(self.pygame_screen, (tile.temperature, 0, 0),
rect_t, 0)
pygame.draw.rect(self.pygame_screen, (0, int(tile.resources), 0),
rect_r, 0)
i_pheromone = min(128, int(tile.pheromone * 128))
pygame.draw.rect(self.pygame_screen,
(127 + i_pheromone, 127 + i_pheromone, 127 + i_pheromone),
rect_p, 0)
# draw also the number of grasshoppers
widget = font.render(str(tile.grasshoppers_p) + '|' + str(tile.grasshoppers_NOT_p), True, pygame.Color("black"))
font_rect = widget.get_rect()
font_rect.center = rect_p.center
self.pygame_screen.blit(widget, font_rect)
# Border
pygame.draw.rect(self.pygame_screen, color.BLACK,
rect_h, 1)
pygame.draw.rect(self.pygame_screen, color.BLACK,
rect_t, 1)
pygame.draw.rect(self.pygame_screen, color.BLACK,
rect_r, 1)
pygame.draw.rect(self.pygame_screen, color.BLACK,
rect_p, 1)
def tick(self):
"""
Moves the simulation one step forward.
"""
grasshoppers = self.grid.update()
self._draw_grid()
pygame.display.update()
return grasshoppers
def save_image(self, i):
"""
Saves the pygame window as an image
:param i: number of the image
"""
pygame.image.save(self.pygame_screen, f'images/screenshot{i}.png')