HTML小游戏2

Wesley13
• 阅读 623

一.“围住神经猫” 新建JS:app.js, js代码 var stage = new createjs.Stage("gameView"); createjs.Ticker.setFPS(30); createjs.Ticker.addEventListener("tick",stage); var gameView = new createjs.Container(); gameView.x = 30; gameView.y = 30; stage.addChild(gameView); var circleArr = [[],[],[],[],[],[],[],[],[]]; var currentCat;//保存这只猫 var MOVE_NONE = -1,MOVE_LEFT = 0,MOVE_UP_LEFT = 1,MOVE_UP_RIGHT = 2,MOVE_RIGHT = 3,MOVE_DOWN_RIGHT = 4,MOVE_DOWN_LEFT = 5; //6个方向的参数 function getMoveDir(cat){ //分别判断它能走的位置 var distanceMap = []; //left var can = true; for (var x = cat.indexX;x>=0;x--) { if(circleArr[x][cat.indexY].getCircleType() == Circle.TYPE_SELECTED){ can = false; distanceMap[MOVE_LEFT] = cat.indexX - x; break; } }

if(can){
    return MOVE_LEFT;
}
//left up
can =true;
var x = cat.indexX , y = cat.indexY;
while(true){
    if(circleArr[x][y].getCircleType() == Circle.TYPE_SELECTED){
        can = false;
        distanceMap[MOVE_UP_LEFT] = can.indexY-y;
        break;
    }
    if(y%2 == 0){
        x--;
    }
    y--;
    if(y<0 ||x<0){
        break;
    }
}
if(can){
    return MOVE_UP_LEFT;
}
//right up
can =true;
var x = cat.indexX , y = cat.indexY;
while(true){
    if(circleArr[x][y].getCircleType() == Circle.TYPE_SELECTED){
        can = false;
        distanceMap[MOVE_UP_RIGHT] = can.indexY-y;
        break;
    }
    if(y%2 == 1){
        x++;
    }
    y--;

    if(y <0||x>8){
        break;
    }
}
if(can){
    return MOVE_UP_RIGHT;
}
//right
can =true;
for (var x= cat.indexX;x<9;x++) {
    if(circleArr[x][cat.indexY].getCircleType() == Circle.TYPE_SELECTED){
        can =false;
        distanceMap[MOVE_RIGHT] = x -cat.indexX;
        break;
    }
}
if(can){
    return MOVE_RIGHT;
}
//ritht down
can = true;
x= cat.indexX,y = cat.indexY;
while(true){
    if(circleArr[x][y].getCircleType() == Circle.TYPE_SELECTED){
        can =false;
        distanceMap[MOVE_DOWN_RIGHT] = y -cat.indexY;
        break;
    }
    if(y%2 == 1){
        x++;
    }
    y++;

    if(y>8 ||x>8){
        break;
    }
}
if(can){
    return MOVE_DOWN_RIGHT;
}
//left down
can = true;
x= cat.indexX,y = cat.indexY;
while(true){
    if(circleArr[x][y].getCircleType() == Circle.TYPE_SELECTED){
        can = false;
        distanceMap[MOVE_DOWN_LEFT] = y -cat.index;
        break;
    }
    if(y%2 == 0){
        x--;
    }
    y++;

    if(y>8 || x<0){
        break;
    }
}
if(can){
    return MOVE_DOWN_LEFT;
}
var maxDir = -1,maxValue = -1;
for (var dir = 0;dir<distanceMap.length;dir++) {
    if(distanceMap[dir]>maxValue){
        maxValue = distanceMap[dir];
        maxDir = dir;
    }
}
if(maxValue > 1){
    return maxDir;
}else{
    return MOVE_NONE;
}

} function circleClicked(event){ if(event.target.getCircleType() != Circle.TYPE_CAT){ event.target.setCircleType(Circle.TYPE_SELECTED); }else{ return; }

//游戏结束判断:猫是否被围住
if(currentCat.indexX == 0 ||currentCat.indexX == 8 ||currentCat.indexY==0 ||currentCat.indexY==8){
    alert("游戏结束");
    return;
}
var dir = getMoveDir(currentCat);
switch (dir){
    //看是否还存在可走的路
    case MOVE_LEFT:
        currentCat.setCircleType(Circle.TYPE_UNSELECTED);
        currentCat = circleArr[currentCat.indexX - 1][currentCat.indexY];
        currentCat.setCircleType(Circle.TYPE_CAT)
    break;
    case MOVE_UP_LEFT:
        currentCat.setCircleType(Circle.TYPE_UNSELECTED);
        currentCat = circleArr[currentCat.indexY%2?currentCat.indexX:currentCat.indexX- 1][currentCat.indexY-1];
        currentCat.setCircleType(Circle.TYPE_CAT)
    break;
    case MOVE_UP_RIGHT:
        currentCat.setCircleType(Circle.TYPE_UNSELECTED);
        currentCat = circleArr[currentCat.indexY%2?currentCat.indexX+1:currentCat.indexX][currentCat.indexY-1];
        currentCat.setCircleType(Circle.TYPE_CAT)
    break;
    case MOVE_RIGHT:
        currentCat.setCircleType(Circle.TYPE_UNSELECTED);
        currentCat = circleArr[currentCat.indexX+1][currentCat.indexY];
        currentCat.setCircleType(Circle.TYPE_CAT)
    break;
    case MOVE_DOWN_RIGHT:
        currentCat.setCircleType(Circle.TYPE_UNSELECTED);
        currentCat = circleArr[currentCat.indexY%2?currentCat.indexX+1:currentCat.indexX][currentCat.indexY+1];
        currentCat.setCircleType(Circle.TYPE_CAT)
    break;
    case MOVE_DOWN_LEFT:
        currentCat.setCircleType(Circle.TYPE_UNSELECTED);
        currentCat = circleArr[currentCat.indexY%2?currentCat.indexX:currentCat.indexX-1][currentCat.indexY+1];
        currentCat.setCircleType(Circle.TYPE_CAT)
    break;
    
    default:
        alert("游戏结束");
}

} function addCircles(){ //生成游戏背景 for (var indexY = 0; indexY <9;indexY++ ) { for (var indexX = 0;indexX<9;indexX++) { var c = new Circle(); gameView.addChild(c); circleArr[indexX][indexY] = c; c.indexX = indexX; c.indexY = indexY;

        c.x = indexY%2?indexX*55+25:indexX*55;
        c.y = indexY * 55;
        if(indexX == 4 && indexY == 4){

            
            c.setCircleType(3);
            currentCat = c;
        }else if(Math.random() <0.1){
            
            c.setCircleType(Circle.TYPE_SELECTED);
        }
        //添加事件
        c.addEventListener("click",circleClicked);
    }
}

} addCircles(); 2.新建JS:circle.js,编写画布的JS

