小游戏(锅打灰太狼)

  今天分享一个自己前几天做的小游戏,是在无参考任何代码的情况下实现的。大概花了几个小時,后来因为出现bug和修改样式适应移动端等,就磕磕碰碰又搞了半天。虽然这些不是很难,但是发现做小游戏挺有趣的。比做网页有意思多了,IE你滚 ̄□ ̄||

  游戏截图:

  

  HTML:  

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="utf-8" />
 5         <meta content="width=device-width,user-scalable=no" name="viewport" id="viewport">
 6         <link rel="stylesheet" type="text/css" href="css/main.css"/>
 7         <title></title>
 8     </head>
 9     <body>
10         <div id="wrap">
11             <div id="score">000</div>
12             <div id="time">250.00 秒</div>
13             <div id="hole1" class="holes"></div>
14             <div id="hole2" class="holes"></div>
15             <div id="hole3" class="holes"></div>
16             <div id="hole4" class="holes"></div>
17             <div id="hole5" class="holes"></div>
18             <div id="hole6" class="holes"></div>
19             <div id="hole7" class="holes"></div>
20             <div id="hole8" class="holes"></div>
21             <div id="hole9" class="holes"></div>
22             <!--开始覆盖-->
23             <div id="cover"></div>
24             <div id="loading">
25                 <img src="img/loading.gif" alt="" />
26             </div>
27             <div id="start">
28                 <button id="beginGame">开始游戏</button>
29             </div>
30             <div id="end">
31                 <div class="end_txt">GAME OVER!</div>
32                 <div class="end_score">
33                     你的得分是:<span id="myScore"></span>
34                 </div>
35                 <div class="end_btns">
36                     <button id="playAgain">重新开始</button>
37                     <button id="keepPlay">继续游戏</button>
38                 </div>
39             </div>
40             <button id="stopGame">暂停游戏</button>
41         </div>
42
43         <script src="js/main.js" type="text/javascript" charset="utf-8"></script>
44         <script src="js/touch.js" type="text/javascript" charset="utf-8"></script>
45     </body>
46 </html>

  CSS:

  1 body,ul,li,input,button,img{
  2     padding: 0;
  3     margin: 0;
  4 }
  5 html,body{
  6     height: 100%;
  7     width: 100%;
  8 }
  9 body{
 10     background: #f06060;
 11     font: 12px/1 Tahoma, Helvetica, Arial, "\5b8b\4f53", sans-serif;
 12 }
 13 img{
 14     border: none;
 15 }
 16 li{
 17     list-style: none;
 18 }
 19 a{
 20     text-decoration: none;
 21 }
 22 a:hover {
 23     text-decoration: underline;
 24 }
 25 button{
 26     border: none;
 27     outline: none;
 28     cursor: pointer;
 29     border-radius: 5%;
 30     background: #F06060;
 31     color: #fff;
 32 }
 33 /*wrap*/
 34 #wrap{
 35     position: relative;
 36     left: 0;
 37     top: 0;
 38     width: 100%;
 39     height: 100%;
 40     background: url(../img/game_bg.jpg) no-repeat;
 41     background-size:100% 100%;
 42 }
 43 #cover{
 44     position: absolute;
 45     width: 100%;
 46     height: 100%;
 47     background: #000;
 48     opacity: 0.6;
 49     z-index: 20;
 50     left: 0;
 51     top: 0;
 52 }
 53 #loading {
 54     position: absolute;
 55     width: 10%;
 56     left: 45%;
 57     top: 46.666%;
 58     z-index: 30;
 59 }
 60 #loading img{
 61     width: 100%;
 62 }
 63 #start {
 64     display: none;
 65     position: absolute;
 66     width: 30%;
 67     height:6%;
 68     left:35%;
 69     top: 47%;
 70     z-index: 30;
 71 }
 72 #beginGame{
 73     width: 100%;
 74     height: 100%;
 75     font-size: 1.2em;
 76     line-height: 2;
 77 }
 78 #stopGame{
 79     display: none;
 80     position: absolute;
 81     left: 35%;
 82     bottom: 2%;
 83     width: 30%;
 84     height: 6%;
 85     font-size: 1.2em;
 86     line-height: 2;
 87 }
 88 #end{
 89     display: none;
 90     position: absolute;
 91     width: 50%;
 92     height: 30%;
 93     left: 25%;
 94     top: 35%;
 95     z-index: 40;
 96 }
 97 #beginGame:hover{
 98     background: #fa6a6a;
 99 }
