Last active
October 8, 2015 23:08
-
-
Save lynxerzhang/3402240 to your computer and use it in GitHub Desktop.
The Snake's Prototype (TODO)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
import flash.display.Sprite; | |
import flash.display.StageAlign; | |
import flash.display.StageScaleMode; | |
import flash.events.Event; | |
import flash.events.KeyboardEvent; | |
import flash.events.TimerEvent; | |
import flash.utils.Timer; | |
/** | |
* TODO | |
* The snake prototype (Document Class) | |
*/ | |
public class Snake extends Sprite | |
{ | |
private var rootS:Sprite; | |
private var player:Sprite; | |
private var food:Sprite; | |
private var column:int; | |
private var rows:int; | |
private var size:int; | |
private var timer:Timer; | |
public function Snake():void { | |
stage.align = StageAlign.TOP_LEFT; | |
stage.scaleMode = StageScaleMode.NO_SCALE; | |
constructInit(); | |
initSnakeBody(); | |
setMotion(); | |
} | |
private function setCurrentLevelTimeRate(rate:int):void { | |
if (timer) { | |
if (timer.running) { | |
timer.stop(); | |
} | |
timer.delay = (Math.floor(1 / rate * 1000)); | |
timer.start(); | |
} | |
} | |
private function setMotion():void { | |
timer = new Timer(0, 0); | |
timer.addEventListener(TimerEvent.TIMER, onTimerHandler); | |
setCurrentLevelTimeRate(15);//use Timer to simulate frame rate (15fps) | |
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler); | |
} | |
private function keyDownHandler(event:KeyboardEvent):void { | |
if (event.keyCode == 37 && currentDirect != RIGHT) { | |
setDirection(LEFT);//left | |
} | |
else if (event.keyCode == 38 && currentDirect != BOTTOM) { | |
setDirection(TOP);//TOP | |
} | |
else if (event.keyCode == 39 && currentDirect != LEFT) { | |
setDirection(RIGHT);//Right | |
} | |
else if (event.keyCode == 40 && currentDirect != TOP) { | |
setDirection(BOTTOM);//Bottom | |
} | |
} | |
private function onTimerHandler(event:TimerEvent):void { | |
reDraw(); | |
} | |
private function constructInit():void { | |
rootS = CreationFactory.createSprite("root", this); | |
player = CreationFactory.createSprite("player"); | |
food = CreationFactory.createSprite("food"); | |
rootS.addChild(player); | |
rootS.addChild(food); | |
size = 10; | |
column = stage.stageWidth / size - 10; | |
rows = stage.stageHeight / size - 10; | |
rootS.graphics.beginFill(0x333333, .2); | |
rootS.graphics.drawRect(0, 0, column * size, rows * size); | |
rootS.graphics.endFill(); | |
for (var k:int = 0; k < rows * column; k++) { | |
foodAry[k] = [int(k % column), int(k / column)]; | |
} | |
rootS.x = (stage.stageWidth - rootS.width) * .5; | |
rootS.y = (stage.stageHeight - rootS.height) * .5; | |
} | |
private var foodAry:Array = []; | |
private function initSnakeBody():void { | |
pathAry.length > 0 && (pathAry.length = 0); | |
pathAry[pathAry.length] = [(column * .5) | 0, (rows * .5) | 0]; | |
player.graphics.clear(); | |
player.graphics.lineStyle(0, 0x0000FF, 1); | |
player.graphics.beginFill(0xFF0000, 1); | |
setDirection(LEFT); | |
createSnakeFood(); | |
} | |
private function createSnakeBody():void { | |
pathAry[pathAry.length] = pathAry[pathAry.length - 1].concat(); | |
} | |
private var currentFoodAry:Array; | |
private function createSnakeFood():void { | |
food.graphics.clear(); | |
food.graphics.beginFill(0x00FF00, 1); | |
var a:Array = foodAry.filter(function(e:Array, ...args):Boolean { | |
var len:int = pathAry.length; | |
for (var j:int = 0; j < len; j++) { | |
if (pathAry[j][0] == e[0] && pathAry[j][1] == e[1]) { | |
return false; | |
} | |
} | |
return true; | |
}); | |
currentFoodAry = a[(Math.random() * a.length)|0]; | |
food.graphics.drawRect(currentFoodAry[0] * size, currentFoodAry[1] * size, size, size); | |
} | |
private function setDirection(val:String):void { | |
currentDirect = val; | |
} | |
private static const LEFT:String = "left"; | |
private static const RIGHT:String = "right"; | |
private static const TOP:String = "top" | |
private static const BOTTOM:String = "Bottom"; | |
private var currentDirect:String = LEFT; | |
private var pathAry:Vector.<Array> = new <Array>[]; | |
/** | |
* every frame will recalculated | |
*/ | |
private function path():void { | |
pathAry.unshift(pathAry[0].concat()); | |
pathAry.splice(pathAry.length - 1, 1); | |
if (currentDirect == LEFT) { | |
pathAry[0][0] - 1 < 0 ? pathAry[0][0] = column - 1 : pathAry[0][0] --; | |
} | |
else if (currentDirect == RIGHT) { | |
pathAry[0][0] + 1 > column - 1 ? pathAry[0][0] = 0 : pathAry[0][0]++; | |
} | |
else if (currentDirect == TOP) { | |
pathAry[0][1] - 1 < 0 ? pathAry[0][1] = rows - 1 : pathAry[0][1]--; | |
} | |
else if (currentDirect == BOTTOM) { | |
pathAry[0][1] + 1 > rows - 1 ? pathAry[0][1] = 0 : pathAry[0][1]++; | |
} | |
} | |
/** | |
* redraw | |
*/ | |
private function reDraw():void { | |
path(); | |
player.graphics.clear(); | |
player.graphics.lineStyle(0, 0x0000FF, 1); | |
player.graphics.beginFill(0xFF0000, 1); | |
var len:int = pathAry.length; | |
while (--len > -1) { | |
player.graphics.drawRect(size * pathAry[len][0], size * pathAry[len][1], size, size); | |
} | |
if (checkEatSelf()) { | |
initSnakeBody(); | |
} | |
else { | |
checkEatFood(); | |
} | |
} | |
/** | |
* check is whether eat food | |
*/ | |
private var score:int = 0; | |
private function checkEatFood():void { | |
if (pathAry[0][0] == currentFoodAry[0] && pathAry[0][1] == currentFoodAry[1]) { | |
createSnakeBody(); | |
createSnakeFood(); | |
} | |
} | |
/** | |
* check is whether eat self | |
*/ | |
private function checkEatSelf():Boolean { | |
var len:int = pathAry.length; | |
var head:Array = pathAry[0]; | |
for (var i:int = 1; i < len; i++) { | |
if (head[0] == pathAry[i][0] && head[1] == pathAry[i][1]) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} | |
} | |
import flash.display.DisplayObjectContainer; | |
import flash.display.Sprite; | |
/** | |
* | |
*/ | |
internal class CreationFactory{ | |
public static function createSprite(name:String = null, parent:DisplayObjectContainer = null):Sprite{ | |
var s:Sprite = new Sprite(); | |
s.mouseChildren = s.mouseEnabled = false; | |
if(name){ | |
s.name = name; | |
} | |
if(parent){ | |
parent.addChild(s); | |
} | |
return s; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment