/
Main.py
495 lines (412 loc) · 16.8 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
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
# Import required modules
import pgzrun, pygame, pgzero
from pgzero.builtins import *
# Import from python files
import Board
import Units
import Dice
import Update
# Game states and global variables
# 0 menu, 1 game over, 2 player1, 3 player2
game_state = 2
move_state = False
actor_pos = 0
last_move_x = 0
last_move_y = 0
# Active tiles for scanning
active_move_tile = []
active_shoot_tile = []
# A function to determine if a click token can be moved,
# and how far they are able to move
def start_action(player, pos):
# Declares global variables for function,
# and resets functions lock state
global actor_pos
global last_move_x
global last_move_y
lock_state = False
# Loops though player tokens
for each in player:
# Check if this token is locked and it has actions left
if each[1].locked is True and each[1].action_taken != 0:
# Change the functions lock state to stop any other token from being activated
lock_state = True
movement_check(pos, each)
shoot_check(pos, each)
# Check if there is no locked tokens
if lock_state is False:
# Loops though player tokens
for each in player:
movement_check(pos, each)
shoot_check(pos, each)
def movement_check(pos, each):
global actor_pos
global last_move_x
global last_move_y
# Create function from here
# Check if this token collides with the mouse click
if each[0].collidepoint(pos):
# check number of actions is equal to or less than total action,
# but more than zero actions
if each[1].action_value >= each[1].action_taken > 0:
# Loop though the boards x axis
for x in range(board_x):
# Loop though the board y axis
for y in range(board_y):
# After check to make sure right token is selected,
# store the active tokens actor position
actor_pos = each[0].pos
# Check which board tile this token collides with
if each[0].collidepoint(board[x][y].pos):
# Loop though this tokens move range
last_move_x = x
last_move_y = y
scan_x = each[1].movement_value
scan_range = each[1].movement_value * 2 + 1
for row in range(scan_range):
scan_y = each[1].movement_value
for col in range(scan_range):
move_scan(x, y, scan_x, scan_y)
scan_y -= 1
scan_x -= 1
def shoot_check(pos, each):
global actor_pos
global last_move_x
global last_move_y
# Create function from here
# Check if this token collides with the mouse click
if each[0].collidepoint(pos):
# check number of actions is equal to or less than total action,
# but more than zero actions
if each[1].action_value >= each[1].action_taken > 0:
# Loop though the boards x axis
for x in range(board_x):
# Loop though the board y axis
for y in range(board_y):
# After check to make sure right token is selected,
# store the active tokens actor position
actor_pos = each[0].pos
# Check which board tile this token collides with
if each[0].collidepoint(board[x][y].pos):
# Loop though this tokens move range
last_move_x = x
last_move_y = y
scan_x = each[1].weapons.max_range
scan_range = scan_x * 2 + 1
for row in range(scan_range):
scan_y = each[1].weapons.max_range
for col in range(scan_range):
if game_state == 2:
shoot_scan(x, y, scan_x, scan_y, player2)
elif game_state == 3:
shoot_scan(x, y, scan_x, scan_y, player1)
scan_y -= 1
scan_x -= 1
# A function to check if a valid move has been made,
# execute move selected,
# reset movement scan,
# and update game state
def end_movement(player, pos):
# Set global states for function
global game_state
swap_turn = False
# Loop though all active tiles
for item in active_move_tile:
# Check mouse pos collides with an active tile
if item.collidepoint(pos):
# Loop though all token in current players turn
for token in player:
# Check token matches active actor pos
if token[0].pos == actor_pos:
# Update board occupation,
# remove token from current pos
board_occupied[last_move_x][last_move_y] = 0
# Move active token to clicked tile
token[0].pos = item.pos
# Loop though x axis
for x in range(board_x):
# Loop though y axis
for y in range(board_y):
# Check which board tile was clicked on
if item.collidepoint(board[x][y].pos):
# Update occupied space
board_occupied[x][y] = 1
# Set the lock state to true
token[1].locked = True
# Decrease the number of action for this token by one
token[1].action_taken -= 1
# Check if this token has used all its actions
if token[1].action_taken == 0:
# Change the swap_turn state to true
swap_turn = True
# Change the token lock state to False
token[1].locked = False
# Loop though all tiles in active tiles
for item in active_move_tile:
# Loop though the boards x axis
for x in range(board_x):
# Loop though boards y axis
for y in range(board_y):
# Check if click collided with this tile
if item.collidepoint(board[x][y].pos):
# Remove board scan from this pos
un_move_scan(item.pos, x, y)
end_turn(swap_turn)
def end_attack(atk_player, def_player, pos):
# Loop though all active tiles
global game_state
swap_turn = False
for item in active_shoot_tile:
# Check mouse pos collides with an active tile
if item.collidepoint(pos):
for token in atk_player:
if token[0].pos == actor_pos:
att_dice = token[1].weapons.atk_colour
print(att_dice)
att_no = token[1].quantity * token[1].weapons.number_dice
print(att_no)
for each in def_player:
if each[0].collidepoint(pos):
print(each[1].name)
def_dice = each[1].def_colour
print(def_dice)
att_outcome = roll_att(att_dice, att_no, def_dice)
print(att_outcome)
if att_outcome is False:
print("Attack Blocked")
else:
for damage in range(att_outcome):
print("Attack hit")
each[1].hp -= 1
att_outcome -= 1
if each[1].hp <= 0:
each[1].quantity -= 1
if each[1].quantity <= 0:
print(each[1].name + " is killed")
# Loop though the boards x axis
for x in range(board_x):
# Loop though boards y axis
for y in range(board_y):
# Check if click collided with this tile
if each[0].collidepoint(board[x][y].pos):
board_occupied[x][y] = 0
each[1].state = 1
def_player.remove(each)
break
# Set the lock state to true
token[1].locked = True
# Decrease the number of action for this token by one
token[1].action_taken -= 1
# Check if this token has used all its actions
if token[1].action_taken == 0:
# Change the swap_turn state to true
swap_turn = True
# Change the token lock state to False
token[1].locked = False
# Loop though all tiles in active tiles
for item in active_shoot_tile:
# Loop though the boards x axis
for x in range(board_x):
# Loop though boards y axis
for y in range(board_y):
# Check if click collided with this tile
if item.collidepoint(board[x][y].pos):
# Remove board scan from this pos
un_move_scan(item.pos, x, y)
end_turn(swap_turn)
# Function to check range of movement and if there are any collisions
def move_scan(x, y, rang_x, rang_y):
# Set global variables for function
global board_occupied
global move_state
global active_move_tile
move_state = True
try:
# Check if tile is occupied first
if board_occupied[x + rang_x][y + rang_y] == 0:
# Added select tile to list if tile is empty
active_move_tile.append(board[x + rang_x][y + rang_y])
# print(active_tile[-1]) Debug
# Change the last tile added to active tile list to green
active_move_tile[-1] = Actor('move_square')
active_move_tile[-1].pos = (((x + rang_x) * 68) + 54, ((y + rang_y) * 68) + 54)
# Check if this tile is on the board
if active_move_tile[-1].collidepoint(board[x + rang_x][y + rang_y].pos):
# Update board with new tile
board[x + rang_x][y + rang_y] = active_move_tile[-1]
else:
active_move_tile.pop()
except:
print("")
def shoot_scan(x, y, scan_x, scan_y, player):
global board_occupied
global move_state
global active_shoot_tile
move_state = True
try:
# Check if tile is occupied first
if board_occupied[x + scan_x][y + scan_y] == 1:
# Added select tile to list if tile is empty
active_shoot_tile.append(board[x + scan_x][y + scan_y])
# print(active_tile[-1]) Debug
# Change the last tile added to active tile list to green
active_shoot_tile[-1] = Actor('shoot_square')
active_shoot_tile[-1].pos = (((x + scan_x) * 68) + 54, ((y + scan_y) * 68) + 54)
# Check if this tile is on the board
if active_shoot_tile[-1].collidepoint(board[x + scan_x][y + scan_y].pos):
for each in player:
if each[0].pos == active_shoot_tile[-1].pos:
# Update board with new tile
board[x + scan_x][y + scan_y] = active_shoot_tile[-1]
else:
active_shoot_tile.pop()
except:
print("")
# Function to remove drawn movement area
def un_move_scan(pos, x, y):
# Set global states for function
global move_state
# reset move state to False
move_state = False
# replace green tile with blank tile
board[x][y] = Actor('blank_token')
board[x][y].pos = pos
def roll_att(att_dice, att_no, def_dice):
if att_dice == 0:
att_value = Dice.roll_white_atk_dice(att_no)
elif att_dice == 1:
att_value = Dice.roll_black_atk_dice(att_no)
elif att_dice == 2:
att_value = Dice.roll_red_atk_dice(att_no)
if def_dice == 0:
def_value = Dice.roll_black_def_dice(att_no)
elif def_dice == 1:
def_value = Dice.roll_red_def_dice(att_no)
print("Rolled Attack: " + str(att_value) + " Rolled Defence: " + str(def_value))
print("Attack Damage: " + str(att_value - def_value))
if att_value > def_value:
return att_value - def_value
else:
return False
def end_turn(swap_turn):
global game_state
global player1
global player2
any_token = False
if Update.end_game(player1, player2) is True:
game_state = 1
if game_state != 1:
if game_state == 2:
for each in player2:
if each[1].action_taken > 0:
any_token = True
if game_state == 3:
for each in player1:
if each[1].action_taken > 0:
any_token = True
# Check swap state and change game state if true
if swap_turn is True and any_token is True:
if game_state == 2:
game_state = 3
elif game_state == 3:
game_state = 2
# Set reset value
reset = True
# Loop though player 1 token
for each in player1:
# Check if token has action left
if each[1].action_taken > 0:
# Update reset
reset = False
# Loop though player 2 token
for each in player2:
# Check if token has action left
if each[1].action_taken > 0:
# Update reset
reset = False
# If all token have used all there moves reset the phase
if reset is True:
# Loop though token in player 1
for each in player1:
# Reset action value and unlock token
each[1].action_taken = each[1].action_value
each[1].locked = False
# Loop though token in player 2
for each in player2:
# Reset action value and unlock token
each[1].action_taken = each[1].action_value
each[1].locked = False
# Function to determine what to do on a mouse click
def on_mouse_down(pos):
# Set global variables
global actor_pos
global last_move_x
global last_move_y
global active_move_tile
global active_shoot_tile
global move_state
# Check if there is a token selected
if move_state is False:
# Check who's turn it is
if game_state == 2:
# Start movement
start_action(player1, pos)
elif game_state == 3:
start_action(player2, pos)
else:
if game_state == 2:
end_movement(player1, pos)
end_attack(player1, player2, pos)
elif game_state == 3:
end_movement(player2, pos)
end_attack(player2, player1, pos)
active_move_tile = []
active_shoot_tile = []
# Display size
WIDTH = 1280
HEIGHT = 720
# Display title
TITLE = "Star Wars Legion Lite"
# The boards size
board_x = 9
board_y = 9
# Generate the board and occupied board list
board = [[0] * board_x for i in range(board_y)]
board_occupied = [[0] * board_x for i in range(board_y)]
# Asigns white tiles to all board spaces
Board.white_board(board, board_x, board_y)
# Builds armys for player 1 and 2
player1 = Units.set_up_rebel()
player2 = Units.set_up_imperial()
# Places tokens on the board and occupies those spaces
player1[0][0].pos = board[1][0].pos
board_occupied[1][0] = 1
player1[1][0].pos = board[7][0].pos
board_occupied[7][0] = 1
player1[2][0].pos = board[4][0].pos
board_occupied[4][0] = 1
player2[0][0].pos = board[1][8].pos
board_occupied[1][8] = 1
player2[1][0].pos = board[7][8].pos
board_occupied[7][8] = 1
player2[2][0].pos = board[4][8].pos
board_occupied[4][8] = 1
# Function to draw to the screen
def draw():
# Clear the screen
screen.clear()
# Loop though board
for row in range(board_x):
for col in range(board_y):
# Draw each board tile
board[row][col].draw()
# Draw all players tokens
for each in player1:
each[0].draw()
for each in player2:
each[0].draw()
if game_state == 1:
screen.clear()
screen.fill((0, 0, 0))
screen.draw.text("Game Over", ((board_x / 2), (board_y / 2)), fontsize=100)
pgzrun.go()