100 .holes{
101     display: none;
102     cursor: pointer;
103 }
104 .end_txt{
105     text-align: center;
106     height: 20%;
107     line-height: 1;
108     font-size: 1.8em;
109     font-weight: bold;
110     color: #F06060;
111 }
112 .end_score{
113     height:20%;
114     width: 90%;
115     padding-left: 10%;
116     line-height: 1.5;
117     color: #fff;
118     margin-top: 10%;
119 }
120 .end_btns{
121     width: 100%;
122     height: 20%;
123     margin-top: 10%;
124 }
125 .end_btns button{
126     height: 100%;
127     width: 45%;
128     line-height: 2.4;
129 }
130 .end_btns button:hover{
131     background: #fa6a6a;
132 }
133 #playAgain{
134     margin-right: 5%;
135 }
136 #score{
137         position: absolute;
138         color: #fff;
139         font-size: 1.43em;
140         top: 0;
141         width: 80%;
142         left: 20%;
143         height: 9.17%;
144         line-height: 2.564;
145     }
146 #time{
147         color: #fff;
148         font-size: 1.17em;
149         width: 72%;
150         left: 28%;
151         line-height: 1.426;
152         height: 4.17%;
153         position: absolute;
154         top: 13.3%;
155     }
156 .holes{
157         width: 33.75%;
158         height: 21.04%;
159         position: absolute;
160     }
161 #hole1{
162         left: 30%;
163         top: 23.96%;
164     }
165 #hole2{
166         left: 5%;
167         top:33.33%;
168     }
169 #hole3{
170         left: 58.13%;
171         top: 29.58%;
172     }
173 #hole4{
174         left: 4.875%;
175         top: 45.83%;
176     }
177 #hole5{
178         left: 31.875%;
179         top: 39.583%;
180     }
181 #hole6{
182         left: 61.875%;
183         top:43.75%;
184     }
185 #hole7{
186         left: 8.75%;
187         top: 61.042%;
188     }
189 #hole8{
190         left: 36.25%;
191         top: 56.67%;
192     }
193 #hole9{
194         left: 63.75%;
195         top: 61.458%;
196     }
197
198 @media  screen and (min-width: 768px) {
199     #wrap{
200         width: 320px;
201         height: 480px;
202         left: 50%;
203         margin-left: -160px;
204     }
205 }

