前言
halo,包子们下午好
今天实现的这个小游戏呀,说实话化学不太好的小伙伴可能看起来会有点懵逼
不过不用担心,咱们今天不是来学化学的,我们是来学习Python的
所以呀,不要太担心啦,大家先好好看看这个实现过程,然后在开始学习就好啦
有啥问题都可以找到小编帮大家解决哈
相关文件
源码领取公众号:Python日志
开发工具
Python版本:3.7.8
相关模块:
pygame模块;
random模块;
bubble模块;
fish模块;
traceback模块;
以及一些python自带的模块。
环境搭建
安装Python并添加到环境变量,pip安装需要的相关模块即可。
效果展示
开始界面
游戏界面
结束界面
代码展示
导入模块
import pygame
from pygame.locals import *
import sys
import os
import traceback
import fish
import bubble
import bullet
from bubble import *
基础设置
# 初始化
pygame.init()
pygame.mixer.init()
# 设置尺寸参数
bg_size = width,height = 480,800
# 设置窗口
screen = pygame.display.set_mode(bg_size)
# 设置标题
pygame.display.set_caption("消灭泡泡 源码领取加QQ群:494958217")
# 加载背景图片
background = pygame.image.load("tuxiang/bg.png").convert()
# 加载游戏开始图片
game_start = pygame.image.load("tuxiang/game_start.png").convert_alpha()
# 加载游戏重新开始图片
game_restart = pygame.image.load("tuxiang/game_restart.png").convert_alpha()
game_restart_rect = game_restart.get_rect()
game_restart_rect.left, game_restart_rect.top = width - game_restart_rect.width - 160, 520
# 加载游戏准备图片
game_loading1 = pygame.image.load("tuxiang/game_loading1.png").convert_alpha()
game_loading2 = pygame.image.load("tuxiang/game_loading2.png").convert_alpha()
game_loading3 = pygame.image.load("tuxiang/game_loading3.png").convert_alpha()
game_loading_rect = game_loading1.get_rect()
# 加载暂停图片
game_stop = pygame.image.load("tuxiang/game_stop.png").convert_alpha()
game_stop_rect = game_stop.get_rect()
# 加载游戏结束图片
game_over = pygame.image.load("tuxiang/game_over.png").convert()
# 加载暂停键图片
game_pause_nor = pygame.image.load("tuxiang/game_pause_nor.png").convert_alpha()
game_pause_pressed = pygame.image.load("tuxiang/game_pause_pressed.png").convert_alpha()
# 加载继续键图片
game_resume_nor = pygame.image.load("tuxiang/game_resume_nor.png").convert_alpha()
game_resume_pressed = pygame.image.load("tuxiang/game_resume_pressed.png").convert_alpha()
# 暂停键按钮范围值
paused_rect = game_pause_nor.get_rect()
paused_rect.left, paused_rect.top = width - paused_rect.width - 10, 10
paused_image = game_pause_nor
resume_image = game_resume_nor
# 加载'生命'数量图片
life_image = pygame.image.load("tuxiang/life.png").convert_alpha()
life_rect = life_image.get_rect()
life_num = 3
# 设置音乐变量
bg_music = pygame.mixer.music
game_achievement_sound = pygame.mixer
game_over_sound = pygame.mixer
bullet_sound = pygame.mixer
bubble1_down_sound = pygame.mixer
bubble2_out_sound = pygame.mixer
bubble2_down_sound = pygame.mixer
pygame.mixer.set_num_channels(15) # 设置音轨通道
volume = 0.6 # 音量
# 加载游戏音乐,设置音量
bg_music.load("music/game_music.mp3")
game_achievement_sound = game_achievement_sound.Sound("music/game_achievement.wav")
game_over_sound = game_over_sound.Sound("music/game_over.wav")
bubble1_down_sound = bubble1_down_sound.Sound("music/bubble1_down.wav")
bubble2_out_sound = bubble2_out_sound.Sound("music/bubble2_out.wav")
bubble2_down_sound = bubble2_down_sound.Sound("music/bubble2_down.wav")
bg_music.set_volume(volume)
game_achievement_sound.set_volume(volume)
game_over_sound.set_volume(volume)
bubble1_down_sound.set_volume(volume)
bubble2_out_sound.set_volume(volume)
bubble2_down_sound.set_volume(volume)
# 基础参数
WHITE = (255,255,255) # 白色
GREEN = (0, 255, 0) # 绿色
RED = (255, 0, 0) # 红色
destroy_speed = 5 # 销毁速度
bullet_speed = 10 # 子弹初始射速
msec = 45 * 1000 # 毫秒数
主函数
def main():
run = True # 循环控制参数
start = False # 游戏开始参数
paussed = True # 暂停标志参数
clock = pygame.time.Clock() # 帧率控制
delay = 100 # 延迟设置
score = 0 # 设置得分
grade1 = 50 # 小型泡泡分数
grade2 = 50
grade3 = 50
level = 1 # 等级水平
life_num = 3 # 生命数
# 字体设置
score_font = pygame.font.Font("font/font.ttf",35)
game_over_font = pygame.font.Font("font/font.ttf",30)
# 设置"游戏准备"图片精灵
game_loadings = list()
game_loadings_index = 0
game_loadings_num = 3
game_loadings.append(game_loading1)
game_loadings.append(game_loading2)
game_loadings.append(game_loading3)
# 背景音乐播放
bg_music.play(-1)
# 实例化鱼
hero = fish.Fish(bg_size)
# 实例化泡泡组
enemies = pygame.sprite.Group()
# 实例化小型泡泡一
mini_enemise = pygame.sprite.Group(</