The goal is to copy the aircraft war, of course, after the copy of the people have their own ability to add different content.
The first is to get some picture material, familiar with the use of image processing software and painting people can make their own, and do not have the skills of students can only and I download the corresponding material from the Internet.
The corresponding image can be found on the Internet, note that all the components of the image if the PNG type of picture, that can have a transparent background, otherwise there will be a white border exposed.
After we find the footage, we're going to start building our airplane war.
The aircraft battle is controlled by fingers, and on the computer we use the mouse instead.
We know what to do, according to the program we used to move the cloud in the sky.
It's just a change of background and outlook. The code is as follows:
#-*-Coding:utf8-*-background_image_filename = ' background.png ' mouse_image_filename = ' hero.png ' #指定图像文件名称import Pygame #导入pygame库from sys import exit #向sys模块借一个exit函数用来退出程序pygame. Init () #初始化pygame, ready to use hardware screen = Pygame.display.set_mode ((480, 650), 0, +) #创建了一个窗口pygame. Display.set_caption ("planefight!") #设置窗口标题background = Pygame.image.load (background_image_filename). CONVERT () Mouse_cursor = Pygame.image.load (mouse_ Image_filename). Convert_alpha () #加载并转换图像while True: #游戏主循环 for event in Pygame.event.get (): if Event.type = = Pygame. QUIT: #接收到退出事件后退出程序 pygame.quit () exit () screen.blit (background, (0,0)) #将背景图画上去 x, y = Pygame.mouse.get_pos () #获得鼠标位置 x-= mouse_cursor.get_width ()/2 y-= mouse_cursor.get_height ()/2 #计算光标的左上角位置 Screen.blit (Mouse_cursor, (x, y)) #把光标画上去 pygame.display.update () #刷新一下画面
The results will be as follows:
The last time the mouse picture does not indicate the location of the mouse to follow the specific statement, here to illustrate, with Pygame.mouse.get_pos () can get a mouse's current coordinate position of the tuple, assign this value to X, Y, and then can be used to facilitate the call, but if we directly use this amount, The picture will appear in the lower right corner of the mouse, which is determined by the picture's coordinate system. If we want to make it mouse in the center of the picture, you must align the mouse coordinates with the center of the picture. For any surface object, you can use the Get_width (), Get_height (), and Gei_size () objects to get its dimensions so that we can align the mouse midpoint with the image size.
Of course, the drawback is that the mouse itself appears in the game, may not seem so harmonious, can be pygame.mouse.set_visible (False) to set the visibility of the mouse. Add this sentence to the program to hide the mouse.
Well, we have finished a part where the plane can be displayed on the screen and can move freely, but this movement is completely moved by us, and how do the planes and bullets that move themselves move?
We now know that the animation of the game is the nature of the change from a picture, the movement of bullets is also the case, we need it each picture is more moving forward than the previous picture, so that the bullet can be moved.
Let's summarize the characteristics of the bullets:
1, the bullet from the front of the plane shot, the launch of the coordinates should be the location of the mouse.
2. Each frame of bullets moves forward a little more.
3. When the bullet flies out of the bottom of the screen, the bullet is no longer processed. (Here I use a trick to get the bullet back to where the mouse is)
4. In order for bullets to fly from under the plane, we need to draw the bullets first and then draw the aircraft on top.
Bullets have such three characteristics, according to these three characteristics, you can write its code:
#-*-Coding:utf8-*-background_image_filename = ' background.png ' mouse_image_filename = ' hero.png ' Bullet_image_ filename = ' bullet.png ' #指定图像文件名称import pygame #导入pygame库from sys import exit #向sys模块借一个exit函数用来退出程序pygame. Init () # Initialize Pygame, prepare to use hardware screen = Pygame.display.set_mode ((480, 650), 0, +) #创建了一个窗口pygame. Display.set_caption ("Planefight !") #设置窗口标题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 () #加载并转换图像bullet_x, bullet_y = 0, -100 #初始化子弹坐标while True: #游戏主循环 for event in Pygame.event.get (): If even T.type = = Pygame. QUIT: #接收到退出事件后退出程序 pygame.quit () exit () Screen.blit (background, (0,0)) #将背景图画上去 x, y = pygame.mouse.get _pos () #获得鼠标位置 if bullet_y <-1: #移动子弹 bullet_x, bullet_y = x, y else:bullet_y-= 1 x-= mouse_cursor.get_wi DTH ()/2 y-= mouse_cursor.get_height ()/2 #计The upper-left corner of the Screen.blit (bullet, (bullet_x, bullet_y)) Screen.blit (Mouse_cursor, (x, y)) #把光标画上去 pygame.display.update () #刷新一下画面
You can get this effect:
The bullets will be fired from the aircraft and will be reset at the top of the screen.
The above code seems to solve the problem of bullet motion, so the movement of the enemy aircraft is also very simple. For the time being, let us think about the specific display of the enemy's code.
But there is a very easy to find the problem, that is, the processing speed of each machine is not the same, although each time the rotation of the coordinates to reduce one, in fact, there is a big difference, the processing speed of the machine may be processed 1000 cycles a second, and the processing of slow machines may be 30 cycles, Two machines see the animation frame rate is completely different, bullet speed is completely different, this should be how to deal with it?
Fortunately Pygame already helped us to do this, we only need to do so can make all the machines have the same speed.
The Pygame.time module provides us with a clock () object that allows us to easily control the frame rate:
Clock = Pygame.time.Clock ()
time_passed = Clock.tick ()
time_passed = Clock.tick (50)
The first line Initializes a Cloc object, the second line returns a time from the last call to the present (in milliseconds), and the third line is a good way to control the frame rate. Add it in each cycle, add parameters in Clock.tick (), represent 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 the frame rate, then we need another optimization method. So how to ensure the control of the uniform speed?
Change the above code one more time:
#-*-Coding:utf8-*-background_image_filename = ' background.png ' mouse_image_filename = ' hero.png ' Bullet_image_ filename = ' bullet.png ' #指定图像文件名称import pygame #导入pygame库from sys import exit #向sys模块借一个exit函数用来退出程序pygame. Init () # Initialize Pygame, prepare to use hardware screen = Pygame.display.set_mode ((480, 650), 0, +) #创建了一个窗口pygame. Display.set_caption ("Planefight !") #设置窗口标题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 () #加载并转换图像bullet_x, bullet_y = 0, -100 #初始化子弹坐标bullet_speed = #初始化子弹速度clock = Pygame.time.Clock () while T Rue: #游戏主循环 for event in Pygame.event.get (): if Event.type = = Pygame. QUIT: #接收到退出事件后退出程序 pygame.quit () exit () time_passed = Clock.tick (+) Time_passed_second = TIME_PASSED/1 000.0 Screen.blit (background, (0,0)) #将背景图画上去 x, y = Pygame.mouse.get_pos () #获得鼠标位置 if bullet_y <-1: #移动子弹 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 #计算光标的左上角位置 screen.blit (bullet, (bullet_x, bullet_y)) Screen.blit (Mouse_cursor, (x, y) ) #把光标画上去 pygame.display.update () #刷新一下画面
I am lazy, directly posted all the code ...
In this case, we read the elapsed time each time, and then according to each passing time, multiplied by the speed coefficient to get how much displacement should be changed. Animations that are adjusted in this way have the same display on different computers.
How to show the enemy aircraft, see how we do. Keep talking tomorrow about the display and randomness of enemy aircraft.