声明:本文为原创文章,如需转载,请注明来源WAxes,谢谢!

  过年的十八天假期迷迷糊糊一下子就过去了(LZ还是实习生,鉴于大学最后一个寒假了,所以就多请了好多天假),又要返工上班了。这是年后的第一篇博客了。其实就是水一下,毕竟不能冷落博客太久啊。

  这两天刚好抱着玩的心情写了个接水管游戏,本来用css3更容易写。。。就旋转那些东西,不过因为LZ比较喜欢canvas的写法,所以还是用了canvas来写。

  先上个DEMO吧:接水管   玩法很简单:点击水管块就可以旋转水管,连通水管后点击上面的水阀,然后就可以了。可能还有bug,LZ暂时没去测试了。

  因为代码也很简单,重点也不多,就先说下游戏思路:

  【游戏逻辑】

  游戏逻辑很简单,就是水管总共有4个面,给四个面一个代号:0,1,2,3;然后水管有两个口,一个进水口,一个出水口,就可以用0,1,2,3来表示水管的两个口(如果想写更复杂的,多个口的,也一个样,一个进水口,多个出水口,用数组保存就行了。我写的这个比较简单,只有两个口,所以后面判断连通性也比较容易。),保存好水管块的进出口数据后,当水管旋转时,相应改变水管进出口参数就行了。

  然后就是连通性判断:水管只有两个口的话就很简单了。一般有一个最初的进水口,然后就从那个水管块开始,先获取出水口,然后再遍历当前水管块周围的水管块的进水口是否跟当前水管块的出水口相连通(被遍历的水管块的进水口和出水口都要进行判断,因为水管可以旋转,也就是说进水口和出水口在一开始是无法确定的,当确定了连通关系后,就可以确定水管的进水口和出水口了,之后的循环也一样),如果对的上。就说明连通了。然后就跳到那个水管块,再进行那个水管块周围的水管块的遍历,就这样重复遍历,直到最后一个也就是出水的那个水管块也连通了,就说明整条水管连通了。

  上面的是只有两个口的水管连通性判断,如果游戏中有三个口的水管甚至四个口的水管,判断方式就没那么简单了。我写的是两个口的,不过也想了一下多口的判断,大概就是找到水管的进水口,因为是多个出水口,所以就得一个一个来,对每一条线路进行判断,每当跳转到一个新的水管块,就把之前的那个水管块保存为新水管块的父类水管。然后对新水管的出水口进行循环判断,如果没有发现有连通的水管,则跳转至父类水管,并将新水管块加入“此路不通”列表,再次遍历父类水管块的其他出水口进行判断,如此循环,当循环到父类为最初进水口的那个水管块的时候,就说明水管没有连通路线,当然,如果循环到新水管块为出口水管时,就说明连通了。

  楼主比较懒,所以就只写了两个口的,没写多个口的了。

  【代码部分】

  首先定义水管块对象:

  1. var Box = function(center , style , Gateway , angle , coordinate){
  2. this.center = center;
  3. this.style = style;
  4. this.angle = angle;
  5. this.endangle = angle;
  6. this.Gateway = Gateway;
  7. this.coordinate = coordinate;
  8. }
  9. Box.prototype = {
  10. constructor:Box,
  11. draw:function(){
  12. this.setHole();
  13.  
  14. if(this.angle!==this.endangle){
  15. canclick = false;
  16. this.rotate();
  17. }
  18.  
  19. var ext = [
  20. {x:0,y:-boxWidth/2},
  21. {x:boxWidth/2,y:0},
  22. {x:0,y:boxWidth/2},
  23. {x:-boxWidth/2,y:0},
  24. ]
  25. ctx.save();
  26. ctx.translate(this.center.x , this.center.y);
  27. ctx.rotate(this.angle);
  28. //画管道
  29. switch(this.style){
  30. case 0:break;
  31. case 1:ctx.drawImage(document.getElementById("pipe1") , -boxWidth/2 , -boxWidth/2 , boxWidth , boxWidth);
  32. break;
  33. case 2:ctx.drawImage(document.getElementById("pipe2") , -boxWidth/2 , -boxWidth/2 , boxWidth , boxWidth);
  34. break;
  35. case 3:ctx.drawImage(document.getElementById("start") , -boxWidth/2 , -boxWidth/2 , boxWidth , boxWidth);
  36. break;
  37. }
  38. ctx.restore();
  39. },
  40. rotate:function(){
  41. if(Math.abs(this.endangle-this.angle)<=0.01){
  42. canclick = true;
  43. this.endangle = this.endangle>=2*Math.PI?0:this.endangle;
  44. this.angle = this.endangle;
  45.  
  46. if(this.style===3){
  47. var result = connectPipes();
  48. if(result){
  49. // alert('成功连通')
  50. connectSuccess = true;
  51. }
  52. else {
  53. alert("游戏失败")
  54. window.location.reload();
  55. }
  56. }
  57. }
  58. else {
  59. this.angle += (this.endangle-this.angle)*0.2;
  60. }
  61. },
  62. setHole:function(){
  63. if(this.style===1){
  64. var zl = this.endangle/(0.5*Math.PI);
  65. var initHole1 = 0 , initHole2 = 2;
  66. this.inHole = (initHole1+zl)>=4?((initHole1+zl)-4):(initHole1+zl);
  67. this.outHole = (initHole2+zl)>=4?((initHole2+zl)-4):(initHole2+zl);
  68. }
  69. else if(this.style===2){
  70. var zl = this.endangle/(0.5*Math.PI);
  71. var initHole1 = 1 , initHole2 = 2;
  72. this.inHole = (initHole1+zl)>=4?((initHole1+zl)-4):(initHole1+zl);
  73. this.outHole = (initHole2+zl)>=4?((initHole2+zl)-4):(initHole2+zl);
  74. }
  75. }
  76. }

