python使用pygame编写游戏--飞机大战全部代码和图片
mkijhwert 2024-07-05 14:05:02 阅读 58
飞机大战
1.全部代码:
<code>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()#游戏更新
2.使用图片:
注:英雄机图片
注:英雄机图片
注:游戏图标图片
注:游戏背景图片
注:子弹图片
注:子弹图片
注:敌机图片
注:敌机图片
注:敌机图片
注:敌机图片
声明
本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。