Getting started with pygame for airplane vs. game with Python (4): getting the mouse position and motion, airplane vs. pygame

Source: Internet
Author: User

Getting started with pygame for airplane vs. game with Python (4): getting the mouse position and motion, airplane vs. pygame

The target is a copy of the aircraft war, of course, after the copy, everyone will have the ability to add different content on their own.

First, you need to get some image materials. People who are familiar with image processing software and painting can make the materials by themselves. Those who do not have this skill can only download the corresponding materials from the internet like me.

You can find the corresponding image on the Internet. Note that if all the component images are png images, they can have a transparent background. Otherwise, a white border will be exposed.

After finding the materials, we are about to start building our planes.

The battle on the plane was controlled by fingers. on the computer, we replaced it with a mouse.

Follow the procedure we used to move the cloud in the sky, and we can know what to do.

It is nothing more than changing the background and prospects. The Code is as follows:

#-*-Coding: utf8-*-background_image_filename = 'background.png 'mouse _ image_filename = 'hero.png' # specify the image file name import pygame # import the pygame library from sys import exit # use an exit function to exit the program from the sys module pygame. init () # initialize pygame to prepare for using hardware screen = pygame. display. set_mode (480,650), 0, 32) # create a window pygame. display. set_caption ("PlaneFight! ") # Set the window title background = pygame. image. load (background_image_filename ). convert () mouse_cursor = pygame. image. load (mouse_image_filename ). convert_alpha () # Load and convert the image while True: # main game loop for event in pygame. event. get (): if event. type = pygame. QUIT: # exit the pygame program after receiving the exit event. quit () exit () screen. bground (background, () # Add the background image x, y = pygame. mouse. get_pos () # obtain the mouse position x-= mouse_cursor.get_width ()/2 y-= mouse_cursor.get_height ()/2 # Calculate the screen position in the upper left corner of the cursor. BITs (mouse_cursor, (x, y) # Draw the cursor over pygame. display. update () # refresh the screen

The result is as follows:

The previous time I did not describe the mouse image following the mouse position. Here I will describe how to use pygame. mouse. get_pos () is used to obtain the tuples of the current Coordinate Position of the mouse, assign the value to x and y, and then use it for convenient calling. However, if we use this amount directly, the image will appear in the lower right corner of the mouse, which is determined by the coordinate system of the image. If we want to place the mouse in the image center, we must align the mouse coordinates with the image center. For any Surface object, you can use the get_width (), get_height (), and gei_size () objects to obtain its size. In this way, you can align the center point with the image size.

