- 分享
- 0
- 人气
- 0
- 主题
- 46
- 帖子
- 3604
- UID
- 123250
- 积分
- 6456
- 阅读权限
- 23
- 注册时间
- 2008-2-7
- 最后登录
- 2017-5-1
- 在线时间
- 5029 小时
|
本帖最后由 宅男-兜着走 于 2011-4-8 03:50 PM 编辑
Office 生活可能是得空的,
不行了, 好难忍。。。
时间慢到不行。
我想要换工做了。。。
今天整个下午, 没事情做, 一字一句慢慢 Code 的
当然并没有写到很完美, 也没什么特别功能。
自己写自己玩
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <script language="javascript" src="http://code.jquery.com/jquery-1.5.2.min.js">
- </script>
- <style type="text/css">
- table {border: 1px solid black}
- td, tr {border: 0}
- </style>
- <script language="javascript">
- $(function()
- {
- /**
- * Up button : 38
- * Down button: 40
- * Left button : 37
- * Right button : 39
- */
- var snakePos =
- [
- {x:5 , y:0},
- {x:4 , y:0},
- {x:3, y: 0},
- {x:2, y: 0},
- {x:1, y: 0},
- {x:0, y: 0}
- ];
- var foodPos = null;
- var direction = 39;
- var rows = 30;
- var cols = 50;
- $("#gameBody")
- .append("<table border='0' align='center' cellspacing='0' cellpadding='0' id='gameGrid' class='borderless'></table>");
- $(this).keydown(function(event)
- {
- direction = event.keyCode;
- });
-
- for(var i = 0; i < rows; i ++)
- {
- var htmlString= "<tr height='6px'>";
- for(var j = 0; j < cols; j++)
- {
- htmlString += "<td width='5px'></td>";
- }
- htmlString += "</tr>";
- $("#gameGrid").append(htmlString);
- }
- function snakeMove()
- {
- var newHeadPosition= {x: snakePos[0].x , y: snakePos[0].y};
- switch(direction)
- {
- case 39:
- newHeadPosition.x+=1;
- break;
- case 40:
- newHeadPosition.y +=1;
- break;
- case 37:
- newHeadPosition.x -= 1;
- break;
- case 38:
- newHeadPosition.y -= 1;
- break;
- }
- swapCell();
- snakePos[0] = newHeadPosition;
- if(snakePos[0].x == foodPos.x && snakePos[0].y == foodPos.y)
- {
- foodPos= generateFood();
- snakePos.push({x:snakePos[0].x, y:snakePos[0].y})
- }
- display();
- }
- function swapCell()
- {
- var temp = snakePos[0];
- for(var i = 1; i < snakePos.length; i++)
- {
- var internalTemp = temp;
- temp = snakePos[i];
- snakePos[i] = internalTemp;
- }
- }
- function generateFood()
- {
- var foodPosition =
- {
- x: Math.floor((cols -1) * Math.random()),
- y: Math.floor((rows-1) * Math.random())
- };
- for(var i = 0; i < snakePos.length; i++)
- {
- if(foodPosition.x == snakePos[i].x && foodPosition.y == snakePos[i].y)
- return generateFood();
- }
- return foodPosition;
- }
- function display()
- {
- $("#gameGrid tbody tr td")
- .css({background: "white"});
- displayFood();
- for(var i = 0; i < snakePos.length; i++)
- {
- $("#gameGrid").find("tr")
- .eq(snakePos[i].y)
- .find("td")
- .eq(snakePos[i].x)
- .css({background: "black"});
- }
- }
- function displayFood()
- {
- $("#gameGrid").find("tr")
- .eq(foodPos.y)
- .find("td")
- .eq(foodPos.x)
- .css({background: "black"});
- }
- foodPos = generateFood();
- setInterval(snakeMove, 200);
-
-
- })
- </script>
- </head>
- <body id="gameBody">
- </body>
- </html>
复制代码 |
|