Today I’ve got a fun tutorial for you. It’s for making a 9x9 Sudoku grid in Python using PyGame. This is one I did a while back for CCG and I’ve edited it down a bit for clarity. Also, I’m adding a text version of the tutorial as part of this newsletter. For the best experience watch the video and refer back to the text of this article as well.
First things first, create an empty file called sudoku_tutorial.py
. In that file write the following code:
import sys, pygame as pg
pg.init()
screen_size = 750,750
screen = pg.display.set_mode(screen_size)
def draw_background():
print("hi")
def game_loop():
for event in pg.event.get():
if event.type = pg.QUIT: sys.exit()
draw_background()
pg.display.flip()
while 1:
game_loop()
That code gets you a basic game loop in pygame, sets the screen size to 750x750, and prints “hi” to the console over and over again. Run that code and see what happens. You should see a 750x750 window pop up and in the terminal background the string “hi” should print repeatedly.
Great, that’s a start. Next, let’s make the draw_background method do something more interesting. Update draw_background to use the following code:
def draw_background():
screen.fill(pg.Color("white"))
pg.draw.rect(screen, pg.Color("black"), pg.Rect(15, 15, 720, 720), 10)
i = 1
while (i * 80) < 720:
pg.draw.line(screen, pg.Color("black"), pg.Vector2((i * 80) + 15, 15), pg.Vector2((i * 80) + 15, 735), 5)
pg.draw.line(screen, pg.Color("black"), pg.Vector2(15, (i * 80) + 15), pg.Vector2(735, (i * 80) + 15), 5)
i += 1
That will draw a 9x9 grid on the screen. But we also want to add a line width for every 3rd line. So, we change the code under the while loop to do the following
while (i * 80) < 720:
line_width = 5 if i % 3 > 0 else 10
pg.draw.line(screen, pg.Color("black"), pg.Vector2((i * 80) + 15, 15), pg.Vector2((i * 80) + 15, 735), line_width)
pg.draw.line(screen, pg.Color("black"), pg.Vector2(15, (i * 80) + 15), pg.Vector2(735, (i * 80) + 15), line_width)
i += 1
The additional code will set line width to 5 in most cases, but 10 on every 3rd line. Go ahead and run the code and see what happens.
Okay, now we need to add numbers to the grid. At the top of the file, add the following code after you define screen.
font = pg.font.SysFont(none, 80)
number_grid = [
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
]
and then add a new method following draw_background called draw_numbers:
def draw_numbers():
row = 0
offset = 35
while row < 9:
col = 0
while col < 9:
output = number_grid[row][col]
n_text = font.render(str(output), True, pg.Color('black'))
screen.blit(n_text, pg.Vector2((col * 80) + offset + 5, (row * 80) + offset - 2))
col += 1
row += 1
and then add the following line to the game_loop method right after draw_background()
draw_numbers()
That should display the number grid. Run the code and see for yourself.
All in total you should have a screen that looks like a Sudoku grid. And that’s it for the tutorial.
This isn’t a full Sudoku game. The next places you might want to take this are things like importing different game boards from files. Adding the basic ruleset to the game. Allow the user to put in their own number guesses. And then maybe a solver to verify solution correctness.
I hope you enjoyed the tutorial and have fun using PyGame!
-Brian