展开CSS

  Javascript: 

  1 //获取元素
  2     var score = document.getElementById(‘score‘);
  3     var time = document.getElementById("time");
  4     var cover = document.getElementById(‘cover‘);
  5     var start = document.getElementById("start");
  6     var end = document.getElementById(‘end‘);
  7     var beginGame = document.getElementById("beginGame");
  8     var keepPlay =  document.getElementById("keepPlay");
  9     var playAgain = document.getElementById(‘playAgain‘);
 10     var stopGame =  document.getElementById("stopGame");
 11     var myScore = document.getElementById(‘myScore‘);
 12     var loading = document.getElementById("loading");
 13     var initScore = 000;//初始分数
 14     var showTimer = null;//初始化定时器
 15     var hideTimer = null;
 16     var imgArr = ["h0","h1","h2","h3","h4","h5","h6","h7","h8","h9","x0","x1","x2","x3","x4","x5","x6","x7","x8","x9"];//图片数组
 17 //点击开始
 18     beginGame.onclick = beginFn;
 19     function beginFn(){
 20             cover.style.display = "none";
 21             start.style.display =  "none";
 22             end.style.display = "none";
 23             stopGame.style.display = "block";
 24             setTime();//开启计时器
 25             var t1 = 900;            //狼出现间隔
 26             iniTimer =  setInterval(
 27                 function(){
 28                     var roleRan = Math.random();
 29                     if(roleRan<0.3){
 30                         showWolf("x");//小狼出现几率
 31                         beatRole("x");
 32                     }else{
 33                         showWolf("h");//大狼出现几率
 34                         beatRole("h");
 35                     }
 36             },t1);
 37     }
 38 //预加载图片
 39     window.onload = loadingFn;
 40     function loadingFn(){
 41         var index = 0;
 42         for (var i = 0; i < imgArr.length; i++) {
 43             var img = new Image();
 44             img.src ="img/" + imgArr[i]+".png";
 45             img.onload = function  () {
 46                 index++;
 47                 if (index==imgArr.length) {
 48                     loading.style.display = "none";
 49                     start.style.display = "block";
 50                 }
 51             };
 52         }
 53     };
 54 //暂停
 55     stopGame.onclick = stopFn;
 56     function stopFn(){
 57         clearInterval(Remain);
 58         clearInterval(iniTimer);
 59         if(!showTimer){//判断是否运行完再清定时器,否则出错,下同
 60             clearInterval(showTimer);
 61         }
 62         if(!hideTimer){
 63             clearInterval(hideTimer);
 64         }
 65         cover.style.display = "block";
 66         end.style.display = "block";
 67         stopGame.style.display = "none";
 68         myScore.innerHTML = initScore;
 69     }
 70 //继续游戏
 71      keepPlay.onclick = function(){
 72          initFn();
 73          beginFn();
 74      }
 75 //清空背景
 76      function initFn(){
 77          for(var i= 1;i<10;i++){
 78             var holes = "hole"+i;
 79             var holeDiv = document.getElementById(holes);
 80             holeDiv.style.background = "none";
 81         }
 82      }
 83  //重新开始
 84      playAgain.onclick = function (){
 85          remainT = 250;//重置时间
 86          initScore =000;//重置分数
 87          score.innerHTML = initScore;
 88          myScore.innerHTML = initScore;
 89          initFn();
 90          beginFn();
 91      }
 92 //计时器
 93     var remainT = 250;//游戏时长
 94     function setTime (){
 95          Remain = setInterval(function (){
 96             remainT-=0.05;
 97             shortT = remainT.toFixed(2);
 98             time.innerHTML = shortT + " 秒";
 99             if(shortT==0){
100                 //结束清空所有定时器
101                 clearInterval(Remain);
102                 clearInterval(iniTimer);
103                 clearInterval(showTimer);
104                 clearInterval(hideTimer);
105                 cover.style.display = "block";
106                 end.style.display = "block";
107             }
108         },50);
109     }
110 //击打
111     function beatRole(r){
112         for(var i= 1;i<10;i++){
113             var holes = "hole"+i;
114             var holeDiv = document.getElementById(holes);
115             holeDiv.index = 0;//判断是否点击
116             //touch.js,手机上触摸触发事件
117             touch.on(holeDiv, ‘tap‘, function(ev){
118                 if((holeDiv.style.display=="block" || holeDiv.style.background !="none")&& holeDiv.index==0){
119                     for(var j=6;j<10;j++){
120                         holeDiv.style.background = "url(img/" + r + j + ".png)";
121                     }
122                     if(r=="h"){//灰太狼
123                         initScore += 10;
124                     }else if(r=="x" && initScore!=0){//小狼
125                         initScore-=10;
126                     }
127                     score.innerHTML = initScore;
128                     this.index=1;//防止重复点击
129                 }
130             });
131             //鼠标点击也可以
132             holeDiv.onclick = function (){
133                 if((this.style.display=="block" || this.style.background !="none")&&this.index==0){  //已显示且背景不为空,和未点击过的
134                     for(var j=6;j<10;j++){
135                         this.style.background = "url(img/" + r + j + ".png)";
136                     }
137                     if(r=="h"){//灰太狼
138                         initScore += 10;
139                     }else if(r=="x" && initScore!=0){//小狼
140                         initScore-=10;
141                     }
142                     score.innerHTML = initScore;
143                     this.index=1;//防止重复点击
144                 }
145             }
146         }
147     }
148 //出现,r代表出现的角色
149     function showWolf(r){
150         var i = 0;
151         var ranNum = Math.floor(Math.random()*9)+1;//获取1到9的随机数
152         var ranId = "hole" + ranNum ;//divID
153         var showDiv = document.getElementById(ranId);
154         showDiv.style.display = "block";
155         showTimer = setInterval(
156             function(){
157                 var imgSrc = "url(img/"+ r + i +".png)";
158                 if(i<6){
159                     showDiv.style.background = imgSrc;
160                     i++;
161                 }
162                 if(i==6){
163                     clearInterval(showTimer);
164                     hideWolf();
165                 }
166         },30);
167         //隐藏
168         function hideWolf(){
169             setTimeout(
170                 function(){
171                     var j=5;
172                     hideTimer = setInterval(
173                         function(){
174                             var imgSrc = "url(img/"+ r + j +".png)";
175                             if(j<6){
176                                 showDiv.style.background = imgSrc;
177                                 j--;
178                             }
179                             if(j<0){
180                                 showDiv.style.background = "none";
181                                 showDiv.style.display = "none";
182                                 clearInterval(hideTimer);
183                             }
184                     },30);
185             },1000);
186         }
187
188     }

  说明: 1、本游戏使用了百度云的touch.js触摸事件库,要引入touch.js文件。在js代码的117行到130行我绑定了手机的tap事件,不需要的可删去;

      2、以上代码均为原创,仅供学习与交流。如需转载,请注明作者与出处,谢谢。

时间: 2024-08-30 01:28:27

小游戏(锅打灰太狼)的相关文章

js选择颜色小游戏(随机生成不含重复数字的数组,通过数组中的数控制定义好的数组)

