Writing Games in Python - Jigsaw Puzzles

Writing games in Python requires the pgzrun module. But it cannot be installed with pip install pgzrun, the correct library name is pgzero, pip install pgzero. 

1. Development tools

Python version: Python 3.6.4 or above.

2. Related modules

The pgzrun module and some standard modules that come with Python.

3. Environment construction

Install Python and add it to the environment variable, and pip installs the required related modules.

Python has a wealth of third-party libraries in various fields. pygame is an application library of python in the game field, which can be used to develop various games. But for beginners, there are still certain thresholds.

pgzero (PyGame zero) is a further package based on PyGame, which makes it very convenient to design a game.

Install syntax:

pip install pgzero   # Note that it is not pip install pgzrun

Create two folders under the running Python file directory: images and fonts.

Note: You must create an images and fonts folder in the same directory as the file. Character images (png format by default) are all placed in the images folder, and Chinese fonts (ttf fonts by default) are placed in the fonts folder. Download the required fonts from the Internet or copy them from the C:\Windows\fonts directory to this fonts folder.

4. Implementation process

1. Bring out the game window

import pgzrun # The library installation name is pgzero, and the import name is pgzrun

2. Global variables and initialization information

TITLE = 'jigsaw puzzle' # window title

WIDTH = 400 # window width

HEIGHT = 500 # window height

3. Drawing Game Elements

def draw():

        pass

4. Update game state

def update():

        pass

5. Handle keyboard events

def on_key_down():

        pass

6. Handle mouse events

def on_mouse_down():

        pass

7. Execute the game program

pgzrun.go()

8. Load the game character

Actor() is used to load game characters and is the way PyGames 0 displays images. Images must be stored in the images folder located in the same location as the game script.

9. Draw graphics and text

circle, filled_circle, filled_rect, line, rect, text, textbox of screen.draw can draw graphics and text.

Draw a circle:

screen.draw.circle(pos, radius, color)

In the formula: pos is the position of the center of the circle, radius is the radius, and color is the color

Draw a filled circle:

screen.draw.filled_circle(pos, radius, color)

In the formula: pos is the position of the center of the circle, radius is the radius, and color is the color

Draw a rectangle:

screen.draw.rect(rect, color)

In the formula: rect is the coordinates of the upper left corner and lower right corner of the rectangle in pixels, and color is the color

For example:

screen.draw.rect(Rect((x,y), (w, h)), 'red')

Where (x, y) is the coordinates of the upper left corner of the rectangle, w is the width of the rectangle, h is the height of the rectangle, and 'red' is red

Draw a filled rectangle:

screen.draw.filled_rect(rect, color)

In the formula: rect is the coordinates of the upper left corner and lower right corner of the rectangle in pixels, and color is the color

write text:

Single line textbox:

screen.draw.text(*args, **kwargs)

Where: args is the parameter list, kwargs is the attribute list (attribute = value)

Common attributes, such as:

screen.draw.text(text, (x, y), fontname=font name, fontsize=font size (point size), color=color)

In the formula: text is the text to be output, (x, y) is the location coordinates, the font used by fontname, the default is the ttf font, and must be stored in the fonts folder at the same location as the game script.

Multiline textbox:

screen.draw.textbox(*args, **kwargs)

Where: args is the parameter list 

10. Directory structure

 

5. Jigsaw puzzle source code

Cut a panda map (750×500) into 24 puzzle pieces of 125×125, and arrange them in 4 rows and 6 columns. Then the sequence is randomized, and gridlines are added between the puzzle pieces to make them more visible. The player clicks on the two puzzle pieces to exchange them, restores the disordered images to normal, records the time spent, and ends the game. The one with the shortest time wins. The complete code is as follows.

6. Implementation effect

When running, the sequence of 24 pictures will be fully disrupted (see Figure 1), and the puzzle pieces can be exchanged by clicking on two puzzle pieces to restore the original picture (see Figure 2). At this time, the window will display the most Good score, and the player's time, if the player's time is less than the previous best score, use the player's time as the new best score.

Figure 1 Randomly shuffled images 

Figure 2 The image after the puzzle is successful

Guess you like

Origin blog.csdn.net/hz_zhangrl/article/details/128425922