Chapter 4. Game of Chess

Let's now build a game of chess in Tkinter. You do not need to be a master at chess to build this game. If you have ever played chess and you know the basic rules that govern the chess pieces, you are ready to write this program.

If you have never played chess and do not know the basic rules, you would better start by reading those rules from the Internet before you start programming this application.

Mission Briefing

In its final form our chess game would look like the following screenshot:

Mission Briefing

Our chess game would enforce all standard rules applicable to the game of chess. Some advanced rules and features are left as an exercise for you to complete.

Why Is It Awesome?

In the process of building our chess application, we get introduced to the Tkinter Canvas widget, which is considered one of the most powerful and versatile features of Tkinter.

As you will see in the course of this project, the Canvas widget is a really powerful tool for a GUI programmer. It can be used to sketch compound objects using lines, rectangles, ovals, and polygons. It will also let you position images on the canvas with great accuracy.

In addition, the Canvas widget will let you place any other widget (such as labels, buttons, scale, and other widgets) on itself. This makes it an ideal container for accommodating widgets for a variety of different GUI programs.

In addition to learning about the Canvas widget, you will also get an insight on how to structure your data using Python built-in types. You will also be introduced to the concepts involved in selecting pertinent objects and structuring them into classes and modules at the right granularity.

As the application develops, we are also introduced to several other Python modules that you will often use in a variety of application development projects.

Your Hotshot Objectives

The following are the key objectives for this project:

  • How to structure a program into its model and view components
  • How to represent a problem domain in a desired notation
  • Peeking into the versatility and power of the Tkinter Canvas widget
  • Basic usage of canvas coordinates, object IDs and tags
  • How to work with newer image formats not supported by the Tkinter photo image class
  • Typical interaction of logic and presentation layers in a GUI program

Mission Checklist

We will need to process PNG images in our program. The Tkinter photo image class and other standard libraries of Python do not support PNG processing. We will use the Python Imaging Library (PIL) to render PNG files.

To install the PIL package visit:

http://www.pythonware.com/products/pil/

If you are working on windows x64 (64 bit)or MacOSX machine, you may instead need to install and work with Pillow, which is a replacement for the PIL, from:

http://www.lfd.uci.edu/~gohlke/pythonlibs/#pillow

After you have installed the package, go to your Python interactive prompt and type:

>>from PIL import ImageTk

If this executes without any error message, you are ready to make the chess application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.136.18.218