当前位置:网站首页>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>边栏推荐
- 购买了fastadmin小程序助手但是问题工单中无法发布工单
- Spiral matrix
- leetcode 300. Longest Increasing Subsequence 最长递增子序列 (中等)
- Tcp网络通信中各个状态的含义
- General introduction to gun make (2)
- Is the securities account recommended by qiniu safe?
- Chemical properties and application of trypsin
- @Query 疑难杂症
- Gun make (7) execute make
- LeetCode 41 ~ 50
猜你喜欢
随机推荐
Calibration...
LeetCode 41 ~ 50
@Query 疑难杂症
Exploring temporary information for dynamic network embedding
将weishi相机图片进行转换
How to set achievable medium - and long-term goals?
Detailed explanation of memory leak check tools
购买了fastadmin小程序助手但是问题工单中无法发布工单
Raspberry pie + AWS IOT introductory experiment
Steps of program compilation precompile compilation assembly connection
Gun make (3) Rules for makefile
连接投影仪
微服务之consul
Visual studio 2013 redistributable is installed, but MySQL installation fails
Abnova actn4 DNA probe solution
将lua print输出到cocos2d控制台输出窗口中
Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) C. Felicity is Coming!
Scala 基础 (二):变量和数据类型
高手常用的电脑快捷键
Detailed explanation of WiFi related knowledge








