Tic-tac-toe using graphics.py

Hello everyone, this post is about developing a Tic-tac-toe game in python using graphics.py library. Tic-tac-toe is a simple game and developing it is a good start and fun as a beginner. This post focuses on developing Tic-tac-toe game by including graphics.py library. We are using graphics.py library here to improve user interface by using mouse events and some other graphic functions available in it. There are more advanced python libraries like Pygame to develop games with good user interfaces. As graphics.py is beginner friendly we are starting with it. We can draw desired shapes, write text, style with desired colors, identify the mouse events like right click, left click in selected area inside the graphics window(output window) with the help of functions from graphics.py. 

Before we start with game, lets learn about graphics.py library. Here is a video that I prefer to watch before going to use this library:

You can find the source code and documentation for graphics.py library in below links:
Go through the documentation to see what are the different functionalities library has offering. 

Now lets design the game structure and features according to the functions that we want to include which are available in the library. I want to keep my game simple and want to play with mouse pointer rather than giving input from keyboard. So, lets start building it now, create a graphics window of particular size (refer documentation to decide what functions to use and how to use from graphics.py).

Plan and divide the graphics window, for the placement of grid with tiles, message display, buttons if any etc... If needed alter window size and adjust according to requirements. Its like we are creating the outline for items inside graphics window using functions available in graphics.py library.

After that declare a array or list to store the status of tic-tac-toe tiles. Initialize it with some values except the values we use to trace the user activity in tic-tac-toe grid. Suppose I am using 'O' and 'X' to represent user choices, then I am avoiding those values by giving numbers from 1 to 9 during initialization of array. 

Now create one or more functions to display items like board, buttons, messages and to print the array contents on that board (to show X's and O's) . Develop functions needed for functioning of each item on graphics window like buttons response, mouse events for tiles, events response etc...

Once users starts the game, after each input(mouse event), keep checking whether it is a valid move, update the array by inserting users value (eg: 'X' or 'O') at users choice and check whether the game has ended. Update the graphic window by clearing the window and print its items with updated data. If the game is ended, show ended message and winner. After the completion of game we can provide restart option, exit option etc...

Increasing the number of functions and decreasing the number of lines in each function will be the good practice. Try to refer documentation for doubts while using or to know the availability of functions in graphics.py and take help from search engines if you are stuck in between.

Here is the output of my code which I developed using graphics.py:



Here is the link to source code :
https://github.com/paruchurimadhurima/Py-Tic-Tac-Toe

Thank you
Madhurima Paruchuri

Comments

Popular Posts