FlashActionscript制作贪吃蛇简陋版

2008-04-02 11:03:17来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折


  全部代码如下:

  
/************2005.5**************/

  /*********贪吃蛇简陋版************/

  /***********寒冰(胡乱编写)********/

  var s_x = 0;

  var s_y = 0;

  var dir = [[-1, 0], [0, -1], [1, 0], [0, 1]];

  //四个方向

  var _dir;

  var c_x = (Stage.width-256)/2;

  var c_y = (Stage.height-256)/2;

  var map;

  var speed;

  var level = 1;

  var child_num;

  var Child;

  var snake_arr;

  init();

  function init() {

  speed = 15;

  child_num = 1;

  snake_arr = [];

  DrawOutRound();

  //画外框

  DrawMap();

  //画地图

  SnakeRun();

  //蛇移动

  MakeWall();

  //创建障碍物

  MakeChild();

  //创建食物(后来变成蛇身)

  createTextField("info", getNextHighestDepth(), 30, 350, 400, 20);

  createTextField("status", getNextHighestDepth(), 320, 370, 100, 20);

  status.text = "状态:playing";

  info.text = "[用方向键控制方向,dead后请点鼠标重新开始]____寒冰制造";

  }

  function DrawBox(name, color, w, h) {

  //画方块

  var _b = createEmptyMovieClip(name, getNextHighestDepth());

  with (_b) {

  lineStyle(0);

  beginFill(color);

  lineTo(w, 0);

  lineTo(w, h);

  lineTo(0, h);

  endFill();

  }

  return _b;

  }

  function DrawOutRound() {

  DrawBox("outround", 0xffffff, 400, 400);

  }

  function DrawMap() {

  var _box = DrawBox("box", 0xffffff, 16, 16);

  _box._visible = false;

  map = [];

  for (var i = 0; i<16; i ) {

  map[i] = [];

  for (var j = 0; j<16; j ) {

  map[i][j] = 0;

  _box.duplicateMovieClip("_map" i j, getNextHighestDepth(), {_x:c_x j*16, _y:c_y i*16});

  }

  }

  //trace(map)

  }

  function MakeSnake() {

  var Snake = DrawBox("snake", 0x000000, 16, 16);

  snake_arr.push(snake);

  Snake.x = s_x;

  Snake.y = s_y;

  //map[Snake.y][Snake.x] = 1;

  Snake._x = c_x s_x*16;

  Snake._y = c_y s_y*16;

  return Snake;

  }

  function SnakeRun() {

  var S = MakeSnake();

  var i = 0;

  _dir = dir[3];

  SnakeControl();

  S.onEnterFrame = function() {

  if ( i>100/speed) {

  ChildRun();

  i = 0;

  if (map[this.y _dir[1]][this.x _dir[0]] == 0) {

  } else if (map[this.y _dir[1]][this.x _dir[0]] == 8) {

  speed ;

  map[this.y _dir[1]][this.x _dir[0]] == 0;

  snake.swapDepths(getNextHighestDepth());

  Child._name = Child._name.substr(1);

  snake_arr.push(Child);

  MakeChild();

  } else {

  status.text = "状态:over";

  delete this.onEnterFrame;

  return;

  }

  this.x = _dir[0];

  this.y = _dir[1];

  this._x = c_x 16*this.x;

  this._y = c_y 16*this.y;

  }

  };

  //trace(S);

  }

  function SnakeControl() {

  var obj = {};

  obj.onKeyDown = function() {

  var key = Key.getCode();

  switch (key) {

  case 37 :

  _dir = dir[0];

  break;

  case 38 :

  _dir = dir[1];

  break;

  case 39 :

  _dir = dir[2];

  break;

  case 40 :

  _dir = dir[3];

  }

  };

  Key.addListener(obj);

  }

  function RndArr() {

  //随机乱数,为了取得随机的障碍物的位置,但是这里用随机是为了偷懒,应该固定位置的.

  var arr = [];

  for (var i = 1; i<256; i ) {

  arr[i-1] = i;

  }

  arr.sort(function () {

  return random(2) ? 1 : -1;

  });

  return arr;

  }

  function MakeChild() {

  var rgb = 0x0000ff;

  Child = DrawBox("_child" child_num, rgb, 16, 16);

  Child._alpha = 0;

  Child.onEnterFrame = function() {

  if ((this._alpha = 5)>100) {

  delete this.onEnterFrame;

  }

  };

  do {

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇: 用flash制作简单拼图游戏

下一篇: Flash技巧:关于人物行走动作分解的简化