飞机大战游戏

一、主函数部分

  1 import pygame
  2 import sys
  3 import traceback
  4 from pygame.locals import *
  5 import myplane
  6 import enemy
  7 import bullet
  8 import supply
  9 import random
 10 
 11 pygame.init()
 12 bg_size = width, height = 400, 700
 13 screen = pygame.display.set_mode(bg_size)
 14 pygame.display.set_caption("飞机大战")
 15 
 16 background = pygame.image.load("images/background.png").convert()
 17 
 18 BLACK = (0,0,0)
 19 GREEN = (0, 255, 0)
 20 RED = (255, 0, 0)
 21 WHITE = (255,255,255)
 22 
 23 #载入游戏音乐
 24 pygame.mixer.music.load("sound/game_music.ogg")
 25 pygame.mixer.music.set_volume(0.2)
 26 bullet_sound = pygame.mixer.Sound("sound/bullet.wav")
 27 bullet_sound.set_volume(0.2)
 28 bomb_sound = pygame.mixer.Sound("sound/use_bomb.wav")
 29 bomb_sound.set_volume(0.2)
 30 supply_sound = pygame.mixer.Sound("sound/supply.wav")
 31 supply_sound.set_volume(0.2)
 32 get_bomb_sound = pygame.mixer.Sound("sound/get_bomb.wav")
 33 get_bomb_sound.set_volume(0.2)
 34 get_bullet_sound = pygame.mixer.Sound("sound/get_bullet.wav")
 35 get_bullet_sound.set_volume(0.2)
 36 upgrade_sound = pygame.mixer.Sound("sound/upgrade.wav")
 37 upgrade_sound.set_volume(0.2)
 38 enemy3_fly_sound = pygame.mixer.Sound("sound/enemy3_flying.wav")
 39 enemy3_fly_sound.set_volume(0.2)
 40 enemy1_down_sound = pygame.mixer.Sound("sound/enemy1_down.wav")
 41 enemy1_down_sound.set_volume(0.2)
 42 enemy2_down_sound = pygame.mixer.Sound("sound/enemy2_down.wav")
 43 enemy2_down_sound.set_volume(0.2)
 44 enemy3_down_sound = pygame.mixer.Sound("sound/enemy3_down.wav")
 45 enemy3_down_sound.set_volume(0.5)
 46 me_down_sound = pygame.mixer.Sound("sound/me_down.wav")
 47 me_down_sound.set_volume(0.2)
 48 
 49 def add_small_enemies(group1, group2, num):
 50     for i in range(num):
 51         e1 = enemy.SmallEnemy(bg_size)
 52         group1.add(e1)
 53         group2.add(e1)
 54 
 55 def add_mid_enemies(group1, group2, num):
 56     for i in range(num):
 57         e1 = enemy.MidEnemy(bg_size)
 58         group1.add(e1)
 59         group2.add(e1)
 60 
 61 def add_big_enemies(group1, group2, num):
 62     for i in range(num):
 63         e1 = enemy.BigEnemy(bg_size)
 64         group1.add(e1)
 65         group2.add(e1)
 66 
 67 def inc_speed(target, inc):
 68     for each in target:
 69         each.speed += inc
 70 
 71 def main():
 72     pygame.mixer.music.play(-1)
 73     
 74     clock = pygame.time.Clock()
 75 
 76     # 中弹图片索引
 77     e1_destroy_index = 0
 78     e2_destroy_index = 0
 79     e3_destroy_index = 0
 80     me_destroy_index = 0
 81 
 82     me = myplane.MyPlane(bg_size)
 83     
 84     enemies = pygame.sprite.Group()
 85 
 86     # 生成敌方小型飞机
 87     small_enemies = pygame.sprite.Group()
 88     add_small_enemies(small_enemies, enemies, 15)
 89 
 90     mid_enemies = pygame.sprite.Group()
 91     add_mid_enemies(mid_enemies, enemies, 5)
 92 
 93     big_enemies = pygame.sprite.Group()
 94     add_big_enemies(big_enemies, enemies, 1)
 95 
 96     running = True
 97     switch_image = True
 98     delay = 100
 99     score = 0
