当前位置:   article > 正文

python使用pygame编写游戏--飞机大战全部代码和图片_pygame素材图

pygame素材图

飞机大战

1.全部代码:

import random
import time
import pygame

pygame.init()
screen = pygame.display.set_mode((480, 550))
pygame.display.set_caption("飞机大战")
icon = pygame.image.load("img/icon.png")
pygame.display.set_icon(icon)

bg_img = pygame.image.load("img/background.png")

hero_img1 = pygame.image.load("img/me1.png")
hero_img2 = pygame.image.load("img/me2.png")

hero_bomb_imgs = ["img/me_destroy_1.png",
                  "img/me_destroy_2.png",
                  "img/me_destroy_3.png",
                  "img/me_destroy_4.png"]


hero_rect = pygame.Rect(190, 526, 66, 80)

# 英雄精灵的x,y轴坐标
heroPlaneX = hero_rect.x
heroPlaneY = hero_rect.y
enemy_img = pygame.image.load('img/enemy2.png')
enemy_img2 = pygame.image.load('img/enemy2_hit.png')
#
enemy_bomb_imgs = ["img/enemy2_down1.png",
                   "img/enemy2_down2.png",
                   "img/enemy2_down3.png",
                   "img/enemy2_down4.png"]

# 定义敌机的rect
enemy_rect = pygame.Rect(206, 0, 69, 89)  # x=480//2-69//2
# 敌机精灵的x,y轴坐标
enemyPlaneX = enemy_rect.x
enemyPlaneY = enemy_rect.y
#绘制敌机精灵
screen.blit(enemy_img,(enemyPlaneX,enemyPlaneY))

pygame.key.set_repeat(20, 30)

# 创建游戏时钟,
clock = pygame.time.Clock()

# 作为切换图片索引
heroIndexShift = 0

# 定义敌机初始移动方向
direct = "left"


class HeroBullet:
    def __init__(self, x, y, screen):
        self.x = x
        self.y = y
        self.screen = screen
        self.pic = pygame.image.load("img/bullet1.png")

    # 画子弹
    def draw(self):
        self.screen.blit(self.pic, (self.x, self.y))
        self.move()

    # 子弹移动方法
    def move(self):
        self.y -= 5


# 英雄机子弹列表
HeroBiulist = []
# 创建敌机子弹
EnemyBiulist = []


# 绘制敌机子弹
class EnemyBullet:
    def __init__(self, x, y, screen):
        self.x = x
        self.y = y
        self.screen = screen
        self.pic = pygame.image.load("img/bullet2.png")

    # 画子弹
    def draw(self):
        self.screen.blit(self.pic, (self.x, self.y))
        self.move()

    def move(self):
        self.y += 5

