闲暇之余写的球球大作战,实现了球球之间的吃和被吃,体重增长函数,体重随时间减小函数,和自己的分身功能和敌人的简单ai,有不对的地方,往大佬们指正!!!多的不说直接看代码。
HTML部分
<div class="chuangkou">
<canvas id="huaban">canvas>
div>
css部分
body {
margin: 0;
padding: 0;
background: black;
}
.chuangkou {
width: 500px;
height: 800px;
}
最主要的js部分来了
var huabu = document.querySelector('#huaban');
var huaban = huabu.getContext('2d');
var w, h, qiu = [], s_X = 0, s_Y = 0, c_x = 0, c_y = 0, c, d, time = 0, r = 30, Food = [];
var color = ['#1db0b8', '#de8100', '#3b200c', '#ff534d', '#edd0be'];
function daXiao() {
window.onresize = arguments.callee;
w = window.innerWidth;
h = window.innerHeight;
huabu.width = w;
huabu.height = h;
}
daXiao();
function mouseMove(ev) {
ev = ev || window.event;
var mousePos = mouseCords(ev);
s_X = mousePos.x, s_Y = mousePos.y;
}
function mouseCords(ev) {
if (ev.pageX || ev.pageY) {
return {
x: ev.pageX, y: ev.pageY
};
}
}
function suiJi(max, min) {
return Math.random() * (max - min) + min;
}
function qiuQiu() {
};
qiuQiu.prototype = {
weiZhi: function () {
this.r = 2 * Math.sqrt(30);
this.x = suiJi(this.r, w - this.r);
this.y = suiJi(this.r, h - this.r);
this.color = color[Math.floor(suiJi(0, 5))];
this.tiZhong = 30;
},
yanSe: function () {
huaban.beginPath();
huaban.fillStyle = this.color;
huaban.arc(this.x, this.y, this.r, 0, Math.PI * 2);
huaban.fill();
},
jiSuan: function () {
c = Math.sqrt((s_X - this.x) ** 2 + (s_Y - this.y) ** 2);
},
yiDong: function () {
if (c != 0) {
if (this.x > this.r && this.x < w - this.r && this.y > this.r && this.y < h - this.r) {
this.x += ((s_X - this.x) / c) * r / this.r;
this.y += ((s_Y - this.y) / c) * r / this.r;
} else if (this.x <= this.r) {
this.x += 0.1;
} else if (this.y <= this.r) {
this.y += 0.1;
}
else if (this.x >= w - this.r) {
this.x -= 0.1;
} else if (this.y >= this.r) {
this.y -= 0.1;
}
} else {
}
this.yanSe();
}
}
var qiuqiu = new qiuQiu();
qiuqiu.weiZhi();
qiuqiu.yanSe();
function enemy() {
};
enemy.prototype = {
weiZhi: function () {
this.r = suiJi(5, 30);
this.tiZhong = this.r ** 2 / 4;
this.x = suiJi(this.r, w - this.r);
this.y = suiJi(this.r, h - this.r);
this.color = color[Math.floor(suiJi(0, 5))];
},
yanSe: function () {
huaban.beginPath();
huaban.fillStyle = this.color;
huaban.arc(this.x, this.y, this.r, 0, Math.PI * 2);
huaban.fill();
},
jiSuan: function () {
this.d = Math.sqrt((qiuqiu.x - this.x) ** 2 + (qiuqiu.y - this.y) ** 2);
},
yiDong: function () {
this.r = 2 * Math.sqrt(this.tiZhong);
if (time == 0) this.b_x = (this.x - qiuqiu.x) / this.d, this.b_y = (this.y - qiuqiu.y) / this.d;
if (this.d < 200 && this.y >= this.r && this.y <= h - this.r && this.x >= this.r && this.x <= w - this.r) {
if (qiuqiu.r > this.r) {
this.b_x = (this.x - qiuqiu.x) / this.d;
this.b_y = (this.y - qiuqiu.y) / this.d;
} else {
this.b_x = (qiuqiu.x - this.x) / this.d;
this.b_y = (qiuqiu.y - this.y) / this.d;
}
if (this.y < this.r || this.y > h - this.r) {
this.b_y = -this.b_y;
}
if (this.x < this.r || this.x > w - this.r) {
this.b_x = -this.b_x;
}
this.x += this.b_x * r / this.r;
this.y += this.b_y * r / this.r;
} else {
if (this.y < this.r || this.y > h - this.r) {
this.b_y = -this.b_y;
}
if (this.x < this.r || this.x > w - this.r) {
this.b_x = -this.b_x;
}
this.x += this.b_x * r / this.r;
this.y += this.b_y * r / this.r;
}
this.yanSe();
},
beiChi: function () {
if (this.d <= qiuqiu.r && qiuqiu.r > this.r) {
qiuqiu.tiZhong += this.tiZhong;
qiuqiu.r = 2 * Math.sqrt(qiuqiu.tiZhong);
delete this.tiZhong, delete this.x, delete this.y, delete this.r;
} else if (this.d <= this.r && qiuqiu.r < this.r) {
this.tiZhong += qiuqiu.tiZhong;
delete qiuqiu.tiZhong, delete qiuqiu.x, delete qiuqiu.y, delete qiuqiu.r;
}
}
}
function creatDiRen(num) {
for (var i = 0; i < num; i++) {
var diRen = new enemy();
diRen.weiZhi();
diRen.yanSe();
qiu.push(diRen);
}
}
creatDiRen(5);
function food() {
};
food.prototype = {
weiZhi: function () {
this.tiZhong = 1;
this.r = 2;
this.x = suiJi(this.r, w - this.r);
this.y = suiJi(this.r, h - this.r);
this.color = '#00FFF7';
},
yanSe: function () {
huaban.beginPath();
huaban.fillStyle = this.color;
huaban.arc(this.x, this.y, this.r, 0, Math.PI * 2);
huaban.fill();
},
beiChi: function () {
this.d_w = Math.sqrt((qiuqiu.x - this.x) ** 2 + (qiuqiu.y - this.y) ** 2);
if (this.d_w < this.r + qiuqiu.r) {
qiuqiu.tiZhong += this.tiZhong;
delete this.tiZhong, delete this.x, delete this.y, delete this.r;
qiuqiu.r = 2 * Math.sqrt(qiuqiu.tiZhong);
}
for (item of qiu) {
this.d_d = Math.sqrt((item.x - this.x) ** 2 + (item - this.y) ** 2);
}
}
}
function creatFood(num) {
for (var i = 0; i < num; i++) {
var shiWu = new food();
shiWu.weiZhi();
shiWu.yanSe();
Food.push(shiWu);
}
}
creatFood(300);
setInterval(function () {
huaban.clearRect(0, 0, w, h);
qiuqiu.jiSuan();
qiuqiu.yiDong();
for (item of qiu) {
item.jiSuan();
item.yiDong();
item.beiChi();
}
for (item of Food) {
item.yanSe();
item.beiChi();
}
if (time % 120 == 0 && time != 0) {
qiuqiu.tiZhong -= 0.5;
console.log(qiuqiu.r, qiuqiu.x, qiuqiu.y)
}
time++;
}, 1000 / 60);
document.onmousemove = mouseMove;