-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
115 lines (82 loc) · 3.15 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
import pygame
# Import stages
from stages.intro import Intro
from stages.battle import Battle
from stages.ship_location import ShipLocation
from stages.podium import Podium
FPS = 30
WIDTH, HEIGHT = 500, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Battleship')
class GameState:
"""
This class manages game states in a way to modularize
stages and keep codebase organized.
Resource: https://www.youtube.com/watch?v=j9yMFG3D7fg
"""
def __init__(self) -> None:
self.client = None
self.state = 'intro'
self.intro_stage = Intro()
self.ship_location_stage: ShipLocation = None
self.battle_stage: Battle = None
self.podium_stage: Podium = Podium()
def intro(self) -> None:
""" Intro stage state handler. """
states = self.intro_stage.process_events()
self.intro_stage.draw(WIN)
if states['players_connected']:
self.state = 'ship_location'
self.client = states['client']
self.ship_location_stage = ShipLocation()
self.ship_location_stage.load_client(self.client)
self.intro_stage = None
def ship_location(self) -> None:
""" Ship location stage state handler. """
states = self.ship_location_stage.process_events()
self.ship_location_stage.draw(WIN)
if states['ship_locked']:
self.state = 'battle'
map_widget, ships, ships_rect = self.ship_location_stage.get_maps_and_ships()
self.battle_stage = Battle()
self.battle_stage.load_client(self.client)
self.battle_stage.load_maps_and_ships(
map_widget, ships, ships_rect)
self.ship_location_stage = None
def battle(self) -> None:
""" Battle stage state handler. """
states = self.battle_stage.process_events()
self.battle_stage.draw(WIN)
if states['game_finished']:
self.state = 'podium'
self.podium_stage = Podium()
self.podium_stage.load_client(self.client)
self.podium_stage.load_winner_name(states['winner_name'])
self.battle_stage = None
def podium(self) -> None:
""" Podium stage state handler. """
states = self.podium_stage.process_events()
self.podium_stage.draw(WIN)
if states['reset_game']:
self.state = 'ship_location'
self.ship_location_stage = ShipLocation()
self.ship_location_stage.load_client(self.client)
self.podium_stage = None
def state_manager(self) -> None:
""" This function keeps tracking of current game state. """
if self.state == 'intro':
self.intro()
elif self.state == 'ship_location':
self.ship_location()
elif self.state == 'battle':
self.battle()
elif self.state == 'podium':
self.podium()
def main() -> None:
game_state = GameState()
clock = pygame.time.Clock()
while True:
clock.tick(FPS) # Force game loop to run at FPS limit
game_state.state_manager()
if __name__ == '__main__':
main()