# 英雄机爆炸
hero_bomb = False
# 英雄机爆炸索引
hero_bomb_index = 0
# 敌机爆炸条件
enemy_bomb = False
# 敌机爆炸索引
enemy_bomb_index = 0
while True:

    clock.tick(60)

    screen.blit(bg_img, (0, 0))

    # 将英雄的飞机绘制到窗口上
    if heroIndexShift == 0:
        screen.blit(hero_img1, (heroPlaneX, heroPlaneY))
        heroIndexShift += 1
    else:
        screen.blit(hero_img2, (heroPlaneX, heroPlaneY))
        heroIndexShift = 0

    # 修改英雄飞机的y轴值
    heroPlaneY -= 1
    # 让飞机从底部飞出
    if heroPlaneY <= 0:
        heroPlaneY = 650

        # 控制精灵机移动
    if direct == "left":
        enemyPlaneX -= 5
        if enemyPlaneX <= 0:
            direct = "right"
    elif direct == "right":
        enemyPlaneX += 5
        if enemyPlaneX >= 480 - 69:
            direct = "left"

    for bullet in HeroBiulist:
        bullet.draw()  # 绘制子弹
        # 让子弹到最上边的时候消失
        HeroBiulist.remove(bullet) if bullet.y < 0 else ' '
        # 定义英雄子弹rect
        hero_bullet_rect = pygame.rect.Rect(bullet.x, bullet.y, 5, 11)
        # 碰撞检测
        flag = hero_bullet_rect.colliderect(enemy_rect)
        if flag:
            print("敌机爆炸")
            enemy_bomb = True
            HeroBiulist.remove(bullet)
    if enemy_bomb == False:
        # 切换图片,绘制敌机未爆炸图片
        if enemy_bomb_index == 0:
            screen.blit(enemy_img, (enemyPlaneX, enemyPlaneY))
            enemy_bomb_index += 1
        else:
            screen.blit(enemy_img2, (enemyPlaneX, enemyPlaneY))
            enemy_bomb_index = 0
    else:
        if enemy_bomb_index == len(enemy_bomb_imgs):  # 当敌机爆炸图片索引和爆炸图片总数相同时,爆炸图片已经加载结束,退出游戏
            # 当敌机爆炸索引与图片总数相同时,退出游戏
            time.sleep(0.2)
            exit(0)

        enemy_bomb_img = pygame.image.load(enemy_bomb_imgs[enemy_bomb_index])
        screen.blit(enemy_bomb_img, (enemyPlaneX, enemyPlaneY))  # 绘制爆炸图片
        enemy_bomb_index += 1
        time.sleep(0.3)
        #画出敌机子弹
    x = random.randint(1, 100)
    if x == 5 or x == 78:
        # 实例化一个子弹
        enemybullet = EnemyBullet(enemyPlaneX + 165 / 2 - 14 / 2, enemyPlaneY + 269, screen)
        # 产生的每个子弹放到一个列表里
        EnemyBiulist.append(enemybullet)

    for bullet in EnemyBiulist:
        bullet.draw()#绘制子弹
        #让子弹到最下面的时候消失
        EnemyBiulist.remove(bullet) if bullet.y > 650 - 150 else ''

        enemy_bullet_rect = pygame.rect.Rect(bullet.x, bullet.y, 14, 143)
        flag2 = enemy_bullet_rect.colliderect(hero_rect)
        if flag2:
            print("英雄机爆炸")
            hero_bomb = True
            EnemyBiulist.remove(bullet)
    if hero_bomb == False:
        if heroIndexShift == 0:
            screen.blit(hero_img1, (heroPlaneX, heroPlaneY))
            heroIndexShift += 1
        else:
            screen.blit(hero_img2, (heroPlaneX, heroPlaneY))
            heroIndexShift = 0
    else:
        if hero_bomb_index == len(hero_bomb_imgs):
                        # 当战机爆炸索引与图片总数相同时,退出游戏
            time.sleep(0.3)
            exit(0)
        hero_bomb_img = pygame.image.load(hero_bomb_imgs[hero_bomb_index])
        screen.blit(hero_bomb_img, (heroPlaneX, heroPlaneY))
        hero_bomb_index += 1
        time.sleep(0.2)
    event_list = pygame.event.get()
    # 2.捕获窗口退出事件
    for event in event_list:
        if event.type == pygame.QUIT:  # 加上这个模块就不卡了
            print("游戏结束了......")
            pygame.quit()  # 卸载模块
            exit(0)  # 终止Python程序,exit(0)表示正常退出,exit(1)表示异常退出
            # 控制英雄精灵移动
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:  # 向左移动
                heroPlaneX = heroPlaneX - 5 if heroPlaneX >= 5 else 0

            elif event.key == pygame.K_RIGHT:  # 向右移动
                heroPlaneX = heroPlaneX + 5 if heroPlaneX <= 375 else 380

            elif event.key == pygame.K_UP:  # 向上移动
                heroPlaneY = heroPlaneY - 5 if heroPlaneY >= 5 else 0

            elif event.key == pygame.K_DOWN:  # 向下移动
                heroPlaneY = heroPlaneY + 5 if heroPlaneY <= 521 else 526

            elif event.key == pygame.K_SPACE:  # 英雄机控制发射子弹
                # 实例化子弹对象
                hero_bullet = HeroBullet(heroPlaneX + 34 - 2, heroPlaneY - 11, screen)
                # 把子弹添加到列表里
                HeroBiulist.append(hero_bullet)
    pygame.display.update()#游戏更新
  • 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
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223

2.使用图片:

注:英雄机图片
在这里插入图片描述
注:英雄机图片
在这里插入图片描述
注:游戏图标图片
在这里插入图片描述
注:游戏背景图片
在这里插入图片描述
注:子弹图片
在这里插入图片描述
注:子弹图片
在这里插入图片描述
注:敌机图片
在这里插入图片描述
注:敌机图片
在这里插入图片描述
注:敌机图片
在这里插入图片描述
注:敌机图片
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/764856
推荐阅读
相关标签
  

闽ICP备14008679号