-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
291 lines (200 loc) · 9.03 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import sys, pygame
from core import Board
from core import BLOCK_SPACE
from core import ANGLE_SPACE
from color import BLACK
from color import GREEN
from color import BLUE
from color import RED
from color import WHITE
from gamelogic import GameLogic
from position import Position
from setting import Setting
import os
def get_cell_size(screen_width:int,
screen_heigth:int,
board_size:tuple)->tuple:
"""
Return (width,height) of individual cell
"""
cell_width = (screen_width/board_size[0])
cell_height = (screen_heigth/board_size[1])
cell_size = (int(cell_width),int(cell_height))
return cell_size
def get_rect(width:int,
height:int,
cell_size:tuple)->pygame.Rect:
return pygame.Rect(height*cell_size[0],
width*cell_size[1],
cell_size[0],
cell_size[1])
def get_font(font_location:str):
"""
Tuple of pygame.font
Return (small_font,medium_font,large_font)
"""
small_font = pygame.font.Font(font_location,20)
medium_font = pygame.font.Font(font_location,28)
large_font = pygame.font.Font(font_location,40)
return (small_font,medium_font,large_font)
def center(screen_width:int,
texts:list,
font:pygame.font)->list:
"""
Return list of tuple(surface,center_rect)
screen_width:Width of the screen
texts:text we wanted to center.Each item of text will be separted by
new line
font:pygame font we wanted to use
"""
center_texts = list()
for i,text in enumerate(texts):
line = font.render(text,True,WHITE)
rect = line.get_rect()
rect.center = ((screen.get_width() / 2), (150 + 30*i))
screen.blit(line, rect)
center_texts.append((line,rect))
return center_texts
def show_game_over_screen(screen:pygame.Surface,
font:pygame.font,
game_logic:GameLogic)->bool:
"""
Show game over
"""
game_turn = "Turn : "+ str(game_logic.get_turn_count())
for line,rect in center(screen.get_width(),["Game Over",
game_turn,
"Press enter to exit"],font):
screen.blit(line,rect)
def show_playing_screen(screen:pygame.Surface,
cell_size:tuple,
angel_icon,
devil_icon,
game_logic:GameLogic):
"""
Show the playing screen
Return (new game logic which is changed,whether a move is made)
"""
move_made = False
board_size = game_logic.get_board().get_size()
board_width = board_size[0]
board_height = board_size[1]
#width/column index
for column_index in range(board_width):
#height/row index
for row_index in range(board_height):
if not (column_index<board_width and row_index<board_height):
continue
rect = get_rect(row_index,column_index,cell_size)
#If the position is angel position
if game_logic.get_board().get_position(row_index,column_index)==ANGLE_SPACE:
screen.blit(angel_icon,rect)
#If the position is devil position
elif game_logic.get_board().get_position(row_index,column_index)==BLOCK_SPACE:
screen.blit(devil_icon,rect)
else:
pygame.draw.rect(screen,BLACK,rect)
positions = list()
is_angel_turn = game_logic.is_angel_turn()
avaliable_moves = set()
if is_angel_turn:
avaliable_moves = game_logic.get_avaliable_angel_moves()
else:
avaliable_moves = game_logic.get_avaliable_devil_moves()
for (i,j) in avaliable_moves:
rect = get_rect(i,j,cell_size)
pygame.draw.rect(screen,BLUE,rect)
positions.append(Position(i,j,rect))
left_clicked,_,_ = pygame.mouse.get_pressed()
#if left clicked
if left_clicked==1:
mouse = pygame.mouse.get_pos()
for position in positions:
#if the avaliable position is clicked
if position.rect.collidepoint(mouse):
move_made = True
#if it is an angel turn,move the angel
if is_angel_turn:
game_logic.angel_move(position.row_index,
position.column_index)
else:
game_logic.devil_move(position.row_index,
position.column_index)
return game_logic,move_made
def show_instruction_screen(screen:pygame.Surface,
font:pygame.font,
game_logic:GameLogic):
lines = list()
if game_logic.is_angel_turn():
lines.append("You are an angel")
lines.append("You need to get away from devil")
lines.append(f"You can move {game_logic.get_angel_n()} from current position")
lines.append("Click on any blue square to run away")
else:
lines.append("You are a devil")
lines.append("You need to catch an angel")
lines.append("Click on any blue square to trap an angel")
lines.append("Press enter to get play the game")
lines.append("If you don't want to see instruction again,Press i")
for line,rect in center(screen.get_width(),lines,font):
screen.blit(line,rect)
def game_loop(screen:pygame.Surface,
cell_size:tuple,
game_logic:GameLogic):
"""
cell_size : Invidiual cell size (cell_width,cell_height)
"""
small_font,medium_font,large_font = get_font(resource_path("assets/fonts/OpenSans-Regular.ttf"))
angel_icon = pygame.image.load(resource_path("assets/images/angel.png")).convert()
angel_icon = pygame.transform.scale(angel_icon,cell_size)
devil_icon = pygame.image.load(resource_path("assets/images/devil.jpg")).convert()
devil_icon = pygame.transform.scale(devil_icon,cell_size)
is_instruction = True
is_ignore_instruction = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill(BLACK)
if game_logic.is_game_over():
show_game_over_screen(screen,
medium_font,
game_logic)
if event.type==pygame.KEYDOWN and event.key==pygame.K_RETURN:
break
elif is_instruction and not is_ignore_instruction:
show_instruction_screen(screen,
medium_font,
game_logic)
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_RETURN:
is_instruction=False
elif event.key==pygame.K_i:
is_ignore_instruction = True
else:
game_logic,move_made = show_playing_screen(screen,
cell_size,
angel_icon,
devil_icon,
game_logic)
is_instruction = move_made
pygame.display.flip()
def resource_path(relative_path):
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
if __name__ == "__main__":
setting = Setting(resource_path("setting.config"))
screen_resolution = setting.get_screen_resolution()
grid = setting.get_grid()
pygame.init()
game_logic = GameLogic(Board(grid[0],grid[1]),setting.get_n())
game_logic.init()
screen = pygame.display.set_mode(screen_resolution)
cell_size = get_cell_size(screen_resolution[0],
screen_resolution[1],
game_logic.get_board().get_size())
game_loop(screen,cell_size,game_logic)