-
Notifications
You must be signed in to change notification settings - Fork 0
/
PygameModule.py
115 lines (92 loc) · 3.82 KB
/
PygameModule.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
import pygame
import time
import concurrent.futures
from multiprocessing import Process
from Field import *
from Obj import *
from Resources import *
from Human import *
from FieldProcessing import *
from Colony import *
from InitialGame import *
class PygameModule:
def __init__(self, game, WIDTH=1920, HEIGHT=1080):
self.game = game
self.WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Game")
pygame.font.init()
self.lst_field = []
self.lst_obj = []
self.lst_human = []
self.run = True
self.break_event = False
def start_game(self):
colony_lst = self.game.get_colony_lst()
while self.run:
self.run = Colony.check_game_over()
for event in pygame.event.get():
if event.type == pygame.QUIT:
for colony in colony_lst:
colony.save_humans_chromosome()
self.run = False
self.break_event = True
self.working_parallel()
self.sorted_obj_to_lst()
self.drawing()
self.lst_obj.clear()
self.lst_human.clear()
InitialGame.clean_game()
pygame.quit()
def working_parallel(self):
colony_lst = self.game.get_colony_lst()
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
executor.map(lambda obj: obj.working(), colony_lst)
executor.shutdown()
def sorted_obj_to_lst(self):
for i in self.game.get_field():
for j in i:
self.lst_field.append(pygame.Rect(0 + 26 * j.get_posX(), +26 * j.get_posY(), 25, 25))
if j.is_obj():
self.lst_obj.append([pygame.Rect(0 + 26 * j.get_posX(), +26 * j.get_posY(), 18, 18), j.obj])
if len(j.lst_human) != 0:
for human in j.lst_human:
self.lst_human.append([pygame.Rect(0 + 26 * j.get_posX(), +26 * j.get_posY(), 18, 18), human])
def drawing(self):
self.drawing_obj()
self.draw_status_bar()
pygame.display.update()
def draw_status_bar(self):
WIN = self.WIN
HEIGHT = WIN.get_height()
font = pygame.font.SysFont('comicsans', 30)
counter_width = 0
for num, colony in enumerate(Colony.lst_colony):
status_bar_rect = pygame.Rect(30 + counter_width, HEIGHT - 210, 110, 200)
pygame.draw.rect(WIN, (20, 50, 0), status_bar_rect)
counter_height = 0
text = font.render("Colony " + str(Colony.lst_colony.index(colony) + 1), 1, colony.get_color_flt())
WIN.blit(text, (40 + counter_width, HEIGHT - 205))
for item in colony.dict_inventory_and_pers:
text = font.render(str(item) + ": " + str(colony.dict_inventory_and_pers[item][0]), 1, colony.get_color_flt())
WIN.blit(text, (40 + counter_width, HEIGHT - 180 + counter_height))
counter_height += 25
counter_width += 150
def drawing_obj(self):
WIN = self.WIN
WIN.fill((0, 0, 0))
for i in self.lst_field:
pygame.draw.rect(WIN, (145, 255, 0), i)
for i in self.lst_obj:
pos, obj = i[0], i[1]
WIN.blit(obj.get_img(), (pos.x, pos.y))
for i in self.lst_human:
pos, human = i[0], i[1]
img = human.get_img()
copy_img = img.copy()
filter_color = human.filter_color
filtered_surface = pygame.Surface(copy_img.get_size(), pygame.SRCALPHA)
filtered_surface.fill(filter_color)
copy_img.blit(filtered_surface, (0, 0), special_flags=pygame.BLEND_RGBA_MULT)
WIN.blit(copy_img, (pos.x, pos.y))
def get_status_break_event(self):
return self.break_event