Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Mini Game ​ flappy-bird by Python

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "Python how to achieve Mini Game flappy-bird". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to achieve Mini Game flappy-bird with Python".

Game action picture:

Source code

It is recommended to read it carefully before looking back at DE8UG's analysis of the source code.

From random import *

From turtle import *

From freegames import vector

Bird = vector (0,0)

Balls = []

Score = 0

Def tap (x, y):

"Move bird up in response to screen tap."

Up = vector (0,30)

Bird.move (up)

Def inside (point):

"Return True if point on screen."

Return-200

< point.x < 200 and -200 < point.y < 200 def draw(alive): "Draw screen objects." clear() goto(bird.x, bird.y) if alive: dot(10, 'green') else: dot(10, 'red') for ball in balls: goto(ball.x, ball.y) dot(20, 'black') update() def move(): "Update object positions." global score bird.y -= 5 for ball in balls: ball.x -= 3 if randrange(10) == 0: y = randrange(-199, 199) ball = vector(199, y) balls.append(ball) while len(balls) >

0 and not inside (balls [0]):

Balls.pop (0)

Score + = 1

Print (f'get {score} scores')

If not inside (bird):

Draw (False)

Return

For ball in balls:

If abs (ball-bird) < 15:

Draw (False)

Return

Draw (True)

# draw a score in the position of the bird

Goto (bird.x, bird.y)

Write (score, font= ('Arial', 30,' normal'))

Ontimer (move, 50)

Setup (420,420,370,0)

Hideturtle ()

Up ()

Tracer (False)

Onscreenclick (tap)

Move ()

Done ()

Running

Copy the above code to a file with the suffix py and name it flappy.py.

Open the console in the directory where the file is located: run pip install freegames, then run python flappy.py

Analysis.

Look at the source code in a py file, and you can initially divide them into regions. There are usually several areas:

Import global variable function call process

We can first roughly look at the definitions of variables and functions in several areas, and then start with the calling process and analyze the code step by step.

This game code is part of a third-party library freegames. As you can see from the top import dependency section, it is mainly dependent on the library turtle.

At the end of from turtle import * is a *, which means that everything in turtle has been imported. It is generally not recommended to import too much content. One is that too much useless code may be loaded, and the other is that classes or functions with the same name may appear. At present, this is only a Mini Game, and a lot of functions in turtle are used later, so it is barely acceptable to write like this.

Looking down, the whole code uses a lot of functions, as long as it is not explicitly imported by import, and the functions not written in this python file are actually the turtle internal functions brought by this *. If you don't know what god horse means, do you remember the super complete online documentation introduced in my course? Https://devdocs.io, you only need to search turtle to see the explanation.

The import dependency part also imports some utility classes and functions in the freegames library: from freegames import vector, you can tell it is a vector by looking at the words, which is used to represent coordinates.

Then there are several functions: tap,inside,draw,move.

Flappy Bird, a game made by a Vietnamese developer a few years ago, is all the rage, addictive and mixed. DE8UG thinks it's a good game, and the python code we've seen so far doesn't fully describe the birds and obstacles, but it's enough from a learning and entertainment point of view.

The story is simple: a bird needs to keep flapping its wings and flying forward while avoiding obstacles.

For the behavior of birds and obstacles, it is not difficult to understand these functions.

Tap means flapping wings, and we simulate flight by clicking on the screen.

Inside determines whether the obstacle is on the screen.

Draw is used to draw birds and obstacles, here are actually dots, the difference is that the bird indicates normal for green, and red indicates failure. Obstacles are represented in black. Here is a parameter called alive, which is used to draw different colors according to the life or death of the bird. At the same time, when drawing obstacles, pay attention to using goto to adjust different positions to draw obstacle balls in different positions.

Move function has more functions, first of all, there must be a bird's automatic landing, indicating that if you do not flap your wings, you will fall to the ground and die. Then you need to draw a black ball to represent the obstacle. Here the obstacle moves to the left relative to the bird, so x is set to-3 (the function ends with ontimer (move, 50) for regular movement). The next step is to randomly simulate the obstacles in different positions, which is mainly the change of the ordinate y on the screen. Next, it is determined that if the obstacle is no longer on the screen, it is removed from the list. I have added a score variable here to accumulate scores after successfully avoiding obstacles. After that, if the location of the bird is not on the screen, add the parameter False to the above draw function, and then return exit. If everything is all right, cycle through all the obstacles and determine whether the position of the bird and the obstacle is small and a threshold, 15 written here. When it is less than this value, it means that the bird collides and the bird is dead. Finally, draw the score in the position of the bird, which will be a process of dynamic refresh.

Thank you for your reading, the above is the content of "how Python achieves Mini Game flappy-bird". After the study of this article, I believe you have a deeper understanding of how Python realizes Mini Game flappy-bird, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report