PyhontPygame 飞船⼤战外星⼈完整代码及资源下载
⼀、游戏介绍 1.游戏规则:左右键移动,空格键射击 2.游戏代码主要由⼀个主运⾏程序(alien_invasion.py)和⼋个模块组成: 1>主函数alinen_invasion.py 2>飞船函数ship.py 3>外星⼈模块alien.py 4>设置模块Setting.py 5>系统功能的模块game_function.py 6>⼦弹模块biu.py 7>系统信息的模块game_state.py 8>按钮模块button.py 9>计分板模块scoreboard.py
⼆、游戏环境搭建
1. 本练习使⽤了python3.5版本
2. pygame 模块安装,打开命令窗⼝,输⼊命令安装:
python -m pip install pygame
冰箱风冷3. 游戏图⽚下载:
数据分析师简历
URl:download.csdn/download/qq_35871505/18992871
三、游戏代码如下:
1.主函数alinen_invasion.py # __author__ = 'lzc'# -*- coding : UTF -8 -*-import sys import pygame from tting import Settings from ship import Ship import game_functions as gf from pygame .sprite import Group from alien import Alien from game_stats import GameStats from button import Button from scoreboard import Scoreboard import time def run_game (): #初始化游戏并创建⼀个屏幕对象 pygame .init () #引⽤ttings 类 ai_ttings =Settings () #屏幕的⼤⼩ screen =pygame .display .t_mode ((ai_ttings .screen_width ,ai_ttings .screen_height )) pygame .display .t_caption ("飞机⼤战") #创建按钮 play_button = Button (ai_ttings ,screen ,'PLAY') #创建飞船 ship = Ship (ai_ttings ,screen ) #创建存储⼦弹的编组 bullets =Group () #创建外星⼈群 aliens =Group () gf .create_fleet (ai_ttings ,screen ,ship ,aliens ) #创建⼀个存储游戏统计信息的实例 stats =GameStats (ai_ttings ) #创建记分牌
1
2
3
4
5
6
7
竞聘词8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
右边的英语
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
2.飞船函数ship.py sb =Scoreboard (ai_ttings ,screen ,stats ) #开始游戏的主循环 while True : #监控键盘和⿏标事件 gf .check_events (ai_ttings ,screen ,stats ,sb ,play_button ,ship ,aliens ,bullets ) if stats .game_active : #更新船的位置 ship .update () #更新⼦弹位置 gf .update_bullets (ai_ttings ,screen ,stats ,sb ,ship ,aliens ,bullets ) #外星⼈移动 gf .update_aliens (ai_ttings ,screen ,stats ,sb ,ship ,aliens ,bullets ) #更新屏幕 gf .update_screen (ai_ttings ,screen ,stats ,sb ,ship ,aliens ,bullets ,play_button ) #time .sleep (0.001)run_game ()
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# __author__ = 'lzc'# -*- coding : UTF -8 -*-import pygame from pygame .sprite import Sprite #需要 ship 继承Sprite ,以便能够创造出飞船编组class Ship (Sprite ): def __init__(lf ,ai_ttings ,screen ): super (Ship ,lf ).__init__() #ship 继承Sprite lf .screen = screen lf .ai_ttings =ai_ttings #加载飞船图像并获取其外接矩形 lf .image = pygame .image .load ('images/ship.bmp') lf .rect = lf .image .get_rect () # 得到⼩飞机的矩形区域 lf .screen_rect = screen .get_rect () # 得到screen 的矩形区域 #将每艘新飞船放在屏幕底部中央 lf .rect .centerx = lf .screen_rect .centerx # ⽔平居中 lf .rect .bottom = lf .screen_rect .bottom # 底部 #lf .rect .centery =lf .screen_rect .centery #置于中⼼ #在飞船属性center 中存储⼩数值 lf .center = float (lf .rect .centerx ) #飞船横坐标的值 lf .centery =float (lf .rect .centery ) #飞船纵坐标的值 #移动标识 lf .moving_right =Fal lf .moving_left =Fal lf .moving_up =Fal lf .moving_down =Fal def update (lf ): #更新飞船的center 值⽽不是rect 值 if lf .moving_right and lf .rect .right < lf .screen_rect .right : lf .center +=lf .ai_ttings .ship_speed_factor if lf .moving_left and lf .rect .left >0: lf .center -=lf .ai_ttings .ship_speed_factor if lf .moving_down and lf .rect .bottom <lf .screen_rect .bottom : lf .centery +=lf .ai_ttings .ship_speed_factor if lf .moving_up and lf .rect .top >0: lf .centery -=lf .ai_ttings .ship_speed_factor #根据lf .center ,更新rect 对象 lf .rect .centerx =s
elf .center lf .rect .centery =lf .centery #飞船置于底部 def botton_ship (lf ): lf .center =lf .screen_rect .centerx lf .centery = lf .screen_rect .bottom - 30 # 底部 def blitme (lf ): lf .screen .blit (lf .image ,lf .rect ) # 在指定位置绘制⼩飞机1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
手中鸟理论
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
3.外星⼈模块alien.py
4.设置模块Setting.py # __author__ = 'lzc'# -*- coding : UTF -8 -*-import pygame from pygame .sprite import Sprite class Alien (Sprite ): '''外星⼈的类''' def __init__(lf ,ai_ttings ,screen ): super (Alien ,lf ).__init__() lf .screen = screen lf .ai_ttings = ai_ttings lf .image = pygame .image .load ('images/alien.bmp') #设置其rect 属性 lf .rect = lf .image .
get_rect () # 得到矩形区域 #将其放置屏幕的左上⾓ lf .rect .x = lf .rect .width lf .rect .y = lf .rect .height lf .x = float (lf .rect .x ) def update (lf ): #向右移动外星⼈ lf .x += (lf .ai_ttings .alien_speed_factor * lf .ai_ttings .fleet_direction ) lf .rect .x = lf .x def blitme (lf ): lf .screen .blit (lf .image ,lf .rect ) # 在指定位置绘制 def check_edges (lf ): screen_rect = lf .screen .get_rect () if lf .rect .right >= screen_rect .right : return True elif lf .rect .left <=0: return True
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
5.系统功能的模块game_function.py # __author__ = 'lzc'# -*- coding : UTF -8 -*-"""存储所有设置的类的睡醒"""class Settings (): def __init__(lf ): #屏幕设置 lf .screen_width =1000 lf .screen_height =700 lf .bg_color =(230,230,230) #飞船设置 lf .ship_limit = 3 #飞船的数量 #⼦弹设置 lf .bullet_width =8 lf .bullet_height =10 lf .bullet_color =60,60,60 lf .bullets_allowed =5 #外星⼈设置 #lf .fleet_drop_speed = 5 #下落速度 lf .speedup_scale =1.1 #加快游戏的进度 lf .initialize_dynamic_ttings () #初始化游戏 def initialize_dynamic_ttings (lf ): lf .ship_speed_factor =1 # 船的速度 lf .bullet_speed_factor =1 #⼦弹的速度 lf .alien_speed_factor =1 #外星⼈的速度 lf .fleet_drop_speed = 5 #下落速度 lf .fleet_direction = 0.5 #外星⼈向右异动的速度 lf .alien_points =50 #打落外星⼈得分数 #提升速度 def increa_speed (lf ): lf .ship_speed_factor *=lf .speedup_scale lf .bullet_speed_factor *=lf .speedup_scale lf .alien_speed_factor *=lf .speedup_scale lf .fleet_drop_speed *=lf .speedup_scale
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34国安傻逼
35
36
37
38
39
40
41亚索大招
42
43
44
45
46
47# __author__ = 'lzc'# -*- coding : UTF -8 -*-import sys import pygame from bullet import Bullet from alien import Alien from random import randint import time #按下按键def check_keydown_events (event ,ai_ttings ,screen ,ship ,bullets ):1
2五谷是哪五谷
3
4
5
6
7
8
9
10
11
12