HTML5贪吃蛇源代码
显示效果例如以下:点击这里!
代码下载:点击这里!
<script>
function show(){
var date = new Date(); //日期对象
var now = "";
now = date.getFullYear()+"年"; //读英文即可了
now = now + (date.getMonth()+1)+"月"; //取月的时候取的是当前月-1假设想取当前月+1就能够了
now = now + date.getDate()+"日";
now = now + date.getHours()+"时";
now = now + date.getMinutes()+"分";
now = now + date.getSeconds()+"秒";
document.getElementById("nowDiv").innerHTML = now; //div的html是now这个字符串
setTimeout("show()",1000); //设置过1000毫秒就是1秒,调用show方法
}
</script>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>网络人VS灰鸽子工作室</title>
<style type="text/css">
*{margin:0;padding:0;list-style-type:none;}
div{
background:-webkit-gradient(linear,0 0, 0 100%, from(yellow), to(red));/**Chrom Safari**/
background:-moz-linear-gradient(top, yellow, red); /** FireFox **/
background:-o-linear-gradient(top, yellow,red);/** Opear **/
background:-ms-linear-gradient(yellow 0%,red 100%);/** IE9 IE10 **/
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='yellow',endColorstr='red',grandientType=1); /** IE7 **/
-ms-filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='yellow',endColorstr='red',grandientType=1); /** IE8 **/
box-shadow:3px 3px 8px yellow;border-radius:5px;
}
input{
margin: 50px 0 0 50px;cursor:pointer;width:100px;height:50px;
-webkit-transform:rotate(20deg);
-moz-transform:rotate(20deg);
transform:rotate(20deg);
-webkit-transition:0.8s;
-moz-transition:0.8s;
}
input:hover{
-webkit-transform:rotate(360deg) scale(1.3);
-moz-transform:rotate(360deg) scale(1.3);
transform:rotate(360deg) scale(1.3);
box-shadow:10px 10px 15px #111;
}
</style>
<script type="text/javascript">
var snake=[];
var oDiv=null;
var oDivSize=600;
var dir="left";
var fruit=null;
var snakeWidth=20;
var t=null;
var aInp=null;
var aSpan=null;
window.onload=function(){
oDiv=document.getElementsByTagName('div')[0];
aInp=document.getElementsByTagName('input');
aSpan=document.getElementsByTagName('span')[0];
oDiv.style.position='absolute';
oDiv.style.width=oDivSize+'px';
oDiv.style.height=oDivSize+'px';
oDiv.style.left ='350px';
oDiv.style.top ='20px';
oDiv.style.border = "15px solid rgba(35,150,60,1)";
init(500);
// t=setInterval(move,interval);
aInp[0].onclick=function(){
var first = snake[0];
var fruitLeft = parseInt(fruit.style.left);
var fruitTop = parseInt(fruit.style.top);
var firstLeft = parseInt(first.style.left);
var firstTop = parseInt(first.style.top);
if(!(firstLeft == - snakeWidth || firstLeft == oDivSize|| firstTop == - snakeWidth || firstTop == oDivSize)){
clearInterval(t);
t=setInterval(move,600);
for(var i=0;i<aInp.length;i++){
aInp[i].style.background='';
}
this.style.background="#ae5";
}
}
aInp[1].onclick=function(){
var first = snake[0];
var fruitLeft = parseInt(fruit.style.left);
var fruitTop = parseInt(fruit.style.top);
var firstLeft = parseInt(first.style.left);
var firstTop = parseInt(first.style.top);
if(!(firstLeft == - snakeWidth || firstLeft == oDivSize|| firstTop == - snakeWidth || firstTop == oDivSize)){
clearInterval(t);
t=setInterval(move,280);
for(var i=0;i<aInp.length;i++){
aInp[i].style.background='';
}
this.style.background="#f66";
}
}
aInp[2].onclick=function(){
var first = snake[0];
var fruitLeft = parseInt(fruit.style.left);
var fruitTop = parseInt(fruit.style.top);
var firstLeft = parseInt(first.style.left);
var firstTop = parseInt(first.style.top);
if(!(firstLeft == - snakeWidth || firstLeft == oDivSize|| firstTop == - snakeWidth || firstTop == oDivSize)){
clearInterval(t);
t=setInterval(move,150);
for(var i=0;i<aInp.length;i++){
aInp[i].style.background='';
}
this.style.background="lightblue"
}
}
aInp[3].onclick=function(){
var first = snake[0];
var fruitLeft = parseInt(fruit.style.left);
var fruitTop = parseInt(fruit.style.top);
var firstLeft = parseInt(first.style.left);
var firstTop = parseInt(first.style.top);
if(!(firstLeft == - snakeWidth || firstLeft == oDivSize|| firstTop == - snakeWidth || firstTop == oDivSize)){
clearInterval(t);
t=setInterval(move,100);
for(var i=0;i<aInp.length;i++){
aInp[i].style.background='';
}
this.style.background="red";
}
}
aInp[4].onclick=function(){
var first = snake[0];
var fruitLeft = parseInt(fruit.style.left);
var fruitTop = parseInt(fruit.style.top);
var firstLeft = parseInt(first.style.left);
var firstTop = parseInt(first.style.top);
if(!(firstLeft == - snakeWidth || firstLeft == oDivSize|| firstTop == - snakeWidth || firstTop == oDivSize)){
clearInterval(t);
t=setInterval(move,50);
for(var i=0;i<aInp.length;i++){
aInp[i].style.background='';
}
this.style.background="#fd5"
}
}
}
function move(){
for(var i=snake.length-1;i>0;i--){
var s = snake[i];
var s2 = snake[i-1];
s.style.left = s2.style.left ;
s.style.top = s2.style.top ;
}
var ss = snake[0];
if(dir == "left"){
ss.style.left = (parseInt(ss.style.left) - snakeWidth) + "px";
} else if(dir == "right"){
ss.style.left = (parseInt(ss.style.left) + snakeWidth) + "px";
} else if(dir == "top"){
ss.style.top = (parseInt(ss.style.top) - snakeWidth) + "px";
}else if(dir == "buttom"){
ss.style.top = (parseInt(ss.style.top) + snakeWidth) + "px";
}
check();
}
function init(appear){
var oneSnake=create(appear,snakeWidth * 2);
snake.push(oneSnake);
oDiv.appendChild(oneSnake);
var oneSnake=create(appear+snakeWidth,snakeWidth * 2);
snake.push(oneSnake);
oDiv.appendChild(oneSnake);
var oneSnake=create(appear+snakeWidth+snakeWidth,snakeWidth * 2);
snake.push(oneSnake);
oDiv.appendChild(oneSnake);
addFruit();
}
function create(left,top){
var div=document.createElement('div');
div.style.position='absolute';
div.style.left=left+'px';
div.style.top=top+'px';
div.style.width=snakeWidth+'px';
div.style.height=snakeWidth+'px';
div.style.border = "1px solid aqua";
return div;
}
function addFruit(){
var left=Math.floor(Math.random()*(oDivSize/snakeWidth))*snakeWidth;
var top=Math.floor(Math.random()*(oDivSize/snakeWidth))*snakeWidth;
for(i=0;i<snake.length;i++){
var s=snake[i];
if(left == parseInt(s.style.left)&&parseInt(top == s.style.top)){
addFruit();
return;
}
}
fruit=create(left,top);
oDiv.appendChild(fruit);
}
function onKeyupEvent(event){
var code = event.keyCode
if(code == 38 && dir != "buttom"){
dir = "top";
} if(code == 40 && dir != "top"){
dir = "buttom";
}if(code == 37 && dir != "right"){
dir = "left";
}if(code == 39 && dir != "left"){
dir = "right";
}
check();
}
function check(){
var num=0;
var first = snake[0];
var fruitLeft = parseInt(fruit.style.left);
var fruitTop = parseInt(fruit.style.top);
var firstLeft = parseInt(first.style.left);
var firstTop = parseInt(first.style.top);
if(firstLeft == - snakeWidth || firstLeft == oDivSize|| firstTop == - snakeWidth || firstTop == oDivSize){
clearInterval(t);
var M=confirm('撞到墙上了,游戏结束!
是否又一次開始?');
if(M){
document.location.reload();
}else{
return false;
}
return;//检验是否撞到墙上了
}
for(var i = 1; i < snake.length ; i++){
var s = snake[i];
if(firstLeft == parseInt(s.style.left) && firstTop == parseInt(s.style.top)){
clearInterval(t);
var N=confirm('撞到自己了,游戏结束!
是否又一次開始?')
if(N){
document.location.reload();
}else{
return false;
}
return; //检验是否撞到自身
}
}
if((dir == "buttom" && (fruitTop - firstTop == snakeWidth) && fruitLeft == firstLeft) ||
(dir == "top" && (fruitTop - firstTop == -snakeWidth) && fruitLeft == firstLeft) ||
(dir == "right" && (fruitLeft - firstLeft == snakeWidth) && fruitTop == firstTop)||
(dir == "left" && (fruitLeft - firstLeft == -snakeWidth) && fruitTop == firstTop)){
var temp = [];
temp.push(fruit);
snake = temp.concat(snake);//吃了
num++;
addFruit();
}
}
</script>
</head>
<style type="text/css">
img{
position:absolute;
left:1100px;
top:100px;
}
</style>
<body onkeyup="onKeyupEvent(event)" style="background:rgba(50,50,50,1)">
<font color="pink" size="5" style='position:absolute;left:1000;top:300'>利用键盘上的↑ ↓ ← → 来控制方向 </font>
<a href="../index.html"><img border="0" src="./image/4.jpg" alt="主页面" title="返回主页面"></a>
<div></div>
<ul>
<li><h1><input type="button" value="開始"></h1></li>
<li><input type="button" value="简单"></li>
<li><input type="button" value="0基础"></li>
<li><input type="button" value="一般"></li>
<li><input type="button" value="困难"></li>
</ul>
<span style="font-size:25px;color:#ae5;font-family:微软雅黑;margin-left:30px;position:absolute;left:10px;bottom:40px;";>请选择难度開始游戏<br>游戏过程中能够随时难度</span>
<br><br><br><br><br><br><br><br><br>
<p></p>
<h2 align=right><font color="blue">
<body onload="show()"> <!-- 网页载入时调用一次 以后就自己主动调用了-->
<font size="5" color="pink"> Design By:</font><a href="http://blog.csdn.net/qq_21792169/article/category/5907695" target="_blank"><font size="5" color="pink">网络人VS灰鸽子工作室</font></a>
<div id="nowDiv"></div> </body>
</font></h2>
</body>
</html>
最新文章
- 熊乐:H3 BPM为加速企业流程管理提供源动力
- EBS 中经常用到的一些值集
- 7、provider: SQL 网络接口, error: 26 - 定位指定的服务器/实例时出错
- Windows安装 ANT
- apache配置文件 httpd-vhosts.conf 和 htaccess
- 网上下载的 java开源项目 如何 打jar包
- 从运行原理及使用场景看Apache和Nginx
- 制作安装包工具NSIS
- Atitit.软件guibuttonand面板---os区-----linux windows搜索文件 目录
- tomcat管理员manager app无法进入解决方法
- 使用jstl 截取字符串
- QtQuick桌面应用程序开发指导 3)达到UI而功能_B 4)动态管理Note物_A
- 熟练使用NTFS的文件链接技术
- C第九次博客作业--指针
- IntelliJ IDE 常用配置
- Unity物品栏、商城3D物品的显示插件
- 解决虚拟机vmware虚拟机安装64位系统“此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态”的问题
- 【转】【Mac】invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library
- happypack 进一步 优化 build速率
- 微信小程序—day01