Pygame Tic-tac-toe Mac OS

broken image


Creating A Python Tic-Tac-Toe Game Using Pygame

  1. Pygame Tic-tac-toe Mac Os Update
  2. Pygame Tic-tac-toe Mac Os Download

Hello guys, welcome back to the Pygame Series, today we are going to build another simple but cool game. Today I am going to show you how you can build a Python. But, moving far let me give you the definition of the Tic Tac Toe Game.

Setcaption ('Tic Tac Toe') clock = pygame. Clock def drawlines (screen, screensize): # Draw vertical lines # Lines go from top of screen to bottom of screen: verticalline1 = int (screensize 0 / 3) # lines mark 1/3 of the board: pygame. Line (screen, BLACK, (verticalline1, 0), (verticalline1, screensize. A PC with any OS installed in it. You can choose Mac Book. A Simple Python Tic-Tac-Toe Game Using Pygame 2 Comments. 5 Easy Way to Build a Python Chat room 5. Tic Tac Toe Pygame Definition of Tic Tac Toe Game: It is a traditional strategy game where two players look for different turns to complete a row, a column, or a diagonal with either three O's or three X's drawn in the spaces of a grid of nine squares. The squares contain different noughts and crosses.

Definition of Tic Tac Toe Game: It is a traditional strategy game where two players look for different turns to complete a row, a column, or a diagonal with either three O's or three X's drawn in the spaces of a grid of nine squares. The squares contain different noughts and crosses. It is typically a paper-and-pencil game but now programmed like a computer game. The player whoever first completes placing three of their marks in a horizontal, vertical, or diagonal row in a square wins.

Now, as we have completed the first step. Let us move on directly on how to create a Python Tic Tac Toe Game using Pygame.

Requirements for the Game:

  • A good & fast processing PC with a Python environment & pygame package installed. You can choose Mac Book, Asus Zenbook, Dell Inspiron, or any Pc with a high processor.
  • Code Ide or Code Editor.
  • A notebook for writing important notation.
  • Your Focus.

Working of our Pygame:

Our Tic Tac Toe is programmed in other to allow two users or players to play the game in the same time. It is GUI game & gives an instant alert when players wins or losses or draw the game.

Steps to build a Tic Tac Toe Game:

  • First of all, create a folder named any in your PC & drag it to your code editor.
  • Secondly, open your command prompt(CMD) & install the Pygame package by typing Pip install Pygame command.
  • Thirdly, make a file called main.py & folders called image. It will you to store all your codes & image resource files needed for the game.
  • Set your codes which are given below to your respective files. You can download the image file that is given in the button link.
  • Lastly, run your Pygame by typing Python main.py in your Command. That's it you have your game.

Code

Conclusion

It is simple beginner friendly Tic-Tac-Toe Game which is easy for any beginners to built & understand. So, enjoy building & playing the game. Also, it is important to follow the steps to avoid any error, if any arises you can comment below or mail us. Lastly, if you find useful please share it. Thank You.

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

  • 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.

Pygame Tic-tac-toe Mac Os Update

Step2:

  • 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.
Pygame Tic-tac-toe Mac OS

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 :

Pygame Tic-tac-toe Mac Os Download

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:

Pygame Tic-tac-toe Mac OS

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 :

Pygame Tic-tac-toe Mac Os Download

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.





broken image