-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
71 lines (52 loc) · 1.81 KB
/
gui.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
"""GUI for the Sudoku Puzzle"""
import sys, pygame as pg
import sudoku
pg.init()
screen_size = 430, 450
screen = pg.display.set_mode(screen_size)
font = pg.font.SysFont(None, 40)
obj = sudoku.Sudoku(9, 2)
# main function returns sudoku puzzle
grid = obj.get_puzzle()
# To draw the background grid like structure
def draw_background():
screen.fill(pg.Color("white"))
pg.draw.rect(screen, pg.Color("black"), pg.Rect(15, 15, 400, 400), 5, 8)
i = 1
while i * 45 <= 400:
line_width = 2 if i % 3 != 0 else 5
pg.draw.line(screen, pg.Color("black"), pg.Vector2((i * 45) + 12, 15), pg.Vector2((i * 45) + 12 , 414), line_width)
pg.draw.line(screen, pg.Color("black"), pg.Vector2(15, (i * 45) + 12), pg.Vector2(414, (i * 45) + 12), line_width)
i += 1
# To draw the numbers in the respective grid
def draw_numbers():
row = 0
offset = 32
while row < 9:
col = 0
while col < 9:
if grid[row][col] != 0:
output = grid[row][col]
else:
output = " "
n_text = font.render(str(output), True, pg.Color("black"))
screen.blit(n_text, pg.Vector2((col * 44) + offset, (row * 44) + offset - 5))
col += 1
row += 1
# Save the Puzzle as png file in the current directory
fname = "Sudoku-Puzzle.png"
pg.image.save(screen, fname)
print("file {} has been saved".format(fname))
def game_loop():
'''
for event in pg.event.get():
if event.type == pg.QUIT:
sys.exit()
'''
# Loop gets Closed Automatically once the image has been saved
draw_background()
draw_numbers()
pg.display.flip()
sys.exit()
while True:
game_loop()