Teach you a game of flying with Python from the full 0 foundation! A person can learn

Source: Internet
Author: User

The default image is the origin (0,0) in the upper-left corner.

Incoming group: 125240963 to get dozens of sets of pdf! To source code can also be a private messages Oh!

Display window

If we set this up, when we run, the window will flash through and there will be no picture of what we imagined. Because the window will be closed just a little bit, so we're going to write a loop that keeps the window up and running. Of course, if we simply write a while true then our program will appear dead Loop, stuck dead.

Also in our resource file is a file called Shoot.pack, which records where each image is located.

We load the resource picture with the code below and get the lead plane we need.

The next step is to refine these four methods.

The simple thing is that when you press the arrow key (W,A,S,D) the plane moves around, but it can't move away from the screen.

At this point we should form a class of our aircraft, which has a method of controlling the aircraft.

It's a little more difficult to write here.

Control of the player

When the plane comes up, we should implement the way we write in the loop. The first thing we need to know is that it's not in the screen, and we can't let the plane fly out. The top and bottom four boundary values of the aircraft picture can be obtained by Rect.top,rect.bottom,rect.left,rect.right four methods.

So we can judge the plane.

Let the bullets fly.

The bullets are going to shoot along the firing direction. You can move on the screen until you move out of the screen.

We just have to define a bullet object so that the object is displayed on the screen.

Define airplane bullets first, basic and definition player, get pictures, crop pictures, set the initial position of pictures, display pictures on the screen

The next step is to let the plane's bullets follow the plane.

We need to add a method inside the Player class.

First we stipulate that the bullets are fired by the space.

The next step is to let the bullets move on the screen.

Create a Move method.

Player's plane, even if it's basically drawn.

Draw enemy aircraft

The next step is to draw the enemy. The enemy is moving from the top of the screen to the bottom of the screen. We need a class to set up the enemy. Set the class to be the same as before us, load the resource, set the RECT, set the location.

Now we should think about the characteristics of the enemy aircraft, in fact, he and the characteristics of bullets basically has been, but the direction is not the same. Another point is that the enemy planes are randomly generated.

Mobile implementation

Collision detection

Planes and planes and bullets are all there, and we now need to complete the collision detection. There are several scenarios below.

    1. The enemy planes collide with the players.
    2. The bullets collided with the enemy planes.

In any case, the collision is actually the intersection of the two pictures.

Inside the Player class, add whether to hit the attribute.

Self.is_hit = False

Modify Loops

To achieve this is basically a plane war. But there's a lot of detail to deal with.

Detail handling

Fine-grained collision detection

From the picture, when the enemy aircraft seems to have not contacted us, but it has been gameover.

The reality is this, all the pictures are rectangles, when the two pictures of the rectangular border line collision, even if two objects collide, so we want to more sophisticated use of collision detection.

Animation

How to do it, feel it does not have a little action, feeling lifeless. Whether it's an airplane flight or a plane being hit, there's no clear feedback.

For 2d games, animation is actually a picture of a continuous change. It is similar to the principle of film. To get our planes moving, we need to define a list to hold the pictures, and then write a loop that keeps him changing the picture all the time.

First we change our protagonist Player

Let's do the old routine and load the picture. Put the loaded picture in the list.

There are only two pictures of the plane flying normally. So we're going to cycle through these two pictures. So each shot one bullet, the picture changes two.

Load Picture enemies_shoot_img = [Plane_img.subsurface (pygame. Rect (267, 347, 57, 43)),

Plane_img.subsurface (Pygame. Rect (873, 697, 57, 43)),

Plane_img.subsurface (Pygame. Rect (267, 296, 57, 43)),

Plane_img.subsurface (Pygame. Rect (930, 697, 57, 43))]

This basically completes the animation effect.

Music

There is also music with animation.

The processing of music is as good as playing music in certain places, such as when a bullet is launched, when the background music is hit, when the game is over, and so on. Their processing logic is the same. Load the resource first and then play it.

Playback of background music.

Pygame is in the Pygame.mixer method when dealing with background music. The play parameters in which the music plays, the first is played several times, 1 is looped, and the subsequent floating-point representation starts from the first few seconds.

Score & Rating

Scores

First draw the score and how many points are displayed on the screen.

Drawing a font is almost as basic as drawing a sprite. To create a font first

Two parameters are font and size, respectively

We also need to cast to Str where the text is.

Score_text = Score_font.render (str (score), True, (128, 128, 128))

Grade

With a score, adding a level will make the game more interesting.

Draw the same level first.

Need source code remember private messages small compilation Oh!

Teach you a game of flying with Python from the full 0 foundation! A person can learn

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.