<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>js网页版小游戏</title> <style media="screen"> .wrap { width: 577px; outline: 1px solid hotpink; margin: 100px auto; box-shadow: 0 0 5px; } .

使用AxureRP7.0制作经典小游戏"大家来找茬"

本案例是<网站蓝图AxureRP7.0从入门到精通视频教程>中的最后一节,适用于对Axure基础知识掌握比较熟练的同学:教程由axure原型库网站录制,转载请注明出处!相信很多刚接触Axure或者学习了一段时间但并没有深入的同学们,看到这个案例一定很惊讶,使用Axure竟然能做出如此逼真的交互效果!通过此案例也可以激发广大同学们学习Axure的热情和信心!试想一下,如果你有情侣的话,把你们珍藏的合影拿出来处理几张,做成大家来找茬的小游戏,不但锻炼了自己的技能,还能给对方一个惊喜,岂不是一举两得

使用AxureRP7.0制作经典数独小游戏原型,axure游戏原型下载

之前,有同学在Q群中提问,如何使用axure制作经典数独解谜小游戏,当时由于时间关系没有来得及亲手制作,而是给同学们提供了Axure6.5版本的一个数独解谜游戏的原型,本教程由axure原型库网站录制,转载请注明出处!但是那个原型做的太过繁杂,所以仅供大家参考交流:在此,金乌老师特地抽时间给同学们使用AxureRP7.0制作了一下,感觉对实战逻辑分析和axure变量的掌握比较有考验,所以就放出来供大家学习交流使用. 在学习的过程中,如果你仅凭自己现有的对axure的掌握,无法准确分析并组织出原型

【H5小游戏开发教程】如何限制微信游戏只能在微信端打开?

在这行里接触的时间多了,就会发现很多有意思的东西. 比如,很多微信小游戏会限制只能在微信端打开,有木有? 有这样的, 也有这样的, 妈蛋,不能用PC访问,这游戏就没法扒呀..... 其实涛舅舅告诉你,这两种都可以扒 而且是用PC! 但是今天,我不教你扒皮 我要教你的是,怎么让你的微信游戏也能限制PC打不开 很想学吧  准备开始! 1.第一种不提了,因为人家是设置了微信授权登录,从微信那里就拦截住了,只能用微信访问,你可能弄不了这么高级的微信授权这块,如果你真能弄,这一讲你也不用听了,因为你已经能

Chrome自带恐龙小游戏的源码研究(完)

在上一篇<Chrome自带恐龙小游戏的源码研究(七)>中研究了恐龙与障碍物的碰撞检测,这一篇主要研究组成游戏的其它要素. 游戏分数记录 如图所示,分数及最高分记录显示在游戏界面的右上角,每达到100分就会出现闪烁特效,游戏第一次gameover时显示历史最高分.分数记录器由DistanceMeter构造函数实现,以下是它的全部代码: 1 DistanceMeter.dimensions = { 2 WIDTH: 10, //每个字符的宽度 3 HEIGHT: 13, //每个字符的高 4 DE

C语言小游戏设计报告

课程设计名称:贪吃蛇小游戏 专业班级:计科15-2 学号:150809229 姓名:XXX 一.设计目标 通过设计,培养学生对电脑的动手能力,使学生巩固<C语言程序设计>课程学习的内容,掌握编写程序的基本方法,强化对其的动手能力,可以独自完成程序的编写. 二.设计内容和要求 设计内容 编写贪吃蛇的小游戏,使其可以成功运行并且操作玩耍. 设计要求 1)源程序要有适当的注释,使程序便于阅读. 2)要有程序运行结果作为依据 三.程序流程 1.编写地图 运用函数,数组编写地图 2.相同的方法把蛇添加进

用Python写一个小游戏

刚学Python时间不长,但也知道了一点,看别人的参考写了一个猜数字小游戏,也算是禹学于乐吧. #!/usr/bin/env   python #coding=utf-8 import random secret = random.randint(1,100) guess,tries = 0,0 print u"已经给出了一个1-99的数字" while guess != secret and tries < 5: print u"请给出你猜的数字:" pri

2D命令行小游戏Beta1.0_代码

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _2D命令行小游戏Beta1_0 { class Game { public Game(int scale) { _scale = scale; peox = 1; peoy = 1; map = new int[_scale, _scale];

python小游戏之课堂提问器

今天,接着前边的python小游戏,又写了个课堂提问器小程序.供大家一乐! #coding:utf-8 from random import randint print '\033[1;32;40m', print '你好,请选择需要几位作答者?\n' print '请输入作答者的人数:', t=1 i=input() data=[] while t<=i:     r=randint(0,32)    # print m[r],     data.append(m[r])     t+=1 p