Play Airplane Games Python+pygame

Source: Internet
Author: User

First install the Python and pygame environment

sudo apt0get install python

sudo apt-get install Python-pygame

There are several diagrams in the code, find your own

1 #-*-coding:utf-8-*-2 ImportPygame3 ImportRandom4  fromSysImportExit5 #defining enemy aircraft classes6 classEnemy:7     defRestart (self):#Reset the position and speed of the enemy aircraft8self.x = Random.randint (50, 400)9SELF.Y = Random.randint (-200,-50)TenSelf.speed = Random.random () +0.1 One     def __init__(self):#Initialize A Self.restart () -Self.image = Pygame.image.load ('Enemy.png'). Convert_alpha () -     defMove (self):#Move Down the         ifSELF.Y < 800 : -Self.y + =Self.speed -         Else:# Reset - Self.restart () +  - #defines the bullet class. Encapsulating bullet-related data and methods + classBullet: A     def __init__(self):#initial variable, two _, the class is created automatically, otherwise it will appear attributeerror:bullet instance has no attribute atself.x =0 -SELF.Y =-1 -Self.image = Pygame.image.load ('Bullet.png'). Convert_alpha () -Self.active = False#not activated by default -     defMove (self):#the movement of the bullets is handled -         ifSelf.active:#Active state, move up inSELF.Y-= 3 -         ifSelf.y <0: toSelf.active =False +     defRestart (self):#Reset Bullet Position -MouseX, Mousey =Pygame.mouse.get_pos () theself.x = Mousex-self.image.get_width ()/2 *SELF.Y = Mousey-self.image.get_height ()/2 $Self.active = True#Activating BulletsPanax Notoginseng #Defining aircraft Classes - classPlane: the     defRestart (self): +self.x = 200 ASELF.Y = 300 the     def __init__(self): + Self.restart () -Self.image = Pygame.image.load ('Plane.png'). Convert_alpha () $     defMove (self): $X, y = Pygame.mouse.get_pos ()#get the position of the mouse -X-= Self.image.get_width ()/2#make the mouse in the middle of the plane image -Y-= Self.image.get_height ()/2 theself.x =x -SELF.Y =yWuyi      the defCheckhit (Enemy, Bullet):#detect if bullets and airplanes collide . -     if(Bullet.x > enemy.x andBullet.x < Enemy.x+enemy.image.get_width ()) and  Wu(Bullet.y > Enemy.y andBullet.y < Enemy.y+enemy.image.get_height ()):#Space +\ for code wrapping - Enemy.restart () AboutBullet.active =False $         returnTrue -     returnFalse -  - defCheckcrash (Enemy, plane):#detects if the enemy plane collided with the aircraft A     if(plane.x + 0.7*plane.image.get_width () > Enemy.x) and(plane.x + 0.3*plane.image.get_width () < enemy.x + Enemy.image.get_width ()) and  +(Plane.y + 0.7*plane.image.get_height () > Enemy.y) and(Plane.y + 0.3*plane.image.get_height () < ENEMY.Y +enemy.image.get_height ()): the         returnTrue -     returnFalse $      the Pygame.init () theScreen = Pygame.display.set_mode ((450,700), 0, 32)#Production window thePygame.display.set_caption ('Fight Plane')#window name theBackground = Pygame.image.load ('background.jpg'). CONVERT ()#loading the background map -Plane = plane ()#creating aircraft Objects inenemies = []#Create an enemy list the  forIinchRange (5): the Enemies.append (Enemy ()) AboutBullets = []#Create Bullets List the  forIinchRange (5):#Add 5 rounds to the list the Bullets.append (Bullet ()) theCount_b = Len (bullets)#Total Bullets +Index_b = 0#the number of bullets that will be activated -Interval_b = 0#firing the bullet interval theGameover =FalseBayiScore =0 theFont = Pygame.font.Font (none,32)#Create a Font object, none is the default font, 32 is the font size the  whileTrue: -      forEventinchpygame.event.get (): -         ifEvent.type = =Pygame. QUIT: the pygame.quit () the exit () the         ifGameover andEvent.type = = Pygame. Mousebuttonup:#Click the mouse to reset the game the Plane.restart () -              forEinchEnemies: the E.restart () the              forBinchBullets: theB.active =False94Score =0 theGameover =False theIndex_b =0 theInterval_b =098Screen.blit (background, (0,0))#draw the background image, starting at the top left corner (0,0) About      -     if  notGameover:101Interval_b-= 1#emission interval decreasing, actual transmit interval affected by CPU102         ifInterval_b < 0:#interval less than 0 o'clock, firing a bullet103 Bullets[index_b].restart ()104Interval_b = 100#Reset time interval theIndex_b = (Index_b + 1)%count_b#periodic decrement of bullet number106          forBinchBullets:#draw an activated bullet107             ifb.active:108 B.move ()109 Screen.blit (B.image, (B.X,B.Y)) the                  forEinchEnemies:111                     ifCheckhit (E, b):#detects if the activated bullet collided with the plane. theScore + = 10113          forEinchEnemies:#movement and description of enemy aircraft the             ifCheckcrash (E, plane): theGameover =True the E.move ()117 Screen.blit (E.image, (e.x, e.y))118 Plane.move ()119Screen.blit (Plane.image, (PLANE.X,PLANE.Y))#Draw a plane -Text = Font.render ("score:%d"%score,1, (0,0,0))121Screen.blit (text, (0,0))#show fractions in the upper-left corner of the screen122     Else :123Text1 = Font.render ("score:%d"%score,1, (0,0,0))124Text2 = Font.render ("Click Restart", 1, (0,0,0)) theScreen.blit (Text1, (190,400))#Show fractions in the middle of the screen126Screen.blit (Text2, (170,420))127Pygame.display.update ()#Refresh

Play Airplane Games Python+pygame

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.