This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
dongxiaoyan-tsg-autotest/05-Other/insert_files/ВставкаСкриптов!@#$%^&(-+=‘;。,?.css

186 lines
7.5 KiB
CSS
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<html>
<head>
<title>新建网页</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="description" content=""/>
<meta name="keywords" content=""/>
<script type="text/javascript">
// 绘制地图
function Map() {
//(便)
var w = 800;
var h = 400;
//
this.showmap = function () {
//divcssbody
var tu = document.createElement('div');
tu.style.width = w + "px";
tu.style.height = h + "px";
tu.style.backgroundImage = "url(./12.jpg)";
document.body.appendChild(tu);
}
}
// 绘制食物
function Food() {
var len = 20;
//()便访
this.xFood = 0;
this.yFood = 0;
this.piece = null; //
//
this.showfood = function () {
//divcssbody
if (this.piece === null) {
this.piece = document.createElement('div');
this.piece.style.width = this.piece.style.height = len + "px";
this.piece.style.backgroundColor = "green";
this.piece.style.position = "absolute";
document.body.appendChild(this.piece);
}
//食物设置绝对定位(position/left/top)
//食物位置随机摆放
//移动步进值20px
//食物权值坐标 X轴(0-39) Y轴(0-19)
//食物真实坐标权值坐标 * 步进值
this.xFood = Math.floor(Math.random() * 40); //0-39的随机数
this.yFood = Math.floor(Math.random() * 20); //0-19的随机数
this.piece.style.left = this.xFood * len + "px";
this.piece.style.top = this.yFood * len + "px";
}
}
// 小蛇
function Snake() {
var len = 20;
this.redirect = "right"; //
//snakebody([xy])
this.snakebody = [[0, 1, 'green', null], [1, 1, 'green', null], [2, 1, 'green', null], [3, 1, 'red', null]];
//a.
this.showsnake = function () {
//
for (var i = 0; i < this.snakebody.length; i++) {
//this.snakebody[i]//
//div
if (this.snakebody[i][3] === null) {//
this.snakebody[i][3] = document.createElement('div');
//css()
this.snakebody[i][3].style.width = this.snakebody[i][3].style.height = len + "px";
this.snakebody[i][3].style.backgroundColor = this.snakebody[i][2];
//
this.snakebody[i][3].style.position = "absolute";
//body
document.body.appendChild(this.snakebody[i][3]);
}
this.snakebody[i][3].style.left = this.snakebody[i][0] * len + "px";
this.snakebody[i][3].style.top = this.snakebody[i][1] * len + "px";
}
}
//b.移动小蛇
this.movesnake = function () {
//( "")
for (var i = 0; i < this.snakebody.length - 1; i++) {
this.snakebody[i][0] = this.snakebody[i + 1][0];
this.snakebody[i][1] = this.snakebody[i + 1][1];
}
if (this.redirect == "right") {
//x
this.snakebody[this.snakebody.length - 1][0] += 1;
}
if (this.redirect == "left") {
//x
this.snakebody[this.snakebody.length - 1][0] -= 1;
}
if (this.redirect == "up") {
//y
this.snakebody[this.snakebody.length - 1][1] -= 1;
}
if (this.redirect == "down") {
//y
this.snakebody[this.snakebody.length - 1][1] += 1;
}
//判断蛇头碰到食物
//蛇头坐标
var xSnake = this.snakebody[this.snakebody.length - 1][0];
var ySnake = this.snakebody[this.snakebody.length - 1][1];
//食物坐标food.xFood/food.yFood;
if (xSnake == food.xFood && ySnake == food.yFood) {
//
var newjie = [this.snakebody[0][0], this.snakebody[0][1], 'green', null];
this.snakebody.unshift(newjie);//newjie
//
food.showfood();
}
//控制小蛇在地图范围内移动
if (xSnake < 0 || xSnake > 39 || ySnake < 0 || ySnake > 19) {
alert('game over');
clearInterval(mytime);
return false;
}
//吃到自己判断(蛇头坐标与其他蛇节坐标一致)
for (var k = 0; k < this.snakebody.length - 1; k++) {
if (this.snakebody[k][0] == xSnake && this.snakebody[k][1] == ySnake) {
alert('game over kill you by yourself');
clearInterval(mytime);
return false;
}
}
//根据新坐标绘制小蛇
this.showsnake();
}
}
window.onload = function () {
var map = new Map();
map.showmap();
food = new Food();//便访
food.showfood();
snake = new Snake();//snake
snake.showsnake();
//
//setInterval()
mytime = setInterval("snake.movesnake()", 200);
//
document.onkeydown = function (evt) {
var num = evt.keyCode;//
if (num == 38) {
snake.redirect = "up";
}
if (num == 40) {
snake.redirect = "down";
}
if (num == 37) {
snake.redirect = "left";
}
if (num == 39) {
snake.redirect = "right";
}
}
}
</script>
<!--
<style type="text/css">
body {
margin: 0;
}
</style> -->
</head>
<body></body>
</html>