How to create Sudoku Pygame using Python3
Hello The Codezine Mates, welcome back to our Pygame Series, today we are going to build another game using pygame. This day I am going to show you how you can create a Python Sudoku game. Interesting, isn’t it?
What is Sudoku Game: It is a puzzle-based strategy game in which players place the numbers 1 to 9 into a grid-shaped box consisting of 9 squares. It is divided into 9 smaller squares in such a way that every number shows once in each line. Simply speaking, it is is a logic-based, multi number-placement puzzle game.
Requirements for the Game:
- A fast processing & high graphics PC with a Python environment installed. Typically, You can choose Mac Book, Asus Zenbook, Dell Inspiron, or any Pc with a high-level processor.
- A Code Ide or Code Editor like an atom or vs code.
- A notebook with a pen for writing important points.
- Your concentration.
Working:
Our GUI game is coded to allow a particular player to solve within a defined time. If a player can solve it within the given then a player wins & an alert pops out and the opposite happens if the player loses. It also grants a user to solve the puzzle automatically if he is unable to solve it.
Steps to build a Sudoku Game:
- Firstly, make a folder named any in your laptop & drag it to your code ide.
- Secondly, open your command prompt(CMD) & install the Pygame package by typing Pip install Pygame command.[It will permit your system to built pygame based game]
- Thirdly, make a file called main.py.
- Now, Set your codes which are given under the section to your respective files.
- Lastly, run your Pygame by typing Python main.py in your Command. That’s it you have your game.
Code
# main.py
import pygame
import time
pygame.font.init()
#Defining Board Size
class Grid:
board = [
[7, 8, 0, 4, 0, 0, 1, 2, 0],
[6, 0, 0, 0, 7, 5, 0, 0, 9],
[0, 0, 0, 6, 0, 1, 0, 7, 8],
[0, 0, 7, 0, 4, 0, 2, 6, 0],
[0, 0, 1, 0, 5, 0, 9, 3, 0],
[9, 0, 4, 0, 6, 0, 0, 0, 5],
[0, 7, 0, 3, 0, 0, 0, 1, 2],
[1, 2, 0, 0, 0, 7, 4, 0, 0],
[0, 4, 9, 2, 0, 6, 0, 0, 7]
]
#defining titles
def __init__(self, rows, cols, width, height, win):
self.rows = rows
self.cols = cols
self.cubes = [[Cube(self.board[i][j], i, j, width, height) for j in range(cols)] for i in range(rows)]
self.width = width
self.height = height
self.model = None
self.update_model()
self.selected = None
self.win = win
def update_model(self):
self.model = [[self.cubes[i][j].value for j in range(self.cols)] for i in range(self.rows)]
def place(self, val):
row, col = self.selected
if self.cubes.value == 0:
self.cubes.set(val)
self.update_model()
if valid(self.model, val, (row,col)) and self.solve():
return True
else:
self.cubes.set(0)
self.cubes.set_temp(0)
self.update_model()
return False
def sketch(self, val):
row, col = self.selected
self.cubes.set_temp(val)
def draw(self):
# Drawing Grid Lines
gap = self.width / 9
for i in range(self.rows+1):
if i % 3 == 0 and i != 0:
thick = 4
else:
thick = 1
pygame.draw.line(self.win, (0,0,0), (0, i*gap), (self.width, i*gap), thick)
pygame.draw.line(self.win, (0, 0, 0), (i * gap, 0), (i * gap, self.height), thick)
# Drawing Cubes
for i in range(self.rows):
for j in range(self.cols):
self.cubes[i][j].draw(self.win)
def select(self, row, col):
# Reset all other
for i in range(self.rows):
for j in range(self.cols):
self.cubes[i][j].selected = False
self.cubes.selected = True
self.selected = (row, col)
def clear(self):
row, col = self.selected
if self.cubes.value == 0:
self.cubes.set_temp(0)
def click(self, pos):
"""
:param: pos
:return: (row, col)
"""
if pos[0]
Conclusion
Though, It is a simple beginner-friendly Game that is easy for any beginner to built & understand it is important to follow the steps carefully. So, that you don’t get any error. Lastly, if you find useful please share it. Thank You.