Book Image

Python Game Programming By Example

By : Alejandro Rodas de Paz, Joseph Howse
Book Image

Python Game Programming By Example

By: Alejandro Rodas de Paz, Joseph Howse

Overview of this book

With a growing interest in learning to program, game development is an appealing topic for getting started with coding. From geometry to basic Artificial Intelligence algorithms, there are plenty of concepts that can be applied in almost every game. Python is a widely used general-purpose, high-level programming language. It provides constructs intended to enable clear programs on both a small and large scale. It is the third most popular language whose grammatical syntax is not predominantly based on C. Python is also very easy to code and is also highly flexible, which is exactly what is required for game development. The user-friendliness of this language allows beginners to code games without too much effort or training. Python also works with very little code and in most cases uses the “use cases” approach, reserving lengthy explicit coding for outliers and exceptions, making game development an achievable feat. Python Game Programming by Example enables readers to develop cool and popular games in Python without having in-depth programming knowledge of Python. The book includes seven hands-on projects developed with several well-known Python packages, as well as a comprehensive explanation about the theory and design of each game. It will teach readers about the techniques of game design and coding of some popular games like Pong and tower defense. Thereafter, it will allow readers to add levels of complexities to make the games more fun and realistic using 3D. At the end of the book, you will have added several GUI libraries like Chimpunk2D, cocos2d, and Tkinter in your tool belt, as well as a handful of recipes and algorithms for developing games with Python.
Table of Contents (9 chapters)
8
Index

Diving into the Canvas widget

So far, we have the window set up and now we can start drawing items on the canvas. The Canvas widget is two-dimensional and uses the Cartesian coordinate system. The origin—the (0, 0) ordered pair—is placed in the top-left corner, and the axis can be represented as shown in the following screenshot:

Diving into the Canvas widget

Keeping this layout in mind, we can use two methods of the Canvas widget to draw the paddle, the bricks, and the ball:

  • canvas.create_rectangle(x0, y0, x1, y1, **options)
  • canvas.create_oval(x0, y0, x1, y1, **options)

Each of these calls returns an integer, which identifies the item handle. This reference will be used later to manipulate the position of the item and its options. The **options syntax represents a key/value pair of additional arguments that can be passed to the method call. In our case, we will use the fill and the tags option.

The x0 and y0 coordinates indicate the top-left corner of the previous screenshot, and x1 and y1 are indicated in the bottom-right corner.

For instance, we can call canvas.create_rectangle(250, 300, 330, 320, fill='blue', tags='paddle') to create a player's paddle, where:

  • The top-left corner is at the coordinates (250, 300).
  • The bottom-right corner is at the coordinates (300, 320).
  • The fill='blue' means that the background color of the item is blue.
  • The tags='paddle' means that the item is tagged as a paddle. This string will be useful later to find items in the canvas with specific tags.

We will invoke other Canvas methods to manipulate the items and retrieve widget information. This table gives the references to the Canvas widget that will be used in this chapter:

Method

Description

canvas.coords(item)

Returns the coordinates of the bounding box of an item.

canvas.move(item, x, y)

Moves an item by a horizontal and a vertical offset.

canvas.delete(item)

Deletes an item from the canvas.

canvas.winfo_width()

Retrieves the canvas width.

canvas.itemconfig(item, **options)

Changes the options of an item, such as the fill color or its tags.

canvas.bind(event, callback)

Binds an input event with the execution of a function. The callback handler receives one parameter of the type Tkinter event.

canvas.unbind(event)

Unbinds the input event so that there is no callback function executed when the event occurs.

canvas.create_text(*position, **opts)

Draws text on the canvas. The position and the options arguments are similar to the ones passed in canvas.create_rectangle and canvas.create_oval.

canvas.find_withtag(tag)

Returns the items with a specific tag.

canvas.find_overlapping(*position)

Returns the items that overlap or are completely enclosed by a given rectangle.

You can check out a complete reference of the event syntax as well as some practical examples at http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm#events.