Of course, the fly in the ointment is that the mouse itself appears on the game and may not seem so harmonious. You can set the mouse visibility through pygame. mouse. set_visible (False. By adding this sentence to the program, you can hide the mouse.

Okay, we have completed a part. The plane can be displayed on the screen and can be moved freely. However, this movement is entirely made by us, how did the planes and bullets that they moved move?

We now know that the essence of the game's animation is the change of a picture, and the movement of bullets is the same. We need it to move a little more forward than the previous one, in this way, bullets can be moved.

We will summarize the characteristics of bullets:

1. The bullet is shot from the front of the plane. The coordinates of the bullet should be the location of the mouse.

2. Each frame of a bullet moves more forward.

3. The bullet is no longer processed when it is flying out of the bottom of the screen. (Here I use a trick to let the bullet go back to the position where the mouse is located)

4. To allow a bullet to fly out of the plane, we need to draw a bullet first and then draw the plane on it.

A bullet has three features:

#-*-Coding: utf8-*-background_image_filename = 'background.png 'mouse _ image_filename = 'hero.png' bullet _ image_filename = 'bullet.png '# specify the image file name import pygame # import the pygame library from sys import exit # Borrow from sys module an exit function is used to exit the pygame program. init () # initialize pygame to prepare for using hardware screen = pygame. display. set_mode (480,650), 0, 32) # create a window pygame. display. set_caption ("PlaneFight! ") # Set the window title pygame. mouse. set_visible (False) background = pygame. image. load (background_image_filename ). convert () mouse_cursor = pygame. image. load (mouse_image_filename ). convert_alpha () bullet = pygame. image. load (bullet_image_filename ). convert_alpha () # Load and convert the image bullet_x, bullet_y = 0,-100 # initialize the bullet coordinate while True: # Game Main Loop for event in pygame. event. get (): if event. type = pygame. QUIT: # exit the pygame program after receiving the exit event. quit () exit () screen. bground (background, () # Add the background image x, y = pygame. mouse. get_pos () # obtain the mouse position if bullet_y <-1: # Move the bullet bullet_x, bullet_y = x, y else: bullet_y-= 1 x-= mouse_cursor.get_width () /2 y-= mouse_cursor.get_height ()/2 # Calculate the screen at the upper left corner of the cursor. blet (bullet, (bullet_x, bullet_y) screen. BITs (mouse_cursor, (x, y) # Draw the cursor over pygame. display. update () # refresh the screen

The following results can be obtained:

The bullet is played from the plane and reset to the top of the screen.

The above Code seems to solve the problem of bullet movement, so the movement of the enemy plane is very simple. For the moment, we will not talk about the code that specifically shows the enemy's servers, so that everyone can think about it.

In fact, there is a very easy-to-find problem, that is, the processing speed of each machine is different. Although the coordinates are reduced by one during each cycle, there is a big difference in reality, A machine with fast processing speed may process one thousand cycles in one second, while a machine with slow processing speed may only process 30 cycles. The animation frame rates of the two machines are completely different, and the bullet speed is also completely different, what should I do?

Fortunately, pygame has helped us do this for a long time. We only need to do this so that all the machines can have the same speed.

The pygame. time module provides a Clock () object for us to easily control the frame rate:

Clock = pygame. time. Clock ()
Time_passed = clock. tick ()
Time_passed = clock. tick (50)

The first row initializes a Cloc object, the second row returns a time from the last call to the present (unit in milliseconds), and the third row is a good way to control the frame rate. In each cycle, add it in clock. adding parameters to tick () indicates the maximum frame rate you set. The maximum frame rate of your screen is the value you write. Of course, sometimes the animation is too complex, it may not be able to reach this frame rate. At that time, we need other optimization methods. So how can we ensure constant control speed?

Change the above Code again:

#-*-Coding: utf8-*-background_image_filename = 'background.png 'mouse _ image_filename = 'hero.png' bullet _ image_filename = 'bullet.png '# specify the image file name import pygame # import the pygame library from sys import exit # Borrow from sys module an exit function is used to exit the pygame program. init () # initialize pygame to prepare for using hardware screen = pygame. display. set_mode (480,650), 0, 32) # create a window pygame. display. set_caption ("PlaneFight! ") # Set the window title pygame. mouse. set_visible (False) background = pygame. image. load (background_image_filename ). convert () mouse_cursor = pygame. image. load (mouse_image_filename ). convert_alpha () bullet = pygame. image. load (bullet_image_filename ). convert_alpha () # Load and convert the image bullet_x, bullet_y = 0,-100 # initialize the bullet coordinate bullet_speed = 600 # initialize the bullet speed clock = pygame. time. clock () while True: # main game loop for event in pygame. event. get (): if event. type = pygame. QUIT: # exit the pygame program after receiving the exit event. quit () exit () time_passed = clock. tick (100) time_passed_second = time_passed/1000.0 screen. bground (background, () # Add the background image x, y = pygame. mouse. get_pos () # obtain the mouse position if bullet_y <-1: # Move the bullet bullet_x, bullet_y = x, y else: bullet_y-= time_passed_second * bullet_speed x-= mouse_cursor.get_width () /2 y-= mouse_cursor.get_height ()/2 # Calculate the screen at the upper left corner of the cursor. blet (bullet, (bullet_x, bullet_y) screen. BITs (mouse_cursor, (x, y) # Draw the cursor over pygame. display. update () # refresh the screen

I am relatively lazy and directly paste all the code...

In this case, we read the elapsed time each time and multiply the speed coefficient to get the changed displacement based on the elapsed time. The animation adjusted in this way has the same display on different computers.

How are enemy planes displayed. We will continue to talk about the display and randomness of enemy planes tomorrow.

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.