100     score_font = pygame.font.Font("font/font.ttf", 36)
101     
102     bullets = []
103 
104     # 生成普通子弹
105     bullet1 = []
106     bullet1_index = 0
107     BULLET1_NUM = 4
108     for i in range(BULLET1_NUM):
109         bullet1.append(bullet.Bullet1(me.rect.midtop))
110 
111     # 生命数量
112     life_image = pygame.image.load("images/life.png").convert_alpha()
113     life_rect = life_image.get_rect()
114     life_num = 5
115 
116     # 游戏结束画面
117     gameover_font = pygame.font.Font("font/font.ttf", 48)
118     again_image = pygame.image.load("images/again.png").convert_alpha()
119     again_rect = again_image.get_rect()
120     gameover_image = pygame.image.load("images/gameover.png").convert_alpha()
121     gameover_rect = gameover_image.get_rect()
122 
123     DOUBLE_BULLET_TIME = USEREVENT + 1
124 
125     # 解除我方飞机无敌状态
126     INVINCEBLE_TIME = USEREVENT + 2
127 
128 
129     # 生成超级子弹
130     bullet2 = []
131     bullet2_index = 0
132     BULLET2_NUM = 8
133     for i in range(BULLET2_NUM//2):
134         bullet2.append(bullet.Bullet2((me.rect.centerx - 33, me.rect.centery)))
135         bullet2.append(bullet.Bullet2((me.rect.centerx + 30, me.rect.centery)))
136 
137     # 标记是否使用超级子弹
138     is_double_bullet = False
139 
140     level = 1
141 
142     # 全屏炸弹
143     bomb_image = pygame.image.load("images/bomb.png").convert_alpha()
144     bomb_rect = bomb_image.get_rect()
145     bomb_font = pygame.font.Font("font/font.ttf", 48)
146     bomb_num = 3
147 
148     # 每30秒发放一个补给包
149     bullet_supply = supply.Bullet_Supply(bg_size)
150     bomb_supply = supply.Bomb_Supply(bg_size)
151     SUPPLY_TIME = USEREVENT
152     pygame.time.set_timer(SUPPLY_TIME, 30 *1000)
153 
154     # 阻止重复读取成绩记录文件
155     recorded = False
156 
157     # 标志是否暂停游戏
158     paused = False
159     paused_nor_image = pygame.image.load("images/pause_nor.png").convert_alpha()
160     pause_pressed_image = pygame.image.load("images/pause_pressed.png").convert_alpha()
161     resume_nor_image = pygame.image.load("images/resume_nor.png").convert_alpha()
162     resume_pressed_image = pygame.image.load("images/resume_pressed.png").convert_alpha()
163     paused_rect = paused_nor_image.get_rect()
164     paused_rect.left, paused_rect.top = width - paused_rect.width -10, 10
165     paused_image = paused_nor_image
166 
167     while running:
168         for event in pygame.event.get():
169             if event.type == QUIT:
170                 pygame.quit()
171                 sys.exit()
172             elif event.type == MOUSEBUTTONDOWN:
173                 if event.button == 1 and paused_rect.collidepoint(event.pos):
174                     paused = not paused
175                     if paused:
176                         pygame.time.set_timer(SUPPLY_TIME, 0)
177                         pygame.mixer.music.pause()
178                         pygame.mixer.pause()
179                         paused_image = resume_pressed_image
180                     else:
181                         pygame.time.set_timer(SUPPLY_TIME, 30 * 1000)
182                         pygame.mixer.music.unpause()
183                         pygame.mixer.unpause()
184                         paused_image = pause_pressed_image
185             elif event.type == MOUSEMOTION:
186                 if paused_rect.collidepoint(event.pos):
187                     if paused:
188                         paused_image = resume_pressed_image
189                     else:
190                         paused_image = pause_pressed_image
191                 else:
192                     if paused:
193                         paused_image = resume_nor_image
194                     else:
195                         paused_image = paused_nor_image
196             elif event.type == KEYDOWN:
197                 if event.key == K_SPACE:
198                     if bomb_num:
199                         bomb_num -= 1
200                         bomb_sound.play()
201                         for each in enemies:
202                             if each.rect.bottom > 0:
203                                 each.active = False
204             elif event.type == SUPPLY_TIME:
205                 supply_sound.play()
206                 if random.choice([True, False]):
207                     bomb_supply.reset()
208                 else:
209                     bullet_supply.reset()
210             elif event.type == DOUBLE_BULLET_TIME:
211                 is_double_bullet = False
212                 pygame.time.set_timer(DOUBLE_BULLET_TIME, 0)
213             elif event.type == INVINCEBLE_TIME:
214                 me.invincible = False
215                 pygame.time.set_timer(INVINCEBLE_TIME, 0)
216 
217 
218         # 根据用户的得分增加难度
219         if level == 1 and score > 50000:
220             level = 2
221             upgrade_sound.play()
222             add_small_enemies(small_enemies, enemies, 3)
223             add_mid_enemies(mid_enemies, enemies, 2)
224             add_big_enemies(big_enemies, enemies, 1)
225             inc_speed(small_enemies, 1)
226         elif level == 2 and score > 300000:
227             level = 3
228             upgrade_sound.play()
229             add_small_enemies(small_enemies, enemies, 5)
230             add_mid_enemies(mid_enemies, enemies, 3)
231             add_big_enemies(big_enemies, enemies, 2)
232             inc_speed(small_enemies, 1)
233             inc_speed(mid_enemies, 1)
234         elif level == 3 and score > 300000:
235             level = 4
236             upgrade_sound.play()
237             add_small_enemies(small_enemies, enemies, 5)
238             add_mid_enemies(mid_enemies, enemies, 3)
239             add_big_enemies(big_enemies, enemies, 2)
240             inc_speed(small_enemies, 1)
241             inc_speed(mid_enemies, 1)
242         elif level == 4 and score > 1000000:
243             level = 5
244             upgrade_sound.play()
245             add_small_enemies(small_enemies, enemies, 5)
246             add_mid_enemies(mid_enemies, enemies, 3)
247             add_big_enemies(big_enemies, enemies, 2)
248             inc_speed(small_enemies, 1)
249             inc_speed(mid_enemies, 1)
250 
251         screen.blit(background, (0,0))
252 
253         if life_num and not paused:
254             # 绘制全屏炸弹补给并检测是否获得
255             if bomb_supply.active:
256                 bomb_supply.move()
257                 screen.blit(bomb_supply.image, bomb_supply.rect)
258                 if pygame.sprite.collide_mask(bomb_supply, me):
259                     get_bomb_sound.play()
260                     if bomb_num < 3:
261                         bomb_num += 1
262                     bomb_supply.active = False
263 
264             #发射子弹
265             if not(delay % 10):
266                 bullet_sound.play()
267                 if is_double_bullet:
268                     bullets = bullet2
269                     bullets[bullet2_index].reset((me.rect.centerx - 33, me.rect.centery))
270                     bullets[bullet2_index + 1].reset((me.rect.centerx + 33, me.rect.centery))
271                     bullet2_index = (bullet2_index + 2) % BULLET2_NUM
272                 else:
273                     bullets = bullet1
274                     bullets[bullet1_index].reset(me.rect.midtop)
275                     bullet1_index = (bullet1_index + 1) % BULLET1_NUM
276 
277             #检测子弹是否击中敌机
278             for b in bullets:
279                 if b.active:
280                     b.move()
281                     screen.blit(b.image, b.rect)
282                     enemy_hit = pygame.sprite.spritecollide(b, enemies, False, pygame.sprite.collide_mask)
283                     if enemy_hit:
284                         b.active = False
285                         for e in enemy_hit:
286                             if e in mid_enemies or e in big_enemies:
287                                 e.hit = True
288                                 e.energy -= 1
289                                 if e.energy == 0:
290                                     e.active = False
291                             else:
292                                 e.active = False
293             
294             # 绘制全屏炸弹补给并检测是否获得
295             if bullet_supply.active:
296                 bullet_supply.move()
297                 screen.blit(bullet_supply.image, bullet_supply.rect)
298                 if pygame.sprite.collide_mask(bullet_supply, me):
299                     get_bullet_sound.play()
300                     is_double_bullet = True
301                     pygame.time.set_timer(DOUBLE_BULLET_TIME, 18 * 1000)
302                     bullet_supply.active = False
303 
304             # 绘制大型敌机
305             for each in big_enemies:
306                 if each.active:
307                     each.move()
308                     if each.hit:
309                         screen.blit(each.image_hit, each.rect)
310                         each.hit = False
311                     else:
312                         if switch_image:
313                             screen.blit(each.image1, each.rect)
314                         else:
315                             screen.blit(each.image2, each.rect)
316                     if each.rect.bottom == -50:
317                         enemy3_fly_sound.play(-1)
318 
319                     # 绘制血槽
320                     pygame.draw.line(screen, BLACK, \
321                             (each.rect.left, each.rect.top - 5),\
322                             (each.rect.right, each.rect.top - 5), \
323                             2)
324                     
325                     energy_remain = each.energy / enemy.BigEnemy.energy
326                     if energy_remain > 0.2:
327                         energy_color = GREEN
328                     else:
329                         energy_color = RED
330                     pygame.draw.line(screen, energy_color, \
331                             (each.rect.left, each.rect.top -5), \
332                             (each.rect.left + each.rect.width * energy_remain, \
333                             each.rect.top - 5), \
334                             2)
335 
336                 else:
337                     if not(delay % 3):
338                         if e3_destroy_index == 0:
339                             enemy3_down_sound.play()
340                         screen.blit(each.destroy_images[e3_destroy_index], each.rect)
341                         e3_destroy_index = (e3_destroy_index+1)%6
342                         if e3_destroy_index == 0:
343                             me_down_sound.stop()
344                             score += 10000
345                             each.reset()
346 
347             # 绘制中型敌机
348             for each in mid_enemies:
349                 if each.active:
350                     each.move()
351 
352                     if each.hit:
353                         screen.blit(each.image_hit, each.rect)
354                         each.hit = False
355                     else:
356                         screen.blit(each.image1, each.rect)
357 
358                     # 绘制血槽
359                     pygame.draw.line(screen, BLACK, \
360                             (each.rect.left, each.rect.top - 5),\
361                             (each.rect.right, each.rect.top - 5), \
362                             2)
363                     
364                     energy_remain = each.energy / enemy.MidEnemy.energy
365                     if energy_remain > 0.2:
366                         energy_color = GREEN
367                     else:
368                         energy_color = RED
369                     pygame.draw.line(screen, energy_color, \
370                             (each.rect.left, each.rect.top -5), \
371                             (each.rect.left + each.rect.width * energy_remain, \
372                             each.rect.top - 5), \
373                             2)
374                 else:
375                     if not(delay % 3):
376                         if e2_destroy_index ==0:
377                             enemy2_down_sound.play()
378                         screen.blit(each.destroy_images[e2_destroy_index], each.rect)
379                         e2_destroy_index = (e2_destroy_index+1)%4
380                         if e2_destroy_index == 0:
381                             score += 5000
382                             each.reset()
383 
384             # 绘制小型敌机
385             for each in small_enemies:
386                 if each.active:
387                     each.move()
388                     screen.blit(each.image1, each.rect)
389                 else:
390                     if not(delay % 3):
391                         if e1_destroy_index ==0:
392                             enemy1_down_sound.play()
393                         screen.blit(each.destroy_images[e1_destroy_index], each.rect)
394                         e1_destroy_index = (e1_destroy_index+1)%4
395                         if e1_destroy_index == 0:
396                             score += 1000
397                             each.reset()    
398 
399             key_pressed = pygame.key.get_pressed()
400             if key_pressed[K_w] or key_pressed[K_UP]:
401                 me.moveUp()
402             if key_pressed[K_s] or key_pressed[K_DOWN]:
403                 me.moveDown()
404             if key_pressed[K_a] or key_pressed[K_LEFT]:
405                 me.moveLeft()
406             if key_pressed[K_d] or key_pressed[K_RIGHT]:
407                 me.moveRight()
408 
409             # 检测我方飞机是否被撞
410             enemies_down = pygame.sprite.spritecollide(me, enemies, False, pygame.sprite.collide_mask)
411             if enemies_down and not me.invincible:
412                 me.active = False
413                 for e in enemies_down:
414                     e.active = False
415 
416             # 绘制我方飞机
417             if me.active:
418                 if switch_image:
419                     screen.blit(me.image1, me.rect)
420                 else:
421                     screen.blit(me.image2, me.rect)
422             else:
423                 me_down_sound.play()
424                 if not(delay % 3):
425                     screen.blit(each.destroy_images[me_destroy_index], each.rect)
426                     me_destroy_index = (me_destroy_index+1)%4
427                     # 剩余生命数量
428                     if me_destroy_index == 0:
429                         life_num -= 1
430                         me.reset()
431                         pygame.time.set_timer(INVINCEBLE_TIME, 3 * 1000)
432 
433              # 绘制剩余炸弹数量
434             bomb_text = bomb_font.render("x %d" % bomb_num, True, WHITE)
435             text_rect = bomb_text.get_rect()
436             screen.blit(bomb_image, (10, height-10 - bomb_rect.height))
437             screen.blit(bomb_text, (20 + bomb_rect.width, height - 5 - text_rect.height))
438 
439 
440             if life_num:
441                 for i in range(life_num):
442                     screen.blit(life_image,\
443                         (width - 10 - (i+1)*life_rect.width, \
444                         height - 10 - life_rect.height))
445             
446             score_text = score_font.render(str("Score: %s" % score), True, WHITE)
447             screen.blit(score_text, (10,5))
448         elif life_num == 0:
449             pygame.mixer.music.stop()
450             pygame.mixer.stop()
451 
452             # 停止发放补给
453             pygame.time.set_timer(SUPPLY_TIME, 0)
454 
455             if not recorded:
456                 recorded = True
457                 # 读取历史最高分
458                 with open("record.txt", "r") as f:
459                     record_score = int(f.read())
460             
461                 if score > record_score:
462                     with open("record.txt", "w") as f:
463                         f.write(str(score))
464 
465             # 绘制结束画面
466             record_score_text = score_font.render("Best: %d" % record_score,True,WHITE)
467             screen.blit(record_score_text, (50,50))
468             
469             gameover_text1 = gameover_font.render("Your Score: ", True, WHITE)
470             gameover_text1_rect = gameover_text1.get_rect()
471             gameover_text1_rect.left, gameover_text1_rect.top = \
472                                 (width - gameover_text1_rect.width) // 2, height // 2
473             screen.blit(gameover_text1, gameover_text1_rect)
474 
475             
476             gameover_text2 = gameover_font.render(str(score), True, WHITE)
477             gameover_text2_rect = gameover_text2.get_rect()
478             gameover_text2_rect.left, gameover_text2_rect.top = \
479                                 (width - gameover_text2_rect.width) // 2, \
480                                 gameover_text1_rect.bottom + 10
481             screen.blit(gameover_text2, gameover_text2_rect)
482 
483             again_rect.left, again_rect.top = \
484                         (width - again_rect.width) // 2,\
485                         gameover_text2_rect.bottom + 50
486             screen.blit(again_image, again_rect)
487 
488             gameover_rect.left, gameover_rect.top = \
489                         (width - again_rect.width) // 2, \
490                         again_rect.bottom + 10
491             screen.blit(gameover_image, gameover_rect)
492 
493             # 检测用户的鼠标操作
494             # 如果用户按下鼠标左键
495             if pygame.mouse.get_pressed()[0]:
496                 pos = pygame.mouse.get_pos()
497                 if again_rect.left < pos[0] < again_rect.right and \
498                    again_rect.top < pos[1] < again_rect.bottom:
499                     main()
500                 elif gameover_rect.left < pos[0] < gameover_rect.right and \
501                      gameover_rect.top < pos[1] < gameover_rect.bottom:
502                      pygame.quit()
503                      sys.exit()
504 
505         screen.blit(paused_image, paused_rect)
506 
507         # 切换图片
508         if not(delay % 5):
509             switch_image = not switch_image
510 
511         delay -= 1
512         if not delay:
513             delay = 100
514 
515         pygame.display.flip()
516         clock.tick(60)
517 
518 if __name__ == "__main__":
519     try:
520         main()
521     except SystemExit:
522         pass
523     except:
524         traceback.print_exc()
525         pygame.quit()
526         input()
527     

二、自己操控的飞机代码

 1 import pygame
 2 
 3 class MyPlane(pygame.sprite.Sprite):
 4     def __init__(self, bg_size):
 5         pygame.sprite.Sprite.__init__(self)
 6 
 7         self.image1 = pygame.image.load("images/me1.png").convert_alpha()
 8         self.image2 = pygame.image.load("images/me2.png").convert_alpha()
 9         self.destroy_image = []
10         self.destroy_image.extend([\
11             pygame.image.load("images/me_destroy_1.png").convert_alpha(),\
12             pygame.image.load("images/me_destroy_2.png").convert_alpha(),\
13             pygame.image.load("images/me_destroy_3.png").convert_alpha(),\
14             pygame.image.load("images/me_destroy_4.png").convert_alpha(),\
15         ])
16         self.active = True
17         self.rect = self.image1.get_rect()
18         self.width, self.height = bg_size[0], bg_size[1]
19         self.rect.left, self.rect.top = \
20                         (self.width - self.rect.width) // 2, \
21                         self.height - self.rect.height - 60
22         self.speed = 10
23         self.mask = pygame.mask.from_surface(self.image1)
24         self.invincible = False
25 
26     def moveUp(self):
27         if self.rect.top > 0:
28             self.rect.top -= self.speed
29         else:
30             self.rect.top = 0
31 
32     def moveDown(self):
33         if self.rect.bottom < self.height - 60:
34             self.rect.top += self.speed
35         else:
36             self.rect.bottom = self.height - 60
37     def moveLeft(self):
38         if self.rect.left > 0:
39             self.rect.left -= self.speed
40         else:
41             self.rect.left = 0
42 
43     def moveRight(self):
44         if self.rect.right < self.width:
45             self.rect.left += self.speed
46         else:
47             self.rect.right = self.width
48 
49     def  reset(self):
50         self.rect.left, self.rect.top = \
51                         (self.width - self.rect.width) // 2, \
52                         self.height - self.rect.height - 60
53         self.active = True
54         self.invincible = True
55 
56 
57         

三、敌机部分代码

  1 import pygame
  2 from random import *
  3 
  4 class SmallEnemy(pygame.sprite.Sprite):
  5     def __init__(self, bg_size):
  6         pygame.sprite.Sprite.__init__(self)
  7 
  8         self.image1 = pygame.image.load("images/enemy1.png").convert_alpha()
  9         self.destroy_images = []
 10         self.destroy_images.extend([\
 11             pygame.image.load("images/enemy1_down1.png").convert_alpha(),\
 12             pygame.image.load("images/enemy1_down2.png").convert_alpha(),\
 13             pygame.image.load("images/enemy1_down3.png").convert_alpha(),\
 14             pygame.image.load("images/enemy1_down4.png").convert_alpha(),\
 15         ])
 16         self.rect = self.image1.get_rect()
 17         self.width, self.height = bg_size[0],bg_size[1]
 18         self.speed = 2
 19         self.active = True
 20         self.rect.left, self.rect.top = \
 21                         randint(0, self.width - self.rect.width),\
 22                         randint(-5*self.height,0)
 23         self.mask = pygame.mask.from_surface(self.image1)
 24     
 25     def move(self):
 26         if self.rect.top < self.height:
 27             self.rect.top += self.speed
 28         else:
 29             self.reset()
 30         
 31     def reset(self):
 32         self.active = True
 33         self.rect.left, self.rect.top = \
 34                         randint(0, self.width - self.rect.width),\
 35                         randint(-5*self.height, 0)
 36 
 37 
 38 class MidEnemy(pygame.sprite.Sprite):
 39     energy = 18
 40 
 41     def __init__(self, bg_size):
 42         pygame.sprite.Sprite.__init__(self)
 43 
 44         self.image1 = pygame.image.load("images/enemy2.png").convert_alpha()
 45         self.image_hit = pygame.image.load("images/enemy2_hit.png").convert_alpha()
 46         self.destroy_images = []
 47         self.destroy_images.extend([\
 48             pygame.image.load("images/enemy2_down1.png").convert_alpha(),\
 49             pygame.image.load("images/enemy2_down2.png").convert_alpha(),\
 50             pygame.image.load("images/enemy2_down3.png").convert_alpha(),\
 51             pygame.image.load("images/enemy2_down4.png").convert_alpha(),\
 52         ])
 53         self.rect = self.image1.get_rect()
 54         self.width, self.height = bg_size[0],bg_size[1]
 55         self.speed = 1
 56         self.active = True
 57         self.hit = False
 58         self.energy = MidEnemy.energy
 59         self.rect.left, self.rect.top = \
 60                         randint(0, self.width - self.rect.width),\
 61                         randint(-5*self.height,-self.height)
 62         self.mask = pygame.mask.from_surface(self.image1)
 63     
 64     def move(self):
 65         if self.rect.top < self.height:
 66             self.rect.top += self.speed
 67         else:
 68             self.reset()
 69         
 70     def reset(self):
 71         self.active = True
 72         self.energy = MidEnemy.energy
 73         self.rect.left, self.rect.top = \
 74                         randint(0, self.width - self.rect.width),\
 75                         randint(-8*self.height,-self.height)
 76 
 77 
 78 class BigEnemy(pygame.sprite.Sprite):
 79     energy = 20
 80 
 81     def __init__(self, bg_size):
 82         pygame.sprite.Sprite.__init__(self)
 83 
 84         self.image1 = pygame.image.load("images/enemy3_n1.png").convert_alpha()
 85         self.image2 = pygame.image.load("images/enemy3_n2.png").convert_alpha()
 86         self.image_hit = pygame.image.load("images/enemy3_hit.png").convert_alpha()
 87         self.destroy_images = []
 88         self.destroy_images.extend([\
 89             pygame.image.load("images/enemy3_down1.png").convert_alpha(),\
 90             pygame.image.load("images/enemy3_down2.png").convert_alpha(),\
 91             pygame.image.load("images/enemy3_down3.png").convert_alpha(),\
 92             pygame.image.load("images/enemy3_down4.png").convert_alpha(),\
 93             pygame.image.load("images/enemy3_down5.png").convert_alpha(),\
 94             pygame.image.load("images/enemy3_down6.png").convert_alpha(),\
 95         ])
 96         self.rect = self.image1.get_rect()
 97         self.width, self.height = bg_size[0],bg_size[1]
 98         self.speed = 1
 99         self.active = True
100         self.hit = False
101         self.energy = BigEnemy.energy
102         self.rect.left, self.rect.top = \
103                         randint(0, self.width - self.rect.width),\
104                         randint(-10*self.height,-5*self.height)
105         self.mask = pygame.mask.from_surface(self.image1)
106     
107     def move(self):
108         if self.rect.top < self.height:
109             self.rect.top += self.speed
110         else:
111             self.reset()
112         
113     def reset(self):
114         self.energy = BigEnemy.energy
115         self.active = True
116         self.rect.left, self.rect.top = \
117                         randint(0, self.width - self.rect.width),\
118                         randint(-10*self.height,-5*self.height)

四、子弹部分代码

 1 import pygame
 2 
 3 class Bullet1(pygame.sprite.Sprite):
 4     def __init__(self, positon):
 5         pygame.sprite.Sprite.__init__(self)
 6 
 7         self.image = pygame.image.load("images/bullet1.png").convert_alpha()
 8         self.rect = self.image.get_rect()
 9         self.rect.left, self.rect.top = positon
10         self.speed = 12
11         self.active = True
12         self.mask = pygame.mask.from_surface(self.image)
13 
14     def move(self):
15         self.rect.top -= self.speed
16 
17         if self.rect.top < 0:
18             self.active = False
19     
20     def reset(self, position):
21         self.rect.left, self.rect.top = position
22         self.active = True
23 
24 class Bullet2(pygame.sprite.Sprite):
25     def __init__(self, positon):
26         pygame.sprite.Sprite.__init__(self)
27 
28         self.image = pygame.image.load("images/bullet2.png").convert_alpha()
29         self.rect = self.image.get_rect()
30         self.rect.left, self.rect.top = positon
31         self.speed = 50
32         self.active = True
33         self.mask = pygame.mask.from_surface(self.image)
34 
35     def move(self):
36         self.rect.top -= self.speed
37 
38         if self.rect.top < 0:
39             self.active = False
40     
41     def reset(self, position):
42         self.rect.left, self.rect.top = position
43         self.active = True

 

posted @ 2019-09-13 10:52  Aiden_Shao  阅读(256)  评论(0编辑  收藏  举报