Book Image

Instant Pygame for Python Game Development How-to

By : Ivan Idris
Book Image

Instant Pygame for Python Game Development How-to

By: Ivan Idris

Overview of this book

<p>Pygame is a library created to make multimedia software documenting easy to design. It adds functionality on top of the excellent SDL library. This allows you to create fully featured games and multimedia programs in the Python language. Pygame comes with functions and tools that will help you create a great user experience."Instant Pygame for Python Game Development How-to" is written in a concise and result-oriented format. Leading you through practical recipes, you'll find that this essential reference guide helps you to create visually appealing and thrilling games with a few clicks.This book starts with the basic instructions to install Pygame on different servers. It then goes into creating a sample game and explaining the features of drawing, animating, using fonts and Matplotlib with Pygame. The book then takes you through recipes to get access to some great sound and graphic effects. Giving you the steps to allow you to configure these games on Android and other networks, it ends with a walkthrough of the features of Sprites, OpenGL, and Simulation.</p>
Table of Contents (7 chapters)

Detecting collisions (Intermediate)


In the sprite demo, we left out the collision detection bit. Pygame has a number of useful collision detection functions in the Rect class. For instance, we can check whether a point is in a rectangle or whether two rectangles overlap.

How to do it...

Beside the collision detection we will replace the mouse cursor with an image of a hammer that we created. It's not a very pretty image, but it beats the boring old cursor.

  1. Updating the hit method: We will update the hit method of the sprite demo code. In the new version, we check whether the mouse cursor is within the avatar sprite. Actually to make it easier to hit the head, we create a slightly bigger rectangle:

    def hit(self):
             mouse_x, mouse_y = pygame.mouse.get_pos()
             collided = False
             bigger_rect = self.rect.inflate(40, 40)
    
             if bigger_rect.collidepoint(mouse_x, mouse_y):
                collided = True
    
             if not self.degrees and collided:
                self.degrees = 1
                self.original = self.image
                self.nhits += 1
             else:                                  
                self.nmisses += 1

  2. Replacing the mouse cursor: All the steps necessary to replace the mouse cursor were already covered. Except making the mouse cursor invisible:

    pygame.mouse.set_visible(False)

    A screenshot of the game is shown as follows:

The complete code for this example can be found in the code bundle of this book.

How it works...

We learned a bit about collision detection, the mouse cursor, and rectangles in this recipe:

Function

Description

pygame.mouse.get_pos()

This gets the mouse position as a tuple.

self.rect.inflate(40, 40)

This creates a bigger rectangle based on an offset. If the offset is negative this results in a smaller rectangle.

bigger_rect.collidepoint(mouse_x, mouse_y)

This checks whether a point is within a rectangle.

pygame.mouse.set_visible(False)

This hides the mouse cursor.