求高手,用flash cs5 AS3.0脚本做一个贪吃蛇的游戏,不需要太难,能运行即可,急用,跪求……

这是已做好的主程序,蛇可以在场景中随键盘移动,但还没有随机生成食物的程序和吃食物长身体的程序。求大侠帮忙……varradius:uint=10;varballOrigin... 这是已做好的主程序,蛇可以在场景中随键盘移动,但还没有随机生成食物的程序和吃食物长身体的程序。求大侠帮忙……
var radius:uint = 10;
var ballOriginCount:uint =5;
var sW:Number = stage.stageWidth;
var sH:Number = stage.stageHeight;
var balls:Array;
init();
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDowmHandler);
stage.addEventListener(Event.ENTER_FRAME,moveSnake);

function init():void
{
balls = new Array();
for (var i:uint=0; i<ballOriginCount; i++)
{
var b:Snake = new Snake(radius,Math.random() * 0xffffff);
balls.push(b);
b.x = sW / 2;
b.y = sH / 2;
b.nextx = b.x;
b.nexty = b.y;
b.vx = b.width;
addChild(b);
}
for (i=1; i<ballOriginCount; i++)
{
balls[i].x = balls[i - 1].x - balls[i].width;
}
}
function keyDowmHandler(e:KeyboardEvent):void
{
var b:Snake = balls[0];
if (e.keyCode == Keyboard.DOWN && b.vx != 0)
{
b.vy = b.width;
b.vx = 0;
}
else if (e.keyCode == Keyboard.UP && b.vx!=0)
{
b.vy = - b.width;
b.vx = 0;
}
else if (e.keyCode == Keyboard.LEFT && b.vy!=0)
{
b.vx = - b.width;
b.vy = 0;
}
else if (e.keyCode == Keyboard.RIGHT && b.vy!=0)
{
b.vx = b.width;
b.vy = 0;
}
}
function moveSnake(event:Event):void
{
var b:Snake = balls[0];
//检测是否碰壁
if (b.x < b.radius || b.x > sW - 2 * b.radius || b.y < b.radius || b.y > sH - 2 * b.radius)
{
stage.removeEventListener(Event.ENTER_FRAME,moveSnake);
}
b.nextx = b.x + b.vx;
b.nexty = b.y + b.vy;
for (var j:uint=1; j<ballOriginCount; j++)
{
var _b:Snake = balls[j];
_b.nextx = balls[j - 1].x;
_b.nexty = balls[j - 1].y;
}
for (j=0; j<ballOriginCount; j++)
{
balls[j].x = balls[j].nextx;
balls[j].y = balls[j].nexty;
}
}
展开
 我来答
