游戏1:HTML5制作网页游戏围住神经猫--createjs

游戏简介:点击小圆圈,是蓝色的小圆圈不跑出圆圈外,跑出则结束游戏

准备工作:

  下载easejs  :下载地址:http://www.createjs.cc/easeljs    中文网站

效果:

  

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>围住神经猫游戏</title>
    <script src="js/easeljs.min.js"></script>
    <script src="js/Circle.js"></script>
</head>
<body>
<canvas width="800px" height="800px" id="gameView"></canvas>
<script src="js/app.js"></script>
</body>
</html>

app.js

/**
 * Created by xxc on 2018/11/24.
 */
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;

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] = cat.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;
    x = cat.indexX,y = cat.indexY;
    while(true){
        if(circleArr[x][y].getCircleType()==Circle.TYPE_SELECTED){
            can = false;
            distanceMap[MOVE_UP_RIGHT] = cat.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;
    }

    //right 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.indexY;
            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(Circle.TYPE_CAT);
                currentCat = c;
            }else if(Math.random()<0.1){
                c.setCircleType(Circle.TYPE_SELECTED);
            }
            c.addEventListener("click",circleClicked);
        }
    }
}
addCircles();

Circle.js

/**
 * Created by xxc on 2018/11/24.
 */
//画圆
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;

 

 

转载于:https://www.cnblogs.com/alex-xxc/p/10013142.html

Beginning HTML5 Games with CreateJS provides a hands-on approach to get you up and running with the most comprehensive tools available for HTML5 Canvas game development. Whether you are brand new to making games or an experienced game developer, you’ll learn to fully utilize the CreateJS suite to bring your new or existing game techniques to desktop and mobile devices. This book covers everything from creating graphics in HTML5 Canvas to capturing user input (whether from keyboard, mouse, or touch) to using a state machine for efficient game control. There are practical (and fun) examples throughout the book, with four full game projects, including a mobile RPG. The book also covers optimizing your games for mobile and publishing them to app stores. HTML5 games are growing more and more popular, and the demand for HTML5 Canvas skills is on the rise. The CreateJS suite is a powerful toolset that will help you manage Canvas drawing and animations, asset loading, sound management, complex tweening, and much more. Using these robust libraries, you can build powerful and engaging games that reach a wide range of audiences and devices. What you’ll learn HTML5 Canvas drawing and animations using EaselJS and TweenJS Loading and managing assets using PreloadJS Sound management using SoundJS Core game development techniques such as state machines and object pooling Extending EaselJS DisplayObjects using object-oriented JavaScript JavaScript debugging Wrapping HTML5 games and publishing them to app store Who this book is for Beginning HTML5 Games with CreateJS is written for both the new and experienced game developer. It covers an in-depth look at the APIs in each part of the CreateJS suite, and teaches you how to use them in game development scenarios. If you’re an experienced developer it will show you how to take your existing gaming techniques and learn how to apply them to the HTML stack using object-oriented programming in JavaScript. Beginning HTML5 Games with CreateJS is a must-read book for anybody wanting to learn more about HTML5 game development using this extremely popular suite of tools. Table of Contents Chapter 1: Getting to Know CreateJS Chapter 2: Making and Animating Graphics Chapter 3: Capturing User Input Chapter 4: Game Project: Progressive Break-it Chapter 5: Using and Managing Bitmap Images Chapter 6: Sprites and Sprite Sheet Animations Chapter 7: Game Project: Fakezee Chapter 8: Extending EaselJS Display Objects Chapter 9: Sounds and Asset Management Chapter 10: The State Machine and Scenes Chapter 11: Game Project: Space Hero Chapter 12: Building for Mobile Browsers Chapter 13: Packaging and Compiling with PhoneGap Build Chapter 14: Game Project: The Villager RPG Book Details Title: Beginning HTML5 Games with CreateJS Author: Brad Manderscheid Length: 416 pages Edition: 1 Language: English Publisher: Apress Publication Date: 2014-03-18 ISBN-10: 1430263407 ISBN-13: 9781430263401
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值