1.安装pygame模块
1.1打开此终端
1.2输入下面这行命令
pip install pygame
1.3安装完成
2.设置窗口以及背景
2.1导入所需的库
import random
import pygame
import time
2.2将窗口可视化
pygame.init() #(创建、显示和管理窗口)
2.3创建游戏窗口和循环
#480是窗口的宽,650是窗口的高
screen = pygame.display.set_mode((480,650)) #设置其大小
#游戏循环
while True:
......
2.4给窗口设置一个标签
pygame.display.set_caption("飞机大战")
2.5给窗口标签导入图片
icon = pygame.image.load("img/icon.png")
pygame.display.set_icon(icon)
2.6加载游戏背景图片
screen.blit(yy, (0, 0)) # 坐标在原点
3.绘制英雄机
3.1绘制英雄机精灵,两张图片为了实现飞机的喷气是动态的
hero_img1 = pygame.image.load("img/me1.png")
hero_img2 = pygame.image.load("img/me2.png")
3.2设置作为切换的索引值
index_s = 0
3.3定义英雄机
#括号里第一个参数是距y轴的边距大小,第二个参数是距x轴的距离;第三个参数是图片的宽度,第四个参数是图片的高度 单位:px
h_rect = pygame.rect.Rect(190,525,100,124)
#用.....if...else....绘制英雄精灵图片
# 绘制英雄机,切换英雄机
if index_s == 0:
screen.blit(hero_img1, (h_planex, h_planey))
index_s = 1
else:
screen.blit(hero_img2, (h_planex, h_planey))
index_s = 0
3.4定义英雄机精灵的xy轴
h_planex = h_rect.x
h_planey = h_rect.y
实现英雄机上下自动移动
#飞机可以从底部飞出
h_planey -=1
#如果y轴距离<0,飞机返回底部重新向上飞
if h_planey<0:
h_planey=650
3.5设置灵敏度
pygame.key.set_repeat(20,30)
3.6设置时钟
clock = pygame.time.Clock()
#每秒钟刷新60次
clock.tick(60)
4.监听事件、控制英雄机移动
4.1对所有事件进行监听
event_list = pygame.event.get()
4.2捕获窗口退出事件
for event in event_list:
if event.type == pygame.QUIT:
print("游戏结束了.......")
pygame.quit()
exit(0) #括号里是0 正常退出
4.3对英雄飞机左右上下经行移动的事件监听
#(KEYDOWN)按下按钮就执行下面的内容
if event.type == pygame.KEYDOWN:
#如果距左侧y轴的距离大于等于5,向左飞,否则为0
if event.key==pygame.K_LEFT:
h_planex = h_planex-5 if h_planex>=5 else 0
#如果距左侧y轴的距离小于等于375,向右飞,否则就返回左侧向右移动
elif event.key==pygame.K_RIGHT:
h_planex=h_planex+5 if h_planex<=375 else 380
#如果距上面x轴的距离大于等于5,向上飞,否则为0
elif event.key==pygame.K_UP:
h_planey=h_planey-5 if h_planex>=5 else 0
#如果距上面x轴的距离小于等于521,向下飞,否则返回顶部向下移动
elif event.key==pygame.K_DOWN:
h_planey=h_planey+5 if h_planey<=521 else 526
5.绘制敌机
5.1加载敌机图片
enemy_img = pygame.image.load('img/enemy2.png')
enemy_img2=pygame.image.load("img/enemy2_hit.png")
5.2定义敌机位置
#括号里第一个参数是距y轴的边距大小,第二个参数是距x轴的距离;第三个参数是图片的宽度,第四个参数是图片的高度 单位:px
enemy_rect = pygame.rect.Rect(158,0,69,99)
5.3敌机x,y轴坐标
enemyplane_x = enemy_rect.x
enemyplane_y = enemy_rect.y
5.4绘制敌机精灵
# 绘制敌机
screen.blit(enemy_img, (enemyplane_x, enemyplane_y))
5.5给敌机设置一个初始方向
direct='左'
5.6控制敌机移动
if direct =='左':
enemyplane_x-=5 #向左移动一次是5px
if enemyplane_x<=0: #距左侧距离为0,就方向变为“右侧”
direct='右'
elif direct=='右': #变为右侧,执行这里
enemyplane_x +=5
if enemyplane_x >=480-80:
direct='左'
6.定义子弹类
6.1定义英雄机子弹
class zd:
def __init__(self,x,y,screen):
self.x = x #x坐标
self.y = y #y坐标
self.screen = screen #窗口
self.pic = pygame.image.load("img/bullet1.png")
6.2画出子弹,定义一个函数,起名叫draw
def draw(self):
self.screen.blit(self.pic,(self.x,self.y))
self.move()
6.3子弹移动方法
def move(self):
self.y -=5 # -=5代表每一次向上移动5px
6.4定义英雄机子弹列表
hero_Bulletlist=[]
6.5定义敌机子弹
class djzd:
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 # +=5代表每一次向下移动5px
6.6定义敌机子弹列表
enemy_Bulletlist=[]
6.7画出英雄机的子弹
#使用空格键来控制子弹发射
elif event.key==pygame.K_SPACE:
# 调用子弹的方法和属性
zdd = zd(h_planex+50-2,h_planey-11,screen)
# 把子弹放入列表中,子弹是多个,我们将多个实例化的子弹放入列表中
hero_Bulletlist.append(zdd)
# 把子弹画出来
for i in hero_Bulletlist:
i.draw()
# 让子弹到最上边的时候消失
hero_Bulletlist.remove (i) if i.y < 0 else " "
6.8画出敌机子弹
x = random.randint(1, 100)
if x == 5 or x == 78:
# 绘制敌机子弹
djzdd = djzd(enemyplane_x + 69 / 2 - 5 / 2, enemyplane_y + 99, screen)
enemy_Bulletlist.append(djzdd)
for b2 in enemy_Bulletlist:
b2.draw()
7.绘制英雄机、敌机爆炸
7.1绘制敌机爆炸图片
#设置敌机爆炸条件
enemy_is_bomb = False
#敌机爆炸图片索引
enemy_bomb_index = 0
#定义英雄子弹rect
hero_bullet_rect = pygame.rect.Rect(i.x, i.y, 5, 11)
# 碰撞检测
flag = hero_bullet_rect.colliderect(enemy_rect)
if flag:
print("敌机 爆炸")
enemy_is_bomb = True
hero_Bulletlist.remove(i)
7.2加载敌机爆炸图片
enemy_bomb_list=["img/enemy2_down1.png",
"img/enemy2_down2.png",
"img/enemy2_down3.png",
"img/enemy2_down4.png"]
7.3如果没检测到爆炸,绘制没爆炸的敌机图片
if enemy_is_bomb == False:
7.3.1绘制敌机未爆炸图片
if enemy_bomb_index == 0:
screen.blit(enemy_img, (enemyplane_x, enemyplane_y))
enemy_bomb_index = 1
else:
screen.blit(enemy_img2, (enemyplane_x, enemyplane_y))
enemy_bomb_index = 0
else:
7.3.2绘制敌机爆炸图片
if enemy_bomb_index == len(enemy_bomb_list): # 当敌机爆炸图片索引和爆炸图片总数相同时,爆炸图片已经加载结束,退出游戏
time.sleep(0.2)
exit(0)
enemy_bomb_image = pygame.image.load(enemy_bomb_list[enemy_bomb_index])
screen.blit(enemy_bomb_image, (enemyplane_x, enemyplane_y)) # 绘制敌机爆炸图片
enemy_bomb_index += 1
time.sleep(0.2) #更换爆炸图片的时间
7.4加载英雄机战损
hero_bomb_imgs=["img/me_destroy_1.png",
"img/me_destroy_2.png",
"img/me_destroy_3.png",
"img/me_destroy_4.png"]
7.5绘制英雄机爆炸图片
7.5.1设置英雄机爆炸初始条件为False
hero_bomb = False
7.5.2英雄机爆炸索引
hero_bomb_index=0
for b2 in enemy_Bulletlist:
b2.draw()
#表示敌机子弹的矩形区域
enemy_bullet_rect = pygame.rect.Rect(b2.x, b2.y, 5, 11)
#碰撞检测
flag2 = enemy_bullet_rect.colliderect(h_rect)
if flag2:
print("战机爆炸")
hero_bomb = True
enemy_Bulletlist.remove(b2) #英雄机爆炸时,移除子弹
if hero_bomb == False:
# 绘制英雄机,切换英雄机
if index_s == 0:
screen.blit(hero_img1, (h_planex, h_planey))
index_s = 1
else:
screen.blit(hero_img2, (h_planex, h_planey))
index_s = 0
else:
if hero_bomb_index == len(hero_bomb_imgs):
# 当战机爆炸索引与图片总数相同时,退出游戏
time.sleep(1)
exit()
hero_bomb_img = pygame.image.load(hero_bomb_imgs[hero_bomb_index])
screen.blit(hero_bomb_img, (h_planex, h_planey)) # 绘制爆炸图片
hero_bomb_index += 1
time.sleep(0.3)
以上就是我对代码的分析,正确格式如下:
import random
import pygame
import time
# 可视化
pygame.init()
# 窗口大小 创建游戏窗口
screen = pygame.display.set_mode((480,650))
# 标签
pygame.display.set_caption("飞机大战")
# 导入图片
icon = pygame.image.load("img/icon.png")
pygame.display.set_icon(icon)
#加载游戏背景图片
yy = 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"]
# 作为切换的索引值
index_s = 0
# 定义英雄机
h_rect = pygame.rect.Rect(190,525,100,124)
#定义英雄精灵的xY轴
h_planex = h_rect.x
h_planey = h_rect.y
#加载敌机图片
enemy_img = pygame.image.load('img/enemy2.png')
enemy_img2=pygame.image.load("img/enemy2_hit.png")
# 加载敌机爆炸图片
enemy_bomb_list=["img/enemy2_down1.png",
"img/enemy2_down2.png",
"img/enemy2_down3.png",
"img/enemy2_down4.png"]
#定义敌机位置 (480-69)/2
enemy_rect = pygame.rect.Rect(158,0,69,99)
enemyplane_x = enemy_rect.x
enemyplane_y = enemy_rect.y
#设置灵敏度
pygame.key.set_repeat(20,30)
#设置时钟
clock = pygame.time.Clock()
# 英雄机爆炸索引
hero_bomb_index=0
direct='左'
# 定义英雄机子弹
class zd:
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
#英雄机子弹列表
hero_Bulletlist=[]
#定义敌机子弹
class djzd:
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
#敌机子弹列表
enemy_Bulletlist=[]
# 英雄机爆炸
hero_bomb = False
# 敌机爆炸条件
enemy_is_bomb = False
# 敌机爆炸图片索引
enemy_bomb_index = 0
while True:
#每秒刷新60次
clock.tick(60)
# 把背景图片放到坐标
screen.blit(yy, (0, 0)) # 坐标
#飞机可以从底部飞出
h_planey -=1
if h_planey<0:
h_planey=650
# 绘制敌机
screen.blit(enemy_img, (enemyplane_x, enemyplane_y))
# 控制敌机移动
if direct =='左':
enemyplane_x-=5
if enemyplane_x<=0:
direct='右'
elif direct=='右':
enemyplane_x +=5
if enemyplane_x >=480-80:
direct='左'
x = random.randint(1, 100)
if x == 5 or x == 78:
# 绘制敌机子弹
djzdd = djzd(enemyplane_x + 69 / 2 - 5 / 2, enemyplane_y + 99, screen)
enemy_Bulletlist.append(djzdd)
for b2 in enemy_Bulletlist:
b2.draw()
#表示敌机子弹的矩形区域
enemy_bullet_rect = pygame.rect.Rect(b2.x, b2.y, 5, 11)
#碰撞检测
flag2 = enemy_bullet_rect.colliderect(h_rect)
if flag2:
print("战机爆炸")
hero_bomb = True
enemy_Bulletlist.remove(b2)
if hero_bomb == False:
# 绘制英雄机,切换英雄机
if index_s == 0:
screen.blit(hero_img1, (h_planex, h_planey))
index_s = 1
else:
screen.blit(hero_img2, (h_planex, h_planey))
index_s = 0
else:
if hero_bomb_index == len(hero_bomb_imgs):
# 当战机爆炸索引与图片总数相同时,退出游戏
time.sleep(1)
exit()
hero_bomb_img = pygame.image.load(hero_bomb_imgs[hero_bomb_index])
screen.blit(hero_bomb_img, (h_planex, h_planey)) # 绘制爆炸图片
hero_bomb_index += 1
time.sleep(0.3)
#对所有事件进行监听
event_list = pygame.event.get()
#捕获窗口退出事件
for event in event_list:
if event.type == pygame.QUIT:
print("游戏结束了.......")
pygame.quit()
exit(0) #括号里是0 正常退出
#对英雄飞机左右上下经行移动的事件监听
if event.type == pygame.KEYDOWN:
if event.key==pygame.K_LEFT:
h_planex = h_planex-5 if h_planex>=5 else 0
elif event.key==pygame.K_RIGHT:
h_planex=h_planex+5 if h_planex<=375 else 380
elif event.key==pygame.K_UP:
h_planey=h_planey-5 if h_planex>=5 else 0
elif event.key==pygame.K_DOWN:
h_planey=h_planey+5 if h_planey<=521 else 526
elif event.key==pygame.K_SPACE:
# 调用子弹的方法和属性
zdd = zd(h_planex+50-2,h_planey-11,screen)
# 把子弹放入列表中,子弹是多个,我们将多个实例化的子弹放入列表中
hero_Bulletlist.append(zdd)
# 把子弹画出来
for i in hero_Bulletlist:
i.draw()
# 让子弹到最上边的时候消失
hero_Bulletlist.remove (i) if i.y < 0 else " "
#定义英雄子弹rect
hero_bullet_rect = pygame.rect.Rect(i.x, i.y, 5, 11)
# 碰撞检测
flag = hero_bullet_rect.colliderect(enemy_rect)
if flag:
print("敌机 爆炸")
enemy_is_bomb = True
hero_Bulletlist.remove(i)
if enemy_is_bomb == False:
#绘制敌机未爆炸图片
if enemy_bomb_index == 0:
screen.blit(enemy_img, (enemyplane_x, enemyplane_y))
enemy_bomb_index = 1
else:
screen.blit(enemy_img2, (enemyplane_x, enemyplane_y))
enemy_bomb_index = 0
else:
#绘制敌机爆炸图片
if enemy_bomb_index == len(enemy_bomb_list): # 当敌机爆炸图片索引和爆炸图片总数相同时,爆炸图片已经加载结束,退出游戏
time.sleep(0.2)
exit(0)
enemy_bomb_image = pygame.image.load(enemy_bomb_list[enemy_bomb_index])
screen.blit(enemy_bomb_image, (enemyplane_x, enemyplane_y)) # 绘制敌机爆炸图片
enemy_bomb_index += 1
time.sleep(0.2)
#更新界面
pygame.display.update()
测试: