2D游戏制作:第五周作业

Wesley13
• 阅读 499

一,寻找不同颜色

1.index.html

<**html** **lang****="en"**>
<**head**>
<**meta** **charset****="UTF-8"**>
<**meta** **name****="viewport"** **content****="width=device-width,user-scalable=no"**>
<**script** **src****="easeljs.min.js"**></**script**>
<**script** **src****="Rect.js"**></**script**>
<**link** **rel****="stylesheet"** **type****="text/css"** **href****="style.css"**>
<**title**>Title</**title**>
</**head**>
<**body**>
<**div** **class****="main"**>
<**canvas** **id****="gameView"**></**canvas**>
</**div**>
<**script** **src****="app.js"**></**script**>
</**body**>
</**html**>

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); function startGame() { _//_函数入口 getCanvasSize();     n = 2;     addRect(); } function addRect() { _//_添加方格 var c1 = parseInt(Math.random() * 1000000); _//_设定放个颜色为一个随机值 var color = ("#"+c1); 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** c2 = parseInt((c1-10*(n-indexY)>0)?(c1-10*(n-indexY)):(c1+10*indexY)); var Rectcolor = ("#"** + c2); var c3 = parseInt((c2-10*(n-indexY)>0)?(c2-10*(n-indexY)):(c2+10*indexY)); var Rectcolor = ("#" + c3); var r = new Rect(n,color,Rectcolor);             gameView.addChild(r);             r.x = indexX;             r.y = indexY; if(r.x == x && r.y == y){                 r.setRectType(2);             }             r.x = indexX * (getSize() / n);             r.y = indexY * (getSize() / n); if (r.getRectType() == 2){                 r.addEventListener("click",clickRect)             }         }     } } function clickRect() { if (n < 7){         ++n;     }     gameView.removeAllChildren();     addRect(); } **function** getCanvasSize() { **var** gView = document.getElementById(**"gameView"**);     gView.height = window.innerHeight - 4;     gView.width = window.innerWidth - 4; } **function** getSize() { **if** (window.innerHeight >= window.innerWidth){ return window.innerWidth;     } else { return window.innerHeight;     } } startGame();

3.rect.js

function Rect(n, color, Rectcolor) {     createjs.Shape.call(this); this.setRectType = function (type) { this._RectType = type; switch (type) { case 1: this.setColor(color); break; case 2: this.setColor(Rectcolor); break;         }     } this.setColor = function (colorString) { this.graphics.beginFill(colorString); this.graphics.drawRect(0,0,getSize()/n-2,getSize()/n-2); this.graphics.endFill();     } this.getRectType = function () { return this._RectType;     } this.setRectType(1); } Rect.prototype = new createjs.Shape();

4.截图

2D游戏制作:第五周作业

2D游戏制作:第五周作业

  • 围住猫
  1. index.html

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

  1. app.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] = 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(3); currentCat = c;             }else if(Math.random()<0.1){ c.setCircleType(Circle.TYPE_SELECTED);             } c.addEventListener("click",circleClicked);         }     } }

addCircles();

  1. circle.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;

  1. 截图

2D游戏制作:第五周作业

2D游戏制作:第五周作业

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
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
Easter79 Easter79
3年前
svg 高宽随父元素自适应
<htmllang"en"<head<metacharset"UTF8"<metaname"viewport"content"widthdevicewidth,initialscale1.0"<metahttpequiv"XUACo
待兔 待兔
3个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Easter79 Easter79
3年前
three.js 显示三维模型(贴图)
<!DOCTYPEhtml<htmllang"en"   <head      <titleid"h2"</title      <metacharset"utf8"      <metaname"viewport"content"widthdevicewidth,user
Wesley13 Wesley13
3年前
MUI上传文件的方法
<!doctypehtml<html<head<metacharset"UTF8"<title</title<metaname"viewport"content"widthdevicewidth,init
Stella981 Stella981
3年前
ES6中的map和reduce
一 实战<!DOCTYPEhtml<htmllang"en"<head<metacharset"UTF8"<metaname"viewport"content"widthdevicewidth,initialscale1.0"
Wesley13 Wesley13
3年前
6位数字,输入验证码框
<!DOCTYPEhtml<htmllang"en"<head<metacharset"UTF8"<metaname"viewport"content"widthdevicewidth,initialscale1.0"<metahttp
Wesley13 Wesley13
3年前
CSS 背景图片 添加 重复和定位。
<!doctypehtml<htmllang"en"<head<metacharset"UTF8"<metaname"Generator"content"EditPlus®"<metaname"Author"content""<metaname"Keywords"content
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
9个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这