-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
270 lines (198 loc) · 8.47 KB
/
main.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
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
from arcade.gui import UIFlatButton, UIManager
import arcade
import time
from easyAI import TwoPlayersGame, Human_Player, AI_Player, Negamax
from config import FRACTIONS_CONFIG, MAP_HEX_RADIUS, ENTITY_ID2COST
from control import SpawnEntity, NextStep, UpdateGameState, MoveUnit
from fractions import Fraction
from game_state import GameState
from map.hexagonal_map import HexMap
from arcade.gui import UIManager
from ui import set_ui, update_ui
has_exit = False
class Game(arcade.Window):
# Играют по очереди, сначала делает ход первый игрок, потом второй и т.д.
hosts: list = [] # список хостов (фракций) игроков (хост - играбельный объект, т.е. страна)
hosts_num = 2
active_host: int # номер текущего активного хоста (тот, который играет)
game_iteration: int # номер хода
host_init_pos = {0: (1, 10), 1: (8, 1)}
gamer_host: int
state = None
select_color = "BLUE"
select2_color = "COLUMBIA_BLUE"
ui_margin_left = None
ui_margin_top = None
game_over = False
def __init__(self):
self.state = GameState()
self.map = HexMap(hex_radius=MAP_HEX_RADIUS)
self.w, self.h = self.map.get_window_size()
super().__init__(self.w + self.w // 4, self.h, "Poliyoy")
arcade.set_background_color(arcade.color.BLACK)
self.ui_manager = UIManager()
self.ui_margin_left = self.w / 15
self.ui_margin_top = self.h / 10
self.game_iteration = 0
self.setup()
def __new__(cls):
if not hasattr(cls, 'instance'):
cls.instance = super(Game, cls).__new__(cls)
return cls.instance
def init_hosts(self):
for (e, frac) in enumerate(FRACTIONS_CONFIG.keys()):
self.hosts.append(Fraction(**FRACTIONS_CONFIG[frac]))
fraction = self.hosts[-1]
self.state.append_fraction(fraction)
if not fraction.isBot:
self.active_host = e
self.gamer_host = e
self.map.place_fraction(fraction, self.host_init_pos[fraction.fraction_id])
self.state.set_fraction(self.gamer_host)
def setup(self):
"""
Здесь рендеринг UI и инициализация пользовательской fraction
Например, разместить сущности стран на карте
:return:
"""
self.map.create_map()
self.init_hosts()
self.after_init() # размещение на карте деревьев и т.п.
# self.map.after_init(self.hosts) # размещение на карте деревьев и т.п.
self.ui_manager.purge_ui_elements()
set_ui(self)
# back = self.state.last_mouse_tile_pos
# back2 = self.state.last_fraction
# self.hosts[not self.active_host].money_amount = 9999
#
# pos = self.hosts[not self.active_host].fraction_capital_pos
# self.state.last_mouse_tile_pos = pos
# self.state.last_fraction = self.hosts[not self.active_host]
# obj = SpawnEntity(self.map, self.state, 1, UpdateGameState(self))
# obj.execute()
# self.state.last_mouse_tile_pos = back
# self.state.last_fraction = back2
# sys.exit()
def after_init(self):
"""
Запуск после размещения fractions
:return:
"""
trees = self.map.get_random_positions()
from config import TREE_ID
fr = self.state.get_last_fraction()
for i in trees:
self.map.spawn_tree(i, fr)
def on_draw(self):
"""
Рендеринг различных объектов
"""
# This command has to happen before we start drawing
arcade.start_render()
self.map.draw()
self.ui_manager.on_draw()
def on_next_step_key_press(self):
# print("before", self.hosts[1].money_amount)
for host in self.hosts:
for tile in host.tiles:
self.map.unuse_entity(tile)
self.game_over = self.state.get_last_fraction().make_step()
self.update_screen_info()
self.game_iteration += 1
self.active_host = not self.active_host
self.state.set_fraction(self.active_host) # upd last host
self.update_screen_info()
fraction = self.state.get_last_fraction()
# if fraction.village_cost_prev != fraction.village_cost:
self.update_village_btn()
def update_screen_info(self):
self.ui_manager.find_by_id("money_amount").text = "Gold: " + str(self.state.get_last_fraction().money_amount)
self.ui_manager.find_by_id("money_step").text = "Delta: " + str(self.state.get_last_fraction().step_delta)
def update_village_btn(self):
# Тяжелая операция, поэтому случай обрабатывается отдельно
# # Дальше костыль для обновления текста кнопки (в либе такое, почему-то, не предусмотрено)
update_ui(self)
def on_mouse_press(self, x, y, button, modifiers):
"""
Обработка пользовательского действия
"""
col, row = self.map.locate(x, y)
host = self.state.get_last_fraction()
pos = (col, row)
self.map.unselect_tiles()
if button == 1:
if self.map.is_in_map(col, row):
self.map.add_tile_to_state(pos)
self.map.select_tile(pos, self.select_color)
self.state.set_pos(col, row)
if pos in host.units_pos.keys():
self.map.show_move_range(pos, self.select_color, self.select2_color)
elif button == 4:
unit_pos = self.state.last_mouse_tile_pos
if unit_pos in host.units_pos.keys():
self.state.last_mouse_right_tile_pos = pos
command = MoveUnit(self.map, self.state, UpdateGameState(self))
command.execute()
if pos in host.tiles.keys():
pass
# print(self.active_host, self.state.get_last_fraction().fraction_id)
def possible_moves(self, pos):
return self.map.show_move_range_quiet(pos)
def bot_move(self):
pass
class AIApi:
def __init__(self):
pass
class GameEnvironment(TwoPlayersGame):
def __init__(self, players):
self.players = players
self.pile = 20 # start with 20 bones in the pile
self.nplayer = 1 # player 1 starts
self.game_window = None
self.set_game_window()
def set_game_window(self):
self.game_window = Game()
self.nplayer = self.game_window.gamer_host + 1
def possible_moves(self):
print(self.game_window.possible_moves())
def make_move(self, move): self.pile -= int(move) # remove bones.
def win(self): return self.pile <= 0 # opponent took the last bone ?
# def is_over(self): return self.win() # Game stops when someone wins.
def is_over(self):
return self.game_window.game_over
def show(self): print("%d bones left in the pile" % self.pile)
# def scoring(self): return 100 if game.win() else 0 # For the AI
def game_loop(game_obj):
global has_exit
game_iters = 0
start_time = time.time()
while not game.is_over():
if has_exit:
break
# if game.nplayer == 1: # we are assuming player 1 is an AI_Player
# move = game.get_move()
# print("AI plays {}".format(move))
#
# game.play_move(move)
game.game_window.switch_to()
game.game_window.dispatch_events()
game.game_window.dispatch_event('on_draw')
game.game_window.flip()
game_iters = game.game_window.game_iteration
current_time = time.time()
elapsed_time = current_time - start_time
start_time = current_time
if elapsed_time < 1. / 60.:
sleep_time = (1. / 60.) - elapsed_time
time.sleep(sleep_time)
game.game_window._dispatch_updates(1 / 60)
game.nplayer = game.game_window.active_host + 1
if __name__ == "__main__":
game = GameEnvironment([AI_Player(Negamax(9)), Human_Player()])
has_exit = False
@game.game_window.event
def on_close():
global has_exit
print("Game over")
has_exit = True
game_loop(game)