Review past

Python implements the “bunny and Bun” game

Python to achieve eight notes small game

Python implements jigsaw puzzles

Python to achieve skiing games

Python implements classic 90 tank battles

Python implementation FlappyBird minigame

Preface:

In this installment, we’ll be recreating an Easter egg game hidden in Chrome for years, called “Dinosaur Jump “(commonly known as T-Rex Rush, of course).

Let’s get started happily

Enter Chrome ://dino in the address bar of Chrome browser, or visit any address after being off the Internet, or visit any website requiring FQ without scientific Internet access, the following interface will appear:

At this point, press the space bar to enter Chrome’s Easter Egg game “Dinosaur Jump” :

rendering

The development tools

Python version: 3.6.4

Related modules:

Pygame module;

And some of the modules that come with Python.

Environment set up

Install Python and add it to the environment variable, and the PIP will install the appropriate module.

The principle is introduced

Introduction to the rules of the game:

Players through the space bar to control a small dinosaur forward way, when the small dinosaur touched the plant or dragon, the game ended. The more plants and dragons the baby dinosaur dodged, the higher the score.

Step by step:

Step1: Define the Sprite class

In order to facilitate collision detection between small dinosaurs and plants and dragons, we first define some Sprite classes, including:

(1) plants

Among them, the function of move function is to make the randomly generated plants constantly move to the left, so as to achieve the effect of the baby dinosaurs constantly move to the right.

(2) the pterosaurs

The definition of a dragon is a little bit more complicated, because if we want a dragon to fly more realistically, we have to change the state of the dragon at very short intervals. To be specific, there are two flight states of the flying dragon, namely, wings up and wings down:

All we have to do is change the wings from one position to the other at very short intervals.

③ Small dinosaurs

The definition of the small dinosaur is similar to that of the flying dragon, but the state is more varied, and the jumping and falling movements of the dinosaur are simulated by the uniformly accelerated linear formula.

Step2: Define the scenario class

We need to define a simple class of scenes (roads, clouds, etc.) to make our game look more realistic:

The only important thing here is that the scene should move left at the same rate as the plants (after all, they are supposed to be relatively static), and all three scenes should move left at the same time to ensure continuity.

Step3: Main loop of the game

Initialize first, including loading the game material file, instantiating the previously defined Sprite and scene classes, defining some necessary constants and user events, etc. :

Then you can enter the main game loop:

The main loop mainly includes four functions, one is event detection; The second is to constantly generate, move and draw the corresponding scene and spirit class; The third is collision detection; Fourth, statistics and display the current score.

This is the end of the article, thank you for watching, Python24 games series, the next article tower defense games

To thank you readers, I’d like to share with you some of my recent collection of dry programming goodies and give something back to every reader in the hope of helping you out.

Dry goods mainly include:

① More than 2000 Python e-books (mainstream and classic books are available)

(2) The Python Standard Library (Chinese version)

③ project source code (40 or 50 interesting and classic hands-on project and source code)

④Python basic introduction, crawler, web development, big data analysis video (suitable for small white learning)

⑤Python Learning Roadmap (Goodbye to Slow Learning)

⑥ Two days of Python crawler training camp live access

All done~ share not easy to remember to give wave thumb up + comments under support ~ see profile or private message for complete source code.