当前位置:网站首页>Snake game
Snake game
2022-06-26 02:09:00 【Asmruan】
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
#map {
width: 800px;
height: 600px;
background-color: #ccc;
position: relative;
/* position: absolute; */
}
/* div{
position: absolute;
background-color: aqua;
width: 10px;
height: 10px;
top: 10px;
}
div:nth-of-type(1){
left: 0px;
}
div:nth-of-type(2){
left: 10px;
}
div:nth-of-type(3){
left: 20px;
}
div:nth-of-type(4){
left: 30px;
background-color: red;
} */
</style>
</head>
<body>
<header id="map">
<!-- <div></div>
<div></div>
<div></div>
<div></div> -->
</header>
</body>
</html>
<script>
var position = "absolute";
var map1 = document.getElementById("map");
// food Wide and high Location X Y Color
function Food(
width = 10,
height = 10,
X = 0,
Y = 0,
backgroundColor = "red"
) {
this.width = width;
this.height = height;
this.X = X;
this.Y = Y;
this.backgroundColor = backgroundColor;
}
// Random Rendering
Food.prototype.renderFood = function (map) {
var maxX = map.offsetWidth - this.width;
var maxY = map.offsetHeight - this.height;
var div = document.createElement("div");
div.style.position = "absolute";
div.style.width = this.width + "px";
div.style.height = this.height + "px";
// Random position Show
// Random food location ,map. Width /food. Width , How many points are there in total food Width , Random . And then multiply by food Width
this.X =
parseInt((Math.random() * map.offsetWidth) / this.width) * this.width;
this.Y =
parseInt((Math.random() * map.offsetHeight) / this.height) * this.height;
div.style.left = this.X + "px";
div.style.top = this.Y + "px";
div.style.backgroundColor = this.backgroundColor;
this.foodDom = div;
map.appendChild(div);
};
// var food1 = new Food()
// width=10,height=10,X=0,Y=0,backgroundColor="red"
var food1 = new Food(20, 20, 0, 0, "green");
food1.renderFood(map1);
// The object of the snake wide high left top Snake-body. ( Array object (X,Y, Color color))
function Snake(width = 10, height = 10, direction) {
this.width = width;
this.height = height;
// Used to preserve snakes dom Elements
this.bodyDom = [];
this.direction = "right";
this.body = [
{ X: 2, Y: 1, backgroundColor: "red" },
{ X: 1, Y: 1, backgroundColor: "blue" },
{ X: 0, Y: 1, backgroundColor: "blue" },
];
}
Snake.prototype.renderSnake = function (map) {
for (var i = 0; i < this.body.length; i++) {
var div = document.createElement("div");
div.style.position = "absolute";
div.style.height = this.height + "px";
div.style.width = this.width + "px";
div.style.left = this.body[i].X * this.width + "px";
div.style.top = this.body[i].Y * this.height + "px";
div.style.backgroundColor = this.body[i].backgroundColor;
// Reserved snake dom
this.bodyDom.push(div);
map.appendChild(div);
}
};
Snake.prototype.moveSnake = function (food, map) {
console.log(this.body[0].X);
// If the head of the snake dies
if (
this.body[0].X < 0 ||
this.body[0].X >= map.offsetWidth / this.width ||
this.body[0].Y < 0 ||
this.body[0].Y >= map.offsetHeight / this.height
) {
clearInterval(timer1);
alert("over");
}
// if(this.body[0].X >= map.offsetWidth/this.width ){
// alert("over")
// }
// if(this.body[0].Y < 0 ){
// alert("over")
// }
// if(this.body[0].Y >= map.offsetHeight/this.height ){
// alert("over")
// }
// If the snake head coincides with the food , Add one to the snake
var lastBody = this.body[this.body.length - 1];
if (
this.body[0].X == food.X / food.width &&
this.body[0].Y == food.Y / food.height
) {
// Delete old food , Make new food
food.foodDom.remove();
food.renderFood(map);
this.body.push({
X: lastBody.X,
Y: lastBody.Y,
backgroundColor: lastBody.backgroundColor,
});
}
// The snake moves
var lastBackgroundColor = this.body[this.body.length - 1].backgroundColor;
for (var i = this.body.length - 1; i > 0; i--) {
this.body[i].X = this.body[i - 1].X;
this.body[i].Y = this.body[i - 1].Y;
this.body[i].backgroundColor = lastBackgroundColor;
}
// Snakehead
if (this.direction == "up") {
this.body[0].Y--;
}
if (this.direction == "down") {
this.body[0].Y++;
}
if (this.direction == "left") {
this.body[0].X--;
}
if (this.direction == "right") {
this.body[0].X++;
}
};
// Delete snake
Snake.prototype.deleteSnake = function () {
for (var i = 0; i < this.bodyDom.length; i++) {
this.bodyDom[i].remove();
}
};
var snake1 = new Snake(20, 20);
// setInterval(function(){
// // Delete the previous snake , Make up the new
// snake1.deleteSnake()
// for(var i=0;i<snake1.body.length;i++){
// snake1.body[i].X++
// }
// snake1.renderSnake(map1)
// },500)
// var list = document.querySelectorAll('div')
// var list = document.getElementsByTagName('div')
// var lastDiv = list[list.length-1]
// console.log(lastDiv);
// lastDiv.remove()
console.log(snake1.bodyDom);
// Listening to keyboard events
// (function keypress() {
document.addEventListener("keypress", function (event) {
var event = event || window.event;
// console.log(event);
// console.log(this);
// a yes 97 The direction is left
if (event.keyCode == 97) {
snake1.direction = "left";
}
if (event.keyCode == 119) {
snake1.direction = "up";
}
if (event.keyCode == 100) {
snake1.direction = "right";
}
if (event.keyCode == 115) {
snake1.direction = "down";
}
console.log(snake1.direction);
});
// })
snake1.renderSnake(map1);
var timer1 = setInterval(function () {
snake1.deleteSnake();
snake1.moveSnake(food1, map1);
snake1.renderSnake(map1);
}, 200);
</script>边栏推荐
猜你喜欢

One minute to understand the difference between synchronous, asynchronous, blocking and non blocking

Disruptor(一)Sequence

SDRAM控制器——添加读写FIFO

论文阅读 Exploring Temporal Information for Dynamic Network Embedding

【js】免费api判断节假日、工作日和周六日

Scala 基础 (二):变量和数据类型
![[JS] free API to judge holidays, working days, Saturdays and Sundays](/img/e1/8b742082385bb5e60f74beb3b81c7d.png)
[JS] free API to judge holidays, working days, Saturdays and Sundays

V4l2+qt video optimization strategy

-- EGFR FISH probe solution

图形渲染管线
随机推荐
Connectez Le projecteur
Tarte aux framboises + AWS IOT Greengrass
Meaning of each state in TCP network communication
Prometeus 2.33.0 新特性
通俗易懂C語言關鍵字static
Output Lua print to the cocos2d console output window
Differences and functions of export set env in makefile
Finding the sum of N multiplications
shell学习记录(一)
General introduction to gun make (2)
Reverse output an integer
shell curl 执行脚本,带传参数,自定义参数
Disruptor(一)Sequence
Ardiuno智能电蚊拍
V4L2+QT视频优化策略
Steps of program compilation precompile compilation assembly connection
SDRAM控制器——仲裁模块的实现
Qtvtkvs2015 test code
Detailed explanation of WiFi related knowledge
Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) D. Felicity‘s Big Secret Revealed