对象主要属性包括:水管位置,水管种类(主要就两个,1为直线的,2为九十度折角的),水管是否可以旋转的判定,水管旋转的初始角度,以及水管所处位置的行和列。

如何让水管点击后旋转,其实也很简单,就是先把画布平移到水管块中心,然后旋转,然后再把水管画出来,就旋转好了。

然后就是判断水管连通性的代码:

  1. //判断水管连通性
  2. function connectPipes(){
  3. var index = 0;
  4. while(1){
  5. var result = getHole(boxes[index]);
  6. if(boxes[index+result.nextBox]){
  7. if(result.hole===boxes[index+result.nextBox].inHole){
  8. index = index+result.nextBox;
  9. }
  10. else if(result.hole===boxes[index+result.nextBox].outHole){
  11. var num = boxes[index+result.nextBox].inHole;
  12. boxes[index+result.nextBox].inHole = result.hole;
  13. boxes[index+result.nextBox].outHole = num;
  14. index = index+result.nextBox;
  15. }
  16. else {
  17. break;
  18. }
  19. }
  20. else {
  21. break;
  22. }
  23. }
  24. if(index===boxes.length-1) return true;
  25. else return false;
  26. }
  27.  
  28. function getHole(box){
  29. var hole="0";
  30. var nextBox=0;
  31. switch(box.outHole){
  32. case 0 : hole = 2;
  33. nextBox = -cols;
  34. break;
  35. case 1 : hole = 3;
  36. if(box.coordinate.cols===cols-1){
  37. nextBox = 1000000;
  38. }
  39. else nextBox = 1;
  40. break;
  41. case 2 : hole = 0;
  42. nextBox = cols;
  43. break;
  44. case 3 : hole = 1;
  45. if(box.coordinate.cols===0){
  46. nextBox = 1000000;
  47. }
  48. else nextBox = -1;
  49. break;
  50. }
  51. return {hole:hole , nextBox:nextBox};
  52. }

