zoukankan      html  css  js  c++  java
  • python飞机大战源码和素材

    python飞机大战素材及代码已上传百度云,链接: https://pan.baidu.com/s/1ptZzCC5Z0tqySrw6p7gHsw     提取码: pqxn

    plane_main

      1 import pygame
      2 from plane_sprites import *
      3 
      4 
      5 class PlaneGame(object):
      6     """飞机大战主游戏"""
      7 
      8     def __init__(self):
      9         print("游戏初始化")
     10 
     11         # 1.创建游戏的窗口
     12         self.screen = pygame.display.set_mode(SCREEN_RECT.size)
     13         # 2.创建游戏的时钟
     14         self.clock = pygame.time.Clock()
     15         # 3.调用私有方法,精灵和精灵组的创建
     16         self.__create_sprites()
     17 
     18         # 4.设置定时器事件——创建敌机 1S
     19         pygame.time.set_timer(CREATE_ENEMY_EVENT, 1000)
     20         pygame.time.set_timer(HERO_FIRE_EVENT, 500)
     21 
     22     # 定义精灵和精灵组
     23     def __create_sprites(self):
     24         # 创建背景精灵和精灵组
     25         bg1 = Background()
     26         bg2 = Background(True)
     27         # bg2起始位置在bg1的上方
     28         # bg2.rect.y = -bg2.rect.height
     29 
     30         self.back_group = pygame.sprite.Group(bg1, bg2)
     31 
     32         # 创建敌机的精灵组
     33         self.enemy_group = pygame.sprite.Group()
     34 
     35         # 创建英雄的精灵和精灵组
     36         self.hero = Hero()
     37         self.hero_group = pygame.sprite.Group(self.hero)
     38 
     39     # 游戏循环
     40     def start_game(self):
     41         print("游戏开始...")
     42 
     43         while True:
     44             # 1.设置刷新帧率
     45             self.clock.tick(FRAME_PER_SEC)
     46             # 2.事件监听
     47             self.__even_handler()
     48             # 3.碰撞检测
     49             self.__check_collide()
     50             # 4.更新/绘制精灵组
     51             self.__update_sprites()
     52             # 5.更新屏幕显示
     53             pygame.display.update()
     54 
     55             pass
     56 
     57     # 定义事件监听函数
     58     def __even_handler(self):
     59         for event in pygame.event.get():
     60 
     61             # 判断是否退出游戏
     62             if event.type == pygame.QUIT:
     63                 PlaneGame.__game_over()
     64             elif event.type == CREATE_ENEMY_EVENT:
     65                 # print("敌机出场...")
     66                 # 创建敌机精灵
     67                 enemy = Enemy()
     68 
     69                 # 将敌机精灵添加到敌机精灵组
     70                 self.enemy_group.add(enemy)
     71             elif event.type == HERO_FIRE_EVENT:
     72                 self.hero.fire()
     73             # 直接判断键盘按键不能持续的获取按键事件
     74             # elif event.type == pygame.KEYDOWN and event.type == pygame.K_RIGHT:
     75             #     print("向右移动...")
     76 
     77         # 使用键盘模块提供的方法获取键盘按键——键盘模块可以持续的获取键盘按键
     78         keys_pressed = pygame.key.get_pressed()
     79         # 判断元祖中对应的按键索引值
     80         if keys_pressed[pygame.K_RIGHT] or keys_pressed[pygame.K_d]:
     81             self.hero.rect.x += 2
     82         elif keys_pressed[pygame.K_LEFT] or keys_pressed[pygame.K_a]:
     83             self.hero.rect.x -= 2
     84         elif keys_pressed[pygame.K_UP] or keys_pressed[pygame.K_w]:
     85             self.hero.rect.y -= 2
     86         elif keys_pressed[pygame.K_DOWN] or keys_pressed[pygame.K_s]:
     87             self.hero.rect.y += 2
     88         else:
     89             self.hero.speed = 0
     90 
     91     # 定义碰撞检测
     92     def __check_collide(self):
     93 
     94         # 1.子弹摧毁敌机—— groupcollide可以判断两个精灵组之间是否碰撞
     95         pygame.sprite.groupcollide(self.hero.bullets, self.enemy_group, True, True)
     96 
     97         # 敌机撞毁英雄——spritecollide可以判断精灵和精灵组之间是否碰撞
     98         enemies = pygame.sprite.spritecollide(self.hero, self.enemy_group, True)
     99 
    100         # 判断列表是否有内容
    101         if len(enemies) > 0:
    102 
    103             # 让英雄牺牲
    104             self.hero.kill()
    105 
    106             # 结束游戏
    107             PlaneGame.__game_over()
    108 
    109     # 定义精灵组调用update()和draw()方法实现屏幕更新
    110     def __update_sprites(self):
    111 
    112         self.back_group.update()
    113         self.back_group.draw(self.screen)
    114         self.enemy_group.update()
    115         self.enemy_group.draw(self.screen)
    116         self.hero_group.update()
    117         self.hero_group.draw(self.screen)
    118         self.hero.bullets.update()
    119         self.hero.bullets.draw(self.screen)
    120 
    121     # 游戏结束
    122     @staticmethod
    123     def __game_over():
    124         print("游戏结束...")
    125 
    126         pygame.quit()
    127         exit()
    128 
    129 
    130 if __name__ == '__main__':
    131 
    132     # 创建游戏对象
    133     game = PlaneGame()
    134 
    135     # 启动游戏
    136     game.start_game()

    plane_sprites

      1 import random
      2 import pygame
      3 
      4 # 定义屏幕大小的常量
      5 SCREEN_RECT = pygame.Rect(0, 0, 480, 700)
      6 # 定义刷新帧率的常量
      7 FRAME_PER_SEC = 60
      8 # 创建敌机的定时器常量
      9 CREATE_ENEMY_EVENT = pygame.USEREVENT
     10 # 英雄发射子弹事件
     11 HERO_FIRE_EVENT = pygame.USEREVENT + 1
     12 
     13 
     14 class GameSprite(pygame.sprite.Sprite):
     15     """飞机大战游戏精灵"""
     16 
     17     def __init__(self, image_name, speed=1):
     18 
     19         # 调用父类的初始化方法
     20         super().__init__()
     21 
     22         # 定义对象的属性
     23         self.image = pygame.image.load(image_name)
     24         self.rect = self.image.get_rect()
     25         self.speed = speed
     26 
     27     def update(self):
     28 
     29         # 在屏幕的垂直方向上移动
     30         self.rect.y += self.speed
     31 
     32 
     33 class Background(GameSprite):
     34     """游戏背景精灵"""
     35     def __init__(self, is_alt=False):
     36 
     37         # 1.调用父类方法实现精灵的创建
     38         super().__init__("./images/background.png")
     39 
     40         if is_alt:
     41             self.rect.y = -self.rect.height
     42 
     43     def update(self):
     44 
     45         # 1.调用父类的方法实现
     46         super().update()
     47 
     48         # 2.判断是否移出屏幕,如果移出,将图像移至屏幕上方
     49         if self.rect.y >= SCREEN_RECT.height:
     50             self.rect.y = -self.rect.height
     51 
     52 
     53 class Enemy(GameSprite):
     54     """敌机精灵"""
     55 
     56     def __init__(self):
     57 
     58         # 1.调用父类方法,创建敌机精灵,同时指定敌机图片
     59         super().__init__("./images/enemy1.png")
     60         # 2.指定敌机的初始随机速度——1到3
     61         self.speed = random.randint(1, 3)
     62 
     63         # 3.指定敌机的初始随机位置
     64         self.rect.bottom = 0   # 设置敌机是从屏幕外飞进屏幕
     65         # 敌机要完整的在屏幕内 — 敌机的最大X值是屏幕的宽减去敌机的宽
     66         max_x = SCREEN_RECT.width-self.rect.width
     67         self.rect.x = random.randint(0, max_x)
     68 
     69     def update(self):
     70 
     71         # 1.调用父类方法保持垂直方向的飞行
     72         super().update()
     73         # 2.判断是否飞出屏幕,如果是 需要从精灵组中删除敌机
     74         if self.rect.y >= SCREEN_RECT.height:    # 敌机的y值大于屏幕的高即飞出屏幕
     75             # print("飞出屏幕,需从精灵组中删除...")
     76             # kill方法可以将精灵从所有精灵组中移出,精灵就好被自动销毁
     77             self.kill()
     78 
     79     # 判断敌机是否被销毁
     80     def __del__(self):
     81         # print("敌机挂了 %s" % self.rect)
     82         pass
     83 
     84 
     85 class Hero(GameSprite):
     86     """英雄精灵"""
     87 
     88     def __init__(self):
     89 
     90         # 1.调用父类方法设置image和速度
     91         super().__init__("./images/me1.png", 0)
     92         # 2.设置英雄的初始位置
     93         self.rect.centerx = SCREEN_RECT.centerx
     94         self.rect.bottom = SCREEN_RECT.bottom - 120
     95 
     96         # 3.创建子弹的精灵组
     97         self.bullets = pygame.sprite.Group()
     98 
     99     def update(self):
    100 
    101         # # 英雄在水平方向移动
    102         # self.rect.x += self.speed
    103 
    104         # 控制英雄不能离开屏幕
    105         if self.rect.x < 0:
    106             self.rect.x = 0
    107         elif self.rect.right > SCREEN_RECT.
    108             self.rect.right = SCREEN_RECT.width
    109         elif self.rect.y < 0:
    110             self.rect.y = 0
    111         elif self.rect.bottom > SCREEN_RECT.bottom:
    112             self.rect.bottom = SCREEN_RECT.bottom
    113 
    114     def fire(self):
    115 
    116         # 1.创建子弹精灵
    117         # bullet = Bullet()
    118         # bullet1 = Bullet()
    119         # bullet2 =Bullet()
    120 
    121         # # 2.设置子弹精灵的位置
    122         # bullet.rect.bottom = self.rect.y - 20
    123         # bullet1.rect.bottom = self.rect.y
    124         # bullet2.rect.bottom = self.rect.y -40
    125         # bullet.rect.centerx = self.rect.centerx
    126         # bullet1.rect.centerx = self.rect.centerx
    127         # bullet2.rect.centerx = self.rect.centerx
    128 
    129         for i in (0, 1, 2):
    130 
    131             # 1.创建子弹精灵
    132             bullet = Bullet()
    133 
    134             # 2.设置子弹的位置
    135             bullet.rect.bottom = self.rect.y - i*20
    136             bullet.rect.centerx = self.rect.centerx
    137 
    138             # 3.将精灵添加到精灵组
    139             self.bullets.add(bullet)
    140         pass
    141 
    142 
    143 class Bullet(GameSprite):
    144     """子弹精灵"""
    145 
    146     def __init__(self):
    147 
    148         # 调用父类方法设置子弹图片和初始速度
    149         super().__init__("./images/bullet2.png", -2)
    150 
    151     def update(self):
    152 
    153         # 调用方法让子弹沿垂直方向飞行
    154         super().update()
    155 
    156         # 判断子弹是否飞出屏幕
    157         if self.rect.bottom < 0:
    158             self.kill()
    159 
    160     # def __del__(self):
    161     #     print("子弹被销毁")
  • 相关阅读:
    Oracle-增加字段
    Oracle数据库将varchar类型的字段改为Clob类型
    将Oracle数据库字段长度进行修改
    http请求util
    读取excel文件后,将一行数据封装成一个对象,多行返回一个map对象即可
    使用tushare 库查阅交易日历
    python winsound模块
    python可视化:matplotlib系列
    期货、股指期权、ETF期权
    股指期货
  • 原文地址:https://www.cnblogs.com/zzmx0/p/12513357.html
Copyright © 2011-2022 走看看