-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku_game.py
201 lines (163 loc) · 8.12 KB
/
sudoku_game.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
import pygame
import requests
########################
# Global Variables #
########################
width = 800
play_width = 550
background_color = (255, 255, 255)
x_tuning = 3.5
y_tuning = 4
#######################
# Calling the API #
#######################
url = "https://sudoku-generator1.p.rapidapi.com/sudoku/generate"
api_key = "" # add own
querystring = {"difficulty":"hard"} # examples: {"seed":"1337", "difficulty":"medium"}, difficulties: easy, medium, hard
headers = {"X-RapidAPI-Key": api_key,
"X-RapidAPI-Host": "sudoku-generator1.p.rapidapi.com"}
response = requests.get(url, headers=headers, params=querystring)
grid = response.json()['puzzle']
grid_original = [[0 for x in range(9)] for y in range(9)]
for i in range(0, 9):
for j in range(0, 9):
if grid[9*i+j].isdigit():
grid_original[i][j] = int(grid[9*i+j])
else:
grid_original[i][j] = 0
######################################
# Function for Drawing the Title #
######################################
def draw_title(win):
pygame.font.init()
title_font = pygame.font.SysFont('arial', 40, bold=False)
game_label = title_font.render('SUDOKU', 1, (0, 0, 0))
win.blit(game_label, (width / 2 - game_label.get_width() / 2 + 26, 12))
#######################################
# Function for Creating the Grids #
#######################################
def draw_grid(win):
for i in range(10):
if i % 3 == 0:
pygame.draw.line(win, (0, 0, 0), (50 + 50 * i, 50), (50 + 50 * i, 500), 4)
pygame.draw.line(win, (0, 0, 0), (50, 50 + 50 * i), (500, 50 + 50 * i), 4)
pygame.draw.line(win, (0, 0, 0), (50 + 50 * i, 50), (50 + 50 * i, 500), 2)
pygame.draw.line(win, (0, 0, 0), (50, 50 + 50 * i), (500, 50 + 50 * i), 2)
pygame.display.update()
#######################################################
# Function for Placing the Numbers into the Grids #
#######################################################
def place_numbers(win):
font = pygame.font.SysFont('arial', 35)
for i in range(9):
for ii in range(9):
if 0 < grid_original[i][ii] < 10:
value = font.render(str(grid[9*i+ii]), 1, (0, 0, 0))
win.blit(value, ((ii + 1) * 50 + 18, (i + 1) * 50 + 8))
pygame.display.update()
################################
# Function for Click Event #
################################
def insert(win, position):
x, y = position[0]//50, position[1]//50
font = pygame.font.SysFont('arial', 35)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
if event.type == pygame.KEYDOWN:
if 0 < x < 10 and 0 < y < 10:
if grid_original[y-1][x-1] != 0:
return
else:
if event.key == pygame.K_1 or event.key == pygame.K_KP1:
pygame.draw.rect(win, (255, 255, 255), (x * 54 - (x - 1) * x_tuning, y * 54 - (y - 1) * y_tuning, 40, 40))
value = font.render(str(1), 1, (125, 36, 69))
win.blit(value, (x * 50 + 18, y * 50 + 8))
pygame.display.update()
return
if event.key == pygame.K_2 or event.key == pygame.K_KP2:
pygame.draw.rect(win, (255, 255, 255), (x * 54 - (x - 1) * x_tuning, y * 54 - (y - 1) * y_tuning, 40, 40))
value = font.render(str(2), 1, (125, 36, 69))
win.blit(value, (x * 50 + 18, y * 50 + 8))
pygame.display.update()
return
if event.key == pygame.K_3 or event.key == pygame.K_KP3:
pygame.draw.rect(win, (255, 255, 255), (x * 54 - (x - 1) * x_tuning, y * 54 - (y - 1) * y_tuning, 40, 40))
value = font.render(str(3), 1, (125, 36, 69))
win.blit(value, (x * 50 + 18, y * 50 + 8))
pygame.display.update()
return
if event.key == pygame.K_4 or event.key == pygame.K_KP4:
pygame.draw.rect(win, (255, 255, 255), (x * 54 - (x - 1) * x_tuning, y * 54 - (y - 1) * y_tuning, 40, 40))
value = font.render(str(4), 1, (125, 36, 69))
win.blit(value, (x * 50 + 18, y * 50 + 8))
pygame.display.update()
return
if event.key == pygame.K_5 or event.key == pygame.K_KP5:
pygame.draw.rect(win, (255, 255, 255), (x * 54 - (x - 1) * x_tuning, y * 54 - (y - 1) * y_tuning, 40, 40))
value = font.render(str(5), 1, (125, 36, 69))
win.blit(value, (x * 50 + 18, y * 50 + 8))
pygame.display.update()
return
if event.key == pygame.K_6 or event.key == pygame.K_KP6:
pygame.draw.rect(win, (255, 255, 255), (x * 54 - (x - 1) * x_tuning, y * 54 - (y - 1) * y_tuning, 40, 40))
value = font.render(str(6), 1, (125, 36, 69))
win.blit(value, (x * 50 + 18, y * 50 + 8))
pygame.display.update()
return
if event.key == pygame.K_7 or event.key == pygame.K_KP7:
pygame.draw.rect(win, (255, 255, 255), (x * 54 - (x - 1) * x_tuning, y * 54 - (y - 1) * y_tuning, 40, 40))
value = font.render(str(7), 1, (125, 36, 69))
win.blit(value, (x * 50 + 18, y * 50 + 8))
pygame.display.update()
return
if event.key == pygame.K_8 or event.key == pygame.K_KP8:
pygame.draw.rect(win, (255, 255, 255), (x * 54 - (x - 1) * x_tuning, y * 54 - (y - 1) * y_tuning, 40, 40))
value = font.render(str(8), 1, (125, 36, 69))
win.blit(value, (x * 50 + 18, y * 50 + 8))
pygame.display.update()
return
if event.key == pygame.K_9 or event.key == pygame.K_KP9:
pygame.draw.rect(win, (255, 255, 255), (x * 54 - (x - 1) * x_tuning, y * 54 - (y - 1) * y_tuning, 40, 40))
value = font.render(str(9), 1, (125, 36, 69))
win.blit(value, (x * 50 + 18, y * 50 + 8))
pygame.display.update()
return
if event.key == pygame.K_0 or event.key == pygame.K_KP_0:
pygame.draw.rect(win, (255, 255, 255), (x * 54 - (x - 1) * x_tuning, y * 54 - (y - 1) * y_tuning, 40, 40))
pygame.display.update()
return
return
def main():
###########################
# Setting up the Game #
###########################
pygame.init()
win = pygame.display.set_mode((play_width, play_width))
pygame.display.set_caption("Sudoku")
win.fill(background_color)
#########################
# Drawing the Title #
#########################
draw_title(win)
##########################
# Creating the Grids #
##########################
draw_grid(win)
##########################################
# Placing the Numbers into the Grids #
##########################################
place_numbers(win)
########################
# Quiting the game #
########################
while True:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP and event.button == 1:
pos = pygame.mouse.get_pos()
insert(win, pos)
if event.type == pygame.QUIT:
pygame.quit()
return
main()