逻辑之前已经说过了,而且代码也比较简单,就不解释了,getHole是返回当前水管块的出水口如果要连通,需要的进水口的参数以及水管在水管数组里的位置。

  然后就是路径,如何保证一定有条成功的路呢?因为如果管道全部随机的话,可能会陷入死路。所以,我就干脆定好几条正确的路径,每次刷新页面就取其中一条,其他水管块也添加一些随机出来的水管作干扰。所以,我就专门用一个data.js文件来存放所有的路径,路径文件代码如下:

  1. //水管路径
  2. var allPath = [
  3. [
  4. {rows:0,cols:0,style:1},
  5. {rows:1,cols:0,style:1},
  6. {rows:2,cols:0,style:1},
  7. {rows:3,cols:0,style:2},
  8. {rows:3,cols:1,style:2},
  9. {rows:2,cols:1,style:2},
  10. {rows:2,cols:2,style:1},
  11. {rows:2,cols:3,style:1},
  12. {rows:2,cols:4,style:1},
  13. {rows:2,cols:5,style:1},
  14. {rows:2,cols:6,style:1},
  15. {rows:2,cols:7,style:2},
  16. {rows:3,cols:7,style:1},
  17. ],
  18.  
  19. [
  20. {rows:0,cols:0,style:1},
  21. {rows:1,cols:0,style:1},
  22. {rows:2,cols:0,style:2},
  23. {rows:2,cols:1,style:2},
  24. {rows:1,cols:1,style:2},
  25. {rows:1,cols:2,style:2},
  26. {rows:2,cols:2,style:2},
  27. {rows:2,cols:3,style:1},
  28. {rows:2,cols:4,style:1},
  29. {rows:2,cols:5,style:1},
  30. {rows:2,cols:6,style:2},
  31. {rows:1,cols:6,style:2},
  32. {rows:1,cols:7,style:2},
  33. {rows:2,cols:7,style:1},
  34. {rows:3,cols:7,style:1},
  35. ],
  36.  
  37. [
  38. {rows:0,cols:0,style:1},
  39. {rows:1,cols:0,style:1},
  40. {rows:2,cols:0,style:1},
  41. {rows:3,cols:0,style:2},
  42. {rows:3,cols:1,style:2},
  43. {rows:2,cols:1,style:1},
  44. {rows:1,cols:1,style:2},
  45. {rows:1,cols:2,style:1},
  46. {rows:1,cols:3,style:1},
  47. {rows:1,cols:4,style:1},
  48. {rows:1,cols:5,style:2},
  49. {rows:1,cols:5,style:2},
  50. {rows:2,cols:5,style:2},
  51. {rows:2,cols:6,style:1},
  52. {rows:2,cols:7,style:2},
  53. {rows:3,cols:7,style:1},
  54. ],
  55.  
  56. ]

路径的每一个点包含的参数就是行列位置以及水管的类型,如果想游戏更多变化,可以再多加几条路径,我就只弄了三条啦:下面是取路径然后生成相对应的水管,同时把水管的旋转角度也随机。

  1. var n = getRandom(0 , allPath.length-1);
  2. var path = allPath[n];
  3. path.foreach(function(){
  4. var index = this.rows*cols + this.cols;
  5. if((this.rows==0&&this.cols==0)||(this.rows==(rows-1)&&this.cols==(cols-1))){
  6. boxes[index] = new Box({x:(this.cols*boxWidth)+boxWidth/2+jiange , y:(this.rows*boxWidth)+boxWidth/2+marginTop} , this.style , true , 0 , {rows:this.rows , cols:this.cols});
  7. }
  8. else if(this.rows>0&&this.rows<3&&this.cols>2&&this.cols<5){
  9. boxes[index] = new Box({x:(this.cols*boxWidth)+boxWidth/2+jiange , y:(this.rows*boxWidth)+boxWidth/2+marginTop} , this.style , true , 0.5*Math.PI , {rows:this.rows , cols:this.cols});
  10. }
  11. else{
  12. boxes[index] = new Box({x:(this.cols*boxWidth)+boxWidth/2+jiange , y:(this.rows*boxWidth)+boxWidth/2+marginTop} , this.style , false , parseInt(getRandom(0,3))*0.5*Math.PI , {rows:this.rows , cols:this.cols});
  13. }
  14. });

然后就木有啦。。。。

源码地址:https://github.com/whxaxes/canvas-test/tree/gh-pages/src/Game-demo/connectPipe