理想1990818
2012-12-28 · 超过12用户采纳过TA的回答
知道答主
回答量:58
采纳率:0%
帮助的人:24.2万
展开全部
package {
import flash.display.Sprite;
import flash.events.*;
import flash.text.*;
import flash.utils.Timer;
import flash.display.StageScaleMode;
public class Main extends Sprite {
var frame:Sprite, randombrick:Sprite;
var txt_level:TextField = new TextField(), txt_score:TextField = new TextField(), txt_explain:TextField = new TextField(), txt_winlose:TextField = new TextField(), txt_author:TextField = new TextField();
var tf1:TextFormat = new TextFormat(), tf2:TextFormat = new TextFormat();
var xx:int, yy:int;
var rank:uint = 1;
var time:Timer;
var p_eat:Boolean = true, p_start:Boolean = true;
var body:uint = 5;
var arr:Array = new Array();
public function Main(){
BASIC();
stage.addEventListener(Event.ENTER_FRAME, TFEVENT);
stage.addEventListener(KeyboardEvent.KEY_DOWN, START);
stage.addEventListener(KeyboardEvent.KEY_DOWN, diction);
function START(e:KeyboardEvent):void {
if (e.keyCode == 13 && p_start == true){
p_start = false;
xx = -20;
yy = 0;
body = 5;
initarr();
txt_winlose.text = "";
time = new Timer(((rank < 5) ? (400 - rank * 50) : (200 - rank * 10)));
time.start();
time.addEventListener(TimerEvent.TIMER, MOVE);
stage.addEventListener(Event.ENTER_FRAME, RANDOMBRICK);
stage.addEventListener(Event.ENTER_FRAME, WINLOSE);
}
}
}
private function WINLOSE(et:Event):void {
if (arr.length == ((rank < 10) ? (20) : (15))){
rank += 1;
WINANDLOSE();
txt_winlose.text = "WIN";
}
if (arr.length != 0){
if (arr[0].x < 0 || arr[0].x > 390 || arr[0].y < 0 || arr[0].y > 390){
WINANDLOSE();
txt_winlose.text = "LOSE";
}
if (arr.length != 0){
for (var a:uint = 2; a < arr.length; a++){
if (arr[0].hitTestObject(arr[a])){
WINANDLOSE();
txt_winlose.text = "LOSE";
}
}
}
}
function WINANDLOSE(){
p_start = true;
for (var m:uint = 0; m < arr.length; m++){
removeChild(arr[m]);
}
arr.splice(0, arr.length - 1);
arr.shift();
time.removeEventListener(TimerEvent.TIMER, MOVE);
stage.removeEventListener(Event.ENTER_FRAME, RANDOMBRICK);
stage.removeEventListener(Event.ENTER_FRAME, WINLOSE);
}
}
private function initarr(){ //初始化数组
for (var i:uint = 0; i < body; i++){
var brick:Sprite = BRICK();
arr.push(brick);
addChild(arr[i]);
arr[i].x = i * 20 + 200 + 3;
arr[i].y = 200 + 3;
}
}
private function MOVE(evt:TimerEvent):void {
var temp:Array = arr.splice(arr.length - 1, 1);
temp[0].x = arr[0].x + xx;
temp[0].y = arr[0].y + yy;
arr.unshift(temp[0]);
}
private function RANDOMBRICK(evt:Event):void {
if (p_eat == true){
p_eat = false;
randombrick = BRICK();
var dx:Number = Math.random() * 400;
var dy:Number = Math.random() * 400;
for (var e:uint = 0; e < 20; e++){
if (dx >= 20 * e && dx <= 20 * (e + 1)){
randombrick.x = e * 20 + 3;
}
if (dy >= 20 * e && dy <= 20 * (e + 1)){
randombrick.y = e * 20 + 3;
}
}
addChild(randombrick);
randombrick.alpha=1;
}
if (arr[0].hitTestObject(randombrick)){
p_eat = true;
body += 1;
arr.unshift(randombrick);
randombrick.alpha = 0.5;
arr[0].x = arr[1].x + xx;
arr[0].y = arr[1].y + yy;
}
}
private function FRAME():Sprite { //绘制方格
frame = new Sprite();
for (var m:uint = 0; m <= 20; m++){
frame.graphics.moveTo(0, m * 20);
frame.graphics.lineStyle(1, 0x0000ff);
frame.graphics.lineTo(400, m * 20);
}
for (var n:uint = 0; n <= 20; n++){
frame.graphics.moveTo(n * 20, 0);
frame.graphics.lineStyle(1, 0x0000ff);
frame.graphics.lineTo(n * 20, 400);
}
return frame;
}
private function TF(){
tf1.size = 14;
tf1.color = 0xffffff;
tf1.bold = true;
tf2.size = 22;
tf2.color = 0xffff00;
tf2.bold = true;
tf2.align = TextFormatAlign.CENTER;
}
private function TFEVENT(event:Event):void {
TF();
txt_explain.setTextFormat(tf1, 0, 7);
txt_explain.setTextFormat(tf1, 12, 17);
txt_explain.setTextFormat(tf2, 8, 10);
txt_explain.setTextFormat(tf2, 17, 19);
txt_level.text = "LEVEL:\n" + rank;
txt_level.setTextFormat(tf1, 0, 6);
txt_level.setTextFormat(tf2, 7, txt_level.length);
txt_score.text = "SCORE:\n" + (body - 5);
txt_score.setTextFormat(tf1, 0, 6);
txt_score.setTextFormat(tf2, 7, txt_score.length);
txt_winlose.setTextFormat(tf2);
txt_author.setTextFormat(tf1);
}
private function BRICK():Sprite { //画蛇身体
var panel:Sprite = new Sprite();
panel.graphics.lineStyle(1, 0x00ffff);
panel.graphics.beginFill(0x00ff00);
panel.graphics.drawRect(0, 0, 18, 18);
panel.graphics.endFill();
panel.alpha = 0.5;
return panel;
}
private function diction(event:KeyboardEvent):void { //控制蛇头移动方向
switch (event.keyCode){
case 37:
xx = -20;
yy = 0;
break;
case 38:
xx = 0;
yy = -20;
break;
case 39:
xx = 20;
yy = 0;
break;
case 40:
xx = 0;
yy = 20;
break;
default:
break;
}
}
private function BASIC(){
stage.scaleMode = StageScaleMode.NO_SCALE; //屏幕不缩放
//加载方格
frame = FRAME();
addChild(frame);
frame.x = frame.y = 2;
//说明
txt_explain.text = "ENTER键:\n开始\n\n方向键:\n移动";
txt_winlose.text = "";
txt_level.text = "LEVEL:\n" + rank;
txt_score.text = "SCORE:\n" + (body - 5);
txt_author.text = "author:\n 给力^_^冬"
txt_explain.selectable = txt_winlose.selectable = txt_level.selectable = txt_score.selectable = txt_author.selectable = false;
txt_explain.x = txt_winlose.x = txt_level.x = txt_score.x = txt_author.x = 405;
txt_level.y = 20;
txt_score.y = 90;
txt_winlose.y = 160;
txt_explain.y = 210;
txt_author.y = 340;
txt_explain.background = txt_winlose.background = txt_level.background = txt_score.background = txt_author.background = true;
txt_explain.backgroundColor = txt_winlose.backgroundColor = txt_level.backgroundColor = txt_score.backgroundColor = txt_author.backgroundColor = 0x400080;
txt_explain.width = txt_winlose.width = txt_level.width = txt_score.width = txt_author.width = 90;
txt_explain.height = 105;
txt_winlose.height = 30;
txt_level.height = txt_score.height = txt_author.height = 50;
addChild(txt_explain);
addChild(txt_winlose);
addChild(txt_level);
addChild(txt_score);
addChild(txt_author);
}
}
}
我用的是as文件做的 代码不是放在帧上,希望对楼主有用
62...8@qq.com
2012-12-26
知道答主
回答量:6
采纳率:0%
帮助的人:8976
展开全部
实在没办法,就只能重新安装试试。也有可能是电脑系统不支持
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式