-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfood.js
29 lines (27 loc) · 944 Bytes
/
food.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* Created by 17654 on 2018/4/24.
*/
function Food(options) {
var options = options || {};
this.x = options.x || 0;
this.y = options.y || 0;
this.width = options.width || 20;
this.height = options.height || 20;
this.bgc = options.bgc || 'green'
}
Food.prototype.render = function (target) {
//创建食物
var foodElement = document.createElement('div');
this.foodElement = foodElement
foodElement.style.position = 'absolute'
foodElement.style.borderRadius = '50%'
foodElement.style.width = this.width + 'px'
foodElement.style.height = this.height + 'px'
foodElement.style.backgroundColor = this.bgc
//设置坐标
this.x = parseInt(Math.random() * (target.offsetWidth / this.width));
this.y = parseInt(Math.random() * (target.offsetHeight / this.height));
foodElement.style.left = this.x * this.width + 'px';
foodElement.style.top = this.y * this.height + 'px';
target.appendChild(foodElement);
}