Python learning-The early days of aircraft wars

Source: Internet
Author: User

Before beginning the formal preparation of the aircraft war game, the Pygame provided the module to learn, the following code just to verify some features, and can not do aircraft war games.

Before beginning to formally write the code must first call the Pygame.init () method, and the corresponding call Pygame.quit () method, here to develop a good habit, put the code to appear together, so as not to finally forget to write, and lead to errors, Then write the game code between Pygame.init () and Pygame.quit ().

ImportPygame#Importing the Pygame module, Pygame is a Python module designed specifically to design games.  fromPlane_spritesImport*#This module inherits the Sprite method in the Sprite class in Pygame and does not need to process the image loading, drawing the image and so on each time after having this module .pygame.init () #加载pygame的模块screen= Pygame.display.set_mode ((480,850))#using the Set_mode method to create a game's main window, the Set_mode method has three default parameters, the first byte, the size of the screen, the default screen is the same as the window size, the second is the flag bit, the default is 0, the third is the depth, the default is 0, note set_ The mode method returns the result, returns a surface object, requires the variable to record this return result, the program receives the result from screen, and subsequent image drawing is done on this windowBackground = Pygame.image.load ("./image/background.png")#Call the Load method to load the background image, where the background image is passed in the Load method. Represents the current directory
Screen.blit (background, (0,0)) #screen调用blit方法, draw the background image in the window you just created, the Bilt method needs to pass in two parameters first for the background image, the second for the background image to draw the position, This parameter can be a tuple or a rectangular object, and if you want to see the image being drawn, you also need to call the update () method in the Display module, and this method updates the screen display, so it's best to write together two methodsHero = Pygame.image.load ("./image/hero.gif")#Load Hero PicturesScreen.blit (Hero, (200,400))#Draw Hero Pictures in the development positionPygame.display.update ()#Update the screen display, you can see the picture of the hero just drawn in the window, it is important to remember that no matter what the image is drawn at the end of the call this method, or in the game window will not be seen in the drawing of the imageClock = Pygame.time.Clock ()#Create a Clock objectHero_rect = Pygame. Rect (200,400,100,124)#call the Rect () method, the Rect () method is used to describe the rectangle object, receive 4 parameters are positional parameters, X, Y represents the position of the drawing image, and the width of the rectangle, with the Hero_rect variable to record the location of the hero pictureEnemy0 = Gamesprite ("./image/enemy0_down1.png") #GameSprite类的实例化 and pass in the corresponding parameter, speed is the default value enemy1= Gamesprite ("./image/enemy0_down2.png", speed = 2) Enemy_group=Pygame.sprite.Group (enemy0,enemy1) #调用精灵组方法 whileTrue:#Game Loop, the game started, the corresponding animation effect, (the so-called animation effect is still picture fast movement, the resulting visual effect is continuous, so you need to call a method to make the image fast moving, fast update) above are the game preparation work, Here is the code that can be used to move the picture continuouslyClock.tick (60)#Game Refresh frame rate, tick () method sets the delay in the loop based on the last time it was called, tick () to specify how often the code executes inside the loop bodyHero_rect.y-= 1#Move the hero image up    ifHero_rect.y <=-124:#determine if the hero image is moved out of the screen, and if you move it back to the bottom of the background imageHERO_RECT.Y = 800Screan.blit (background, (0,0))#Redraw the background image and re-draw each image to a new background image, or the ghosting will appear .    Screan.blit (Hero, Hero_rect) #在hero_rect上绘制英雄图像, Hero_rect is actually recording every frame of heroic picture movement
Enemy_group.update ()#call the Update method in the sprite group so that all sprites in the sprite group call the Update methodEnemy_group.draw (screen)#调用精灵组中的draw方法,draw all sprites in the sprite Group to screenPygame.display.update ()#Update screen DisplayPygame.quit () #卸载所有pygame模块

Python learning-The early days of aircraft wars

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.