forked from wojciech1871/Castle_game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameController.py
276 lines (246 loc) · 10.9 KB
/
GameController.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
from GameView import *
from GameMenu import *
from GameModel import *
from GameOptions import *
import sys
class Gauntlet(pygame.sprite.Sprite):
resolution = (60, 60)
def __init__(self):
super().__init__()
self.image = FunContainer.load_image("gauntlet.jpg", -1)
self.clickedImage = pygame.transform.scale(self.image, (self.resolution[0]-8, self.resolution[1]-8))
self.normalImage = pygame.transform.scale(self.image, self.resolution)
self.image = self.normalImage
self.rect = self.image.get_rect()
pygame.mouse.set_visible(False)
self.clickedSound = FunContainer.load_sound("click.wav")
self.muted = None
def update(self):
self.rect.midtop = pygame.mouse.get_pos()
def clicked(self):
if not self.muted:
self.clickedSound.play()
self.image = self.clickedImage
def unclicked(self):
self.image = self.normalImage
class GameController:
FPS = 30
music = "stronghold.mp3"
def __init__(self, gameView: GameView, gameMenu: GameMenu, gameOptions: GameOptions, deep: tuple):
self.muted = False
self.game_mode = None
self.deep = deep
self.gauntlet = Gauntlet()
self.gauntlet.muted = self.muted
self.gameView = gameView
self.gameView.gauntlet = self.gauntlet
self.gameModel = self.gameView.gameModel
self.gameOptions = gameOptions
self.gameOptions.gauntlet = self.gauntlet
self.gameOptions.backToMenuButton.action = self.main_menu
self.gameOptions.soundButton.action = self.on_off_sound
self.gameOptions.changePlayerButton.action = self.change_mode
self.set_indicators()
self.gameMenu = gameMenu
self.gameMenu.gauntlet = self.gauntlet
self.gameMenu.optionsButton.action = self.main_options
self.gameMenu.playButton.action = self.game_mode
self.gameMenu.quitButton.action = self.exit
self.clock = pygame.time.Clock()
pygame.mixer.music.load(os.path.join(FunContainer.data_dir, self.music))
def set_indicators(self):
self.gameOptions.changePlayerIndicator.set_state(False)
self.gameOptions.soundIndicator.set_state(False)
self.game_mode = self.player_vs_computer
def main_menu(self):
if not pygame.mixer.music.get_busy() and not self.muted:
pygame.mixer.music.play(-1)
pygame.time.delay(500)
self.gameMenu.init_draw()
while True:
self.clock.tick(self.FPS)
for event in pygame.event.get():
if event.type == QUIT:
self.exit()
elif event.type == KEYDOWN and event.key == K_ESCAPE:
self.exit()
elif event.type == MOUSEBUTTONDOWN:
self.gauntlet.clicked()
pos = pygame.mouse.get_pos()
spriteClicked = self.gameMenu.allButtons.focused_sprite(pos)
if spriteClicked:
self.gameMenu.view_update()
spriteClicked.action()
elif event.type == MOUSEBUTTONUP:
self.gauntlet.unclicked()
self.gameMenu.view_update()
def main_game(self):
pygame.time.delay(500)
pygame.mixer.music.stop()
self.gameView.init_draw()
spriteClicked = None
while True:
self.clock.tick(self.FPS)
for event in pygame.event.get():
if event.type == QUIT:
self.exit()
elif event.type == KEYDOWN and event.key == K_ESCAPE:
self.main_menu()
elif event.type == MOUSEBUTTONDOWN:
self.gauntlet.clicked()
if not spriteClicked:
pos1 = pygame.mouse.get_pos()
pos1 = self.gameView.cartesian2board(pos1)
ballsClickedColor = self.gameModel.ballsMap[pos1]
if ballsClickedColor == self.gameModel.activePlayer.color:
spriteClicked = True
print("bill taken correctly")
else:
pos2 = pygame.mouse.get_pos()
pos2 = self.gameView.cartesian2board(pos2)
try:
if self.gameModel.move_ball(pos1, pos2):
self.gameView.balls_update()
if self.gameModel.check_if_game_finish():
raise EndGame
self.gameModel.change_player()
print("bill moved corectly")
except(SystemExit):
exit(0)
# self.game.new_game()
# self.set_player_indicator()
# self.main_menu()
spriteClicked = False
elif event.type == MOUSEBUTTONUP:
self.gauntlet.unclicked()
self.gameView.view_update()
def computer_vs_computer(self):
pygame.time.delay(500)
pygame.mixer.music.stop()
self.gameView.init_draw()
player1Turn = True
while True:
self.clock.tick(self.FPS)
for event in pygame.event.get():
if event.type == QUIT:
self.exit()
elif event.type == KEYDOWN and event.key == K_ESCAPE:
self.main_menu()
if player1Turn:
if event.type == MOUSEBUTTONDOWN:
print("Player 1 - Black - is making his move", self.gameModel.activePlayer.color)
self.gameModel.intelligent_move(self.deep[0])
self.gameView.balls_update()
print("Computer 1 made his move!")
if self.gameModel.check_if_game_finish():
raise EndGame
player1Turn = False
self.gameModel.change_player()
else:
print("Player 2 - White - is making his move", self.gameModel.activePlayer.color)
self.gameModel.intelligent_move(self.deep[1])
self.gameView.balls_update()
print("Computer 2 made his move!")
if self.gameModel.check_if_game_finish():
raise EndGame
player1Turn = True
self.gameModel.change_player()
self.gameView.view_update()
def player_vs_computer(self):
pygame.time.delay(500)
pygame.mixer.music.stop()
self.gameView.init_draw()
spriteClicked = None
player1Turn = True
while True:
self.clock.tick(self.FPS)
for event in pygame.event.get():
if event.type == QUIT:
self.exit()
elif event.type == KEYDOWN and event.key == K_ESCAPE:
self.main_menu()
if player1Turn:
if event.type == MOUSEBUTTONDOWN:
self.gauntlet.clicked()
if not spriteClicked:
pos1 = pygame.mouse.get_pos()
pos1 = self.gameView.cartesian2board(pos1)
ballsClickedColor = self.gameModel.ballsMap[pos1]
if ballsClickedColor == self.gameModel.activePlayer.color:
spriteClicked = True
print("Sprite clicked")
else:
pos2 = pygame.mouse.get_pos()
pos2 = self.gameView.cartesian2board(pos2)
try:
if self.gameModel.move_ball(pos1, pos2):
self.gameView.balls_update()
self.gameModel.change_player()
player1Turn = False
print("player changed")
except(SystemExit):
exit(0)
# self.game.new_game()
# self.set_player_indicator()
# self.main_menu()
print("Sprite unclicked")
spriteClicked = False
elif event.type == MOUSEBUTTONUP:
self.gauntlet.unclicked()
else:
#funkcja inteligent move, atrybut to parametr określający głębokość drzewa przeszukiwania
self.gameModel.intelligent_move(self.deep[1])
self.gameView.balls_update()
if self.gameModel.check_if_game_finish():
raise EndGame
self.gameModel.change_player()
player1Turn = True
self.gameView.view_update()
def main_options(self):
pygame.time.delay(500)
self.gameOptions.init_draw()
while True:
self.clock.tick(self.FPS)
for event in pygame.event.get():
if event.type == QUIT:
self.exit()
elif event.type == KEYDOWN and event.key == K_ESCAPE:
self.main_menu()
elif event.type == MOUSEBUTTONDOWN:
self.gauntlet.clicked()
pos = pygame.mouse.get_pos()
spriteClicked = self.gameOptions.allButtons.focused_sprite(pos)
if spriteClicked:
self.gameOptions.view_update()
spriteClicked.action()
elif event.type == MOUSEBUTTONUP:
self.gauntlet.unclicked()
self.gameOptions.view_update()
def change_mode(self):
self.gameOptions.changePlayerIndicator.change_state()
if self.game_mode == self.player_vs_computer:
self.game_mode = self.computer_vs_computer
self.gameMenu.playButton.action = self.game_mode
print("Computer vs computer set")
else:
self.game_mode = self.player_vs_computer
self.gameMenu.playButton.action = self.game_mode
print("Player vs computer set")
def on_off_sound(self):
self.gameOptions.soundIndicator.change_state()
if self.muted:
pygame.mixer.unpause()
pygame.mixer.music.play()
self.muted = False
print("Sound turned on")
else:
pygame.mixer.music.stop()
pygame.mixer.pause()
self.muted = True
print("Sound turned off")
self.gauntlet.muted = self.muted
@classmethod
def exit(cls):
pygame.time.delay(500)
pygame.quit()
sys.exit(0)