求解flash3.0写的贪吃蛇的源代码不懂球大神能给详细的注释代码求大神可怜
1个回答
展开全部
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.text.TextField;
import flash.utils.setTimeout;
public class Snake extends MovieClip
{
public var levelTf:TextField;
public var scoreTf:TextField;
public var pop:Pop;
private var gameCon:Sprite;
private const POS:Object = {"up": [0, -1], "down": [0, 1], "left": [-1, 0], "right": [1, 0]};
private var s:Array;
private var curPos:String = "up";
private var foods:Array;
private var map:Array;
private var levelConfig = [{delay: 500, count: 10}, {delay: 400, count: 12}, {delay: 300, count: 15}, {delay: 200, count: 18}, {delay: 100, count: 20}, {delay: 80, count: 25}, {delay: 50, count: 30}, {delay: 40, count: 100}];
private var delay:int = 500;
private var count:int = 10;
private var level:int = 0;
private var score:int = 0;
private const CW:int = 15;
private const CH:int = 15;
private const W:int = 15;
private const H:int = 20;
public function Snake()
{
// constructor code
stage ? initStage(null) : addEventListener(Event.ADDED_TO_STAGE, initStage);
}
private function initStage(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, initStage);
gameCon = new Sprite();
addChild(gameCon);
gameCon.x = 50;
gameCon.y = 50;
gameCon.graphics.lineStyle(1, 0xff0000, 1.0);
gameCon.graphics.drawRect(0, 0, CW * W, CH * H);
addChild(pop);
initGame();
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
}
private function keyDownHandler(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case 37: //left
if (curPos != "right")
curPos = "left";
break;
case 38: //up
if (curPos != "down")
curPos = "up";
break;
case 39: //right
if (curPos != "left")
curPos = "right";
break;
case 40: //down
if (curPos != "up")
curPos = "down";
break;
}
}
private function initGame():void
{
while (gameCon.numChildren)
{
gameCon.removeChildAt(0);
}
curPos = "up";
foods = [];
map = [];
s = [];
delay = levelConfig[level].delay;
count = levelConfig[level].count;
setLevel(level + 1);
setScore(score);
for (var i:int = 0; i < W; i++)
{
map[i] = [];
for (var j:int = 0; j < H; j++)
{
map[i][j] = 0;
}
}
for (i = 3; i > 0; i--)
{
var ii:int = int(W * 0.5);
var jj:int = int(H * 0.5) + i;
var n:Node = new Node(CW, CH, ii, jj, 0x000000,"snake");
s.push(n);
gameCon.addChild(n);
map[ii][jj] = n;
}
setTimeout(update, delay);
}
private function update():void
{
var head:Node = s[s.length - 1];
var ti:* = head.i + POS[curPos][0];
var tj:* = head.j + POS[curPos][1];
if (ti < 0 || ti >= W || tj < 0 || tj >= H)
{
pop.show("撞墙了\nGame Over", function() {
level = 0;
setLevel(level + 1);
score = 0;
setScore(score);
initGame();
})
return;
}
else if (map[ti][tj] != 0)
{ //map[ti][tj]是蛇或食物,都是Node类
var no:Node = map[ti][tj];
if (no.type=="snake")
{
pop.show("撞到自己了\nGame Over", function() {
level = 0;
setLevel(level + 1);
score = 0;
setScore(score);
initGame();
})
return;
}
else
{
removeItem(foods, no);
s.push(no);
no.i = ti;
no.j = tj;
no.setColor(0x000000);
no.type = "snake";
score++;
setScore(score);
}
}
else
{ //蛇移动一位
map[s[0].i][s[0].j] = 0;
var len:int = s.length;
for (var i:int = 0; i < len; i++)
{
if (i == len - 1)
{
s[i].i = ti;
s[i].j = tj;
}
else
{
s[i].i = s[i + 1].i;
s[i].j = s[i + 1].j;
}
s[i].x = s[i].i * CW;
s[i].y = s[i].j * CH;
map[s[i].i][s[i].j] = s[i];
}
}
if (s.length >= count)
{
level++;
setLevel(level + 1);
score += 5 * level;
setScore(score);
if (level >= levelConfig.length)
level = 0;
pop.show("恭喜过关", function() {
initGame();
})
return;
}
while (foods.length < 2)
{
createFoods();
}
setTimeout(update, delay);
}
private function removeItem(arr:Array, item:Object)
{
for (var i:int = 0; i < arr.length; i++)
{
if (arr[i] == item)
{
arr.splice(i, 1);
return;
}
}
}
function createFoods()
{
var i:int = randomAB(0, W);
var j:int = randomAB(0, H);
while (map[i][j] != 0)
{
i = randomAB(0, W);
j = randomAB(0, H);
}
var f:Node = new Node(CW, CH, i, j, 0x00cc00,"food");
gameCon.addChild(f);
foods.push(f);
map[i][j] = f;
}
/**[a,b)区间的随机整数**/
private function randomAB(a:int, b:int):int
{
return int(Math.random() * (b - a)) + a;
}
private function setScore(value:int):void
{
scoreTf.text = "得分:" + value;
}
private function setLevel(value:int):void
{
levelTf.text = "第 " + value + " 关";
}
}
}
import flash.display.Sprite;
class Node extends Sprite
{
public var i:int;
public var j:int;
private var _w:int;
private var _h:int;
public var type:String;
public function Node(w:int, h:int, i:int, j:int, c:uint,type:String)
{
this.type = type;
_w = w;
_h = h;
this.i = i;
this.j = j;
graphics.beginFill(c, 1.0);
graphics.drawRect(0, 0, w, h);
graphics.endFill();
x = w * i;
y = h * j;
}
public function setColor(value:uint):void
{
graphics.clear();
graphics.beginFill(value, 1.0);
graphics.drawRect(0, 0, _w, _h);
graphics.endFill();
}
}
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.text.TextField;
import flash.utils.setTimeout;
public class Snake extends MovieClip
{
public var levelTf:TextField;
public var scoreTf:TextField;
public var pop:Pop;
private var gameCon:Sprite;
private const POS:Object = {"up": [0, -1], "down": [0, 1], "left": [-1, 0], "right": [1, 0]};
private var s:Array;
private var curPos:String = "up";
private var foods:Array;
private var map:Array;
private var levelConfig = [{delay: 500, count: 10}, {delay: 400, count: 12}, {delay: 300, count: 15}, {delay: 200, count: 18}, {delay: 100, count: 20}, {delay: 80, count: 25}, {delay: 50, count: 30}, {delay: 40, count: 100}];
private var delay:int = 500;
private var count:int = 10;
private var level:int = 0;
private var score:int = 0;
private const CW:int = 15;
private const CH:int = 15;
private const W:int = 15;
private const H:int = 20;
public function Snake()
{
// constructor code
stage ? initStage(null) : addEventListener(Event.ADDED_TO_STAGE, initStage);
}
private function initStage(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, initStage);
gameCon = new Sprite();
addChild(gameCon);
gameCon.x = 50;
gameCon.y = 50;
gameCon.graphics.lineStyle(1, 0xff0000, 1.0);
gameCon.graphics.drawRect(0, 0, CW * W, CH * H);
addChild(pop);
initGame();
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
}
private function keyDownHandler(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case 37: //left
if (curPos != "right")
curPos = "left";
break;
case 38: //up
if (curPos != "down")
curPos = "up";
break;
case 39: //right
if (curPos != "left")
curPos = "right";
break;
case 40: //down
if (curPos != "up")
curPos = "down";
break;
}
}
private function initGame():void
{
while (gameCon.numChildren)
{
gameCon.removeChildAt(0);
}
curPos = "up";
foods = [];
map = [];
s = [];
delay = levelConfig[level].delay;
count = levelConfig[level].count;
setLevel(level + 1);
setScore(score);
for (var i:int = 0; i < W; i++)
{
map[i] = [];
for (var j:int = 0; j < H; j++)
{
map[i][j] = 0;
}
}
for (i = 3; i > 0; i--)
{
var ii:int = int(W * 0.5);
var jj:int = int(H * 0.5) + i;
var n:Node = new Node(CW, CH, ii, jj, 0x000000,"snake");
s.push(n);
gameCon.addChild(n);
map[ii][jj] = n;
}
setTimeout(update, delay);
}
private function update():void
{
var head:Node = s[s.length - 1];
var ti:* = head.i + POS[curPos][0];
var tj:* = head.j + POS[curPos][1];
if (ti < 0 || ti >= W || tj < 0 || tj >= H)
{
pop.show("撞墙了\nGame Over", function() {
level = 0;
setLevel(level + 1);
score = 0;
setScore(score);
initGame();
})
return;
}
else if (map[ti][tj] != 0)
{ //map[ti][tj]是蛇或食物,都是Node类
var no:Node = map[ti][tj];
if (no.type=="snake")
{
pop.show("撞到自己了\nGame Over", function() {
level = 0;
setLevel(level + 1);
score = 0;
setScore(score);
initGame();
})
return;
}
else
{
removeItem(foods, no);
s.push(no);
no.i = ti;
no.j = tj;
no.setColor(0x000000);
no.type = "snake";
score++;
setScore(score);
}
}
else
{ //蛇移动一位
map[s[0].i][s[0].j] = 0;
var len:int = s.length;
for (var i:int = 0; i < len; i++)
{
if (i == len - 1)
{
s[i].i = ti;
s[i].j = tj;
}
else
{
s[i].i = s[i + 1].i;
s[i].j = s[i + 1].j;
}
s[i].x = s[i].i * CW;
s[i].y = s[i].j * CH;
map[s[i].i][s[i].j] = s[i];
}
}
if (s.length >= count)
{
level++;
setLevel(level + 1);
score += 5 * level;
setScore(score);
if (level >= levelConfig.length)
level = 0;
pop.show("恭喜过关", function() {
initGame();
})
return;
}
while (foods.length < 2)
{
createFoods();
}
setTimeout(update, delay);
}
private function removeItem(arr:Array, item:Object)
{
for (var i:int = 0; i < arr.length; i++)
{
if (arr[i] == item)
{
arr.splice(i, 1);
return;
}
}
}
function createFoods()
{
var i:int = randomAB(0, W);
var j:int = randomAB(0, H);
while (map[i][j] != 0)
{
i = randomAB(0, W);
j = randomAB(0, H);
}
var f:Node = new Node(CW, CH, i, j, 0x00cc00,"food");
gameCon.addChild(f);
foods.push(f);
map[i][j] = f;
}
/**[a,b)区间的随机整数**/
private function randomAB(a:int, b:int):int
{
return int(Math.random() * (b - a)) + a;
}
private function setScore(value:int):void
{
scoreTf.text = "得分:" + value;
}
private function setLevel(value:int):void
{
levelTf.text = "第 " + value + " 关";
}
}
}
import flash.display.Sprite;
class Node extends Sprite
{
public var i:int;
public var j:int;
private var _w:int;
private var _h:int;
public var type:String;
public function Node(w:int, h:int, i:int, j:int, c:uint,type:String)
{
this.type = type;
_w = w;
_h = h;
this.i = i;
this.j = j;
graphics.beginFill(c, 1.0);
graphics.drawRect(0, 0, w, h);
graphics.endFill();
x = w * i;
y = h * j;
}
public function setColor(value:uint):void
{
graphics.clear();
graphics.beginFill(value, 1.0);
graphics.drawRect(0, 0, _w, _h);
graphics.endFill();
}
}
追问
能写上详细的注释吗。我看不懂,可怜我一下吧
追答
亲,把这个写上注释,能把人累死!!!
没有AS基础,怎么能做出这种游戏呢?!!!
实在不行,您再等等其他大仙吧。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询