用canvas写个接水管小游戏的更多相关文章

  1. canvas写个简单的小游戏

    之前在HTML5 Canvas属性和方法汇总一文中,介绍过Canvas的各种属性以及方法的说明,并列举了自己写的一些Canvas demo,接下来开始写一个简单的小游戏吧,有多简单,这么说吧,代码不到 ...

  2. 每个人都可以用C语言写的推箱子小游戏!今天你就可以写出属于自己项目~

    C语言,作为大多数人的第一门编程语言,重要性不言而喻,很多编程习惯,逻辑方式在此时就已经形成了.这个是我在大一学习 C语言 后写的推箱子小游戏,自己的逻辑能力得到了提升,在这里同大家分享这个推箱子小游 ...

  3. html5+Canvas实现酷炫的小游戏

    最近除了做业务,也在尝试学习h5和移动端,在这个过程中,学到了很多,利用h5和canvas做了一个爱心鱼的小游戏.点这里去玩一下 PS: 貌似有点闪屏,亲测多刷新两下就好了==.代码在本地跑都不会闪, ...

  4. python写的battle ship小游戏 - 1.0

    最近学python,这是今天写的一个小游戏. from random import randint class Board(object): board = [] def __init__(self, ...

  5. 10分钟用scratch写一个大鱼吃小鱼的小游戏

    第一次给张江小朋友教Scratch课程之前,还在担心一些概念能不能向小朋友解释清楚,可上完课发现,我严重低估了小朋友的聪明程度,发现现在的孩子相比较自己8.9岁的时候,简直聪明太多倍了. 所以总结了半 ...

  6. 发个2012年用java写的一个控制台小游戏

    时间是把杀狗刀 突然发现了12年用java写的控制台玩的一个文字游戏,有兴趣的可以下载试试哈汪~ 里面难点当时确实遇到过,在计算倒计时的时候用了多线程,当时还写了好久才搞定.很怀念那个时间虽然不会做游 ...

  7. linux下用gtk+写比赛赌博GUI小游戏

    游戏界面全部由gtk的GUI完成,没有使用openGL之类的高端货. 游戏玩法就是8位选手比赛跑步,你可以在赛前赌哪位选手会赢,如果输了cash会被扣除,反之cash会增加. 无聊写了3个选项:小数时 ...

  8. python Tkinter 写一个弹球的小游戏

    #!usr/bin/python #-*- coding:utf-8 -*- from Tkinter import * import Tkinter import random import tim ...

  9. js+canvas 一只一担小游戏

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

随机推荐

  1. asp.net core 自定义404等友好错误页面

    Home控制器里: [Route("Home/Error/{statusCode}")] public IActionResult Error(int statusCode) { ...

  2. CentOS6.9安装HDFS

    1.安装依赖包 yum install -y gcc openssh-clients 2.升级glib2.14 升级glibc-2.14用到的rpm 下载地址:https://pan.baidu.co ...

  3. html5 之 local storage \sessjion storage

    转载: HTMl5的sessionStorage和localStorage html5中的Web Storage包括了两种存储方式:sessionStorage和localStorage. sessi ...

  4. ionic2中使用moment.js

    安装 npm i moment --save 使用 import { Pipe, PipeTransform } from '@angular/core'; import Moment from 'm ...

  5. github的pull request是指什么意思

    有一个仓库,叫Repo A.你如果要往里贡献代码,首先要Fork这个Repo,于是在你的Github账号下有了一个Repo A2,.然后你在这个A2下工作,Commit,push等.然后你希望原始仓库 ...

  6. windows server远程连接提示“终端服务器超出了最大允许连接”

  7. Python 私有属性

    #!/usr/bin/env python # -*- coding:utf-8 -*- # 作者:Presley # 邮箱:1209989516@qq.com # 时间:2018-08-05 # 类 ...

  8. 函数防抖 debounce

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. datetime库运用

    1. date(),time(),datetime() 时间数据概用: 2. datetime.datetime.now() 获取当前时间 datetime.datetime.utcnow() 获取格 ...

  10. 最短路(bellman)-hdu2066

    题目链接:https://vjudge.net/problem/HDU-2066 题目描述: 代码实现: #include <cstdio> #include <cstring> ...