In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article Xiaobian for you to introduce in detail "how to use Python to achieve the collision between birds and water pipes of the game", the content is detailed, the steps are clear, the details are handled properly, I hope that this "how to use Python to achieve the collision between birds and water pipes of the game" article can help you solve doubts, following the editor's ideas slowly in-depth, together to learn new knowledge.
Development tools
Python version: 3.6.4
Related modules:
Pygame module
And some modules that come with Python.
Environment building
Install Python and add it to the environment variable, and pip installs the relevant modules you need.
Introduction of principle
Introduction to FlappyBird games:
Players use the space bar to control a bird to cross obstacles made up of pipes of different lengths. When the bird hits the obstacle or falls to the bottom of the screen, the game ends.
Step by step:
Step1: define sprite classes
In order to facilitate the collision detection between birds and water pipes, we first define some elves, including:
① birds
With the passage of time, the bird should have the ability to update its position, that is, when the player presses the space bar, the bird jumps up, otherwise the bird falls down. In addition, in order to make the game scene more realistic, the bird should adjust its body angle before jumping up or falling down. The specific code implementation is as follows:
② pipe class
The pipe is divided into the pipe body and the pipe head. Pipe head:
Pipe body:
It can be simply defined as follows:
Obviously, a pipe head and several pipe bodies form a pipe obstacle, which is lined up in pairs, leaving some space between them for birds to pass through, like this:
Therefore, we define a large pipe class to build a complete pipe obstacle, and the code implementation is as follows:
Among them, the function of updating the pipe is to achieve the effect of the bird constantly moving to the right by moving to the left of the pipe.
Step2: realize the main cycle of the game
Initialize first, load files such as pictures, music, fonts, and define some necessary constants:
Now you can begin to define the main cycle of the game! The logic of the main cycle of the game is very simple, first display the background of the game, then update the position of the bird according to the player's operation, automatically update the position of the pipe, and judge whether the game is over by collision detection and the ordinate of the bird. If the game is over, the game end screen is displayed. Of course, you also need to update the player's current score in real time according to the number of channels passed by the player. This step must be written at the end, otherwise the score will be covered by the pipeline, which is obviously unreasonable. The specific implementation is as follows:
Read here, this "how to use Python to achieve the collision between birds and water pipes between the game" article has been introduced, want to master the knowledge of this article also need to practice and use in order to understand, if you want to know more about the article, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.