Pygame Tic-tac-toe Mac OS
This is a variable declaration more than a function. The interface of Tic Tac Toe Python code is created by using the GraphWin, setBackground and Line methods available in graphics.py module. This article will guide you and give you a basic idea of designing a game Tic Tac Toe using pygame library of Python. Solutions; You may also like. In this video we continue working on our Pygame tic tac toe game! This is part 2 of a 3 part series.Other Videos in the Series: Part 1: https://www.youtube.c.
In this Python tutorial, we will learn how to create a game in python using pygame. It is easy to create a game in Python using pygame. We will see here, how to develop a Tic tac toe game using Python pygame. We will also cover below topics:
- What is the use of the grid in python pygame
- Python pygame grid game
- About tic tac toe game
- Python tic tac toe game using pygame
Contents
What is the use of grid in Python Pygame
The use of grid in python pygame is for game like tic-tac-toe, minesweeper and many more. The adventure games keep data in a grid for game. The grid of numbers can also be called a two-dimensional array or matrix.
You may like Python Pygame tutorial
Python Pygame grid
Mac Os Versions
- Firstly, we will import pygame. The pygame.init() is used to initialize all the required modules of the pygame.
- After that, we will define some colors like black, white, and red.
- Now, we will set the width and height of each grid location. The MARGIN = 5 will set the margin between each cell.
- We need to create a two-dimensional array. A two-dimension array is simply a list of lists.
- An empty list is created grid = []. Also, a loop for each row, create a list that will represent an entire row. To append a cell we will use grid[row].append(0).
- The grid[1][5] = 1 will set the row as 1 and column as 5 to one.
- Set the height and width of the screen using window_size = [255, 255].
- Also, set the title of the screen using pygame.display.set_caption(“Grid”). Loop until the user clicks the close button.
- The pygame.time.Clock() is used to manage how fast the screen updates.
- The main loop is used for the clicked event. If the user clicks the mouse then get the position.
- Also, change the x/y screen coordinates to grid coordinates.
- Set the screen background as scr.fill(black).
- for row in range(10) is used for drawing the grid. The clock.tick(50) is the limit of the frames per second.
- The pygame.display.flip() is used to update the screen with what we have drawn.
Example:
In this output, we can see that the new window appears with the grid in python pygame. The red box is the clicked position in the grid by the mouse.
In the below screenshot, we can see while clicking on the screen we get the mouse position by clicking and the coordinates.
About tic tac toe game
- Tic tac toe game is one of the most popular games. Let’s understand how this game work in python pygame.
- Tic tac toe is basically two players games. It has noughts and crosses which are represented as “O” and “X”.
- X and O takes a turns marking the spaces in a 3×3 grid.
- The player who succeeds in placing three of their marks in horizontal, vertical, or diagonal rows wins the game and the game gets over.
- We will use Python libraries to build this game.
Create tic tac toe game using Python Pygame
Here, we will explain the easy way to code the tic tac toegame in python using pygame. It is recommended to go throw the below step.
Step1.
- Firstly, we will import pygame, sys, and NumPy python libraries are used to build this game.
- Set the width and height of the game window. Also, set the background color as BG_COLOR = (20, 200, 160)
- Set the LINE_COLOR, CIRCLE_COLOR, and CROSS_COLOR according to your choice.
- To create a board we will use np.zeros().
- pygame.draw.line() is used for drawing a horizontal line and vertical line.
Step2:
Pygame Tic-tac-toe Mac Os Catalina
- def draw_figures() is used for drawing the circle and line for the cross.
- def mark_square() function is used for marking on the board and it will have 3 parameters.
- def available_square() will return true if the square is available and it’s going to return false if not available.
- def is_board_full() function will return true if the board is full and if it’s not full then it will return false. And it will loop through rows and columns.
- if board[row][col] 0 that means we have found the empty square and it will return false and if we don’t find the empty square then it will return true.
Step3:
- Here, def check_win(player) function is used. Also, the for col in range(BOARD_COLS) is used to check the vertical winning line.
- for row in range(BOARD_ROWS) is used to check the horizontal winning line.
- Now, we will check for a diagonal win for ascending draw_asc_diagonal(player) and for descending draw_desc_diagonal(player).
- def draw_vertical_winning_line(col, player) is used to draw a vertical winning line.
- if player 1 then circle color, elif player 2 then cross color.
Step4:
- def draw_horizontal_winning_line() this function is used for drawing horizontal winning line.
- if player 1 then circle color, elif player 2 then cross color.
- def draw_asc_diagonal(player) for drawing a winning line for ascending diagonal.
- if player 1 then circle color, elif player 2 then cross color
Step5:
- def draw_desc_diagonal(player) is used for drawing a descending winning diagonal.
- if player 1 then circle color, elif player 2 then cross color.
- pygame.draw.line() is used for drawing a line.
- def restart() this function is called when your game ends and you want to play again.
Step6:
- Now, we need to set the game_over = False, if the player wins then set it to true and the game is over.
- while True the main loop starts.
- Call the function check win in the mainloop.
- We used the sys library of python to exit the game.
- But if the mouse is pressed, the event.get() will return “MOUSEBUTTONDOWN” and call to user_click(). To know the exact coordinates of the board where the user has clicked.
- if event.key pygame.K_r then restart the game by pressing “r” and start playing
Complete code for tic tac toe game in python using pygame :
Output1:
In the below output, you can see that “X” wins the game.
Output2:
In the below output, you can see that “O” wins the game.
Output3:
In the below output, you can see that no one wins the game.
You may like the following Python tutorials:
I hope now you can create a grid game likethe tic tac toe game in python and also we learned how to use the pygame and sys module. Here, we learned about how to create a tic tac toe game in Python using Pygame and we have the full source code with an explanation.
Entrepreneur, Founder, Author, Blogger, Trainer, and more. Check out my profile.