项目环境
python版本:3.8.5
所需模块:pygame random os
pygame版本:20.1
开发环境:pycharm专业版
硬件环境:win11 8G内存以上
项目介绍
使用python的第三方库–pygame 制作飞机大战小游戏
小游戏的内容包括:
- 玩家player的移动
- 子弹的发射
- 陨石的随机掉落(包括旋转 大小 下落角度)
- 玩家 子弹 陨石的碰撞交互
- 血量条
- 积分计数
- 游戏道具随机生成
完成总计时长:
6小时
图片预览
文章来源:https://www.toymoban.com/news/detail-764232.html
笔记
初始化 pygame :pygame.init()
创建窗口:screen = pygame.display.set_mode([width,height])
加载图片资源::pic = pygame.image.load(“图片”)
加载音乐资源:pygame.mixer.music.load(‘音乐’)
os库查找文件:os.path.join(‘文件夹’,’文件‘)
yx = pygame.mixer.Sound(“f音效”)
播放音效:yx.play()
获取所有的事件: pygame.event.get()文章来源地址https://www.toymoban.com/news/detail-764232.html
源码
import pygame
import random
import os
#游戏内的常量
FPS = 60
WIDTH = 500
HEIGHT = 600
WHITE = (255,255,255)
GREEN = (0,255,0)
RED =(255,0,0)
YELLOW = (255,255,0)
BLACK = (0,0,0)
#游戏初始化
pygame.init()
pygame.mixer.init()#初始化声音混合器
screen = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("飞机大战")
clock = pygame.time.Clock()
#引入图片文件
#os.path.join可以寻找到图片文件 convert方法可以把东西转化为对象
background_img = pygame.image.load(os.path.join("img","background.png")).convert()
player_img = pygame.image.load(os.path.join("img","player.png")).convert()
player_mini_img = pygame.transform.scale(player_img,(25,20)) #scale是改变大小的方法
player_mini_img.set_colorkey(BLACK)
rock_img = pygame.image.load(os.path.join("img","rock.png")).convert()
bullet_img = pygame.image.load(os.path.join("img","bullet.png")).convert()
#存储许多陨石图片
rock_images = []
for i in range(7):
rock_images.append(pygame.image.load(os.path.join("img",f"rock{i}.png")).convert())
#爆炸动画的储存
expl_anim = {}
expl_anim['lg'] = []
expl_anim['sm'] = []
expl_anim['player'] = []
for i in range(9):
expl_img = pygame.image.load(os.path.join("img",f"expl{i}.png")).convert()
expl_img.set_colorkey(BLACK)
expl_anim['lg'].append(pygame.transform.scale(expl_img,(75,75)))
expl_anim['sm'].append(pygame.transform.scale(expl_img, (30, 30)))
player_expl_img = pygame.image.load(os.path.join("img",f"player_expl{i}.png")).convert()
player_expl_img.set_colorkey(BLACK)
expl_anim['player'].append(player_expl_img)
#引入宝箱的图片
power_imgs = {}
power_imgs['shield'] = pygame.image.load(os.path.join("img","shield.png")).convert()
power_imgs['gun'] = pygame.image.load(os.path.join("img","gun.png")).convert()
#射击的声音
shoot_sound = pygame.mixer.Sound(os.path.join("sound","shoot.wav"))
#爆炸的音效
expl_sounds =[
pygame.mixer.Sound(os.path.join("sound","expl0.wav")),
pygame.mixer.Sound(os.path.join("sound","expl1.wav")),
]
#死亡爆炸
die_sound = pygame.mixer.Sound(os.path.join("sound","rumble.ogg"))
#接到宝箱
shield_sound = pygame.mixer.Sound(os.path.join("sound","pow0.wav"))
gun_sound = pygame.mixer.Sound(os.path.join("sound","pow1.wav"))
#背景音乐
pygame.mixer.music.load(os.path.join("sound","background.ogg"))
pygame.mixer.music.set_volume(0.3)
running = True
#引入字体
font_name = pygame.font.match_font("arial")
#绘制得分的函数
def draw_text(surf,text,size,x,y):
font = pygame.font.Font(font_name,size)
text_surface = font.render(text,True,WHITE)#渲染一下字体
text_rect = text_surface.get_rect()
text_rect.centerx = x
text_rect.top = y
surf.blit(text_surface,text_rect)
#生成陨石的函数
def new_rock():
rock = Rock()
all_sprites.add(rock)
rocks.add(rock)
#绘制血量条的函数
def draw_health(surf,hp,x,y):
if hp < 0 :
hp = 0
BAR_LENGTH = 100
BAR_HEIGHT = 10
fill = (hp/100)*BAR_LENGTH #内部的长度
outline_rect = pygame.Rect(x,y,BAR_LENGTH,BAR_HEIGHT) #外框
fill_rect = pygame.Rect(x,y,fill,BAR_HEIGHT) #内部
pygame.draw.rect(surf,GREEN,fill_rect)
pygame.draw.rect(surf, WHITE, outline_rect,2)
#绘制右上角代表生命的飞机
def draw_lives(surf,lives,img,x,y):
for i in range(lives):
img_rect = img.get_rect()
img_rect.x = x+30*i
img_rect.y = y
surf.blit(img,img_rect)
#创造玩家类
class Player(pygame.sprite.Sprite):
#初始化方法
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.transform.scale(player_img,(50,40)) #设置好大小的图片
self.image.set_colorkey(BLACK)#把黑色的东西设置成透明
#定位
self.rect = self.image.get_rect()
#圆形图片轮廓
self.radius = 23
# pygame.draw.circle(self.image,RED,self.rect.center,self.radius)
self.rect.centerx = WIDTH/2
self.rect.bottom = HEIGHT -20
self.health = 100
self.lives = 3
self.hidden = False
self.speedx = 8
self.hide_time = pygame.time.get_ticks()
self.gun = 1
#更新的方法
def update(self):
now = pygame.time.get_ticks()
key_pressed = pygame.key.get_pressed() #获取键盘操作
#控制上下左右的移动
if key_pressed[pygame.K_RIGHT]:
self.rect.x = self.rect.x + self.speedx
if key_pressed[pygame.K_LEFT]:
self.rect.x = self.rect.x - self.speedx
if key_pressed[pygame.K_DOWN]:
self.rect.y = self.rect.y + self.speedx
if key_pressed[pygame.K_UP]:
self.rect.y = self.rect.y - self.speedx
#上下左右移动的限制
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right > WIDTH:
self.rect.right = WIDTH
if self.rect.top < 0:
self.rect.top = 0
if self.rect.bottom > HEIGHT:
self.rect.bottom = HEIGHT
#恢复正常
if self.hidden and (pygame.time.get_ticks() - self.hide_time > 1000):
self.hidden = False
self.rect.centerx = WIDTH / 2
self.rect.bottom = HEIGHT - 20
#时间到了变成单射
if self.gun > 1 and now - self.gun_time > 10000:
self.gun = 1
#射击方法
def shoot(self):
if not (self.hidden):
if self.gun == 1:#单射
bullet = Bullet(self.rect.centerx,self.rect.centery)
all_sprites.add(bullet)
bullets.add(bullet)
shoot_sound.play()
elif self.gun >= 2:#双射
bullet1 = Bullet(self.rect.left, self.rect.centery)
bullet2 = Bullet(self.rect.right, self.rect.centery)
all_sprites.add(bullet1)
all_sprites.add(bullet2)
bullets.add(bullet1)
bullets.add(bullet2)
shoot_sound.play()
#隐藏右上角的飞机
def hide(self):
self.hidden = True
self.hide_time = pygame.time.get_ticks()
self.rect.center = (WIDTH/2,HEIGHT+500)
#增加弹道的发方法
def gunup(self):
self.gun += 1
self.gun_time = pygame.time.get_ticks()
#陨石类
class Rock(pygame.sprite.Sprite):
#初始化方法
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image_origin = random.choice(rock_images)
self.image_origin.set_colorkey(BLACK)
#图片副本
self.image = self.image_origin.copy()
#定位
self.rect = self.image.get_rect()
#圆形图片轮廓
self.radius = self.rect.width / 2.2
self.rect.x = random.randrange(0,WIDTH-self.rect.width)
self.rect.y = random.randrange(-180,-100)
self.speedy = random.randrange(2,10)
self.speedx = random.randrange(-3,+3)
self.rot_degree = random.randrange(-3,3) #旋转的角度
self.total_degree = 0
#石头旋转的方法
def rotate(self):
self.total_degree = self.total_degree + self.rot_degree
self.total_degree = self.total_degree % 360
self.image = pygame.transform.rotate(self.image_origin,self.total_degree)#transform是旋转方法
center = self.rect.center
self.rect= self.image.get_rect()
self.rect.center = center
#更新的方法
def update(self):
self.rotate()
self.rect.y += self.speedy
self.rect.x += self.speedx
#如果屏幕上没有陨石则会重新生成
if (self.rect.top > HEIGHT) or (self.rect.right < 0)or(self.rect.left > WIDTH) :
self.rect.x = random.randrange(0, WIDTH - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speedy = random.randrange(2, 10)
self.speedx = random.randrange(-3, +3)
#子弹类
class Bullet(pygame.sprite.Sprite):
# 初始化方法
def __init__(self,x,y):
pygame.sprite.Sprite.__init__(self)
self.image = bullet_img
self.image.set_colorkey(BLACK)
# 定位
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.speedy = -10
# 更新的方法
def update(self):
self.rect.y += self.speedy
# 如果屏幕上没有子弹则会消除掉
if (self.rect.bottom < 0) :
self.kill()
#爆炸类
class Explosion(pygame.sprite.Sprite):
# 初始化方法
def __init__(self,center,size):
pygame.sprite.Sprite.__init__(self)
self.size = size
self.image = expl_anim[self.size][0]
# 定位
self.rect = self.image.get_rect()
self.rect.center = center
self.frame = 0
self.last_update = pygame.time.get_ticks()
# 更新的方法
def update(self):
now = pygame.time.get_ticks()
if now - self.last_update > 50:
self.last_update = now
self.frame = self.frame + 1
if self.frame == len(expl_anim[self.size]):
self.kill()
else:
self.image = expl_anim[self.size][self.frame]
center = self.rect.center
self.rect = self.image.get_rect()
self.rect.center = center
#宝箱类
class Power(pygame.sprite.Sprite):
# 初始化方法
def __init__(self,center):
pygame.sprite.Sprite.__init__(self)
self.type = random.choice(['shield','gun'])
self.image = power_imgs[self.type]
self.image.set_colorkey(BLACK)
#音效
if self.type == 'shield':
shield_sound.play()
elif self.type == 'gun':
gun_sound.play()
# 定位
self.rect = self.image.get_rect()
self.rect.center = center
self.speedy = 3
# 更新的方法
def update(self):
self.rect.y += self.speedy
# 如果屏幕上没有子弹则会消除掉
if (self.rect.bottom > HEIGHT) :
self.kill()
#变量的初始化
all_sprites = pygame.sprite.Group() #所有的
rocks = pygame.sprite.Group() #陨石
powers = pygame.sprite.Group() #宝箱
bullets = pygame.sprite.Group() #子弹
player = Player()#实例化
all_sprites.add(player)
score = 0
#播放声音
pygame.mixer.music.play(-1)
#生成掉落的陨石
for i in range(8):
new_rock()
#主程序循环
while running:
clock.tick(FPS) #设定帧率
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
player.shoot()
#更新游戏
all_sprites.update()
#检测陨石子弹互相碰撞的情况
hits_rockandbullet = pygame.sprite.groupcollide(rocks,bullets,True,True)
#陨石子弹碰撞消除一次就生成一个陨石
for hit in hits_rockandbullet:
random.choice(expl_sounds).play()
expl = Explosion(hit.rect.center,'lg')
all_sprites.add(expl)
die_sound.play()
r = Rock()
all_sprites.add(r)
rocks.add(r)
score = score + int(hit.radius)
#有概率生成宝箱
if random.random() > 0.7:
p = Power(hit.rect.center)
all_sprites.add(p)
powers.add(p)
#飞船碰到宝箱
hits_playerandpower = pygame.sprite.spritecollide(player,powers,True)
for hit in hits_playerandpower:
if hit.type == 'shield':
player.health = player.health + 20
if player.health > 100:
player.health =100
elif hit.type=='gun':
player.gunup()
#检测飞机碰撞的情况
hits_playerandrock = pygame.sprite.spritecollide(player,rocks,True,pygame.sprite.collide_circle)
#
for hit in hits_playerandrock:
player.health -= hit.radius
new_rock()
expl = Explosion(hit.rect.center,'sm')
all_sprites.add(expl)
if player.health <= 0:
death_expl = Explosion(player.rect.center,'player')
all_sprites.add(death_expl)
player.lives -= 1
player.health = 100
player.hide()
elif player.lives == 0:
running = False
#显示画面
screen.fill(BLACK)
screen.blit(background_img,(0,0))#设置背景图片
all_sprites.draw(screen)
draw_text(screen,str(score),18,WIDTH/2,0) #显示得分
draw_health(screen,player.health,10,30)
draw_lives(screen,player.lives,player_mini_img,WIDTH-100,15)
pygame.display.update() #刷新界面
pygame.quit()
到了这里,关于【python】 pygame学习示例 --飞机大战小游戏制作的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!