|
9 | 9 | from sys import exit
|
10 | 10 | from pygame.locals import *
|
11 | 11 | from gameRole import *
|
| 12 | +import random |
| 13 | + |
12 | 14 |
|
13 | 15 | # 初始化游戏
|
14 | 16 | pygame.init()
|
|
18 | 20 | # 载入背景图
|
19 | 21 | background = pygame.image.load('resources/image/background.png').convert()
|
20 | 22 |
|
| 23 | +filename = 'resources/image/shoot.png' |
| 24 | +plane_img = pygame.image.load(filename) |
| 25 | + |
21 | 26 | # 设置玩家相关参数
|
22 | 27 | player_rect = []
|
23 | 28 | player_rect.append(pygame.Rect(0, 99, 102, 126))
|
24 | 29 | player_rect.append(pygame.Rect(165, 360, 102, 126))
|
25 | 30 | player_pos = [200, 600]
|
26 |
| -filename = 'resources/image/shoot.png' |
| 31 | +player = Player(plane_img, player_rect, player_pos) |
| 32 | + |
| 33 | +# 定义子弹对象使用的surface相关参数 |
| 34 | +bullet_rect = pygame.Rect(1004, 987, 9, 21) |
| 35 | +bullet_img = plane_img.subsurface(bullet_rect) |
| 36 | + |
| 37 | +# 定义敌机对象使用的surface相关参数 |
| 38 | +enemy1_rect = pygame.Rect(534, 612, 57, 43) |
| 39 | +enemy1_img = plane_img.subsurface(enemy1_rect) |
| 40 | +enemy1_down_imgs = [] |
| 41 | +enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(267, 347, 57, 43))) |
| 42 | +enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(873, 697, 57, 43))) |
| 43 | +enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(267, 296, 57, 43))) |
| 44 | +enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(930, 697, 57, 43))) |
27 | 45 |
|
28 |
| -planeImg = pygame.image.load(filename) |
29 |
| -player = Player(planeImg, player_rect, player_pos) |
| 46 | +enemies1 = pygame.sprite.Group() |
30 | 47 |
|
31 |
| -player_img_index = 0 |
| 48 | +# 存储被击毁的飞机,用来渲染击毁精灵动画 |
| 49 | +enemies_down = pygame.sprite.Group() |
| 50 | + |
| 51 | +shoot_frequency = 0 |
| 52 | +enemy_frequency = 0 |
| 53 | + |
| 54 | +clock = pygame.time.Clock() |
32 | 55 |
|
33 | 56 | while 1:
|
| 57 | + # 控制游戏最大帧率为60 |
| 58 | + clock.tick(60) |
| 59 | + |
| 60 | + # 控制发射子弹频率,并发射子弹 |
| 61 | + if shoot_frequency % 15 == 0: |
| 62 | + # 添加声音 |
| 63 | + player.shoot(bullet_img) |
| 64 | + shoot_frequency += 1 |
| 65 | + if shoot_frequency >= 15: |
| 66 | + shoot_frequency = 0 |
| 67 | + |
| 68 | + # 生成敌机 |
| 69 | + if enemy_frequency % 50 == 0: |
| 70 | + enemy1_pos = [random.randint(0, SCREEN_WIDTH - enemy1_rect.width), 0] |
| 71 | + enemy1 = Enemy(enemy1_img, enemy1_down_imgs, enemy1_pos) |
| 72 | + enemies1.add(enemy1) |
| 73 | + enemy_frequency += 1 |
| 74 | + if enemy_frequency >= 100: |
| 75 | + enemy_frequency = 0 |
| 76 | + |
| 77 | + |
| 78 | + # 移动子弹,若超出窗口范围则删除 |
| 79 | + for bullet in player.bullets: |
| 80 | + bullet.move() |
| 81 | + if bullet.rect.bottom < 0: |
| 82 | + player.bullets.remove(bullet) |
| 83 | + |
| 84 | + # 移动敌机,若超出窗口范围则删除 |
| 85 | + for enemy in enemies1: |
| 86 | + enemy.move() |
| 87 | + if enemy.rect.top < 0: |
| 88 | + enemies1.remove(enemy) |
| 89 | + |
| 90 | + # 将被击中的敌机对象添加到击毁敌机Group中,用来渲染击毁动画 |
| 91 | + enemies1_down = pygame.sprite.groupcollide(enemies1, player.bullets, 1, 1) |
| 92 | + for enemy_down in enemies1_down: |
| 93 | + enemies_down.add(enemy_down) |
| 94 | + |
| 95 | + # 绘制背景 |
34 | 96 | screen.fill(0)
|
35 |
| - screen.blit(background, (0,0)) |
36 |
| - screen.blit(player.image[player_img_index], player.rect) |
37 |
| - if player_img_index == 0: |
38 |
| - player_img_index = 1 |
39 |
| - else: |
40 |
| - player_img_index = 0 |
41 |
| - pygame.display.flip() |
| 97 | + screen.blit(background, (0, 0)) |
| 98 | + |
| 99 | + # 绘制玩家飞机 |
| 100 | + screen.blit(player.image[player.img_index], player.rect) |
| 101 | + |
| 102 | + # 更换图片索引使飞机有动画效果 |
| 103 | + player.img_index = shoot_frequency / 8 |
| 104 | + |
| 105 | + # 绘制击毁动画 |
| 106 | + for enemy_down in enemies_down: |
| 107 | + if enemy_down.down_index > 7: |
| 108 | + enemies_down.remove(enemy_down) |
| 109 | + break |
| 110 | + screen.blit(enemy_down.down_imgs[enemy_down.down_index / 2], enemy_down.rect) |
| 111 | + enemy_down.down_index += 1 |
| 112 | + |
| 113 | + # 绘制子弹和敌机 |
| 114 | + player.bullets.draw(screen) |
| 115 | + enemies1.draw(screen) |
| 116 | + |
| 117 | + # 更新屏幕 |
| 118 | + pygame.display.update() |
| 119 | + |
42 | 120 | for event in pygame.event.get():
|
43 | 121 | if event.type == pygame.QUIT:
|
44 | 122 | pygame.quit()
|
45 | 123 | exit()
|
46 | 124 |
|
47 |
| - |
| 125 | + # 监听键盘事件 |
48 | 126 | key_pressed = pygame.key.get_pressed()
|
49 |
| - if key_pressed[K_w]: |
50 |
| - player.moveUp() |
51 |
| - if key_pressed[K_s]: |
52 |
| - player.moveDown() |
53 |
| - if key_pressed[K_a]: |
54 |
| - player.moveLeft() |
55 |
| - if key_pressed[K_d]: |
56 |
| - player.moveRight() |
57 |
| - |
| 127 | + # 若玩家被击中,则无效 |
| 128 | + if not player.is_hit: |
| 129 | + if key_pressed[K_w] or key_pressed[K_UP]: |
| 130 | + player.moveUp() |
| 131 | + if key_pressed[K_s] or key_pressed[K_DOWN]: |
| 132 | + player.moveDown() |
| 133 | + if key_pressed[K_a] or key_pressed[K_LEFT]: |
| 134 | + player.moveLeft() |
| 135 | + if key_pressed[K_d] or key_pressed[K_RIGHT]: |
| 136 | + player.moveRight() |
0 commit comments