import pygame
import random
#1.初始化界面
pygame.init() #初始化
screen = pygame.display.set_mode((800, 600)) #访问窗口,宽和高
pygame.display.set_caption('小草打飞机') #设置标题
icon = pygame.image.load('ufo.png') #加载好图片icon
pygame.display.set_icon(icon) #引入图片
bgImg = pygame.image.load('bg.png') #加入背景图
#添加背景音效
pygame.mixer.music.load('bg.wav') #音乐放在加载上面
pygame.mixer.music.play(-1) #参数-1表示不停播放,单曲循环
#创建射中音效
bao_sound = pygame.mixer.Sound('exp.wav') #创建射中音效
#5.添加飞机
playerImg = pygame.image.load('player.png') #加入飞机图
#创建飞机坐标
playerX = 400 #玩家的X坐标
playerY = 500 #玩家的Y坐标
playerStep = 0 #玩家移动的速度
# 分数
score = 0
font = pygame.font.Font('freesansbold.ttf', 32) #字体文件和大小,创建字体
def show_score():
text = f'score:{score}' # 分数文字,python不认识,用英文代替
score_render = font.render(text, True, (0, 255, 0)) #字体渲染,颜色,也就是显示分数
screen.blit(score_render, (10, 10)) #显示出来,并放在左上角(10,10)处
#游戏结束
is_over = False
over_font = pygame.font.Font('freesansbold.ttf', 64) #字体文件和大小,创建字体
def check_is_over():
if is_over:
text = 'Game Over'
render = over_font.render(text, True, (255, 0, 0)) #字体渲染,红色,也就是显示分数
screen.blit(render, (200, 250)) #显示出来,并放在左上角(10,10)处
#6.添加敌人
number_of_enemies = 10 #敌人的数量
#敌人类,创建敌人只需要做一次,所以不需要在主循环里调用
class Enemy:
def __init__(self):
self.img = pygame.image.load('enemy.png')
self.x = random.randint(200, 600)
self.y = random.randint(50, 250)
self.step = random.randint(1, 4) #敌人移动的速度
#当被射中时恢复位置
def reset(self):
self.x = random.randint(200, 600)
self.y = random.randint(50, 250)
enemies = [] #列表保存所有的敌人
for i in range(number_of_enemies):
enemies.append(Enemy())
#两个点之间的距离,定义一个方法,不属于任何类
def distance(bx, by, ex, ey):
a = bx - ex
b = by - ey
return (a*a + b*b) ** 0.5 #开根号
print(distance(1,1,4,5))
#子弹类
class Bullet:
def __init__(self):
self.img = pygame.image.load('bullet.png')
self.x = playerX + 16 #子弹在飞机左侧太偏,(64-32)/2
self.y = playerY - 10
self.step = 8 #子弹移动的速度
#击中
def hit(self):
global score
for e in enemies:
if (distance(self.x, self.y, e.x, e.y) < 30):
#射中了
bao_sound.play() #调用射中音效
bullets.remove(self)
e.reset()
score += 1
print(score)
bullets = [] #保存现有的子弹,支持连发
#显示并移动子弹
def show_bullets():
for b in bullets:
screen.blit(b.img, (b.x, b.y))
b.hit() #调用,尝试看看是否击中了敌人
b.y -= b.step #移动子弹
#判断子弹是否出了界面,如果出了,就移除掉
if b.y < 0:
bullets.remove(b)
#显示敌人并且实现敌人的移动和下沉
def show_enemy():
global is_over
for e in enemies:
screen.blit(e.img, (e.x, e.y)) #显示enemy
e.x += e.step
#防止飞机出界,变方向
if (e.x > 736 or e.x < 0):
e.step *= -1
e.y += 40 # 碰到两边边界会下沉40
if e.y > 450:
is_over = True
print('游戏结束啦')
enemies.clear()
def move_player():
global playerX #方法里面改变全局变量的值加global
playerX += playerStep #让飞机移动,停就是0,往左移就是负数,往右移就是正数
#防止飞机出界
if playerX > 736: #736是背景图800-飞机图大小64
playerX = 736
if playerX < 0:
playerX = 0
# 2.游戏主循环,让显示出来的界面一直呆在那
running = True
while running:
screen.blit(bgImg, (0, 0)) #画出背景图,里面元组要写坐标
show_score() #显示分数
for event in pygame.event.get(): #把当前发生的事件都返回过来
if event.type == pygame.QUIT: #点叉叉X会关掉窗口
running = False
#通过键盘事件来控制飞机的移动
if event.type == pygame.KEYDOWN: #键盘按下就移动
if event.key == pygame.K_RIGHT: #判断是哪一个键按下了
playerStep = 1.5
elif event.key == pygame.K_LEFT:
playerStep = -1.5
elif event.key == pygame.K_SPACE: #空格键响应发射子弹
print('发射子弹')
#创建一颗子弹
b = Bullet()
bullets.append(b) #把子弹加进去
if event.type == pygame.KEYUP: #抬起来就不动
playerStep = 0
screen.blit(playerImg, (playerX, playerY)) #画出飞机图,可以放在背景图代码下面一行,但是不能放在上面,会被覆盖住
move_player() #移动玩家
show_enemy() #显示敌人
show_bullets() #显示子弹
check_is_over() #显示游戏结束字段
pygame.display.update() #画完必须界面更新,而且必须放在最后
#将游戏一帧一帧铺到里面,每一次循环画一张画,pygame比较灵活,但是什么事情都得你自己做,所有图片坐标原点都是左上角
#KEYDOWN和KEYUP,通过控制坐标来控制图片的运行
#def只有在类里面,才至少用到self参数
#方法里面改变全局变量的值加global
打飞机小游戏
最新推荐文章于 2023-03-17 15:45:33 发布