game1--- bugku
第一部分js代码:
<script>
var domReady, loadFinish, canvasReady, loadError, gameStart, game, score, successCount //定义一些变量
// init window height and width
var gameWidth = window.innerWidth //设置游戏界面大小
var gameHeight = window.innerHeight
var ratio = 1.5
if (gameHeight / gameWidth < ratio) {
gameWidth = Math.ceil(gameHeight / ratio) //根据除法算出上舍入:2.1=>3
}
$('.content').css({ "height": gameHeight + "px", "width": gameWidth + "px" })
$('.js-modal-content').css({ "width": gameWidth + "px" })
// loading animation 加载动画
function hideLoading() {
if (domReady && canvasReady) {
$('#canvas').show()
loadFinish = true
setTimeout(function () {
$('.loading').hide()
$('.landing').show()
}, 1000)
}
}
function updateLoading(status) { //加载游戏函数
var success = status.success
var total = status.total
var failed = status.failed
if (failed > 0 && !loadError) {
loadError = true
alert('加载失败 请刷新后重试')
return
}
var percent = parseInt((success / total) * 100);
if (percent === 100 && !canvasReady) {
canvasReady = true
hideLoading()
}
percent = percent > 98 ? 98 : percent
percent = percent + '%'
$('.loading .title').text(percent);
$('.loading .percent').css({
'width': percent
})
}
function overShowOver() { //游戏结束函数
$('#modal').show()
$('#over-modal').show()
if (window.XMLHttpRequest)
{// IE7+, Firefox, Chrome, Opera, Safari 浏览器执行
xmlhttp=new XMLHttpRequest();
}
else
{// IE6, IE5 浏览器执行
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("livesearch").innerHTML=xmlhttp.responseText;
document.getElementById("livesearch").style.border="1px solid #A5ACB2";
}
}
var ppp='223.104.90.251';
var sign = Base64.encode(score.toString());
xmlhttp.open("GET","score.php?score="+score+"&ip="+ppp+"&sign="+sign,true); //创建一个新的http请求,并指定此请求的方法、URL以及验证信息,也就是每局游戏结束,都会发送一个url请求,带有分数和ip和sign的值,这个sign的值与分数有关,所以我们只修改score是不全面的
xmlhttp.send();
$('#over-zero').show()
}
// game customization options 游戏定制选项
const option = {
width: gameWidth,
height: gameHeight,
canvasId: 'canvas',
soundOn: true,
setGameScore: function (s) {
score = s
},
setGameSuccess: function (s) {
successCount = s
},
setGameFailed: function (f) {
$('#score').text(score)
if (f >= 3) overShowOver()
}
}
// game init with option 带选项的游戏初始化
function gameReady() {
game = TowerGame(option)
game.load(function () {
game.playBgm()
game.init()
}, updateLoading)
}
var isWechat = navigator.userAgent.toLowerCase().indexOf("micromessenger") !== -1
if (isWechat) {
document.addEventListener("WeixinJSBridgeReady", gameReady, false)
} else {
gameReady()
}
function indexHide() {
$('.landing .action-1').addClass('slideTop')
$('.landing .action-2').addClass('slideBottom')
setTimeout(function () {
$('.landing').hide()
}, 950)
}
// click event
$('#start').on('click', function () {
if (gameStart) return
gameStart = true
indexHide()
setTimeout(game.start, 400)
})
$('.js-reload').on('click', function () {
window.location.href = window.location.href + '?s=' + (+new Date())
})
$('.js-invite').on('click', function () {
$('.wxShare').show()
})
$('.wxShare').on('click', function () {
$('.wxShare').hide()
})
// listener
window.addEventListener('load', function () {
domReady = true
hideLoading()
}, false);
</script>
第二部分JS代码:
<script>
(function (i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r;
i[r] = i[r] || function () {
(i[r].q = i[r].q || []).push(arguments)
}, i[r].l = 1 * new Date();
a = s.createElement(o),
m = s.getElementsByTagName(o)[0];
a.async = 1;
a.src = g;
m.parentNode.insertBefore(a, m)
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
ga('create', 'UA-46444752-20', 'auto');
ga('send', 'pageview');
var _hmt = _hmt || [];
(function () {
var hm = document.createElement("script"); //在对象中创建一个对象
hm.src = "https://hm.baidu.com/hm.js?c1b044f909411ac4213045f0478e96fc";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>