-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
277 lines (259 loc) · 10.7 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
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
import pygame
import sys
from oop_cell_board import *
# imported all the previous code and initiate pygame
pygame.init()
pygame.display.set_caption("Sudoku")
screen = pygame.display.set_mode((width, height))
screen_color = white
line_color = black
screen.fill(white)
number_font = pygame.font.Font(None, num_size)
button_font = pygame.font.Font(None, butt_size)
text_font = pygame.font.Font(None, menu_size)
pygame.display.update()
game_over = False
game_start = False
def draw_game_over_won():
screen.fill(screen_color)
end_text = "Game Over! You won!"
end_surf = text_font.render(end_text, True, black)
end_rect = end_surf.get_rect(center=(width // 2, height // 2 - 50))
screen.blit(end_surf, end_rect)
# exit button
exit_text = button_font.render("Exit", True, white)
exit_surface = pygame.Surface((exit_text.get_size()[0] + 20, exit_text.get_size()[1] + 20))
exit_surface.fill(black)
exit_surface.blit(exit_text, (10, 10))
exit_rectangle = exit_surface.get_rect(
center=(3 * (width // 4), height // 2 + 235))
screen.blit(exit_surface, exit_rectangle)
def draw_game_over_lost():
end_text = "Game Over! You filled out the wrong numbers!"
end_surf = text_font.render(end_text, True, black)
end_rect = end_surf.get_rect(center=(width // 2, height // 2 - 50))
screen.blit(end_surf, end_rect)
# restart button
restart_text = button_font.render("Restart", True, white)
restart_surface = pygame.Surface((restart_text.get_size()[0] + 20, restart_text.get_size()[1] + 20))
restart_surface.fill(black)
restart_surface.blit(restart_text, (10, 10))
restart_rectangle = restart_surface.get_rect(
center=(width // 2, height // 2 + 235))
screen.blit(restart_surface, restart_rectangle)
def draw_start_game():
screen.fill(screen_color)
# top text
top_text = "Welcome to Sudoku!"
top_surf = number_font.render(top_text, True, red)
top_rect = top_surf.get_rect(center=(width // 2, height // 2 - 220))
screen.blit(top_surf, top_rect)
# middle text
mid_text = "Select Game Mode:"
mid_surf = number_font.render(mid_text, True, red)
mid_rect = mid_surf.get_rect(center=(width // 2, height // 2 - 110))
screen.blit(mid_surf, mid_rect)
# add buttons for easy medium and difficult - maybe add user criteria to pull the func to take out the values
# Initialize buttons
# button text
easy_text = button_font.render("Easy", True, white)
med_text = button_font.render("Medium", True, white)
hard_text = button_font.render("Hard", True, white)
# Initialize button background color and text
# easy
easy_surface = pygame.Surface((easy_text.get_size()[0] + 20, easy_text.get_size()[1] + 20))
easy_surface.fill(red)
easy_surface.blit(easy_text, (10, 10))
# med
med_surface = pygame.Surface((med_text.get_size()[0] + 20, med_text.get_size()[1] + 20))
med_surface.fill(red)
med_surface.blit(med_text, (10, 10))
# hard
hard_surface = pygame.Surface((hard_text.get_size()[0] + 20, hard_text.get_size()[1] + 20))
hard_surface.fill(red)
hard_surface.blit(hard_text, (10, 10))
# Initialize button rectangle
easy_rectangle = easy_surface.get_rect(
center=(width // 2, height // 2 + 50))
med_rectangle = med_surface.get_rect(
center=(width // 2, height // 2 + 125))
hard_rectangle = hard_surface.get_rect(
center=(width // 2, height // 2 + 200))
# Draw buttons
screen.blit(easy_surface, easy_rectangle)
screen.blit(med_surface, med_rectangle)
screen.blit(hard_surface, hard_rectangle)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if easy_rectangle.collidepoint(event.pos):
difficulty = 30
return difficulty
elif med_rectangle.collidepoint(event.pos):
difficulty = 40
return difficulty
elif hard_rectangle.collidepoint(event.pos):
difficulty = 50
return difficulty
pygame.display.update()
# need buttons for:
# • The Reset button will reset the board to its initial state.
# • The Restart button will take the user back to the Game Start screen.
# • The Exit button will end the program.
# button text
reset_text = button_font.render("Reset", True, white)
restart_text = button_font.render("Restart", True, white)
exit_text = button_font.render("Exit", True, white)
# Initialize button background color and text
# reset
reset_surface = pygame.Surface((reset_text.get_size()[0] + 20, reset_text.get_size()[1] + 20))
reset_surface.fill(black)
reset_surface.blit(reset_text, (10, 10))
# restart
restart_surface = pygame.Surface((restart_text.get_size()[0] + 20, restart_text.get_size()[1] + 20))
restart_surface.fill(black)
restart_surface.blit(restart_text, (10, 10))
# exit
exit_surface = pygame.Surface((exit_text.get_size()[0] + 20, exit_text.get_size()[1] + 20))
exit_surface.fill(black)
exit_surface.blit(exit_text, (10, 10))
# Initialize button rectangle
reset_rectangle = reset_surface.get_rect(
center=(width // 4, height // 2 + 235))
restart_rectangle = restart_surface.get_rect(
center=(2 * (width // 4), height // 2 + 235))
exit_rectangle = exit_surface.get_rect(
center=(3 * (width // 4), height // 2 + 235))
# Draw buttons
def print_butt():
screen.blit(reset_surface, reset_rectangle)
screen.blit(restart_surface, restart_rectangle)
screen.blit(exit_surface, exit_rectangle)
def win_or_lose():
if sud_board.check_board() is True:
screen.fill(white)
draw_game_over_won()
pygame.display.update()
elif sud_board.check_board() is False:
screen.fill(white)
draw_game_over_lost()
pygame.display.update()
while True:
if game_start is False:
pygame.display.update()
pygame.time.delay(1000)
difficulty = draw_start_game()
# figure out how to do if they click easy, med and hard buttons
# when those are clicked game_start is True
# event loop
sud_board = Board(width, height, screen, difficulty)
game_start = True
game_over = False
screen.fill(white)
pygame.display.update()
sud_board.draw()
print_butt()
pygame.display.update()
for event in pygame.event.get():
if sud_board.is_full() and sud_board.sketched is False:
if sud_board.check_board():
screen.fill(white)
draw_game_over_won()
pygame.display.update()
else:
screen.fill(white)
draw_game_over_lost()
pygame.display.update()
if event.type == pygame.QUIT: # exit button?
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if exit_rectangle.collidepoint(event.pos):
sys.exit()
elif reset_rectangle.collidepoint(event.pos):
sud_board.reset_to_original()
screen.fill(white)
sud_board.draw()
print_butt()
pygame.display.update()
elif restart_rectangle.collidepoint(event.pos):
screen.fill(white)
game_start = False
game_over = True
continue
if event.type == pygame.MOUSEBUTTONDOWN and not game_over:
click_pos = pygame.mouse.get_pos()
x, y = click_pos
row = y // square_size
col = x // square_size
sud_board.select(row, col)
# if selected and empty let user place value
if event.type == pygame.KEYDOWN and not game_over:
if sud_board.select:
if event.key == pygame.K_1:
sud_board.sketch(1)
screen.fill(white)
sud_board.draw()
print_butt()
pygame.display.update()
if event.key == pygame.K_2:
sud_board.sketch(2)
screen.fill(white)
sud_board.draw()
print_butt()
pygame.display.update()
if event.key == pygame.K_3:
sud_board.sketch(3)
screen.fill(white)
sud_board.draw()
print_butt()
pygame.display.update()
if event.key == pygame.K_4:
sud_board.sketch(4)
screen.fill(white)
sud_board.draw()
print_butt()
pygame.display.update()
if event.key == pygame.K_5:
sud_board.sketch(5)
screen.fill(white)
sud_board.draw()
print_butt()
pygame.display.update()
if event.key == pygame.K_6:
sud_board.sketch(6)
screen.fill(white)
sud_board.draw()
print_butt()
pygame.display.update()
if event.key == pygame.K_7:
sud_board.sketch(7)
screen.fill(white)
sud_board.draw()
print_butt()
pygame.display.update()
if event.key == pygame.K_8:
sud_board.sketch(8)
screen.fill(white)
sud_board.draw()
print_butt()
pygame.display.update()
if event.key == pygame.K_9:
sud_board.sketch(9)
screen.fill(white)
sud_board.draw()
print_butt()
pygame.display.update()
if event.key == pygame.K_TAB:
sud_board.place_number(sud_board.board[sud_board.selected_cell[0]][sud_board.selected_cell[1]])
screen.fill(white)
sud_board.draw()
print_butt()
pygame.display.update()
if event.key == pygame.K_0:
sud_board.clear()
screen.fill(white)
sud_board.draw()
print_butt()
pygame.display.update()