求flash贪吃蛇AS3.0游戏源文件
3个回答
展开全部
写了一个简易版贪吃蛇代码,供你参考。
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.geom.Point;
public class Main extends Sprite
{
private var mapArray:Array = [];
private var items:Array = [];
private var startX:uint = 5;
private var startY:uint = 5;
private var mapWidth:uint = 20;
private var mapHeight:uint = 20;
private var itemWidth:int = 15;
private var itemHeight:int = 15;
private var enemyX:int = startX;
private var enemyY:int = startY;
private var snakeArray:Array = [[startX,startY]];
private var directionX:int = 1;
private var directionY:int = 0;
public function Main()
{
stage.frameRate = 2;
graphics.lineStyle(1);
graphics.drawRect(0,0,mapWidth*itemWidth,mapHeight*itemHeight);
for(var i:int = 0;i<mapHeight;i++)
{
items[i] = [];
mapArray[i] = [];
for(var k:int = 0;k<mapWidth;k++)
{
var spr:Sprite = new Sprite;
addChild(spr);
spr.graphics.beginFill(0);
spr.graphics.drawRect(0,0,itemWidth,itemHeight);
spr.x = k*itemWidth;
spr.y = i*itemHeight;
spr.visible = false;
items[i][k] = spr;
mapArray[i][k] = 0;
}
}
while(enemyX == startX && enemyY == startY)
{
enemyX = Math.random()*mapWidth;
enemyY = Math.random()*mapHeight;
}
mapArray[startY][startX] = 1;
items[startY][startX].visible = true;
items[enemyY][enemyX].visible = true;
stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
addEventListener(Event.ENTER_FRAME,onEnterFrame);
}
protected function onKeyDown(event:KeyboardEvent):void
{
switch(event.keyCode)
{
case 38:///上
case 87:///W
directionX = 0;
directionY = -1;
break;
case 40:///下
case 83:///S
directionX = 0;
directionY = 1;
break;
case 37:///左
case 65:///A
directionX = -1;
directionY = 0;
break;
case 39:///右
case 68:///D
directionX = 1;
directionY = 0;
break;
}
}
protected function onEnterFrame(event:Event):void
{
var addX:int = snakeArray[0][0]+directionX;
var addY:int = snakeArray[0][1]+directionY;
if(addX >= mapWidth || addX < 0 || addY >= mapHeight || addY < 0 || mapArray[addY][addX])
{
stage.removeEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
removeEventListener(Event.ENTER_FRAME,onEnterFrame);
trace("======== GAME OVER ========");
}else if(addX == enemyX && addY == enemyY)
{
mapArray[addY][addX] = 1;
snakeArray.unshift([addX,addY]);
var isFail:Boolean = true;
while(isFail)
{
enemyX = Math.random()*mapWidth;
enemyY = Math.random()*mapHeight;
isFail = false;
for(var i:int = 0;i<snakeArray.length;i++)
{
if(enemyX == snakeArray[i][0] && enemyY == snakeArray[i][1])
{
isFail = true;
break;
}
}
}
items[enemyY][enemyX].visible = true;
stage.frameRate++;
}else
{
snakeArray.unshift([addX,addY]);
mapArray[addY][addX] = 1;
items[addY][addX].visible = true;
var tail:Array = snakeArray.pop();
mapArray[tail[1]][tail[0]] = 0;
items[tail[1]][tail[0]].visible = false;
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询