function Circle(){ createjs.Shape.call(this); this.setCircleType = function(type){ this._circleType = type; switch (type){ //没有点击过的颜色 case Circle.TYPE_UNSELECTED: this.setColor("#cccccc"); break; //点击过的颜色 case Circle.TYPE_SELECTED: this.setColor("#ff6600"); break; //猫的颜色 case Circle.TYPE_CAT: this.setColor("#0000ff"); break; } } this.setColor = function(colorString){ this.graphics.beginFill(colorString); this.graphics.drawCircle(0,0,25); this.graphics.endFill(); } this.getCircleType = function(){ return this._circleType; } this.setCircleType(1); } Circle.prototype = new createjs.Shape(); //三种状态 表示 一个为点击之后的 一个点击之前 一个是猫 Circle.TYPE_UNSELECTED = 1; Circle.TYPE_SELECTED = 2; Circle.TYPE_CAT = 3; 3.新建HTML文件,然后导入JS和画布

运行截图 !\[\](https://oscimg.oschina.net/oscnet/up-1b2b9b10b26d97e24d02fd9fa95c32b8e40.png)

二、寻找不同颜色 1.创建index.html

Title
2.创建app.js

var stage = new createjs.Stage("gameView"); createjs.Ticker.setFPS(30); createjs.Ticker.addEventListener("tick",stage); var gameView = new createjs.Container(); stage.addChild(gameView); var n=2; function addRect(){ var cl = parseInt(Math.random()*1000000); var color="#"+cl; var x= parseInt(Math.random()*n); var y= parseInt(Math.random()*n);

for(var indexX = 0;indexX<n;indexX ++){
    for (var indexY=0;indexY<n;indexY++){
        var r = new Rect(n,color);//var r = new Rect(n,color,RectColor);
        gameView.addChild(r);
        r.x = indexX;
        r.y = indexY;
        if(r.y == y&&r.x == x){
            r.setRectType(2);
        }
        r.x = indexX*(400/n);
        r.y = indexY*(400/n);
        if(r.getRectType()==2){
            r.addEventListener("click",function(){
                if(n<7){
                    ++n;
                }
                gameView.removeAllChildren();//移除所有图形
                addRect();//重新创建
            })
        }
    }
}

} addRect(); 3.创建rect.js

function Rect(n,color){
createjs.Shape.call(this); this.setRectType = function (type){ this._RectType = type; switch(type){ case 1: this.setColor(color); break; case 2: this.setColor("#ff0000"); break; } } this.setColor = function(colorString){ this.graphics.beginFill(colorString);
this.graphics.drawRect(0,0,400/n-5,400/n-5); this.graphics.endFill();
} //设置类型 this.getRectType = function(){ return this._RectType; } this.setRectType(1); } //初始化 Rect.prototype = new createjs.Shape(); 运行截图 HTML小游戏2 HTML小游戏2

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
2年前
java将前端的json数组字符串转换为列表
记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Java修道之路,问鼎巅峰,我辈代码修仙法力齐天
<center<fontcolor00FF7Fsize5face"黑体"代码尽头谁为峰,一见秃头道成空。</font<center<fontcolor00FF00size5face"黑体"编程修真路破折,一步一劫渡飞升。</font众所周知,编程修真有八大境界:1.Javase练气筑基2.数据库结丹3.web前端元婴4.Jav
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这