(五)项目二日志12.8
Github:https://github.com/kevin-lh/SoftwarePractice/tree/main/Gluttonous%20Snake
一、功能完善
1.1 界面跳转
开始界面与游戏主界面的跳转采用electron自带的方法实现,在mian.js文件中添加以下头文件
const ipc = require('electron').ipcMain
代码如下:
ipc.on('add',()=>
{
let newwin = new BrowserWindow({
width: 750,
height: 600,
frame:false,
parent: win, //win是主窗口
frame: true,
webPreferences: {
nodeIntegration: true,
enableRemoteModule:true
}
})
//win.webContents.openDevTools()
win.hide();
newwin.loadURL(url.format({
pathname: path.join(__dirname, '../Model/index.html'),
protocol: 'file:',
slashes: true
}));
//newwin.loadURL(path.join('file:',__dirname,'../Model/IndexMain.html')); //new.html是新开窗口的渲染进程
newwin.on('closed',()=>{newwin = null})
})
1.2 操作控制
操作控制分为两块,菜单栏与边栏
- 菜单栏
菜单栏通过重写electron菜单栏函数实现,增加退出游戏、帮助、重写开始等选项。
const Menus = [
{
label:'退出游戏',
submenu:[{
label:'退出游戏',
role:'exit',
click:function () {
app.quit();
}
}]
},
{
label: '帮助',
role:'help',
click:function () {
createWindowHelp();
}
},
{
label: '重新开始',
role:'reload',
click:function () {
app.relaunch();
}
}
];
- 边栏
边栏功能为开始游戏与暂停,点击“开始”图标调用start函数即可,点击“暂停”图标将定时器暂停即可。
//开始
function ifStart() {
game.start();
}
//暂停
function pause() {
clearInterval(game.timer);
}
1.3 难度控制
难度控制实现也及其简单,可以想一下如何改变蛇的速度呢?
很简单,因为蛇的移动是通过定时器实现的,所以只要改变定时器的时间间隔即可。
简单模式:250ms 正常模式:200ms 困难模式:100ms
//模式选择
function easy() {
game.speed = 250;
}
function normal() {
game.speed = 200;
}
function hard() {
game.speed = 100;
}
1.4 背景音乐
为了游戏增加游戏体验,加入了背景音乐,其中包括整体背景音乐以及死亡后的音效(这里是模仿的《贪食蛇大战》hhh)
添加的方法如下:利用Audio类,定义audio对象即可,play方法开始播放,pause方法暂停播放。
renderMusic(){
this.audio = "../Resources/BGM.mp3";
this.audio = new Audio(this.audio);
this.audio.play();
}
renderDieMusic(){
var audio;
audio = "../Resources/DIE.mp3";
audio = new Audio(audio);
audio.play();
}
二、整体演示
2.1 基本功能演示
点击“开始游戏”开始,食物随机生成,蛇吃到食物长度增加,并且关卡数随之增加。
2.1 控制功能演示
开始,暂停,重新开始功能演示
蛇触碰边界或者首尾相碰后->结束界面
难度选择功能演示:
简单->正常->困难
2.3 其他功能演示
退出游戏功能:
帮助功能:
三、附录
背景音效演示视频如下:
贪吃蛇