Python打飞机(飞机大战)

敌机和主机

Python打飞机(飞机大战)_第1张图片
Snipaste_2018-05-13_19-17-22.png

发子弹

Python打飞机(飞机大战)_第2张图片
Snipaste_2018-05-13_19-23-02.png

连发子弹

Python打飞机(飞机大战)_第3张图片
Snipaste_2018-05-13_19-25-20.png

飞机大战(加音乐代码)

Snipaste_2018-05-13_19-28-17.png

飞机大战代码:

import pygame 
import random 
SCREEN_RECT = pygame.Rect(0,0,480,700) 
CREATE_ENEMY_EVENT = pygame.USEREVENT
精灵组
class GameSprite(pygame.sprite.Sprite): 
  def __init__(self,image_name,speed=1): 
    super().__init__()
    self.image = pygame.image.load(image_name)
    self.rect = self.image.get_rect()
    self.speed = speed
  def update(self):
    self.rect.y += self.speed
飞机大战敌机
class Enemy(GameSprite): 
  def __init__(self): 
    super().__init__("./images/enemy1.png")
    self.speed = random.randint(1, 3)
    self.rect.bottom = 0
    max_x = SCREEN_RECT.width - self.rect.width
    self.rect.x = random.randint(0, max_x)
  def update(self): 
    super().update()
    if self.rect.y >= SCREEN_RECT.height:
        print("敌机飞出屏幕...")
飞机大战背景
 class Background(GameSprite):
    def __init__(self,is_alt=False):
         image_name = "./images/background.png"
         super().__init__(image_name)
         if is_alt:
                 self.rect.y = -self.rect.height
    def update(self):
         super().update()
         if self.rect.y >= SCREEN_RECT.height:
         self.rect.y = -self.rect.height 
class Enemy(GameSprite):
  def __init__(self):
    image_name = "./images/enemy-1.gif"
    super().__init__(image_name) 
    self.speed = random.randint(1,3)
    max_x = SCREEN_RECT.width - self.rect.width 
    self.rect.x = random.randint(0,max_x)
    self.rect.bottom = 0 
  def update(self): 
    super().update()
    if self.rect.y >= SCREEN_RECT.height:
        self.kill()
  def __del__(self):
    print('删除%s'%self.rect)
class Hero(GameSprite):
  def __init__(self):
    super().__init__('./images/hero.gif',0) 
    self.rect.centerx = SCREEN_RECT.centerx 
    self.rect.bottom = SCREEN_RECT.bottom - 120
    self.bullets = pygame.sprite.Group()
  def fire(self):
    bullet = Bullet()
    bullet.rect.bottom = self.rect.y-20
    bullet.rect.centerx = self.rect.centerx
    self.bullets.add(bullet)
上下左右移动
     def update(self):
            keys_pressed = pygame.key.get_pressed()
    if keys_pressed[pygame.K_RIGHT]:
        self.rect.x += 10
    if keys_pressed[pygame.K_LEFT]:
        self.rect.x -= 10
    if keys_pressed[pygame. K_UP]:
        self.rect.y -= 10
    if keys_pressed[pygame.K_DOWN]:
        self.rect.y += 10
    if keys_pressed[pygame.K_SPACE]:
        self.fire()
    if self.rect.left < 0:
        self.rect.left = 0
    if self.rect.right > SCREEN_RECT.right:
        self.rect.right = SCREEN_RECT.right
    if self.rect.top < 0:
        self.rect.top = 0
    if self.rect.bottom > SCREEN_RECT.height:
        self.rect.bottom = SCREEN_RECT.height
子弹
      class Bullet(GameSprite):
          def __init__(self):
               super().__init__('./images/bullet1.png',-10)
          def update(self):
               super().update()     
               if self.rect.bottom < 0:
                  self.kill()

精灵组

   import pygame 
   import random 
   SCREEN_RECT = pygame.Rect(0,0,480,700) 
   CREATE_ENEMY_EVENT = pygame.USEREVENT
   class GameSprite(pygame.sprite.Sprite): 
        def __init__(self,image_name,speed=1): 
              super().__init__()
              self.image = pygame.image.load(image_name)
              self.rect = self.image.get_rect()
              self.speed = speed
            def update(self):
               self.rect.y += self.speed
     class Enemy(GameSprite): 
         def __init__(self): 
             super().__init__("./images/enemy1.png")
             self.speed = random.randint(1, 3)
             self.rect.bottom = 0
            max_x = SCREEN_RECT.width - self.rect.width
                self.rect.x = random.randint(0, max_x)
        def update(self): 
            super().update()
            if self.rect.y >= SCREEN_RECT.height:
                print("敌机飞出屏幕...")
      class Background(GameSprite):
          def __init__(self,is_alt=False):
             image_name = "./images/background.png"
             super().__init__(image_name)
             if is_alt:
                self.rect.y = -self.rect.height
            def update(self):
            super().update()
            if self.rect.y >= SCREEN_RECT.height:
                self.rect.y = -self.rect.height 
       class Enemy(GameSprite):
           def __init__(self):
            image_name = "./images/enemy-1.gif"
            super().__init__(image_name) 
            self.speed = random.randint(1,3)
            max_x = SCREEN_RECT.width - self.rect.width 
            self.rect.x = random.randint(0,max_x)
            self.rect.bottom = 0 
                def update(self): 
                    super().update()
                    if self.rect.y >= SCREEN_RECT.height:
                self.kill()
                def __del__(self):
                     print('删除%s'%self.rect)
         class Hero(GameSprite):
                def __init__(self):
                   super().__init__('./images/hero.gif',0) 
                   self.rect.centerx = SCREEN_RECT.centerx  
                   self.rect.bottom = SCREEN_RECT.bottom - 120
                   self.bullets = pygame.sprite.Group()
                def fire(self):
                    bullet = Bullet()
                    bullet.rect.bottom = self.rect.y-20
                    bullet.rect.centerx = self.rect.centerx
                    self.bullets.add(bullet)
                def update(self):
                    keys_pressed = pygame.key.get_pressed()
                    if keys_pressed[pygame.K_RIGHT]:
                        self.rect.x += 10
                    if keys_pressed[pygame.K_LEFT]:
                        self.rect.x -= 10
                    if keys_pressed[pygame. K_UP]:
                        self.rect.y -= 10
                    if keys_pressed[pygame.K_DOWN]:
                        self.rect.y += 10
                    if keys_pressed[pygame.K_SPACE]:
                        self.fire()
                    if self.rect.left < 0:
                        self.rect.left = 0
                    if self.rect.right > SCREEN_RECT.right:
                        self.rect.right = SCREEN_RECT.right
                    if self.rect.top < 0:
                        self.rect.top = 0
                    if self.rect.bottom > SCREEN_RECT.height:
                        self.rect.bottom = SCREEN_RECT.height
            class Bullet(GameSprite):
               def __init__(self):
                 super().__init__('./images/bullet1.png',-1)
               def update(self):
                 super().update()       
                 if self.rect.bottom < 0:
                 self.kill()

你可能感兴趣的:(Python打飞机(飞机大战))