forked from stlucasgarcia/connect-four
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_screen.py
More file actions
195 lines (149 loc) · 6.28 KB
/
main_screen.py
File metadata and controls
195 lines (149 loc) · 6.28 KB
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
import pygame as pg
import math
import sys
from ending_screens import EndingScreen
from utilities import UtilitiesMain
from controller import Controller
from settings import Settings
from menu import OptionsMenu
class MainScreen:
def __init__(
self,
screen,
is_controller,
option: str,
) -> None:
self.config = Settings()
self.option = option
self.screen: pg.Surface = screen
self.background_image: pg.Surface = self.config.bg_image
self.is_controller = is_controller
self.sound_chip_1 = self.config.sound_chip_1
self.sound_chip_2 = self.config.sound_chip_2
self.volume = self.config.volume
self.chip_1 = self.config.chip_1
self.chip_2 = self.config.chip_2
self.ai_chip = 2
def main_screen(self, scores: list, usernames: list):
control = Controller()
utilities = UtilitiesMain(self.screen)
start_time = pg.time.get_ticks()
attr = {
"res": self.config.size,
"style": self.config.style,
"label": self.config.op_label,
"resume": self.config.op_resume,
"st_menu": self.config.op_start_menu,
"quit": self.config.op_quit,
"theme": self.config.theme,
}
menu = OptionsMenu(**attr)
clock = pg.time.Clock()
matrix = utilities.create_matrix()
chip = self.chip_2 if self.option == "Player vs Player" else self.chip_1
close_loop: bool = False
x, y = 950, 15
player_turn: int = 1 # if not option else option
pg.mixer.music.play(loops=-1)
pg.mixer.music.set_volume(self.volume)
while not close_loop:
pg.mouse.set_visible(False)
utilities.draw_board(matrix, start_time, usernames)
self.screen.blit(chip, (x, y))
for event in pg.event.get():
if event.type == pg.QUIT:
close_loop = True
sys.exit()
if self.is_controller:
if control.checkController():
x = control.x_hd
y = 8
if event.type == pg.MOUSEMOTION:
x = event.pos[0]
y = event.pos[1]
if event.type == pg.MOUSEBUTTONDOWN:
save_chip = chip
column = utilities.location_X(pg.mouse.get_pos()[0])
if column != 0:
(play_again, player_turn, chip, scores) = utilities.playersTurn(
column,
matrix,
player_turn,
self.sound_chip_1,
self.sound_chip_2,
usernames,
start_time,
scores,
self.option,
)
if play_again != None:
return play_again
if chip == None:
chip = save_chip
if self.is_controller:
if control.isControllerDropEvent(event):
save_chip = chip
column = utilities.location_X(control.get_x_pos(event))
if column != 0:
(
play_again,
player_turn,
chip,
scores,
) = utilities.playersTurn(
column,
matrix,
player_turn,
self.sound_chip_1,
self.sound_chip_2,
usernames,
start_time,
scores,
self.option,
)
if play_again != None:
return play_again
if chip == None:
chip = save_chip
if event.type == pg.KEYDOWN:
if event.key == pg.K_ESCAPE:
play_again = menu.run(self.screen, clock)
if not play_again:
return play_again
if control.isControllerEscEvent(event):
play_again = menu.run(self.screen, clock)
if not play_again:
return play_again
if control.isControllerEvent(event):
screen = self.screen
control.check_event(event)
if self.option == "Player vs AI" and player_turn % 2 == 1:
column, minimax_score = utilities.minimaxTree(
matrix, 5, -math.inf, math.inf, True
)
pg.time.wait(200)
if utilities.is_available(matrix, column):
row = utilities.get_open_row(matrix, column)
utilities.drop_piece(matrix, row, column, self.ai_chip)
self.sound_chip_1.play()
if utilities.is_victory(matrix, self.ai_chip):
scores[1] += 1
utilities.draw_board(matrix, start_time, usernames)
pg.display.update()
pg.time.wait(1500)
data = {usernames[0]: scores[0], usernames[1]: scores[1]}
ending = EndingScreen(
self.screen,
data,
res=self.config.size,
pg_res=self.config.win_pg,
sm_res=self.config.win_sm,
quit_res=self.config.win_quit,
lb_res=self.config.win_ld,
)
play_again = ending.scores()
return play_again
player_turn += 1
player_turn = player_turn % 2
pg.display.update()
clock.tick(75)