-
Notifications
You must be signed in to change notification settings - Fork 1
/
GameController.py
184 lines (161 loc) · 7.08 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
from GameView import *
from GameMenu import *
from GameModel 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):
self.muted = False
self.gauntlet = Gauntlet()
self.gauntlet.muted = self.muted
self.gameView = gameView
self.gameView.gauntlet = self.gauntlet
self.gameModel = self.gameView.gameModel
self.gameMenu = gameMenu
self.gameMenu.gauntlet = self.gauntlet
self.gameMenu.playButton.action = self.main_game
self.gameMenu.quitButton.action = self.exit
self.clock = pygame.time.Clock()
pygame.mixer.music.load(os.path.join(FunContainer.data_dir, self.music))
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.game.new_game()
# self.set_player_indicator()
# self.main_menu()
# elif event.type == KEYDOWN and event.key == K_s:
# self.game.save_game()
# elif event.type == KEYDOWN and event.key == K_l:
# self.game.load_game()
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
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()
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 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.game.new_game()
# self.set_player_indicator()
# self.main_menu()
# elif event.type == KEYDOWN and event.key == K_s:
# self.game.save_game()
# elif event.type == KEYDOWN and event.key == K_l:
# self.game.load_game()
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:
self.gameModel.intelligent_move()
self.gameView.balls_update()
player1Turn = True
self.gameView.view_update()
@classmethod
def exit(cls):
pygame.time.delay(500)
pygame.quit()
sys.exit(0)