-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngeeAnnCity.py
1080 lines (945 loc) · 48.2 KB
/
ngeeAnnCity.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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import pygame
import random
import sys
import csv
import os
# Initialize Pygame
pygame.init()
pygame.mixer.init() # Initialize mixer for sound
# Constants
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 850
GRID_SIZE_ARCADE = 20 # Arcade Mode Grid Size
CELL_SIZE = 25
TITLE_FONT = pygame.font.SysFont("Arial", 40)
MENU_TITLE_FONT = pygame.font.SysFont("Arial", 120)
BUTTON_FONT = pygame.font.SysFont("Arial", 30)
GAME_FONT = pygame.font.SysFont("Arial", 20)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREY = (200, 200, 200)
BLUE = (173, 216, 230)
GREEN = (144, 238, 144)
BUILDINGS = ['Residential', 'Industry', 'Commercial', 'Park', 'Road']
BUILDING_SYMBOLS = {'Residential': 'R', 'Industry': 'I', 'Commercial': 'C', 'Park': 'O', 'Road': '*'}
BACKGROUND_COLOR = (25, 25, 25)
GRID_COLOR = BLACK
CELL_COLOR = WHITE
MARGIN_LEFT = 50
MARGIN_TOP = 200
MARGIN_BOTTOM = 50
MARGIN_RIGHT = 250
TEXT_MARGIN_RIGHT = 20
TEXT_PADDING_LEFT = 20
TEXT_PADDING_TOP = 10
# Load sound effects
pygame.mixer.music.load('./MenuSoundtrack.mp3')
pygame.mixer.music.set_volume(0.6) # Set volume to 60%
pygame.mixer.music.play(-1) # -1 means the music will loop indefinitely
mouse_click_sound = pygame.mixer.Sound('./MouseClick.mp3')
# Screen setup
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.RESIZABLE)
pygame.display.set_caption("Ngee Ann City")
# Function to draw buttons and handle hover effect
def draw_button_with_hover(button, text, font, color, hover_color, surface):
mouse_pos = pygame.mouse.get_pos()
if button.collidepoint(mouse_pos):
pygame.draw.rect(surface, hover_color, button, border_radius=8)
else:
pygame.draw.rect(surface, color, button, border_radius=8)
pygame.draw.rect(surface, BLACK, button, 1, border_radius=8)
draw_centered_text(text, font, BLACK, surface, button)
# Functions to load Ngee Ann Poly image
def load_background_image(path):
return pygame.image.load(path)
def draw_background_image(image, surface):
surface.blit(image, (0, 0))
# Functions to draw text
def draw_text(text, font, color, surface, x, y):
# Draw centered text
text_obj = font.render(text, True, color)
text_rect = text_obj.get_rect(center=(x, y))
surface.blit(text_obj, text_rect)
def draw_left_aligned_text(text, font, color, surface, x, y):
# Draw left-aligned text
text_obj = font.render(text, True, color)
text_rect = text_obj.get_rect(topleft=(x, y))
surface.blit(text_obj, text_rect)
def draw_centered_text(text, font, color, surface, rect):
# Draw centered text within a given rectangle
text_obj = font.render(text, True, color)
text_rect = text_obj.get_rect(center=rect.center)
surface.blit(text_obj, text_rect)
# Prompt for player name
def prompt_player_name():
# Prompt the player to enter their name
pygame.display.update()
screen.fill(BACKGROUND_COLOR)
draw_text('Enter your name:', BUTTON_FONT, WHITE, screen, SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 - 40)
pygame.display.update()
name = ""
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
return name
elif event.key == pygame.K_BACKSPACE:
name = name[:-1]
else:
name += event.unicode
screen.fill(BACKGROUND_COLOR)
draw_text('Enter your name:', BUTTON_FONT, WHITE, screen, SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 - 40)
draw_text(name, BUTTON_FONT, WHITE, screen, SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 + 10)
pygame.display.update()
# Save leaderboard to CSV
def save_leaderboard(name, score):
# Save the player's name and score to the leaderboard CSV file
# Prevent CSV injection
name = name.replace("=", "").replace("+", "").replace("-", "").replace("@", "")
with open('NgeeAnnCity_Arcade_Leaderboard.csv', 'a', newline='') as file:
writer = csv.writer(file)
writer.writerow([name, score])
# Clear saved game for Arcade mode
def clear_saved_game_arcade():
# Delete the saved game file for Arcade mode if it exists
if os.path.exists('NgeeAnnCity_Arcade_SavedGame.csv'):
os.remove('NgeeAnnCity_Arcade_SavedGame.csv')
# Clear saved game for Arcade mode
def clear_saved_game_free_play():
# Delete the saved game file for Arcade mode if it exists
if os.path.exists('NgeeAnnCity_FreePlay_SavedGame.csv'):
os.remove('NgeeAnnCity_FreePlay_SavedGame.csv')
# Display leaderboard
def display_leaderboard():
# Display the leaderboard on the screen
displaying = True
while displaying:
screen.fill(BACKGROUND_COLOR)
draw_text('Leaderboard - Arcade Mode', TITLE_FONT, WHITE, screen, SCREEN_WIDTH // 2, 50)
if os.path.exists('NgeeAnnCity_Arcade_Leaderboard.csv'):
# Load and display the top 10 scores from the leaderboard CSV file
leaderboard = []
with open('NgeeAnnCity_Arcade_Leaderboard.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
leaderboard.append((row[0], int(row[1])))
leaderboard.sort(key=lambda x: x[1], reverse=True) # Sort by score in descending order
y_offset = 100
for i, (name, score) in enumerate(leaderboard[:10]): # Display top 10
draw_text(f'{i + 1}. {name}: {score}', BUTTON_FONT, WHITE, screen, SCREEN_WIDTH // 2, y_offset)
y_offset += 40
else:
draw_text('No leaderboard data available.', BUTTON_FONT, WHITE, screen, SCREEN_WIDTH // 2, 100)
draw_text('Press B to go back to main menu', BUTTON_FONT, WHITE, screen, SCREEN_WIDTH // 2, SCREEN_HEIGHT - 40)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_b:
displaying = False
pygame.display.update()
screen.fill(BACKGROUND_COLOR) # Clear the screen to remove lingering text
pygame.display.update()
return
# Arcade Mode Functions
def calculate_points_arcade(grid, restricted_residential):
points = 0
for row in range(GRID_SIZE_ARCADE):
for col in range(GRID_SIZE_ARCADE):
cell = grid[row][col]
if cell == 'R':
points += calculate_residential_points_arcade(grid, row, col, restricted_residential)
elif cell == 'I':
points += calculate_industry_points_arcade(grid, row, col)
elif cell == 'C':
points += calculate_commercial_points_arcade(grid, row, col)
elif cell == 'O':
points += calculate_park_points_arcade(grid, row, col)
elif cell == '*':
points += calculate_road_points_arcade(grid, row, col)
return points
def calculate_residential_points_arcade(grid, row, col, restricted_residential):
if restricted_residential.get((row, col), False):
return 0 # If this Residential building is restricted, it can't gain or lose points
points = 0
adjacents = [(row-1, col), (row+1, col), (row, col-1), (row, col+1)]
adjacent_to_industry = False
for r, c in adjacents:
if 0 <= r < GRID_SIZE_ARCADE and 0 <= c < GRID_SIZE_ARCADE:
if grid[r][c] == 'I':
adjacent_to_industry = True
restricted_residential[(row, col)] = True # Mark this Residential building as restricted
break
if adjacent_to_industry:
return 1
for r, c in adjacents:
if 0 <= r < GRID_SIZE_ARCADE and 0 <= c < GRID_SIZE_ARCADE:
if grid[r][c] == 'R':
if any(0 <= rr < GRID_SIZE_ARCADE and 0 <= rc < GRID_SIZE_ARCADE and grid[rr][rc] == 'I' for rr, rc in [(r-1, c), (r+1, c), (r, c-1), (r, c+1)]):
points += 1
else:
points += 2
elif grid[r][c] == 'C':
points += 1
elif grid[r][c] == 'O':
points += 2
return points
def calculate_industry_points_arcade(grid, row, col):
points = 1
adjacents = [(row-1, col), (row+1, col), (row, col-1), (row, col+1)]
for r, c in adjacents:
if 0 <= r < GRID_SIZE_ARCADE and 0 <= c < GRID_SIZE_ARCADE:
if grid[r][c] == 'R':
residential_adjacents = [(r-1, c), (r+1, c), (r, c-1), (r, c+1)]
skip_residential = False
for rr, rc in residential_adjacents:
if (rr != row or rc != col) and 0 <= rr < GRID_SIZE_ARCADE and 0 <= rc < GRID_SIZE_ARCADE and grid[rr][rc] == 'I':
skip_residential = True
break
if not skip_residential:
points += 1
return points
def calculate_commercial_points_arcade(grid, row, col):
points = 0
adjacents = [(row-1, col), (row+1, col), (row, col-1), (row, col+1)]
for r, c in adjacents:
if 0 <= r < GRID_SIZE_ARCADE and 0 <= c < GRID_SIZE_ARCADE:
if grid[r][c] == 'R':
residential_adjacents = [(r-1, c), (r+1, c), (r, c-1), (r, c+1)]
skip_residential = False
for rr, rc in residential_adjacents:
if 0 <= rr < GRID_SIZE_ARCADE and 0 <= rc < GRID_SIZE_ARCADE and grid[rr][rc] == 'I':
skip_residential = True
break
if not skip_residential:
points += 1 # No points for adjacent Residential
elif grid[r][c] == 'C':
points += 2
return points
def calculate_park_points_arcade(grid, row, col):
points = 0
adjacents = [(row-1, col), (row+1, col), (row, col-1), (row, col+1)]
for r, c in adjacents:
if 0 <= r < GRID_SIZE_ARCADE and 0 <= c < GRID_SIZE_ARCADE:
if grid[r][c] == 'R':
residential_adjacents = [(r-1, c), (r+1, c), (r, col-1), (r, col+1)]
skip_residential = False
for rr, rc in residential_adjacents:
if 0 <= rr < GRID_SIZE_ARCADE and 0 <= rc < GRID_SIZE_ARCADE and grid[rr][rc] == 'I':
skip_residential = True
break
if not skip_residential:
points += 2
elif grid[r][c] == 'O':
points += 2
return points
def calculate_road_points_arcade(grid, row, col):
# Calculate points for a road
points = 0
adjacents = [(row-1, col), (row+1, col), (row, col-1), (row, col+1)]
for r, c in adjacents:
if 0 <= r < GRID_SIZE_ARCADE and 0 <= c < GRID_SIZE_ARCADE:
if grid[r][c] == '*':
points += 2 # Gain 2 points for each adjacent road
return points
def generate_coins_for_commercial_arcade(grid, row, col):
# Generate coins for a commercial building
coins = 0
adjacents = [(row-1, col), (row+1, col), (row, col-1), (row, col+1)]
for r, c in adjacents:
if 0 <= r < GRID_SIZE_ARCADE and 0 <= c < GRID_SIZE_ARCADE:
if grid[r][c] == 'R':
coins += 1
return coins
def generate_coins_for_industry_arcade(grid, row, col):
# Generate coins for an industry building
coins = 0
adjacents = [(row-1, col), (row+1, col), (row, col-1), (row, col+1)]
for r, c in adjacents:
if 0 <= r < GRID_SIZE_ARCADE and 0 <= c < GRID_SIZE_ARCADE:
if grid[r][c] == 'R':
coins += 1
return coins
def is_adjacent_to_existing_building_arcade(grid, row, col):
# Check if a cell is adjacent to an existing building
adjacents = [(row-1, col), (row+1, col), (row, col-1), (row, col+1)]
for r, c in adjacents:
if 0 <= r < GRID_SIZE_ARCADE and 0 <= c < GRID_SIZE_ARCADE and (grid[r][c] is not None and grid[r][c] != ''):
return True
return False
def save_game_arcade(grid, coins, turn, score, restricted_residential):
# Save the current game state to a CSV file for Arcade mode
with open('NgeeAnnCity_Arcade_SavedGame.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['Grid', 'Coins', 'Turn', 'Score', 'RestrictedResidential'])
for row in grid:
writer.writerow(row)
writer.writerow([coins, turn, score])
for key, value in restricted_residential.items():
writer.writerow([key[0], key[1], value])
def load_game_arcade():
# Load the saved game state from a CSV file for Arcade mode
if os.path.exists('NgeeAnnCity_Arcade_SavedGame.csv'):
with open('NgeeAnnCity_Arcade_SavedGame.csv', 'r') as file:
reader = csv.reader(file)
next(reader) # Skip header
grid = []
for i in range(GRID_SIZE_ARCADE):
row = next(reader, None)
if row is None:
return None, None, None, None, None
grid.append([None if cell == 'None' else cell for cell in row])
coins, turn, score = map(int, next(reader))
restricted_residential = {}
for row in reader:
restricted_residential[(int(row[0]), int(row[1]))] = row[2] == 'True'
return grid, coins, turn, score, restricted_residential
return None, None, None, None, None
# Free Play Mode Functions
def calculate_points_free_play(grid, restricted_residential):
points = 0
for row in range(len(grid)):
for col in range(len(grid[0])):
if grid[row][col] == 'R':
points += calculate_residential_points_free_play(grid, row, col, restricted_residential)
elif grid[row][col] == 'I':
points += calculate_industry_points_free_play(grid, row, col)
elif grid[row][col] == 'C':
points += calculate_commercial_points_free_play(grid, row, col)
elif grid[row][col] == 'O':
points += calculate_park_points_free_play(grid, row, col)
elif grid[row][col] == '*':
points += calculate_road_points_free_play(grid, row, col)
return points
def calculate_residential_points_free_play(grid, row, col, restricted_residential):
if restricted_residential.get((row, col), False):
return 0 # If this Residential building is restricted, it can't gain or lose points
points = 0
adjacents = [(row-1, col), (row+1, col), (row, col-1), (row, col+1)]
adjacent_to_industry = False
for r, c in adjacents:
if 0 <= r < len(grid) and 0 <= c < len(grid[0]):
if grid[r][c] == 'I':
adjacent_to_industry = True
restricted_residential[(row, col)] = True # Mark this Residential building as restricted
break
if adjacent_to_industry:
return 1
for r, c in adjacents:
if 0 <= r < len(grid) and 0 <= c < len(grid[0]):
if grid[r][c] == 'R':
if any(0 <= rr < len(grid) and 0 <= rc < len(grid[0]) and grid[rr][rc] == 'I' for rr, rc in [(r-1, c), (r+1, c), (r, c-1), (r, c+1)]):
points += 1
else:
points += 2
elif grid[r][c] == 'C':
points += 1
elif grid[r][c] == 'O':
points += 2
return points
def calculate_industry_points_free_play(grid, row, col):
points = 1
adjacents = [(row-1, col), (row+1, col), (row, col-1), (row, col+1)]
for r, c in adjacents:
if 0 <= r < len(grid) and 0 <= c < len(grid[0]):
if grid[r][c] == 'R':
residential_adjacents = [(r-1, c), (r+1, c), (r, c-1), (r, c+1)]
skip_residential = False
for rr, rc in residential_adjacents:
if (rr != row or rc != col) and 0 <= rr < len(grid) and 0 <= rc < len(grid[0]) and grid[rr][rc] == 'I':
skip_residential = True
break
if not skip_residential:
points += 1
return points
def calculate_commercial_points_free_play(grid, row, col):
points = 0
adjacents = [(row-1, col), (row+1, col), (row, col-1), (row, col+1)]
for r, c in adjacents:
if 0 <= r < len(grid) and 0 <= c < len(grid[0]):
if grid[r][c] == 'R':
residential_adjacents = [(r-1, c), (r+1, c), (r, c-1), (r, c+1)]
skip_residential = False
for rr, rc in residential_adjacents:
if 0 <= rr < len(grid) and 0 <= rc < len(grid[0]) and grid[rr][rc] == 'I':
skip_residential = True
break
if not skip_residential:
points += 1 # No points for adjacent Residential
elif grid[r][c] == 'C':
points += 2
return points
def calculate_park_points_free_play(grid, row, col):
points = 0
adjacents = [(row-1, col), (row+1, col), (row, col-1), (row, col+1)]
for r, c in adjacents:
if 0 <= r < len(grid) and 0 <= c < len(grid[0]):
if grid[r][c] == 'R':
residential_adjacents = [(r-1, c), (r+1, c), (r, col-1), (r, col+1)]
skip_residential = False
for rr, rc in residential_adjacents:
if 0 <= rr < len(grid) and 0 <= rc < len(grid[0]) and grid[rr][rc] == 'I':
skip_residential = True
break
if not skip_residential:
points += 2
elif grid[r][c] == 'O':
points += 2
return points
def calculate_road_points_free_play(grid, row, col):
# Calculate points for a road
points = 0
adjacents = [(row-1, col), (row+1, col), (row, col-1), (row, col+1)]
for r, c in adjacents:
if 0 <= r < len(grid) and 0 <= c < len(grid[0]):
if grid[r][c] == '*':
points += 2 # Gain 2 points for each adjacent road
return points
def generate_coins_for_commercial_free_play(grid, row, col):
# Generate coins for a commercial building
coins = 0
adjacents = [(row-1, col), (row+1, col), (row, col-1), (row, col+1)]
for r, c in adjacents:
if 0 <= r < len(grid) and 0 <= c < len(grid[0]):
if grid[r][c] == 'R':
coins += 1
return coins
def generate_coins_for_industry_free_play(grid, row, col):
# Generate coins for an industry building
coins = 0
adjacents = [(row-1, col), (row+1, col), (row, col-1), (row, col+1)]
for r, c in adjacents:
if 0 <= r < len(grid) and 0 <= c < len(grid[0]):
if grid[r][c] == 'R':
coins += 1
return coins
def is_adjacent_to_existing_building_free_play(grid, row, col):
# Check if a cell is adjacent to an existing building
adjacents = [(row-1, col), (row+1, col), (row, col-1), (row, col+1)]
for r, c in adjacents:
if 0 <= r < len(grid) and 0 <= c < len(grid[0]) and (grid[r][c] is not None and grid[r][c] != ''):
return True
return False
def expand_grid(grid, new_size):
# Expand the grid to a new size, centered on the existing grid
new_grid = [[None for _ in range(new_size)] for _ in range(new_size)]
old_size = len(grid)
offset = (new_size - old_size) // 2
for row in range(old_size):
for col in range(old_size):
new_grid[row][col] = grid[row][col]
return new_grid
def save_game_free_play(grid, coins, turn, score, restricted_residential, expansion_count):
# Save the current game state to a CSV file for Free Play mode
with open('NgeeAnnCity_FreePlay_SavedGame.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['Grid', 'Coins', 'Turn', 'Score', 'RestrictedResidential', 'ExpansionCount'])
for row in grid:
writer.writerow(row)
writer.writerow([coins, turn, score, expansion_count])
for key, value in restricted_residential.items():
writer.writerow([key[0], key[1], value])
def load_game_free_play():
# Load the saved game state from a CSV file for Free Play mode
if os.path.exists('NgeeAnnCity_FreePlay_SavedGame.csv'):
with open('NgeeAnnCity_FreePlay_SavedGame.csv', 'r') as file:
reader = csv.reader(file)
next(reader) # Skip header
grid = []
row = next(reader, None)
while row and len(row) > 0 and not row[0].isdigit():
grid.append([None if cell == 'None' else cell for cell in row])
row = next(reader, None)
if row:
coins, turn, score, expansion_count = map(int, row)
restricted_residential = {}
for row in reader:
restricted_residential[(int(row[0]), int(row[1]))] = row[2] == 'True'
return grid, coins, turn, score, restricted_residential, expansion_count
return None, None, None, None, None, 0
# Main Menu with buttons
def main_menu():
background_image = load_background_image('./Background.png') # Replace 'background.jpg' with your image file path
while True:
screen.fill(BLACK) # Fill the screen with black color
draw_background_image(background_image, screen) # Draw the background image
button_width = 400
button_height = 70
play_button = pygame.Rect(SCREEN_WIDTH // 2 - button_width // 2, 450, button_width, button_height)
leaderboard_button = pygame.Rect(SCREEN_WIDTH // 2 - button_width // 2, 535, button_width, button_height)
settings_button = pygame.Rect(SCREEN_WIDTH // 2 - button_width // 2, 620, button_width, button_height) # New Settings Button
exit_button = pygame.Rect(SCREEN_WIDTH // 2 - button_width // 2, 705, button_width, button_height)
draw_button_with_hover(play_button, 'Play', BUTTON_FONT, WHITE, GREY, screen)
draw_button_with_hover(leaderboard_button, 'Leaderboard', BUTTON_FONT, WHITE, GREY, screen)
draw_button_with_hover(settings_button, 'Settings', BUTTON_FONT, WHITE, GREY, screen) # Draw Settings Button
draw_button_with_hover(exit_button, 'Exit', BUTTON_FONT, WHITE, GREY, screen)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONUP:
mouse_click_sound.play()
if play_button.collidepoint(event.pos):
play_menu()
elif leaderboard_button.collidepoint(event.pos):
display_leaderboard()
elif settings_button.collidepoint(event.pos): # Handle Settings Button Click
settings_menu()
elif exit_button.collidepoint(event.pos):
pygame.quit()
sys.exit()
pygame.display.update()
# Settings Menu with buttons
def settings_menu():
background_image = load_background_image('./MenuBackground.png') # Replace with your image file path
while True:
screen.fill(BLACK) # Fill the screen with black color
draw_background_image(background_image, screen)
draw_text('Settings', MENU_TITLE_FONT, WHITE, screen, SCREEN_WIDTH // 2, 200)
button_width = 400
button_height = 70
mute_button = pygame.Rect(SCREEN_WIDTH // 2 - button_width // 2, 450, button_width, button_height)
back_button = pygame.Rect(SCREEN_WIDTH // 2 - button_width // 2, 535, button_width, button_height)
draw_button_with_hover(mute_button, 'Mute/Unmute Music', BUTTON_FONT, WHITE, GREY, screen)
draw_button_with_hover(back_button, 'Back', BUTTON_FONT, WHITE, GREY, screen)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONUP:
mouse_click_sound.play()
if mute_button.collidepoint(event.pos):
if pygame.mixer.music.get_volume() > 0:
pygame.mixer.music.set_volume(0)
else:
pygame.mixer.music.set_volume(0.6) # Set to your desired volume level
elif back_button.collidepoint(event.pos):
return
pygame.display.update()
# Play Menu with buttons
def play_menu():
background_image = load_background_image('./MenuBackground.png') # Replace 'background.jpg' with your image file path
while True:
screen.fill(BLACK) # Fill the screen with black color
draw_background_image(background_image, screen)
draw_text('Play', MENU_TITLE_FONT, WHITE, screen, SCREEN_WIDTH // 2, 200)
button_width = 400
button_height = 70
arcade_mode_button = pygame.Rect(SCREEN_WIDTH // 2 - button_width // 2, 450, button_width, button_height)
free_play_button = pygame.Rect(SCREEN_WIDTH // 2 - button_width // 2, 535, button_width, button_height)
back_button = pygame.Rect(SCREEN_WIDTH // 2 - button_width // 2, 620, button_width, button_height)
draw_button_with_hover(arcade_mode_button, 'Arcade Mode', BUTTON_FONT, WHITE, GREY, screen)
draw_button_with_hover(free_play_button, 'Free Play', BUTTON_FONT, WHITE, GREY, screen)
draw_button_with_hover(back_button, 'Back', BUTTON_FONT, WHITE, GREY, screen)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONUP:
mouse_click_sound.play()
if arcade_mode_button.collidepoint(event.pos):
arcade_menu()
elif free_play_button.collidepoint(event.pos):
free_play_menu()
elif back_button.collidepoint(event.pos):
return
pygame.display.update()
# Arcade Menu with buttons
def arcade_menu():
background_image = load_background_image('./MenuBackground.png') # Replace 'background.jpg' with your image file path
while True:
screen.fill(BLACK) # Fill the screen with black color
draw_background_image(background_image, screen)
draw_text('Arcade Mode', MENU_TITLE_FONT, WHITE, screen, SCREEN_WIDTH // 2, 200)
button_width = 400
button_height = 70
load_saved_game_button = pygame.Rect(SCREEN_WIDTH // 2 - button_width // 2, 450, button_width, button_height)
start_new_button = pygame.Rect(SCREEN_WIDTH // 2 - button_width // 2, 535, button_width, button_height)
back_button = pygame.Rect(SCREEN_WIDTH // 2 - button_width // 2, 620, button_width, button_height)
draw_button_with_hover(load_saved_game_button, 'Load Saved Game', BUTTON_FONT, WHITE, GREY, screen)
draw_button_with_hover(start_new_button, 'Start New', BUTTON_FONT, WHITE, GREY, screen)
draw_button_with_hover(back_button, 'Back', BUTTON_FONT, WHITE, GREY, screen)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONUP:
mouse_click_sound.play()
if load_saved_game_button.collidepoint(event.pos):
grid, coins, turn, score, restricted_residential = load_game_arcade()
if grid is not None:
arcade_game(grid, coins, turn, score, restricted_residential)
else:
draw_text('No saved game found. Please start a new game.', BUTTON_FONT, WHITE, screen, SCREEN_WIDTH // 2, 360)
pygame.display.update()
pygame.time.wait(2000)
elif start_new_button.collidepoint(event.pos):
arcade_game()
elif back_button.collidepoint(event.pos):
return
pygame.display.update()
# Free Play Menu with buttons
def free_play_menu():
background_image = load_background_image('./MenuBackground.png') # Replace 'background.jpg' with your image file path
while True:
screen.fill(BLACK) # Fill the screen with black color
draw_background_image(background_image, screen)
draw_text('Free Play Mode', MENU_TITLE_FONT, WHITE, screen, SCREEN_WIDTH // 2, 200)
button_width = 400
button_height = 70
load_saved_game_button = pygame.Rect(SCREEN_WIDTH // 2 - button_width // 2, 450, button_width, button_height)
start_new_button = pygame.Rect(SCREEN_WIDTH // 2 - button_width // 2, 535, button_width, button_height)
back_button = pygame.Rect(SCREEN_WIDTH // 2 - button_width // 2, 620, button_width, button_height)
draw_button_with_hover(load_saved_game_button, 'Load Saved Game', BUTTON_FONT, WHITE, GREY, screen)
draw_button_with_hover(start_new_button, 'Start New', BUTTON_FONT, WHITE, GREY, screen)
draw_button_with_hover(back_button, 'Back', BUTTON_FONT, WHITE, GREY, screen)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONUP:
mouse_click_sound.play()
if load_saved_game_button.collidepoint(event.pos):
grid, coins, turn, score, restricted_residential, expansion_count = load_game_free_play()
if grid is not None:
free_play_game(grid, coins, turn, score, restricted_residential, expansion_count)
else:
draw_text('No saved game found. Please start a new game.', BUTTON_FONT, WHITE, screen, SCREEN_WIDTH // 2, 360)
pygame.display.update()
pygame.time.wait(2000)
elif start_new_button.collidepoint(event.pos):
free_play_game()
elif back_button.collidepoint(event.pos):
return
pygame.display.update()
def arcade_game(grid=None, coins=None, turn=None, score=None, restricted_residential=None):
pygame.mixer.music.load('./GameSoundtrack.mp3')
pygame.mixer.music.set_volume(0.6) # Set volume to 60%
pygame.mixer.music.play(-1) # -1 means the music will loop indefinitely
if grid is None:
grid = [[None for _ in range(GRID_SIZE_ARCADE)] for _ in range(GRID_SIZE_ARCADE)]
if coins is None:
coins = 16
if turn is None:
turn = 0
if score is None:
score = 0
if restricted_residential is None:
restricted_residential = {}
demolish_mode = False
buildings = random.sample(BUILDINGS, 2)
first_turn = (turn == 0)
selected_building = None
animation_frame = 0
illegal_placement = False
def draw_grid():
for row in range(GRID_SIZE_ARCADE):
for col in range(GRID_SIZE_ARCADE):
rect = pygame.Rect(col * CELL_SIZE + MARGIN_LEFT, row * CELL_SIZE + MARGIN_TOP, CELL_SIZE, CELL_SIZE)
pygame.draw.rect(screen, GRID_COLOR, rect, 1)
pygame.draw.rect(screen, CELL_COLOR, rect.inflate(-1, -1))
if grid[row][col]:
draw_centered_text(grid[row][col], GAME_FONT, BLACK, screen, rect)
def draw_rules():
rules_x = SCREEN_WIDTH - MARGIN_RIGHT + TEXT_MARGIN_RIGHT + 50
rules_y = MARGIN_TOP
draw_text("Legend", BUTTON_FONT, WHITE, screen, rules_x, rules_y)
legend_y = rules_y + 40
for building, symbol in BUILDING_SYMBOLS.items():
draw_text(f'{building}: {symbol}', GAME_FONT, WHITE, screen, rules_x, legend_y)
legend_y += 20
draw_text("Points System", BUTTON_FONT, WHITE, screen, rules_x, legend_y + 20)
legend_y += 60
draw_text("Residential:", GAME_FONT, WHITE, screen, rules_x, legend_y)
draw_text("1 pt if adjacent to Industry", GAME_FONT, WHITE, screen, rules_x, legend_y + 20)
draw_text("Otherwise:", GAME_FONT, WHITE, screen, rules_x, legend_y + 40)
draw_text("+1 pt each adjacent R/C", GAME_FONT, WHITE, screen, rules_x, legend_y + 60)
draw_text("+2 pts each adjacent Park", GAME_FONT, WHITE, screen, rules_x, legend_y + 80)
draw_text("Industry:", GAME_FONT, WHITE, screen, rules_x, legend_y + 120)
draw_text("1 pt each Industry in city", GAME_FONT, WHITE, screen, rules_x, legend_y + 140)
draw_text("+1 coin each adjacent R", GAME_FONT, WHITE, screen, rules_x, legend_y + 160)
draw_text("Commercial:", GAME_FONT, WHITE, screen, rules_x, legend_y + 200)
draw_text("1 pt each adjacent Commercial", GAME_FONT, WHITE, screen, rules_x, legend_y + 220)
draw_text("+1 coin each adjacent Residential", GAME_FONT, WHITE, screen, rules_x, legend_y + 240)
draw_text("Park:", GAME_FONT, WHITE, screen, rules_x, legend_y + 280)
draw_text("1 pt each adjacent Park", GAME_FONT, WHITE, screen, rules_x, legend_y + 300)
draw_text("Road:", GAME_FONT, WHITE, screen, rules_x, legend_y + 340)
draw_text("1 pt each connected road", GAME_FONT, WHITE, screen, rules_x, legend_y + 360)
draw_text("in the same row", GAME_FONT, WHITE, screen, rules_x, legend_y + 380)
def update_score_and_coins(row, col, operation):
nonlocal score, coins
building = grid[row][col]
points = 0
coins_gained = 0
if building == 'R':
points = calculate_residential_points_arcade(grid, row, col, restricted_residential)
elif building == 'I':
points = calculate_industry_points_arcade(grid, row, col)
coins_gained = generate_coins_for_industry_arcade(grid, row, col)
elif building == 'C':
points = calculate_commercial_points_arcade(grid, row, col)
coins_gained = generate_coins_for_commercial_arcade(grid, row, col)
elif building == 'O':
points = calculate_park_points_arcade(grid, row, col)
elif building == '*':
points = calculate_road_points_arcade(grid, row, col)
if operation == 'add':
score += points
coins += coins_gained
elif operation == 'remove':
# No points or coins change on removal
pass
while True:
screen.fill(BACKGROUND_COLOR)
draw_text(f'Turn: {turn} Coins: {coins} Score: {score}', GAME_FONT, WHITE, screen, SCREEN_WIDTH // 2, 20)
draw_grid()
draw_rules()
if demolish_mode:
draw_left_aligned_text('Demolish Mode: Click on a building to remove it', GAME_FONT, WHITE, screen, 20, 60)
draw_left_aligned_text('Press D to return to building mode', GAME_FONT, WHITE, screen, 20, 80)
else:
draw_left_aligned_text(f'Choose a building: {BUILDINGS[BUILDINGS.index(buildings[0])]}, {BUILDING_SYMBOLS[buildings[0]]} (1) or {BUILDINGS[BUILDINGS.index(buildings[1])]}, {BUILDING_SYMBOLS[buildings[1]]} (2)', GAME_FONT, WHITE, screen, 20, 60)
draw_left_aligned_text('Press D to toggle Demolish Mode', GAME_FONT, WHITE, screen, 20, 80)
draw_left_aligned_text('Press M to return to Main Menu', GAME_FONT, WHITE, screen, 20, 100)
if selected_building:
draw_left_aligned_text(f'Building {selected_building}. Click on grid to place.', GAME_FONT, WHITE, screen, 20, 130)
if illegal_placement:
draw_left_aligned_text("Illegal placement. Try again.", GAME_FONT, WHITE, screen, 20, 160)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_m:
save_game_arcade(grid, coins, turn, score, restricted_residential)
pygame.mixer.music.load('./MenuSoundtrack.mp3')
pygame.mixer.music.set_volume(0.6) # Set volume to 60%
pygame.mixer.music.play(-1) # -1 means the music will loop indefinitely
return
if event.key == pygame.K_d:
demolish_mode = not demolish_mode
illegal_placement = False
if not demolish_mode and coins > 0:
if event.key == pygame.K_1:
selected_building = buildings[0]
elif event.key == pygame.K_2:
selected_building = buildings[1]
if event.type == pygame.MOUSEBUTTONUP:
x, y = event.pos
col = (x - MARGIN_LEFT) // CELL_SIZE
row = (y - MARGIN_TOP) // CELL_SIZE
if 0 <= col < GRID_SIZE_ARCADE and 0 <= row < GRID_SIZE_ARCADE:
if demolish_mode and grid[row][col] is not None:
update_score_and_coins(row, col, 'remove')
grid[row][col] = None
coins -= 1 # Only decrease coins by 1
turn += 1
illegal_placement = False
demolish_mode = False
elif not demolish_mode and (grid[row][col] is None or grid[row][col] == '') and selected_building:
if first_turn or is_adjacent_to_existing_building_arcade(grid, row, col):
grid[row][col] = BUILDING_SYMBOLS[selected_building]
coins -= 1
turn += 1
update_score_and_coins(row, col, 'add')
save_game_arcade(grid, coins, turn, score, restricted_residential)
buildings = random.sample(BUILDINGS, 2)
first_turn = False
selected_building = None
animation_frame = 30
illegal_placement = False
if animation_frame > 0:
animation_frame -= 1
draw_text('+', GAME_FONT, WHITE, screen, SCREEN_WIDTH // 2 + 50, 20)
pygame.display.update()
if coins <= 0 or turn >= 16:
name = prompt_player_name()
save_leaderboard(name, score)
clear_saved_game_arcade()
pygame.mixer.music.load('./MenuSoundtrack.mp3')
pygame.mixer.music.set_volume(0.6) # Set volume to 60%
pygame.mixer.music.play(-1) # -1 means the music will loop indefinitely
break
def free_play_game(grid=None, coins=None, turn=None, score=None, restricted_residential=None, expansion_count=0):
pygame.mixer.music.load('./GameSoundtrack.mp3')
pygame.mixer.music.set_volume(0.6) # Set volume to 60%
pygame.mixer.music.play(-1) # -1 means the music will loop indefinitely
if grid is None:
grid = [[None for _ in range(5)] for _ in range(5)]
if coins is None:
coins = 0
if turn is None:
turn = 0
if score is None:
score = 0
if restricted_residential is None:
restricted_residential = {}
demolish_mode = False
selected_building = None
animation_frame = 0
illegal_placement = False
first_turn = (turn == 0) # Check if it's the first turn
def draw_grid():
# Draw the game grid for Free Play mode
grid_size = len(grid)
for row in range(grid_size):
for col in range(grid_size):
rect = pygame.Rect(col * CELL_SIZE + MARGIN_LEFT, row * CELL_SIZE + MARGIN_TOP, CELL_SIZE, CELL_SIZE)
pygame.draw.rect(screen, GRID_COLOR, rect, 1)
pygame.draw.rect(screen, CELL_COLOR, rect.inflate(-1, -1))
if grid[row][col]:
draw_centered_text(grid[row][col], GAME_FONT, BLACK, screen, rect)
def draw_rules():
# Draw the rules and legend for Free Play mode
rules_x = SCREEN_WIDTH - MARGIN_RIGHT + TEXT_MARGIN_RIGHT + 50
rules_y = MARGIN_TOP
draw_text("Legend", BUTTON_FONT, WHITE, screen, rules_x, rules_y)
legend_y = rules_y + 40
for building, symbol in BUILDING_SYMBOLS.items():
draw_text(f'{building}: {symbol}', GAME_FONT, WHITE, screen, rules_x, legend_y)
legend_y += 20
draw_text("Points System", BUTTON_FONT, WHITE, screen, rules_x, legend_y + 20)
legend_y += 60
draw_text("Residential:", GAME_FONT, WHITE, screen, rules_x, legend_y)
draw_text("1 pt if adjacent to Industry", GAME_FONT, WHITE, screen, rules_x, legend_y + 20)
draw_text("Otherwise:", GAME_FONT, WHITE, screen, rules_x, legend_y + 40)
draw_text("+1 pt each adjacent R/C", GAME_FONT, WHITE, screen, rules_x, legend_y + 60)
draw_text("+2 pts each adjacent Park", GAME_FONT, WHITE, screen, rules_x, legend_y + 80)
draw_text("Industry:", GAME_FONT, WHITE, screen, rules_x, legend_y + 120)
draw_text("1 pt each Industry in city", GAME_FONT, WHITE, screen, rules_x, legend_y + 140)
draw_text("+1 coin each adjacent R", GAME_FONT, WHITE, screen, rules_x, legend_y + 160)
draw_text("Commercial:", GAME_FONT, WHITE, screen, rules_x, legend_y + 200)
draw_text("1 pt each adjacent Commercial", GAME_FONT, WHITE, screen, rules_x, legend_y + 220)
draw_text("+1 coin each adjacent Residential", GAME_FONT, WHITE, screen, rules_x, legend_y + 240)
draw_text("Park:", GAME_FONT, WHITE, screen, rules_x, legend_y + 280)
draw_text("1 pt each adjacent Park", GAME_FONT, WHITE, screen, rules_x, legend_y + 300)
draw_text("Road:", GAME_FONT, WHITE, screen, rules_x, legend_y + 340)
draw_text("1 pt each connected road", GAME_FONT, WHITE, screen, rules_x, legend_y + 360)
draw_text("in the same row", GAME_FONT, WHITE, screen, rules_x, legend_y + 380)
def update_score_and_coins_free_play(row, col, operation):
nonlocal score, coins
building = grid[row][col]
points = 0
coins_gained = 0
if building == 'R':
points = calculate_residential_points_free_play(grid, row, col, restricted_residential)
elif building == 'I':
points = calculate_industry_points_free_play(grid, row, col)
coins_gained = generate_coins_for_industry_free_play(grid, row, col)
elif building == 'C':
points = calculate_commercial_points_free_play(grid, row, col)
coins_gained = generate_coins_for_commercial_free_play(grid, row, col)
elif building == 'O':
points = calculate_park_points_free_play(grid, row, col)
elif building == '*':
points = calculate_road_points_free_play(grid, row, col)
if operation == 'add':
score += points
coins += coins_gained
elif operation == 'remove':
# No points or coins change on removal
pass
while True:
screen.fill(BACKGROUND_COLOR)
draw_text(f'Turn: {turn} Coins: {coins} Score: {score}', GAME_FONT, WHITE, screen, SCREEN_WIDTH // 2, 20)
draw_grid()
draw_rules()
if demolish_mode:
draw_left_aligned_text('Demolish Mode: Click on a building to remove it', GAME_FONT, WHITE, screen, 20, 60)
draw_left_aligned_text('Press D to return to building mode', GAME_FONT, WHITE, screen, 20, 80)
else:
draw_left_aligned_text(f'Choose a building: Residential (R) (1), Industry (I) (2), Commercial (C) (3), Park (O) (4), Road (*) (5)', GAME_FONT, WHITE, screen, 20, 60)
draw_left_aligned_text('Press D to toggle Demolish Mode', GAME_FONT, WHITE, screen, 20, 80)
draw_left_aligned_text('Press M to return to Main Menu', GAME_FONT, WHITE, screen, 20, 100)
if selected_building:
draw_left_aligned_text(f'Building {selected_building}. Click on grid to place.', GAME_FONT, WHITE, screen, 20, 130)
if illegal_placement:
draw_left_aligned_text("Illegal placement. Try again.", GAME_FONT, WHITE, screen, 20, 160)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_m:
save_game_free_play(grid, coins, turn, score, restricted_residential, expansion_count)
pygame.mixer.music.load('./MenuSoundtrack.mp3')
pygame.mixer.music.set_volume(0.6) # Set volume to 60%
pygame.mixer.music.play(-1) # -1 means the music will loop indefinitely
return
if event.key == pygame.K_d:
demolish_mode = not demolish_mode
illegal_placement = False
if not demolish_mode:
if event.key == pygame.K_1:
selected_building = 'Residential'
elif event.key == pygame.K_2: