python飞机大战源码和素材

摘要:
python飞机大战素材及代码已上传百度云,链接:https://pan.baidu.com/s/1ptZzCC5Z0tqySrw6p7gHsw提取码:pqxnplane_main1importpygame2fromplane_spritesimport*345classPlaneGame(object):6"""飞机大战主游戏"""78def__init__(self):9print("游戏初始

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

plane_main

1 importpygame
2 from plane_sprites import *
3 
4 
5 classPlaneGame(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     defstart_game(self):
41         print("游戏开始...")
42 
43         whileTrue:
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 inpygame.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] orkeys_pressed[pygame.K_d]:
81             self.hero.rect.x += 2
82         elif keys_pressed[pygame.K_LEFT] orkeys_pressed[pygame.K_a]:
83             self.hero.rect.x -= 2
84         elif keys_pressed[pygame.K_UP] orkeys_pressed[pygame.K_w]:
85             self.hero.rect.y -= 2
86         elif keys_pressed[pygame.K_DOWN] orkeys_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 importrandom
2 importpygame
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 classGameSprite(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     defupdate(self):
28 
29         #在屏幕的垂直方向上移动
30         self.rect.y +=self.speed
31 
32 
33 classBackground(GameSprite):
34     """游戏背景精灵"""
35     def __init__(self, is_alt=False):
36 
37         #1.调用父类方法实现精灵的创建
38         super().__init__("./images/background.png")
39 
40         ifis_alt:
41             self.rect.y = -self.rect.height
42 
43     defupdate(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 classEnemy(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     defupdate(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 classHero(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     defupdate(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     deffire(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 classBullet(GameSprite):
144     """子弹精灵"""
145 
146     def __init__(self):
147 
148         #调用父类方法设置子弹图片和初始速度
149         super().__init__("./images/bullet2.png", -2)
150 
151     defupdate(self):
152 
153         #调用方法让子弹沿垂直方向飞行
154 super().update()
155 
156         #判断子弹是否飞出屏幕
157         if self.rect.bottom <0:
158 self.kill()
159 
160     #def __del__(self):
161     #print("子弹被销毁")

免责声明:文章转载自《python飞机大战源码和素材》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇JDBC的批量查询报告内存溢出解决方法springboot + ehcache下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

解决python中import时无法识别自己写的包和模块的方法

我们用pycharm打开自己写的代码,当多个文件之间有相互依赖的关系的时候,import无法识别自己写的文件,但是我们写的文件又确实在同一个文件夹中, 这种问题可以用下面的方法解决: 1)打开File--》Setting—》打开 Console下的Python Console,把选项(Add source roots to PYTHONPAT)点击勾选上...

python 快速触发adb 命令, 快速点击

import subprocess import time # 按照次数来点击它 def loop_click_for_android(run_num=30): res = subprocess.Popen('adb devices', shell=True, stdout=subproce...

python 换行符的识别问题,Unix 和Windows 中是不一样的

关于换行符的识别问题,在Unix 和Windows 中是不一样的(分别是n 和rn)。默认情况下,Python 会以统一模式处理换行符。这种模式下,在读取文本的时候,Python 可以识别所有的普通换行符并将其转换为单个nn 字符。类似的,在输出时会将换行符nn 转换为系统默认的换行符。如果你不希望这种默认的处理方式,可以给open() 函数传入参数new...

实现crontab定时调用python脚本,以及command not found的问题

操作 1.修改 /etc/crontab文件调用python脚本和其他sh的不同是:需要写清楚调用哪个python解释器例如:* 12 * * * root /usr/bin/python /home/admin/test.py 需要用/usr/bin/python 全路径指定.另外需要在此前写root 表示调用账户.2.增加日志使用/home/admin...

python自动化测试-D9-学习笔记之一(线程池)

# 封装 线程池import threadpoolclass MyPool(object):def __init__(self,func,size=20,data=None):#func 函数,size线程数,data数据self.func = funcself.size = sizeself.data = dataself.pool()def pool(...

pywin3的简介

     微软Windows的Python扩展提供了对Win32 API的访问、创建和使用COM对象的能力以及PythOnWin环境。Pywin32是一个Python库,为python提供访问Windows API的扩展,提供了齐全的windows常量、接口、线程以及COM机制等等。...