今天想用pygame库写一个击打外星人飞船的python程序
这个游戏的效果是操纵一个位于屏幕底端的飞船,通过上下左右控制飞船移动方向,按空格发射子弹。游戏中击杀一批飞船后进入下一关卡。每一关卡击打飞船获得的得分递增。若外星飞船触碰飞船或屏幕底端则减一条命。最终计算总得分。
游戏中主要包括了:飞船对象,alien对象,子弹对象,游戏设置,游戏状态,游戏控制按钮,计分板。
1.飞船部分
由于pygame中已有sprite类,故飞船可直接继承sprite类
注意在继承类时class中标注继承了什么类,_ _init_ _函数中要使用super(Ship,self).__init__()来完成继承工作。同时init中也引入了ai—setting和screen(这些部分的代码会在后面写出)
然后为了构建这个飞船的形态,需要在当前文件夹中保存一个飞船的图像。初始化时设置飞船矩形(rect)的中心与游戏界面的中心位于同一垂直线上,飞船底部位于屏幕底端。初始化时定义四个位移变量(上下左右)并设置为false,以便后面按键时对其进行改变。
接下来定义了update函数,这个函数的作用是改变飞船的当前状态,如按下←键时使得飞船左移。同时需要注意:飞船的左侧不能超过屏幕的左端,否则会很影响游戏体验。其余同理。
然后是blitme函数,这个函数完成了将飞船状态及时更新在屏幕上的工作。
最后写了一个centership函数,是用来在需要的时刻将飞船重新放置到屏幕中央。
1 import pygame
2 from pygame.sprite import Sprite
3
4 class Ship(Sprite):#继承sprite类
5
6 def __init__(self,ai_settings,screen):
7 super(Ship,self).__init__()
8 self.screen=screen
9 self.ai_settings=ai_settings
10
11 self.image=pygame.image.load('images/ship.bmp')
12 self.rect=self.image.get_rect()
13 self.screen_rect=screen.get_rect()
14
15 self.rect.centerx=self.screen_rect.centerx
16 self.rect.bottom=self.screen_rect.bottom
17
18 self.center=float(self.rect.centerx)
19 self.high=float(self.rect.bottom)
20
21 self.moving_right=False
22 self.moving_left=False
23 self.moving_up=False
24 self.moving_down=False
25
26
27 def update(self):
28 if self.moving_right and self.rect.right<self.screen_rect.right:
29 #self.rect.centerx+=1
30 self.center+=self.ai_settings.ship_speed_factor_horizen
31 if self.moving_left and self.rect.left>self.screen_rect.left:
32 #self.rect.centerx-=1
33 self.center-=self.ai_settings.ship_speed_factor_horizen
34 if self.moving_up and self.rect.top>self.screen_rect.top:
35
36 self.high-=self.ai_settings.ship_speed_factor_height
37 if self.moving_down and self.rect.bottom<self.screen_rect.bottom:
38
39 self.high+=self.ai_settings.ship_speed_factor_height
40
41
42
43 self.rect.centerx=self.center
44 self.rect.bottom=self.high
45
46 def blitme(self):
47
48 self.screen.blit(self.image,self.rect)
49 def center_ship(self):
50 self.center=self.screen_rect.centerx
View Code(ship)
给出alien部分:
alien部分生成了飞船群
然后为了产生初始的随机方向飞行,引入了random库。
同样的,这里的alien也继承了Sprite精灵。
注意,这里的飞船图像必须保存在当前文件夹下,然后用pygame.load来加载图片。
一个细节,由于alien移动距离是浮点型,所以要先计算移动的浮点类型,最后将其转换成整形以保证移动的连续性。同时由于alien图像必须保证一直处在screen内,所以这里定义了一个方法check_edges,通过self.screen.get_rect()来获取屏幕的位置,将alien与screen都视为矩形,比较两者的边界即可。
1 import pygame
2 import random##########
3 from pygame.sprite import Sprite
4
5 class Alien(Sprite):
6
7 def __init__(self,ai_settings,screen):
8 super(Alien,self).__init__()
9 self.screen=screen
10 self.ai_settings=ai_settings
11
12
13 self.image=pygame.image.load('images/alien.bmp')
14 self.rect=self.image.get_rect()
15
16 self.rect.x=self.rect.width#-20
17 self.rect.y=self.rect.height#-20
18
19 self.direction=random.choice([1,-1])
20
21
22 self.x=float(self.rect.x)
23
24 def check_edges(self):
25 screen_rect=self.screen.get_rect()
26 if self.rect.right>=screen_rect.right:
27 return True
28 elif self.rect.left<=screen_rect.left:
29 return True
30
31
32 def blitme(self):
33 self.screen.blit(self.image,self.rect)
34
35
36 def update(self):
37 #由于x可以精确到小数,故先更新x,再用x更新rect
38 self.x+=(self.ai_settings.alien_speed_factor*
39 self.direction)#*random.choice([1,-1])#####
40 self.rect.x=self.x
View Code(alien)
给出bullet部分。
bullet同ship,alien一样,也继承了sprite。
注意,由于子弹是由飞船从飞船上部发出,故初始化子弹时要将其位置设置在飞船上部中央,子弹大小也不能过大。因为子弹大小较小,所以用pygame中的draw.rect(self.screen,self.color,self.rect)来画出一个小矩形即可,这种方法比较省事。如果想要使子弹的形状更加美观,可以用上面ship和alien中加载位图的方式。
1 import pygame
2 from pygame.sprite import Sprite
3
4 class Bullet(Sprite):
5
6
7 def __init__(self,ai_settings,screen,ship):
8
9 super(Bullet,self).__init__()
10 self.screen=screen
11
12 self.rect=pygame.Rect(0,0,ai_settings.bullet_width,
13 ai_settings.bullet_height)
14 self.rect.centerx=ship.rect.centerx
15 self.rect.bottom=ship.rect.bottom
16
17 self.y=float(self.rect.y)
18
19 self.color=ai_settings.bullet_color
20 self.speed_factor=ai_settings.bullet_speed_factor
21
22 def update(self):
23 self.y-=self.speed_factor
24 self.rect.y=self.y
25
26 def draw_bullet(self):
27 pygame.draw.rect(self.screen,self.color,self.rect)
View Code(bullet)
给出游戏设置部分。
我们把游戏中所有涉及数据的部分都放在这个文件中,包括了屏幕,速度,子弹,飞船,外星人,游戏节奏以及游戏关卡更新。
1 class Settings():
2 """存储所有设置的类"""
3 def __init__(self):
4 self.high_score=0
5
6 #屏幕设置
7 self.screen_width=1200
8 self.screen_height=650
9 self.bg_color=(230,230,230)
10 #速度设置
11 self.ship_speed_factor_horizen=1
12 self.ship_speed_factor_height=0.5
13 self.ship_limit=3
14
15 #子弹设置
16 self.bullet_speed_factor=1
17 self.bullet_width=3
18 self.bullet_height=15
19 self.bullet_color=60,60,60
20 self.bullets_allowed=3
21
22 #外星人设置
23 self.alien_speed_factor=1
24 self.fleet_drop_speed=10
25 #direction为1表示右移,-1表示左移
26 self.fleet_direction=1
27 self.alien_points=50
28
29 #游戏节奏设置
30 self.speedup_scale=1.1
31 self.score_scale=1.5
32
33 self.initialize_dynamic_settings()
34
35 def initialize_dynamic_settings(self):
36 self.ship_speed_factor=1.5
37 self.bullet_speed_factor=3
38 self.alien_speed_factor=1
39
40 self.fleet_direction=1
41
42 def increase_speed(self):
43 self.ship_speed_factor*=self.speedup_scale
44 self.bullet_speed_factor*=self.speedup_scale
45 self.alien_speed_factor*=self.speedup_scale
46
47 self.alien_points=int(self.alien_points*self.score_scale)
View Code(settings)
给出游戏状态部分。
将游戏中的状态部分都放置在这个文件中。
1 class GameStats():
2 def __init__(self,ai_settings):
3 self.ai_settings=ai_settings
4 self.reset_stats()
5 self.game_active=False
6
7 self.high_score=0
8
9 def reset_stats(self):
10
11 self.ships_left=self.ai_settings.ship_limit
12 self.score=0
13 self.level=1
View Code(state)
由于游戏初始通常有一个START按钮用于开始游戏,所以这里需要定义一个按钮类。这里需要引入pygame.font用于其中的字体部分。
loading.......
1 import pygame.font
2
3 class Button():
4
5 def __init__(self,ai_settings,screen,message):
6 '''初始化按钮的属性'''
7 self.screen=screen
8 self.screen_rect=screen.get_rect()
9
10 self.width,self.height=200,50
11 self.button_color=(0,255,0)
12 self.text_color=(255,255,255)
13 self.font=pygame.font.SysFont(None,48)
14
15 self.rect=pygame.Rect(0,0,self.width,self.height)
16 self.rect.center=self.screen_rect.center
17
18
19 self.prep_message(message)
20
21
22 def prep_message(self,message):
23 #图像渲染
24 self.message_image=self.font.render(message,True,self.text_color,
25 self.button_color)
26 self.message_image_rect=self.message_image.get_rect()
27 self.message_image_rect.center=self.rect.center
28
29 def draw_button(self):
30 self.screen.fill(self.button_color,self.rect)
31 self.screen.blit(self.message_image,self.message_image_rect)
View Code(button)
给出计分板部分:
1 import pygame.font
2 from pygame.sprite import Group
3
4 from ship import Ship
5
6 class Scoreboard():
7
8
9 def __init__(self,ai_settings,screen,stats):
10 self.screen=screen
11 self.screen_rect=screen.get_rect()
12 self.ai_settings=ai_settings
13 self.stats=stats
14
15 self.text_color=(30,30,30)
16 self.font=pygame.font.SysFont(None,48)
17
18
19
20 self.prep_score()
21 self.prep_high_score()
22 self.prep_history_high_score()##########
23 self.prep_level()
24 self.prep_ships()
25
26
27 def prep_score(self):
28 '''将得分渲染成图像'''
29 rounded_score=int(round(self.stats.score,-1))
30 score_str="score:"+"{:,}".format(rounded_score)
31 self.score_image=self.font.render(score_str,True,self.text_color,
32 self.ai_settings.bg_color)
33
34
35 #将得分放在右上角
36 self.score_rect=self.score_image.get_rect()
37 self.score_rect.right=self.screen_rect.right-20
38 self.score_rect.top=20#存疑
39
40 def prep_high_score(self):
41 high_score=int(round(self.stats.high_score,-1))
42 high_score_str="High score:"+"{:,}".format(high_score)
43 self.high_score_image=self.font.render(high_score_str,True,
44 self.text_color,self.ai_settings.bg_color)
45 self.high_score_rect=self.high_score_image.get_rect()
46 self.high_score_rect.centerx=self.screen_rect.centerx-195
47 self.high_score_rect.top=self.score_rect.top
48
49 def prep_history_high_score(self):###########
50 with open("highscore.txt") as file1:
51 lines=file1.readlines()
52 temp_string=''
53 for line in lines:
54 temp_string+=line.rstrip()
55 history_high_score=eval(temp_string)
56 history_high_score_str="Highest score:"+"{:,}".format(history_high_score)
57 self.history_high_score_image=self.font.render(history_high_score_str,
58 True,self.text_color,self.ai_settings.bg_color)
59 self.history_high_score_rect=self.history_high_score_image.get_rect()
60 self.history_high_score_rect.left=self.high_score_rect.right+45
61 self.history_high_score_rect.top=self.high_score_rect.top
62
63 def prep_level(self):
64 self.level_image=self.font.render("Level:"+str(self.stats.level),True,
65 self.text_color,self.ai_settings.bg_color)
66 '''将等级放在得分下方'''
67 self.level_rect=self.level_image.get_rect()
68 self.level_rect.right=self.score_rect.right
69 self.level_rect.top=self.score_rect.bottom+10
70
71 def prep_ships(self):
72 self.ships=Group()
73 for ship_number in range(self.stats.ships_left):
74 ship=Ship(self.ai_settings,self.screen)
75 ship.rect.x=10+ship_number*ship.rect.width
76 ship.rect.y=10
77 self.ships.add(ship)
78
79 def show_score(self):
80 self.screen.blit(self.score_image,self.score_rect)
81 self.screen.blit(self.high_score_image,self.high_score_rect)
82 self.screen.blit(self.history_high_score_image,
83 self.history_high_score_rect)####
84 self.screen.blit(self.level_image,self.level_rect)
85 self.ships.draw(self.screen)
View Code(scoreboard)
给出游戏中用到的各种函数部分:
1 import sys
2 import pygame
3 import random
4 import time
5 from bullet import Bullet
6 from alien import Alien
7 from time import sleep
8
9
10 def check_keydown_events(event,ai_settings,stats,screen,ship,bullets):#stats
11 if event.key==pygame.K_RIGHT:
12 #向右移动一格
13 ship.moving_right=True
14 elif event.key==pygame.K_LEFT:
15 ship.moving_left=True
16 elif event.key==pygame.K_UP:
17 ship.moving_up=True
18 elif event.key==pygame.K_DOWN:
19 ship.moving_down=True
20 elif event.key==pygame.K_SPACE:
21 fire_bullets(ai_settings,screen,ship,bullets)
22 elif event.key==pygame.K_q:#按q键退出界面
23 renew_history_high_score(stats)###########
24 pygame.quit()
25 sys.exit()
26
27 def check_keyup_events(event,ship):
28 if event.key==pygame.K_RIGHT:
29 ship.moving_right=False
30 elif event.key==pygame.K_LEFT:
31 ship.moving_left=False
32 elif event.key==pygame.K_UP:
33 ship.moving_up=False
34 elif event.key==pygame.K_DOWN:
35 ship.moving_down=False
36
37
38 def fire_bullets(ai_settings,screen,ship,bullets):
39 if len(bullets)<ai_settings.bullets_allowed:
40
41 new_bullet=Bullet(ai_settings,screen,ship)
42 bullets.add(new_bullet)
43
44
45 def check_events(ai_settings,screen,stats,sb,play_button,ship,aliens,
46 bullets):
47
48 for event in pygame.event.get():
49
50 if event.type==pygame.QUIT:
51 renew_history_high_score(stats)###########
52 pygame.quit()
53 sys.exit()
54
55 elif event.type==pygame.KEYDOWN:
56 check_keydown_events(event,ai_settings,stats,screen,ship,bullets)
57 ##########stats
58 elif event.type==pygame.KEYUP:
59 check_keyup_events(event,ship)
60 elif event.type==pygame.MOUSEBUTTONDOWN:
61 mouse_x,mouse_y=pygame.mouse.get_pos()
62 check_play_button(ai_settings,screen,stats,sb,play_button,ship,
63 aliens,bullets,mouse_x,mouse_y)
64
65
66 def check_play_button(ai_settings,screen,stats,sb,play_button,ship,aliens,
67 bullets,mouse_x,mouse_y):
68 button_clicked=play_button.rect.collidepoint(mouse_x,mouse_y)
69 if button_clicked and not stats.game_active:
70 ai_settings.initialize_dynamic_settings()
71 #隐藏光标
72 pygame.mouse.set_visible(False)
73 #重置统计信息
74 stats.reset_stats()
75 stats.game_active=True
76 #重置记分牌图像
77 sb.prep_score()
78 sb.prep_high_score()
79 sb.prep_level()
80 sb.prep_ships()
81
82 #清空外星人和子弹
83 aliens.empty()
84 bullets.empty()
85
86 create_fleet(ai_settings,screen,ship,aliens)
87 ship.center_ship()
88
89
90
91
92 def update_screen(ai_settings,screen,stats,sb,ship,aliens,bullets,play_button):
93
94 screen.fill(ai_settings.bg_color)
95 for bullet in bullets.sprites():
96 bullet.draw_bullet()
97 ship.blitme()
98 #alien.blitme
99 aliens.draw(screen)
100 sb.show_score()
101 #如果处于非活动状态,绘制Play按钮
102 if not stats.game_active:
103 play_button.draw_button()
104 pygame.display.flip()
105
106 def check_bullet_alien_collisions(ai_settings,screen,stats,sb,
107 ship,aliens,bullets):
108 collisions=pygame.sprite.groupcollide(bullets,aliens,True,True)
109 if collisions:
110 for aliens in collisions.values():
111 stats.score+=ai_settings.alien_points*len(aliens)
112 #每个值都是一个列表!!!
113 sb.prep_score()
114 check_high_score(stats,sb)
115 if len(aliens)==0:
116
117 bullets.empty()
118 ai_settings.increase_speed()
119 stats.level+=1
120 sb.prep_level()
121 create_fleet(ai_settings,screen,ship,aliens)
122
123 def update_bullets(ai_settings,screen,stats,sb,ship,aliens,bullets):
124 bullets.update()
125
126 for bullet in bullets.copy():
127 if bullet.rect.bottom<=0:
128 bullets.remove(bullet)
129 #碰撞后返回一个字典
130 check_bullet_alien_collisions(ai_settings,screen,stats,sb,
131 ship,aliens,bullets)
132
133 def get_number_aliens_x(ai_settings,alien_width):
134 available_space_x=ai_settings.screen_width-2*alien_width
135 number_aliens_x=int(available_space_x/(2*alien_width))
136 return number_aliens_x
137
138 def get_number_rows(ai_settings,ship_height,alien_height):
139 ''''计算屏幕可容纳行数'''
140 available_space_y=(ai_settings.screen_height-
141 (3*alien_height)-ship_height)
142 number_rows=int(available_space_y/(2*alien_height))
143 return number_rows
144
145
146 def create_alien(ai_settings,screen,aliens,alien_number,row_number):
147 alien=Alien(ai_settings,screen)
148 alien_width=alien.rect.width
149 alien.x=alien_width+2*alien_width*alien_number
150 alien.rect.x=alien.x
151 alien.rect.y=alien.rect.height+2*alien.rect.height*row_number
152 aliens.add(alien)
153
154 def create_fleet(ai_settings,screen,ship,aliens):
155 '''创建外星人群'''
156 #外星人间距为外星人宽度
157 alien=Alien(ai_settings,screen)
158 number_aliens_x=get_number_aliens_x(ai_settings,alien.rect.width)
159 number_rows=get_number_rows(ai_settings,ship.rect.height,alien.rect.height)
160 total_alien_number=number_aliens_x*number_rows
161 for row_number in range(number_rows):
162 for alien_number in range(number_aliens_x):
163 #创建一个外星人并将其加入当前行
164 create_alien(ai_settings,screen,aliens,alien_number,
165 row_number)
166
167
168
169
170 def change_fleet_direction(ai_settings,aliens):
171 for alien in aliens.sprites():
172 alien.rect.y+=ai_settings.fleet_drop_speed
173 ai_settings.fleet_direction*=-1########
174
175
176 def ship_hit(ai_settings,stats,sb,screen,ship,aliens,bullets):
177 if stats.ships_left>0:
178
179 stats.ships_left-=1
180 sb.prep_ships()
181 aliens.empty()
182 bullets.empty()
183
184 create_fleet(ai_settings,screen,ship,aliens)
185 ship.center_ship()
186
187 sleep(0.5)
188 else:
189 stats.game_active=False
190 pygame.mouse.set_visible(True)
191
192 def check_aliens_bottom(ai_settings,stats,sb,screen,ship,aliens,bullets):
193 screen_rect=screen.get_rect()
194 for alien in aliens.sprites():
195 if alien.rect.bottom>=screen_rect.bottom:
196 ship_hit(ai_settings,stats,sb,screen,ship,aliens,bullets)
197 break
198
199 def check_fleet_edges(ai_settings,aliens):
200 for alien in aliens.sprites():
201 if alien.check_edges():
202 #change_fleet_direction(ai_settings,aliens)
203 #break
204 alien.direction*=-1#random.choice([1,-1])
205 alien.rect.y+=ai_settings.fleet_drop_speed
206
207 #def change_alien_direction()
208
209 def check_time(ai_settings,aliens):
210 now=time.time()
211 running_time=int(now-start)
212 if running_time==1:
213 for alien in aliens.sprites():
214 alien.direction*=-1#random.choice([1,-1])
215 alien.rect.y+=ai_settings.fleet_drop_speed
216
217
218 def update_aliens(ai_settings,stats,sb,screen,ship,aliens,bullets):
219 #check_time(start,ai_settings,aliens)
220 check_fleet_edges(ai_settings,aliens)
221 #check_time(ai_settings,aliens)
222 aliens.update()
223
224 if pygame.sprite.spritecollideany(ship,aliens):
225 ship_hit(ai_settings,stats,sb,screen,ship,aliens,bullets)
226 check_aliens_bottom(ai_settings,stats,sb,screen,ship,aliens,bullets)
227
228 def check_high_score(stats,sb):
229 if stats.score>stats.high_score:
230 stats.high_score=stats.score
231 sb.prep_high_score()
232
233 def renew_history_high_score(stats):##########
234 filename='highscore.txt'
235 with open(filename) as file_object:
236 lines=file_object.readlines()
237 temp_string=''
238 for line in lines:
239 temp_string+=line.rstrip()
240 if stats.high_score>eval(temp_string):
241 with open(filename,'w') as file:
242 file.write(str(stats.high_score))
View Code
最后给出程序的主要部分
主要部分中引入了pygame ,time以及Group
创建了 飞船对象,alien对象,子弹对象,游戏设置,游戏状态,游戏控制按钮,计分板。
1 """
2 待加入项目:
3 1.引入历史最高分 ### 已完成
4 2.加入随机飞行系统 ### 基本完成/衰
5 """
6
7 import pygame
8 import time
9 from pygame.sprite import Group
10 from game_stats import GameStats
11
12 from settings import Settings
13 from ship import Ship
14 from alien import Alien
15 from button import Button
16 from scoreboard import Scoreboard
17 import game_functions as gf
18
19
20 def run_game():
21 #初始化对象并创建一个屏幕对象
22 pygame.init() #初始化
23 ai_settings=Settings()
24 screen=pygame.display.set_mode((
25 ai_settings.screen_width,ai_settings.screen_height))
26 pygame.display.set_caption("Alien Invasion")
27 #创建一个按钮
28 play_button=Button(ai_settings,screen,'Play')
29 ship=Ship(ai_settings,screen)#创建一个飞船
30
31 #创建一个子弹编组
32 bullets=Group()
33 aliens=Group()
34 bg_color=(230,230,230)
35 alien=Alien(ai_settings,screen)
36 gf.create_fleet(ai_settings,screen,ship,aliens)
37 #创建存储游戏统计信息的实例,并创建记分牌
38 stats=GameStats(ai_settings)
39 sb=Scoreboard(ai_settings,screen,stats)
40 #start=time.time()
41 while True:
42
43 #监视键盘和鼠标事件
44 gf.check_events(ai_settings,screen,stats,sb,play_button,ship,aliens,
45 bullets)
46 if stats.game_active:
47 ship.update()
48 gf.update_bullets(ai_settings,screen,stats,sb,ship,aliens,bullets)
49 gf.update_aliens(ai_settings,stats,sb,screen,ship,aliens,bullets)
50 gf.update_screen(ai_settings,screen,stats,sb,ship,aliens,bullets,
51 play_button)
52
53
54 run_game()
55 chenk_events部分检测了鼠标以及键盘按键
View Code