• 【python实例】飞机大战


      1 import pygame, time, random
      2 from pygame.locals import *
      3 
      4 
      5 class BasePlane:
      6     def __init__(self, x, y, img_path, screen):  # 显示的窗口
      7         self.x = x
      8         self.y = y
      9         self.img = pygame.image.load(img_path)
     10         self.screen = screen  # 窗口属性
     11         self.bullet_list = []
     12 
     13     def display(self):
     14         self.screen.blit(self.img, (self.x, self.y))
     15         bullet_list_copy = self.bullet_list.copy()
     16 
     17         for bullet in bullet_list_copy:
     18             if bullet.is_border():
     19                 self.bullet_list.remove(bullet)
     20             else:
     21                 bullet.display()
     22                 bullet.move()
     23 
     24 
     25 class HeroPlane(BasePlane):
     26     def __init__(self, screen):
     27         super().__init__(190, 526, "../feiji/hero1.png", screen)
     28 
     29     def move_left(self):
     30         self.x -= 10
     31 
     32     def move_right(self):
     33         self.x += 10
     34 
     35     def shoot(self):  # 发射子弹的方法
     36         # 创建子弹对象
     37         b = HeroBullet(self.x + 39, self.y - 22, "../feiji/bullet.png", self.screen)
     38         self.bullet_list.append(b)
     39 
     40 
     41 class EnemyPlane(BasePlane):
     42     def __init__(self, screen):
     43         super().__init__(0, 0, '../feiji/enemy0.png', screen)
     44         self.direction = "right"
     45 
     46     def move(self):  # 敌机移动
     47         if self.direction == "right":
     48             self.x += 5
     49         elif self.direction == "left":
     50             self.x -= 5
     51 
     52         if self.x >= 480 - 51:
     53             self.direction = "left"
     54         elif self.x <= 0:
     55             self.direction = "right"
     56 
     57     def shoot(self):
     58         num = random.randint(1, 100)
     59         if num > 97:
     60             b = EnemyBullet(self.x + 21, self.y + 39, "../feiji/bullet1.png", self.screen)
     61             self.bullet_list.append(b)
     62 
     63 
     64 class BaseBullet:
     65     def __init__(self, x, y, img_path, screen):
     66         self.x = x
     67         self.y = y
     68         self.img = pygame.image.load(img_path)
     69         self.screen = screen
     70 
     71     def move(self):
     72         pass
     73 
     74     def display(self):
     75         self.screen.blit(self.img, (self.x, self.y))
     76 
     77     def is_border(self):
     78         pass
     79 
     80 
     81 class HeroBullet(BaseBullet):
     82     def move(self):
     83         self.y -= 10
     84 
     85     def is_border(self):  # 判断是否超出边界
     86         if self.y <= -22:
     87             return True
     88         else:
     89             return False
     90 
     91 
     92 class EnemyBullet(BaseBullet):
     93     def move(self):
     94         self.y += 10
     95 
     96     def is_border(self):  # 判断是否超出边界
     97         if self.y >= 650 + 21:
     98             return True
     99         else:
    100             return False
    101 
    102 
    103 # 控制飞机移动的事件方法
    104 def control_even(hero):  # 参数:  hero 为我方飞机对象
    105 
    106     # 不停地接收用户的事件: pygame.event.get()返回值是个列表
    107     for event in pygame.event.get():  # 遍历获取用户的每一个事件对象
    108         if event.type == QUIT:   # 右上角的叉号
    109             exit()
    110         elif event.type == KEYDOWN:  # 键盘事件
    111             if event.key == K_LEFT or event.key == K_a:  # 接收向左的键盘事件
    112                 hero.move_left()
    113             elif event.key == K_RIGHT or event.key == K_d:  # 接收向右的键盘事件
    114                 hero.move_right()
    115             elif event.key == K_SPACE:
    116                 hero.shoot()
    117                 print("----piupiupiu----")
    118 
    119 
    120 def main():
    121     screen = pygame.display.set_mode((480, 650))
    122 
    123     bg = pygame.image.load("../feiji/background.png")  # 加载背景图片
    124     hero = HeroPlane(screen)  # 创建我机对象
    125     enemy = EnemyPlane(screen)  # 创建敌机对象
    126 
    127     while True:
    128         screen.blit(bg, (0, 0))  # 窗口显示背景图片
    129 
    130         control_even(hero)  # 调用控制飞机移动的方法
    131 
    132         hero.display()  # 调用方法显示我机
    133         enemy.display()  # 显示敌机
    134         enemy.shoot()  # 敌机发射子弹
    135         enemy.move()  # 敌机自动移动
    136 
    137         pygame.display.update()  # 更新
    138         time.sleep(0.05)
    139 
    140 
    141 if __name__ == "__main__":
    142     main()
  • 相关阅读:
    NET网络配置
    NTP时间同步器
    NFS配置linux
    DNS配置域名解析
    ftp配置windows
    samba配置与windows
    NTP服务和DNS服务
    NFS服务和DHCP服务scp
    SAMBA服务和FTP服务
    find用法
  • 原文地址:https://www.cnblogs.com/Tree0108/p/12116876.html
Copyright © 2020-2023  润新知