首页 >> 动画知识 >> flash游戏

Animation knowledge

flash游戏

Flash游戏中贪吃蛇的设计函数

大字小字2014年9月28日

  贪吃蛇游戏,在游戏行业里是最基础的一款Flash设计的游戏,但它的设计衡量并不简单。下面是Flash游戏状态判断函数区分:

  function gameStatus():Boolean {//游戏状态判断函数;

  if (xn>18 || xn<0 || yn>11 || yn<0 || placeArry[xn][yn] == 1) {

  delete this.onEnterFrame;

  overFun(2);

  return true;

  }

  if(placeArry[xn][yn] == 2){

  foodFun();//重新生成食物;

  lenNum++;//蛇身加长一节;

  score_txt.text = scoreNum += 10;//加10分;显示得分;

  addscoreFun();//吃到食物时闪出小星星的函数;

  }

  }

  function arrayFun() {//给数组赋值;即记录蛇所在的位置的函数;

  placeArry[xn][yn] = 1;

  this["mc"+firstNum].xn = xn;

  this["mc"+firstNum].yn = yn;

  var tmpX = this["mc"+lastNum].xn;

  var tmpY = this["mc"+lastNum].yn;

  placeArry[tmpX][tmpY] = 0;

  }

  function countFun(cycle:Number) {//算出蛇头、蛇尾的序号的函数;

  firstNum = (firstNum+1+cycle)%cycle;

  secondlyNum = (firstNum-1+cycle)%cycle;

  lastNum = (firstNum-lenNum+cycle)%cycle;

  }

  function foodFun(){//随机生成食物的函数;

  do{

  var xm = random(19);

  var ym = random(12);

  }while(placeArry[xm][ym] == 1 || placeArry[xm][ym] == 2);

  attachMovie("food_mc","food_mc",cycleNum+10,{_x:n*xm,_y:n*ym});

  food_mc.gotoAndStop(random(4)+2);

  placeArry[xm][ym] = 2;

  }

  function addscoreFun() {//吃到食物时闪出小星星的函数;

  sound_mc.gotoAndStop(random(7)+2);//吃到食物后的随机音效;

  for (var i = 0; i<30; i++) {

  var xnum = n*xn+random(10)-5;

  var ynum = n*yn+random(10)-5;

  mc = attachMovie("xin_mc", "mc"+i, i+500, {_x:xnum, _y:ynum});

  mc.onEnterFrame = function() {

  this.nx = 0;

  this.ny = 0;

  if(!this.ax){

  this.ax = random(60)-30;

  this.ay = random(60)-30;

  }

  if(random(20)>8){

  this.nx = random(4)-2;

  this.ny = random(4)-2;

  }

  this.ax *= .75;

  this.ay *= .75;

  this._x += this.ax+this.nx;

  this._y += this.ay+this.ny;

  this._alpha -= 7;

  if (this._alpha<=0) {

  delete this.onEnterFrame;

  }

  };

  }

  }

  function overFun(n:Number){ //游戏结束或开始的初始函数;

  var mc=attachMovie("over_mc","over_mc",getNextHighestDepth());

  mc.gotoAndStop(n);

  mc.score_txt.text = score_txt.text;

  localObjFun(mc)//加载并显示积分榜;

  mc.cego_mc._visible = false;

  mc.kong_mc.setMask(mc.mask_mc);

  var kongY = mc.kong_mc._y;

  var kongH = mc.kong_mc._height+10;

  var maskH = mc.mask_mc._height;

  mc.kong_mc.onEnterFrame = function(){//积分榜过长时让它滚动;

  if(mc.mask_mc.hitTest(_root._xmouse,_root._ymouse,false)){

  if(kongH > maskH){

  if(mc.mask_mc._ymouse > maskH/2){

  if(mc.kong_mc._y >maskH-kongH+kongY){

  mc.kong_mc._y += (maskH/2-mc.mask_mc._ymouse)*.2;

  }else{

  mc.kong_mc._y = maskH-kongH+kongY;

  }

  }else{

  if(mc.kong_mc._y < kongY){

  mc.kong_mc._y -= (mc.mask_mc._ymouse-maskH/2)*.2;

  }else{

  mc.kong_mc._y = kongY;

  }

  }

  }

  }

  }

  mc.start_btn.onPress = function(){

  this._parent.removeMovieClip();

  gameFun();//游戏开始;

  }

  mc.save_btn.onPress = function(){

  mc.cego_mc._visible = true;

  saveFun(mc);//保存积分;

  localObjFun(mc)//刷新积分榜;

  this.enabled = false;

  }

  mc.again_btn.onPress = function(){

  var aa=this._parent._parent;

  for(var i in aa){

  aa[i].removeMovieClip();

  }

  initializeFun()//重新初始化所有变量;

  this._parent.removeMovieClip();

  gameFun();//游戏开始;

  }

  }

  function initializeFun(){//重新初始化所有变量;

  lenNum = 4;//蛇身长度

  xn=4;//初始化蛇出现的位置;

  yn=6;//初始化蛇出现的位置;

  firstNum = 0;//蛇头的序号;

  secondlyNum = 0;//蛇身的序号;

  lastNum = 0;//蛇尾的序号;

  reatNum = 3;//蛇速度初始值;

  scoreNum = 0;//记录玩家得分;

  fangxang = "right";//存储方向的变量;

  moveX = 1;//向右移动;

  moveY = 0;//上下没有移动;

  placeArry = new Array();//记录蛇的每个节点位置的数组;

  for(var i=0;i<19;i++){ placeArry[i]=[];}//声明为二维数组;

  }

  function localObjFun(mc:MovieClip){//显示玩家的成绩;即显示积分榜;

  var localObj:SharedObject = SharedObject.getLocal("snakeGame", "/");

  if(localObj.data.snakeGame){

  var len = localObj.data.snakeGame.length;

  var soArray:Array = localObj.data.snakeGame;

  soArray.sortOn("score",18);//按score的大小排序;

  for(var i=0;i< p>

  var new_mc = mc.kong_mc.attachMovie("积分","jifen_mc"+i,i,{_y:i*16})

  new_mc.rank_txt.text = i+1;

  new_mc.content_txt.text = soArray[i].score+"分 -"+soArray[i].username;

  }

  }else{

  localObj.data.snakeGame = [];

  localObj.flush();

  }

  }

  function saveFun(mc:MovieClip){//保存玩家的成绩

  var localObj:SharedObject = SharedObject.getLocal("snakeGame", "/");

  var saveN = mc.name_txt.text;

  var saveS = Number(mc.score_txt.text);

  var saveObj:Object = {username:saveN,score:saveS};

  var saveArray:Array = localObj.data.snakeGame;

  saveArray.push(saveObj);

  localObj.data.snakeGame = saveArray;

  localObj.flush();

  }

  function pauseFun():Boolean{

  delete this.onEnterFrame;

  return true;

  }

  var fangxang:String = "right";//存储方向的变量;

  var moveX:Number = 1;//向右移动

  var moveY:Number = 0;//上下没有移动;

  var keyListener:Object = {};//响应鼠标的对象;

  Key.addListener(keyListener);

  keyListener.onKeyDown = function() {

  keyName = Key.getCode();

  if (keyName == Key.DOWN) {

  if(moveY==0)fangxang = "down";

  } else if (keyName == Key.LEFT) {

  if(moveX==0)fangxang = "left";

  } else if (keyName == Key.UP) {

  if(moveY==0)fangxang = "up";

  } else if (keyName == Key.RIGHT) {

  if(moveX==0)fangxang = "right";

  }

  if(keyName == 32){

  if(un=!un){

  pauseFun();

  }else{

  gameFun();

  }

  }

  };

  function keyResponseFun() {

  if (moveX == 0) {

  (fangxang == "left") ? (moveX=-1, moveY=0) : (moveX);

  (fangxang == "right") ? (moveX=1, moveY=0) : (moveX);

  }

  if (moveY == 0) {

  (fangxang == "up") ? (moveY=-1, moveX=0) : (moveY);

  (fangxang == "down") ? (moveY=1, moveX=0) : (moveY);

  }

  }

  var n:Number = 25;//移动频辐25个象素;

  var xn:Number = 4;//初始值,定位蛇初始x位置;

  var yn:Number = 6;//初始值,定位蛇初始y位置;

  var lenNum:Number = 4;//初始值,定义蛇最初的长度;

  var cycleNum:Number = 12*19;//循环周期值;

  var firstNum:Number = 0;//蛇头的序号;

  var secondlyNum:Number = 0;//蛇身的序号;

  var lastNum:Number = 0;//蛇尾的序号;

  var reatNum:Number = 3;//蛇速度初始值;

  var scoreNum:Number = 0;//记录玩家得分;

  var placeArry:Array = new Array();//记录蛇的每个节点位置的数组;

  for(var i=0;i<19;i++){ placeArry[i]=[];}//声明为二维数组;

  //-----------------------------------------//

  function gameFun(){

  if(keyName != 32){

  foodFun();//显示食物;

  }

  onEnterFrame = function(){

  if(reatNum==0){

  reatNum=4;//蛇速度

  keyResponseFun();//键盘响应函数;

  xn += moveX;//xn=xn%19;

  yn += moveY;//yn=yn%12;

  countFun(cycleNum);//算出蛇头蛇尾的序号;

  if(gameStatus()){return;}//游戏状态判断;

  this.attachMovie("a_mc","mc"+firstNum,firstNum,{_x:n*xn,_y:n*yn});

  this["mc"+firstNum].gotoAndStop(fangxang);

  arrayFun();//给数组赋值;即记录蛇所在的位置;

  this["mc"+secondlyNum].gotoAndStop("body");

  this["mc"+lastNum].removeMovieClip();

  }

  reatNum--;//

  }

  }

  overFun(1);

  Stage.scaleMode = "noscale";

分享打印返回


动画知识Animation knowledge

广州形动动画设计有限公司

电话:020-87612322

传真:020-87615976

手机:13570023373

Q Q: 853033818

邮箱:853033818@qq.com

地址:广州市越秀区达道路16号

最新动态

形动动画承接以下业务:flash制作、动漫制作、二维动画制作、2d动画设计,如有这方面的需要,请与我们联系:020-87612322!

Copyright © 2007-2014 广州形动动画设计有限公司  网站备案/许可证号:粤ICP备17050071号-1

业务咨询:020-87612322  招聘热线:020-87615976  地址:中国·广东省广州市越秀区东山街道寺右新马路108号丰伟大厦A座7楼