-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscenemanager.py
312 lines (246 loc) · 13.9 KB
/
scenemanager.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
"""
Handles rendering and game logic
"""
import pygame
from json import load
from board import Board
from pathlib import Path
from button import TextButton
from button import ImageButton
import texture
import sys
import time
import button
import player
import piece
import engine
class SceneManager():
"""
Handles rendering and game logic
"""
def __init__(self):
pygame.init()
# Open config file, and assign read json to config
config_f = open(Path("config.json"))
self.config = load(config_f)
config_f.close()
# Create window, and scale it to be fullscreen
win = pygame.display.set_mode((0,0))
self.screen_resolution = [win.get_width(), win.get_height()]
self.win_size = self.screen_resolution
self.win = pygame.display.set_mode(self.win_size)
# Set window caption, as indicated in config
pygame.display.set_caption(self.config["title_text"])
# Load in texture pack indicated in config, and load in starting position
self.board = Board("assets/images/board_and_pieces/"+ self.config["texture_pack"], [48, 48])
self.board.loadfen(self.config["starting_position"])
# "Game states", indicate which function should be called at start of every frame
self.MAINGAME_STATE = 1
self.state = self.MAINGAME_STATE
self.state_to_function = {self.MAINGAME_STATE: self.maingame}
# Unscaled size the actual surface of game
self.surface_size = [950, 540]
self.surface = pygame.Surface(self.surface_size)
# Offset applied from (0,0)
self.board_offset = [round((self.surface_size[0] - self.board.square_size[0] * 8) / 2), round((self.surface_size[1] - self.board.square_size[0] * 8) / 2)]
self.board_bottomright = [self.board_offset[0]+ self.board.surface.get_width(), self.board_offset[1]+ self.board.surface.get_height()]
# Colors used in ui
self.ui_bg = (10,10,10)
self.ui_white = (255,255,255)
self.ui_black = (0,0,0)
self.ui_secondary = (30,30,30)
self.ui_text = (255,255,255)
self.ui_popup = (100, 100, 255)
# Fonts used in ui
self.title_font = pygame.font.Font(Path("assets/fonts/Gamer.TTF"),50)
self.buttons_font = pygame.font.Font(Path("assets/fonts/Gamer.TTF"),50)
# Title surface, and its position
self.title_surface = self.title_font.render(self.config["title_text"], False, self.ui_text)
self.title_pos = [texture.center_x(self.title_surface, self.surface_size), 20]
# Directory containing sounds
self.sound_pack = "assets/sounds/" + self.config["sound_pack"] +"/"
# Sounds
self.click_sound = self.sound_pack + "click.wav"
# "main ui" buttons
buttons = ["Play", "Settings", "Exit"]
self.main_ui_buttons = button.ButtonHandler([])
start = [35, self.board_offset[1]+5]
height = 50
width = 200
gap = 5 + height
for y in range (0,3):
self.main_ui_buttons.buttons.append( button.TextButton([width,height], [start[0],start[1] + gap * y],Path(self.sound_pack + "click.wav"), buttons[y], self.buttons_font, self.ui_text, self.ui_secondary))
# Player list, and portret ui
self.portrets = "assets/images/ui/portrets/"+self.config["portrets"]+"/"
self.portret_size = [75,75]
self.players = [player.Human(self.portrets + "human.png", self.portret_size, "Human", self.buttons_font, self.ui_text), player.RandomBot(self.portrets + "bot.png", self.portret_size, "Random", self.buttons_font, self.ui_text), player.BasicBot(self.portrets + "bot.png", self.portret_size, "Dumbo1", self.buttons_font, self.ui_text, False, 1), player.BasicBot(self.portrets + "bot.png", self.portret_size, "Dumbo2", self.buttons_font, self.ui_text, False, 2), player.BasicBot(self.portrets + "bot.png", self.portret_size, "Dumbo3", self.buttons_font, self.ui_text, False, 3), player.BasicBot(self.portrets + "bot.png", self.portret_size, "4", self.buttons_font, self.ui_text, False, 4), player.BasicBot(self.portrets + "bot.png", self.portret_size, "Dumbo5", self.buttons_font, self.ui_text, False, 5), player.BasicBot(self.portrets + "bot.png", self.portret_size, "Dumbo6", self.buttons_font, self.ui_text, False, 6)]
self.player_index = 0
self.active_players = [None, None]
self.total_players = len(self.players)
self.portret_underlay_gap = 10
self.other_ui_rect = pygame.Rect([start[0],start[1]*3+height - 20],[width, height*4])
self.portret_rect = pygame.Rect([self.other_ui_rect.left +texture.center_x(self.portret_size, self.other_ui_rect.size), self.other_ui_rect.top + round(self.portret_size[1] /4)], self.portret_size)
self.portret_underlay = self.portret_rect.move([-self.portret_underlay_gap, -self.portret_underlay_gap])
self.portret_underlay.size = [self.portret_underlay.width + self.portret_underlay_gap*2, self.portret_underlay.height+self.portret_underlay_gap*2]
self.portret_name_rect = self.portret_rect.move(0, self.portret_size[1]+20)
self.arrow_buttons = button.ButtonHandler([])
arrow_buttons_size = [40,40]
arrow_buttons_gap = 18
self.arrow_buttons.buttons.append(button.TextButton(arrow_buttons_size, [self.portret_name_rect.left-arrow_buttons_gap-arrow_buttons_size[0], self.portret_name_rect.top] , Path(self.sound_pack + "click.wav"), "<", self.buttons_font, self.ui_text, self.ui_bg))
self.arrow_buttons.buttons.append(button.TextButton(arrow_buttons_size, [self.portret_name_rect.right+arrow_buttons_gap, self.portret_name_rect.top] , Path(self.sound_pack + "click.wav"), ">", self.buttons_font, self.ui_text, self.ui_bg))
confirm_button_size = [150,35]
confirm_button_gap = 50
self.confirm_button = button.TextButton(confirm_button_size, [self.other_ui_rect.left +texture.center_x(confirm_button_size, self.other_ui_rect.size), self.portret_name_rect.top + confirm_button_gap], Path(self.sound_pack + "click.wav"),"Confirm",self.buttons_font, self.ui_black, self.ui_white)
# Ui state
self.PLAY_STATE = "PLAY"
self.SETTINGS_STATE = "SETTINGS"
self.ui_state = None
self.select_color = piece.WHITE
self.ingame = False
# Game stuff
self.ui_promotion_pos = None
self.ui_promotion_size = self.board.square_size
self.ui_promotion_buttons = button.ButtonHandler([])
# Delta time
self.dt = 0
self.last_time = time.time()
# Is [-1,-1] if player hasn't clicked this frame, else is click position on surface
self.click_pos = [-1,-1]
# Board underlay
self.board_underlay_size = [40,40]
self.board_underlay = pygame.Rect([self.board_offset[0]- round(self.board_underlay_size[0]/2), self.board_offset[1]- round(self.board_underlay_size[1]/2)],[(self.board.square_size[0] * 8) + self.board_underlay_size[0], (self.board.square_size[1] * 8)+self.board_underlay_size[1]])
def update_main_ui(self):
# Play pressed:
if self.main_ui_buttons.buttons[0].pressed:
self.main_ui_buttons.buttons[0].pressed = False
self.ui_state = self.PLAY_STATE
# Settings pressed:
elif self.main_ui_buttons.buttons[1].pressed:
self.main_ui_buttons.buttons[1].pressed = False
self.ui_state = self.SETTINGS_STATE
# Exit
elif self.main_ui_buttons.buttons[2].pressed:
self.quit()
def update_arrow_buttons(self):
self.arrow_buttons.updates(self.click_pos)
# Left
if self.arrow_buttons.buttons[0].pressed == True:
self.arrow_buttons.buttons[0].pressed = False
self.player_index -= 1
if self.player_index <= -self.total_players:
self.player_index = 0
# Right
if self.arrow_buttons.buttons[1].pressed == True:
self.arrow_buttons.buttons[1].pressed = False
self.player_index += 1
if self.player_index >= self.total_players:
self.player_index = 0
def update_confirm_button(self):
self.confirm_button.update(self.click_pos)
#
if self.confirm_button.pressed:
self.confirm_button.pressed = False
if self.ui_state == self.PLAY_STATE:
if self.select_color == piece.WHITE:
self.confirm_button.change_font_color(self.ui_white)
self.confirm_button.change_bg_color(self.ui_black)
self.active_players[0] = self.players[self.player_index]
elif self.select_color == piece.BLACK:
self.confirm_button.change_font_color(self.ui_black)
self.confirm_button.change_bg_color(self.ui_white)
self.active_players[1] = self.players[self.player_index]
self.select_color += 1
if self.select_color > 1:
self.select_color = 0
self.ui_state = None
self.ingame = True
def draw_main_ui(self):
self.main_ui_buttons.draws(self.surface)
if self.ui_state == self.PLAY_STATE:
self.update_arrow_buttons()
self.update_confirm_button()
pygame.draw.rect(self.surface, self.ui_secondary, self.other_ui_rect)
self.surface.blit(self.players[self.player_index].image, self.portret_rect)
self.portret_name_rect.x = self.other_ui_rect.left + texture.center_x(self.players[self.player_index].name_surf, self.other_ui_rect.size)
pygame.draw.rect(self.surface, self.ui_bg, self.portret_underlay)
self.surface.blit(self.players[self.player_index].image, self.portret_rect)
self.surface.blit(self.players[self.player_index].name_surf, self.portret_name_rect)
self.arrow_buttons.draws(self.surface)
self.confirm_button.draw(self.surface)
elif self.ui_state == self.SETTINGS_STATE:
pass
def is_click_on_board(self) -> bool:
if self.click_pos[0] >= self.board_offset[0] and self.click_pos[1] >= self.board_offset[1]:
if self.click_pos[0] <= self.board_bottomright[0] and self.click_pos[1] <= self.board_bottomright[1]:
return True
return False
def update_board(self):
if isinstance(self.active_players[self.board.board_data.active], player.Human):
# If click is on board
if self.is_click_on_board():
# Please forgive me god
pos = [-1,-1]
for i in range(0, self.click_pos[0] - self.board_offset[0], self.board.square_size[0]):
pos[0] += 1
for j in range(0, self.click_pos[1] - self.board_offset[1], self.board.square_size[1]):
pos[1] += 1
self.active_players[self.board.board_data.active].touch(self.board.board_data, engine.Coordinate(pos[1], pos[0]))
else:
move = self.active_players[self.board.board_data.active].move(self.board.board_data)
if move != None:
self.board.board_data.apply_move(move)
def maingame(self):
self.main_ui_buttons.updates(self.click_pos)
self.update_main_ui()
if self.ingame:
self.update_board()
# Drawing loop
self.surface.fill(self.ui_bg)
pygame.draw.rect(self.surface, self.ui_secondary, self.board_underlay)
selected = None
self.ui_promotion_buttons.buttons = []
legal_moves = []
#
if isinstance(self.active_players[self.board.board_data.active], player.Human):
#
if self.active_players[self.board.board_data.active].selected != None:
selected = [self.active_players[self.board.board_data.active].selected]
legal_moves = self.active_players[self.board.board_data.active].legal_moves
# Pop up ui for promotion
if self.active_players[self.board.board_data.active].promotion != None:
# Create the buttons
self.ui_promotion_pos = [700,200]
for piece in range(0, 4):
self.ui_promotion_buttons.buttons.append(ImageButton(self.ui_promotion_size, [self.ui_promotion_pos[0], self.ui_promotion_pos[1] +self.ui_promotion_size[1]*piece], self.click_sound, self.board.texture_pack.pieces[self.board.board_data.active][piece]))
# Ui buttons
for i in range(0,len(self.ui_promotion_buttons.buttons)):
self.ui_promotion_buttons.buttons[i].update(self.click_pos)
if self.ui_promotion_buttons.buttons[i].pressed:
self.active_players[self.board.board_data.active].tell_type(self.board.board_data, i)
break;
self.board.draw(self.surface, ((self.board_offset, self.board_offset)), selected, legal_moves)
self.surface.blit(self.title_surface, self.title_pos)
self.ui_promotion_buttons.draws(self.surface)
self.draw_main_ui()
def update(self):
self.state_to_function[self.state]()
def screen_to_window(self):
self.win.blit(pygame.transform.scale(
self.surface, self.win_size), (0, 0))
def start(self):
while True:
self.click_pos = [-1,-1]
self.dt = time.time() - self.last_time
self.last_time = time.time()
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.quit()
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
self.click_pos = button.convert_window_pos(event.pos, self.win_size, self.surface_size)
self.update()
self.screen_to_window()
pygame.display.flip()
def quit(self):
pygame.quit()
sys.exit()