-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.py
750 lines (630 loc) · 33.4 KB
/
update.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
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
"""
This file is contains main game logic in a Game class
This class is invoked in main.py file
Update method of this class is called every 0.02 seconds (60 FPS (Depends on what the value of self.MAX_FPS is))
"""
# TODO: CREATE GAME MODES <COMPLETE EXCEPT STEP-GAME>
# CLASSIC: CURRENT RULES OF THE GAME
# CONTROL: ALL PLAYERS WAIT FOR THE ONE TO MAKE A MOVE
# FAST: MAX ENERGY = 20
# TODO: ADD SPECIAL EFFECTS
# TODO: ADD MUSIC
# TODO: CREATE MINIMAP
# TODO: CREATE DIFFERENT MAPS
# TODO: CREATE LEVEL EDITOR
# TODO: CREATE LEVELS
# TODO: USE DAMN GPU
# TODO: CREATE MULTIPLAYER
import pygame
from objects import *
import random
class Game:
"""
Root Wars game class
"""
def __init__(self, app):
""" Game initialisation """
# app variables
self.app = app
self.mode = "main menu"
self.version = "1.1"
# app lists
self.main_menu_objects = []
self.settings_objects = []
self.info_objects = []
self.rules_objects = []
self.new_game_objects = []
# game settings variables that you can change (DEFAULT SETTINGS)
self.scroll_scale = 40
self.navigation_speed = 30
self.max_energy = 40
self.hexagon_size = 100
self.min_hexagon_size = 60
self.max_hexagon_size = 100
self.map_move_reaction = 2
self.grid_line_width = 5
self.grid_hex_width = 5
self.grid_line_color = (200, 200, 200, 255)
self.grid_hex_color = (20, 20, 20)
self.grid_hex_outline_color = (200, 200, 200, 255)
self.player_color = (0, 0, 255)
self.selected_hexagon_color = (100, 0, 150)
self.enemy_color = (255, 0, 0)
self.selected_enemy_hexagon_color = (255, 255, 0)
self.nearby_hexagon_color = (0, 255, 0)
self.difficulties = ["Easy", "Normal", "Hard", "Super Hard"]
self.speeds = ["Slow", "Normal", "Fast", "Super Fast"]
self.colors = [(255, 0, 0), (255, 255, 0), (0, 255, 0), (0, 255, 255), (0, 0, 255), (100, 0, 150),
(150, 255, 0), (255, 255, 255), (255, 0, 255), (0, 255, 150)]
self.player_colors = self.colors
self.selected_root_colors = self.colors
self.nearby_hexagon_colors = self.colors
self.game_modes = ["Classic", "Fast"]
self.maps = ["Two-Way"]
self.background_image = pygame.transform.scale(pygame.image.load("background.jpg"),
[self.app.WIDTH, self.app.HEIGHT])
# settings variables
self.SETTINGS_OBJECTS_CREATED = False
self.FPS_ENABLED = False
self.fps_label = Label(self, foreground=(0, 255, 0), font_size=40, font_name="Courier").percent(95, 2)
self.create_main_menu_objects()
# test
# self.bloom_objects = []
# self.mode = "bloom"
# self.bloom_test()
# self.counter = 0
# def bloom_test(self):
# """ Bloom test function """
#
# self.bloom_objects.append(Bloom3(self, [self.app.H_WIDTH, self.app.H_HEIGHT], colorkey=(0, 0, 0)))
def create_main_menu_objects(self):
""" Init main menu objects """
self.game_title_label = Label(self, text="Root Wars").percent_y(10)
self.play_button = Button(self, text="Play").percent_y(35)
self.settings_button = Button(self, text="Settings").percent_y(45)
self.info_button = Button(self, text="Info").percent_y(55)
self.rules_button = Button(self, text="Rules").percent_y(65)
self.exit_button = Button(self, text="Exit").percent_y(75)
self.main_menu_objects.append(self.game_title_label)
self.main_menu_objects.append(self.play_button)
self.main_menu_objects.append(self.settings_button)
self.main_menu_objects.append(self.info_button)
self.main_menu_objects.append(self.rules_button)
self.main_menu_objects.append(self.exit_button)
def create_new_game_objects(self):
""" Init new game objects """
self.new_game_title = Label(self, text="Create New Game").percent_y(10)
self.difficulty_options = OptionButton(self, text="Difficulty: ", options=self.difficulties).percent(10, 30)
self.speed_options = OptionButton(self, text="Speed: ", options=self.speeds, current_option=1).percent(10, 40)
self.player_color_picker = ColorOptionButton(self, text="Player Color: ", options=self.player_colors,
current_option=4).percent(10, 50)
self.enemy_color_picker = ColorOptionButton(self, text="Enemy Color: ", options=self.player_colors).percent(10,
60)
self.selected_hexagon_color_picker = ColorOptionButton(self, text="Selected Root Color: ",
options=self.selected_root_colors,
current_option=5).percent(10, 70)
self.nearby_hexagon_color_picker = ColorOptionButton(self, text="Nearby Root Color: ",
options=self.nearby_hexagon_colors,
current_option=2).percent(10, 80)
self.game_mode_options = OptionButton(self, text="Game Mode: ", options=self.game_modes).percent(60, 30)
self.map_options = OptionButton(self, text="Map: ", options=self.maps).percent(60, 40)
self.start_game_button = Button(self, text="Start Game", font_size=80).percent(60, 68)
self.back_button = Button(self, text="Back").percent(8, 8)
self.new_game_objects.append(self.new_game_title)
self.new_game_objects.append(self.difficulty_options)
self.new_game_objects.append(self.speed_options)
self.new_game_objects.append(self.player_color_picker)
self.new_game_objects.append(self.enemy_color_picker)
self.new_game_objects.append(self.selected_hexagon_color_picker)
self.new_game_objects.append(self.nearby_hexagon_color_picker)
self.new_game_objects.append(self.game_mode_options)
self.new_game_objects.append(self.map_options)
self.new_game_objects.append(self.start_game_button)
self.new_game_objects.append(self.back_button)
def create_settings_objects(self):
""" Init settings objects """
if not self.SETTINGS_OBJECTS_CREATED:
self.fps_button = Button(self, text="Show fps").percent_y(10)
self.back_button = Button(self, text="Back").percent(8, 8)
self.info_text = Text(self, text=""
"Navigation\n\n"
"Drag mouse cursor to one of the sides of the screen\n"
"to move the camera\n"
"\n\n"
"Key settings\n\n"
"Return to main menu: Escape\n"
"Select your root: Left Mouse Button\n"
"Place your root on available position: Left Mouse Button\n"
"\n").percent_y(20, x=100)
self.settings_objects.append(self.fps_button)
self.settings_objects.append(self.back_button)
self.settings_objects.append(self.info_text)
self.SETTINGS_OBJECTS_CREATED = True
def create_rules_objects(self):
""" Init rules objects """
self.info_text = Text(self, text="Rules\n\n"
"To win this game, you need to capture the enemy root.\n"
"Enemy root is dyed red.\n"
"To do this, you need to grow your root.\n"
"Your root is dyed blue.\n\n"
"When you click on your root you can choose up to 6\n"
"positions where root can grow.\n"
"Positions where your root can grow are dyed green.\n\n"
f"Every root has energy. Energy can be from 0 to {self.max_energy}.\n"
f"Root energy is a number on root.\n"
"If root that you clicked has more than 1 energy,\n"
"you can click on available positions\n"
"where root can grow to grow your root.\n\n"
"If root that you clicked has more energy than enemy root,\n"
"you can grow your root on enemy root, enemy root will be destroyed.").percent_y(0)
self.back_button = Button(self, text="Back").percent(8, 8)
self.rules_objects.append(self.info_text)
self.rules_objects.append(self.back_button)
def create_info_objects(self):
""" Init info objects """
self.info_text = Text(self, text="Hello\n\n"
"This is a simple RTS (Real Time Strategy) game.\n\n"
"Defeat the enemy root and conquer this hexagon map!\n\n"
"Made on 03.02.23.\n\n"
"Written on Python programming language\n\n"
"using module Pygame that uses SDL.\n\n"
"Made by @ved3v.\n\n"
f"Game version: {self.version}").percent_y(10)
self.back_button = Button(self, text="Back").percent(8, 8)
self.info_objects.append(self.info_text)
self.info_objects.append(self.back_button)
def get_pos_for_hex_grid(self, position, size):
""" Returns position for hex grid using given x and y coordinates """
x = position[0]
y = position[1]
should_offset = y % 2 == 0
width = pow(3, 0.5) * size
height = 2 * size
horizontal_distance = width
vertical_distance = height * (3 / 4)
offset = width / 2 if should_offset else 0
pos = [x * horizontal_distance + offset,
y * vertical_distance]
if self.grid_map[x][y]:
return pos
else:
return None
def add_hexagon(self, x, y, color, outline_color):
""" Adds hexagon object to the list of hexagons """
pos = self.get_pos_for_hex_grid(position=[x, y], size=self.hexagon_size)
hexagon = Hexagon(self, pos=pos,
hexagon_size=[self.hexagon_size, self.hexagon_size],
hex_pos=[x, y],
color=color,
outline_color=outline_color,
foreground=(100, 100, 100))
if pos is not None:
if y % 2 == 0:
if x % 3 == 0:
self.hexagons.append(hexagon)
elif (x + 1) % 3 == 0:
self.hexagons.append(hexagon)
def create_hex_grid(self):
""" Generates hexagon grid map """
for y in range(len(self.grid_map)):
for x in range(len(self.grid_map)):
self.add_hexagon(x, y, self.grid_hex_color, self.grid_hex_outline_color)
def create_hex_grid_lines(self, i):
""" Generates lines connecting all hexagons on grid map """
for obj in self.hexagons:
for j, p in enumerate(self.player.pos_list):
pos1 = [p[0] + i.pos[0],
p[1] + i.pos[1]]
pos2 = [round(pos1[0] + math.sin(deg_to_rad(j * 60 + 120)) * self.hexagon_grid_length),
round(pos1[1] + math.cos(deg_to_rad((j * 60 + 120))) * self.hexagon_grid_length)]
if touched(obj.pos[0] + Hexagon.surface_size[0] / 2, Hexagon.surface_size[0] / 2, pos2[0], 1,
obj.pos[1] + Hexagon.surface_size[0] / 2, Hexagon.surface_size[0] / 2, pos2[1], 1):
self.lines.append(Line(self, pos1, pos2, color=self.grid_line_color))
def new_game(self):
""" game variables that you need to reset to make a new game """
# boolean main variables
self.FIRST_ITERATION = True
self.WIN = False
self.LOSE = False
# game variables that you don't need to change here
self.cords = [-1385 + self.app.WIDTH / 2 - Hexagon.surface_size[0],
-2250 + self.app.HEIGHT / 2 - Hexagon.surface_size[1]]
self.counter = 1
self.hexagon_grid_length = self.hexagon_size * 2
self.selected_hexagon = None
self.selected_enemy_hexagon = None
self.nearby_hexagons = []
self.nearby_enemy_hexagons = []
self.player_hexagons = []
self.enemy_hexagons = []
# game map variables
self.hexagons = []
self.lines = []
self.grid_map_image = pygame.transform.rotate(pygame.image.load("maps/two-way.png"), 90)
self.grid_map = [[1 if self.grid_map_image.get_at((x, y)) == (0, 0, 0, 255) else 0 for x in
range(self.grid_map_image.get_size()[0])] for y in range(self.grid_map_image.get_size()[1])]
self.grid_map_size = [len(self.grid_map) * (self.hexagon_size + self.hexagon_grid_length),
len(self.grid_map[0]) * (self.hexagon_size + self.hexagon_grid_length)]
# user specified game options
self.difficulty = self.difficulty_options.get_current_option()
self.speed = self.speed_options.get_current_option()
self.player_color = self.player_color_picker.get_current_option()
self.selected_hexagon_color = self.selected_hexagon_color_picker.get_current_option()
self.nearby_hexagon_color = self.nearby_hexagon_color_picker.get_current_option()
self.enemy_color = self.enemy_color_picker.get_current_option()
self.game_mode = self.game_mode_options.get_current_option()
# changing difficulty
if self.difficulty == "Easy":
self.player_wait_ticks = 120
self.enemy_wait_ticks = 180
if self.difficulty == "Normal":
self.player_wait_ticks = 120
self.enemy_wait_ticks = 120
if self.difficulty == "Hard":
self.player_wait_ticks = 180
self.enemy_wait_ticks = 120
if self.difficulty == "Super Hard":
self.player_wait_ticks = 240
self.enemy_wait_ticks = 120
# changing speed
if self.speed == "Slow":
self.player_wait_ticks /= 0.5
self.enemy_wait_ticks /= 0.5
if self.speed == "Fast":
self.player_wait_ticks /= 2
self.enemy_wait_ticks /= 2
if self.speed == "Super Fast":
self.player_wait_ticks /= 4
self.enemy_wait_ticks /= 4
# changing game mode
if self.game_mode == "Fast":
self.max_energy = 20
def create_game_objects(self):
""" Init game objects """
self.new_game()
self.win_label = Label(self, text="You win!").center()
self.lose_label = Label(self, text="You lose!").center()
self.back_button = Button(self, text="Menu").percent(8, 8)
self.create_hex_grid()
for i in self.hexagons:
if isinstance(i, Hexagon):
if i.hex_pos == [8, 15]:
self.player = i
if i.hex_pos == [8, 1]:
self.enemy = i
self.player.set_color(self.player_color)
self.player.set_energy(1)
self.enemy.set_color(self.enemy_color)
self.enemy.set_energy(1)
self.selected_hexagon = None
for obj in self.hexagons:
self.create_hex_grid_lines(obj)
def change_mode(self, mode):
"""
Changes mode to a new mode if it's matches one of the possible modes,
clearing all variables of all modes except settings
"""
def clear():
""" Clears all variables of all modes except settings """
self.main_menu_objects.clear()
self.info_objects.clear()
self.rules_objects.clear()
self.new_game_objects.clear()
if mode == "main menu":
self.mode = mode
clear()
self.create_main_menu_objects()
if mode == "settings":
self.mode = mode
clear()
self.create_settings_objects()
elif mode == "info":
self.mode = mode
clear()
self.create_info_objects()
elif mode == "rules":
self.mode = mode
clear()
self.create_rules_objects()
elif mode == "new game":
self.mode = mode
clear()
self.create_new_game_objects()
elif mode == "game":
self.mode = mode
clear()
self.create_game_objects()
def scroll_info_text(self, event):
""" Scrolls self.info_text on mouse wheel event """
if event.type == pygame.MOUSEWHEEL:
if event.y < 0 and self.info_text.pos[1] > -self.info_text.size[1]:
self.info_text.update_y(self.info_text.pos[1] + event.y * self.scroll_scale)
elif event.y > 0 and self.info_text.pos[1] < self.info_text.size[1] - self.scroll_scale:
self.info_text.update_y(self.info_text.pos[1] + event.y * self.scroll_scale)
def select_hexagon(self, obj):
""" Selects player hexagon """
self.selected_hexagon = obj
self.selected_hexagon.set_color(self.selected_hexagon_color)
def select_enemy_hexagon(self, obj):
""" Selects enemy hexagon """
self.selected_enemy_hexagon = obj
self.selected_enemy_hexagon.set_color(self.selected_enemy_hexagon_color)
def create_player_hexagon(self, obj):
""" Creates player hexagon """
self.selected_hexagon.set_energy(self.selected_hexagon.energy - 1)
obj.set_color(self.player_color)
obj.set_energy(1)
self.player_hexagons.append(obj)
self.selected_hexagon = obj
def create_enemy_hexagon(self, obj):
""" Creates enemy hexagon """
self.selected_enemy_hexagon.set_energy(self.selected_enemy_hexagon.energy - 1)
obj.set_color(self.enemy_color)
obj.set_energy(0)
self.enemy_hexagons.append(obj)
self.selected_enemy_hexagon = obj
def get_nearby_pos(self, j, hexagon_point, selected_hexagon):
"""
:param j: one of the angles of the hexagon that could be from 0 to 6 not included
:param hexagon_point: position of one of the angles of the hexagon
:param selected_hexagon: selected Hexagon object
:return: position of neighbor hexagon for given side for player or enemy selected hexagon
"""
pos1 = [hexagon_point[0] + selected_hexagon.pos[0] + self.cords[0],
hexagon_point[1] + selected_hexagon.pos[1] + self.cords[1]]
pos2 = [round(pos1[0] + math.sin(deg_to_rad(j * 60 + 120)) * (self.hexagon_grid_length + 20)),
round(pos1[1] + math.cos(deg_to_rad((j * 60 + 120))) * (self.hexagon_grid_length + 20))]
return pos1, pos2
def get_nearby_hexagons_for_enemy(self):
""" Locates nearby hexagons for enemy using their position """
self.selected_enemy_hexagon = random.choice([self.enemy] + self.enemy_hexagons)
self.nearby_enemy_hexagons.clear()
for j, p in enumerate(self.player.pos_list):
pos1, pos2 = self.get_nearby_pos(j, p, self.selected_enemy_hexagon)
for obj in self.hexagons:
if touched(obj.pos[0] + self.cords[0] + Hexagon.surface_size[0] / 2, Hexagon.surface_size[0] / 2,
pos2[0], 1,
obj.pos[1] + self.cords[1] + Hexagon.surface_size[0] / 2, Hexagon.surface_size[0] / 2,
pos2[1], 1) and obj not in self.enemy_hexagons:
self.nearby_enemy_hexagons.append(obj)
def get_nearby_hexagons_for_player(self):
""" Locates nearby hexagons for enemy using their position """
if self.selected_hexagon is not None:
self.nearby_hexagons.clear()
for j, p in enumerate(self.player.pos_list):
pos1, pos2 = self.get_nearby_pos(j, p, self.selected_hexagon)
for obj in self.hexagons:
if touched(obj.pos[0] + self.cords[0] + Hexagon.surface_size[0] / 2, Hexagon.surface_size[0] / 2,
pos2[0], 1,
obj.pos[1] + self.cords[1] + Hexagon.surface_size[0] / 2, Hexagon.surface_size[0] / 2,
pos2[1], 1) and obj not in self.player_hexagons:
obj.set_color(self.nearby_hexagon_color)
self.nearby_hexagons.append(obj)
def update(self, mouse_buttons, mouse_position, events, keys):
""" Main game logic """
# if self.mode == "bloom":
# # test
#
# # self.app.DISPLAY.fill((0, 0, 0))
#
# for obj in self.bloom_objects:
# obj.update()
# if mouse_buttons[0]:
# if obj.last_pos == [-100, -100]:
# obj.last_pos = mouse_position
# obj.pos = mouse_position
# else:
# obj.pos = [-100, -100]
# obj.last_pos = [-100, -100]
#
# self.counter += 1
# if self.counter > 100:
# self.alpha = 0
# self.counter = 0
#
# # for event in events:
# # if event.type == pygame.MOUSEBUTTONDOWN and event.button == pygame.BUTTON_LEFT:
# # for obj in self.bloom_objects:
# # obj.pos = Pos.sub_pos(mouse_position, [Bloom.surface_size // 2, Bloom.surface_size // 2])
# # obj.reset()
#
# # self.counter += 1
# # if self.counter > 8:
# # self.counter = 0
# # self.bloom_objects[0].alpha += 1
# # # self.bloom_objects[0].set_size(self.bloom_objects[0].s + 4)
# # if self.bloom_objects[0].alpha > 255:
# # self.bloom_objects[0].alpha = 0
# # print(self.bloom_objects[0].alpha)
if self.mode == "main menu":
self.app.DISPLAY.blit(self.background_image, (0, 0))
for obj in self.main_menu_objects:
obj.update()
if self.play_button.clicked(mouse_buttons, mouse_position):
self.change_mode("new game")
if self.settings_button.clicked(mouse_buttons, mouse_position):
self.change_mode("settings")
if self.info_button.clicked(mouse_buttons, mouse_position):
self.change_mode("info")
if self.rules_button.clicked(mouse_buttons, mouse_position):
self.change_mode("rules")
if self.exit_button.clicked(mouse_buttons, mouse_position):
self.app.RUN = False
if self.mode == "settings":
self.app.DISPLAY.blit(self.background_image, (0, 0))
for obj in self.settings_objects:
obj.update()
for event in events:
if event.type == pygame.MOUSEWHEEL:
if event.y < 0 and self.info_text.pos[1] > -self.info_text.size[1]:
self.info_text.update_y(self.info_text.pos[1] + event.y * self.scroll_scale, 100)
elif event.y > 0 and self.info_text.pos[1] < self.info_text.size[1] - self.scroll_scale:
self.info_text.update_y(self.info_text.pos[1] + event.y * self.scroll_scale, 100)
if self.fps_button.clicked(mouse_buttons, mouse_position):
self.FPS_ENABLED = not self.FPS_ENABLED
if self.FPS_ENABLED:
self.fps_button.update_text("Hide fps")
else:
self.fps_button.update_text("Show fps")
if self.back_button.clicked(mouse_buttons, mouse_position):
self.change_mode("main menu")
if self.mode == "info":
self.app.DISPLAY.blit(self.background_image, (0, 0))
for obj in self.info_objects:
obj.update()
for event in events:
self.scroll_info_text(event)
if self.back_button.clicked(mouse_buttons, mouse_position):
self.change_mode("main menu")
if self.mode == "rules":
self.app.DISPLAY.blit(self.background_image, (0, 0))
for obj in self.rules_objects:
obj.update()
for event in events:
self.scroll_info_text(event)
if self.back_button.clicked(mouse_buttons, mouse_position):
self.change_mode("main menu")
if self.mode == "new game":
self.app.DISPLAY.blit(self.background_image, (0, 0))
for obj in self.new_game_objects:
obj.update()
self.difficulty_options.clicked(mouse_buttons, mouse_position)
self.speed_options.clicked(mouse_buttons, mouse_position)
self.player_color_picker.clicked(mouse_buttons, mouse_position)
self.enemy_color_picker.clicked(mouse_buttons, mouse_position)
self.selected_hexagon_color_picker.clicked(mouse_buttons, mouse_position)
self.nearby_hexagon_color_picker.clicked(mouse_buttons, mouse_position)
self.game_mode_options.clicked(mouse_buttons, mouse_position)
self.map_options.clicked(mouse_buttons, mouse_position)
if self.start_game_button.clicked(mouse_buttons, mouse_position):
self.change_mode("game")
if self.back_button.clicked(mouse_buttons, mouse_position):
self.change_mode("main menu")
if self.mode == "game":
self.app.DISPLAY.blit(self.background_image, (0, 0))
# self.app.DISPLAY.fill((0, 0, 0))
if not self.WIN and not self.LOSE:
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN and not self.FIRST_ITERATION:
# set colors
for i, obj in enumerate(self.hexagons):
if obj == self.player or obj in self.player_hexagons:
obj.set_color(self.player_color)
elif obj == self.enemy or obj in self.enemy_hexagons:
obj.set_color(self.enemy_color)
elif obj in self.nearby_hexagons:
obj.set_color(self.grid_hex_color)
else:
obj.set_color(self.grid_hex_color)
# player logic
for i, obj in enumerate(self.hexagons):
if touched(obj.pos[0] + self.cords[0] + Hexagon.surface_size[0] / 2,
Hexagon.surface_size[0] / 2, mouse_position[0], 1,
obj.pos[1] + self.cords[1] + Hexagon.surface_size[0] / 2,
Hexagon.surface_size[0] / 2, mouse_position[1], 1):
if obj == self.player or obj in self.player_hexagons and obj.energy > 1:
self.select_hexagon(obj)
self.get_nearby_hexagons_for_player()
if obj in self.nearby_hexagons and self.selected_hexagon is not None and \
self.selected_hexagon.energy > 1:
if obj == self.enemy or obj in self.enemy_hexagons:
energy = self.selected_hexagon.energy - 1
obj.set_energy(obj.energy - energy)
self.selected_hexagon.set_energy(self.selected_hexagon.energy - energy)
if obj.energy <= 0:
if obj == self.enemy:
self.WIN = True
break
else:
self.enemy_hexagons.remove(obj)
self.player_hexagons.append(obj)
obj.set_energy(-obj.energy)
obj.set_color(self.player_color)
else:
self.create_player_hexagon(obj)
self.select_hexagon(obj)
self.get_nearby_hexagons_for_player()
break
# user input handling
if keys[pygame.K_ESCAPE]:
self.change_mode("main menu")
# game map navigation
# top
if mouse_position[1] - self.map_move_reaction < 0 and self.cords[1] < self.grid_map_size[1] / 8:
self.cords[1] += self.navigation_speed * self.app.delta_time * self.app.MAX_FPS
# bottom
if mouse_position[1] + self.map_move_reaction > self.app.HEIGHT and self.cords[1] > -self.grid_map_size[1] / 2:
self.cords[1] -= self.navigation_speed * self.app.delta_time * self.app.MAX_FPS
# left
if mouse_position[0] - self.map_move_reaction < 0 and self.cords[0] < self.grid_map_size[0] / 8:
self.cords[0] += self.navigation_speed * self.app.delta_time * self.app.MAX_FPS
# right
if mouse_position[0] + self.map_move_reaction > self.app.WIDTH and self.cords[0] > -self.grid_map_size[0] / 2:
self.cords[0] -= self.navigation_speed * self.app.delta_time * self.app.MAX_FPS
# show grid lines
for obj in self.lines:
obj.update()
# show grid hexagons
for i, obj in enumerate(self.hexagons):
obj.update()
if not self.WIN and not self.LOSE:
if self.counter % self.player_wait_ticks == 0:
# increasing player energy
for obj in self.player_hexagons:
if obj.energy < self.max_energy:
obj.set_energy(obj.energy + 1)
if self.player.energy < self.max_energy:
self.player.set_energy(self.player.energy + 1)
# bot logic
if self.counter % self.enemy_wait_ticks == 0:
self.get_nearby_hexagons_for_enemy()
for obj in self.hexagons:
if obj in self.nearby_enemy_hexagons and self.selected_enemy_hexagon is not None and \
self.selected_enemy_hexagon.energy > 1:
if obj in self.player_hexagons:
energy = self.selected_enemy_hexagon.energy - 1
obj.set_energy(obj.energy - energy)
self.selected_enemy_hexagon.set_energy(self.selected_enemy_hexagon.energy - energy)
if obj.energy <= 0:
self.player_hexagons.remove(obj)
self.enemy_hexagons.append(obj)
obj.set_energy(-obj.energy)
obj.set_color(self.enemy_color)
else:
self.create_enemy_hexagon(obj)
break
# increasing bot energy
for obj in [self.enemy] + self.enemy_hexagons:
if obj.energy < self.max_energy:
obj.set_energy(obj.energy + 1)
# win
if len(self.player_hexagons) == len(self.hexagons) - 1:
self.WIN = True
# lose
if self.player in self.enemy_hexagons:
self.LOSE = True
if self.WIN:
self.win_label.update()
self.back_button.update()
if self.back_button.clicked(mouse_buttons, mouse_position):
self.change_mode("main menu")
if self.counter % 12 == 0:
self.selected_hexagon = random.choice(self.hexagons)
self.selected_hexagon.set_color(self.player_color)
if self.LOSE:
self.lose_label.update()
self.back_button.update()
if self.back_button.clicked(mouse_buttons, mouse_position):
self.change_mode("main menu")
if self.counter % 12 == 0:
self.selected_hexagon = random.choice(self.hexagons)
self.selected_hexagon.set_color(self.enemy_color)
self.counter += 1
if self.counter > 2000:
self.counter = 0
if self.FIRST_ITERATION and self.counter % 30 == 0:
self.FIRST_ITERATION = False
# things that settings change
if self.FPS_ENABLED:
self.fps_label.update_text(round(self.app.CLOCK.get_fps()))
self.fps_label.update()