社区应用 最新帖子 精华区 社区服务 会员列表 统计排行 道具中心
主题 : 100行实现一个飞行射击游戏。(CS4)
貘良了 离线
级别: 分版主
显示用户信息 
0  发表于: 2009-12-13   

100行实现一个飞行射击游戏。(CS4)

记得用CS4版本,因为有Vector,CS3编译不了。
CTRL 射击,方向键移动,SHIFT减慢并固定射击角度。
flash: http://dl.dropbox.com/u/2877500/download/100.swf

复制代码
  1. package {
  2.     import flash.display.*;
  3.     import flash.events.*;
  4.     import flash.filters.*;
  5.     import flash.text.*;
  6.     import flash.geom.*;
  7.     [SWF(width = "465", height = "465", frameRate = "30", backgroundColor = "#ffffff")]
  8.     public class main extends Sprite {
  9.         function main() {
  10.             var i:int, ii:*, cnt:int=0, kf:uint=0, kmap:Array=[32,16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,4,8],
  11.                 sb:TextField=new TextField(), tt:TextField=new TextField(), fld:Sprite=addChild(new Sprite()) as Sprite,
  12.                 ee:Array=[new GlowFilter(0xff0000)], oe:Array=[new GlowFilter(0x0000ff)], rc:Array=[1,2,2,2,2],
  13.                 me:MovieClip, pd:Point = new Point(1,0), _b:Array=[], _s:Array=[], score:int=0, level:int=0;
  14.             $(tt,{x:180,y:230,htmlText:"<font color='#406080' size='40' face='_sans'>100Lines Arena</font>",width:282});
  15.             stage.addEventListener("keyDown",function(e:KeyboardEvent) : void { kf |=  kmap[e.keyCode-16]; });
  16.             stage.addEventListener("keyUp",  function(e:KeyboardEvent) : void { kf &= ~kmap[e.keyCode-16]; });
  17.             addChild(sb).parent.addChild(tt).parent.addEventListener("enterFrame", function(e:Event) : void {
  18.                 sb.htmlText="<font color='#80c0ff' size='20' face='_sans'>Score:" + String(score) + "</font>";
  19.                 if (tt.visible && cnt>30 && kf!=0) tt.visible = Boolean(level = score = 0);
  20.                 if (!tt.visible && (tt.visible = !(me.visible = !hit(me, _b, 36)))) cnt = 0;
  21.                 if (!((++cnt)&31) && !tt.visible) {
  22.                     var r:Number = Math.random()*32,
  23.                         x:Number = (r>=24)?0:(r>=16)?8:(r>=8)?(r-8):r, y:Number = (r<8)?0:(r<16)?8:(r<24)?(r-16):(r-24);
  24.                     ii = {x:x*100-400, y:y*100-400, v:new Point(4-x,4-y), fn:_e, cnt:0, r:5, l:3, filters:ee};
  25.                     $(fld.addChild(new MovieClip()), ii, 0xffff80, rc, [20,20,20,-20,-20,-20,-20,20,20,20]);
  26.                 }
  27.             });
  28.             for (i=-400; i<=400; i+=100) $(fld, {rotationX:-30}, 0x80c0ff, [1,2,1,2], [-400,i,400,i,i,-400,i,400]);
  29.             $(fld.addChild(me=new MovieClip()), {fn:_p,l:0,filters:oe}, 0x80ffff, [1,2,2,2], [-9,6,-9,-6,9,0,-9,6]);
  30.             function $(mc:*, props:*, color:uint=0, commands:Array=null, vertices:Array=null) : void {
  31.                 for (var p:String in props) mc[p] = props[p];
  32.                     if (color)    mc.graphics.lineStyle(1, color, 1, false, "normal", null, null,3);
  33.                 if (commands) mc.graphics.drawPath(Vector.<int>(commands), Vector.<Number>(vertices), "nonZero");
  34.                 if (props.fn) mc.addEventListener("enterFrame", props.fn);
  35.                 if (props.ar) mc.ar.push(mc);
  36.             }
  37.             function _p(e:Event) : void {   // player
  38.                 var r:Number=(kf&32)?6:9, dir:Point = new Point((((kf&4)>>2)-(kf&1))*r, (((kf&8)>>3)-((kf&2)>>1))*r);
  39.                 me.x += (me.x+dir.x>-390 && me.x+dir.x<390) ? dir.x : 0;
  40.                 me.y += (me.y+dir.y>-390 && me.y+dir.y<390) ? dir.y : 0;
  41.                 fld.x += ((232-me.x) - fld.x)*0.1;
  42.                 fld.y += ((232-me.y) - fld.y)*0.1;
  43.                 if (!(kf&32)) {
  44.                     pd.offset(dir.x*0.05, dir.y*0.05);
  45.                     pd.normalize(1);
  46.                     me.rotation = Math.atan2(pd.y, pd.x)*57.29577951308232;
  47.                 }
  48.                 if (!tt.visible && (kf&16) && (cnt&1)) {    // create shot
  49.                     ii={x:me.x,y:me.y,v:new Point(pd.x*24,pd.y*24),rotation:me.rotation,r:0,filters:oe,fn:mv,ar:_s};
  50.                     $(fld.addChild(new MovieClip()), ii, 0x80ffff, [1,2,1,2], [6,6,-6,6,6,-6,-6,-6]);
  51.                 }
  52.             }
  53.             function _e(e:Event) : void {   // enemy
  54.                 e.target.cnt++;
  55.                 if (!tt.visible && !(e.target.cnt % (80-level))) {  // create bullet
  56.                     var v:Point = new Point(me.x - e.target.x, me.y - e.target.y);
  57.                     v.normalize((Math.random()*0.06+0.03)*(50+level));
  58.                     ii = {x:e.target.x, y:e.target.y, v:v, r:-5, fn:mv, ar:_b, filters:ee};
  59.                     $(fld.addChild(new MovieClip()), ii, 0xffffff, rc, [3,3,-3,3,-3,-3,3,-3,3,3]);
  60.                 }
  61.                 if (hit(e.target, _s, 400)) {  // destruction
  62.                     kill(e.target);
  63.                     level = (++score<30) ? (score*2) : (score<60) ? (score*0.5+45) : 75;
  64.                     for (i=0; i<8; i++) {   // create particles
  65.                         v = new Point(Math.random()*16-8, Math.random()*16-8);
  66.                         ii = {x:e.target.x, y:e.target.y, v:v, r:20, fn:_x, cnt:30, filters:oe};
  67.                         $(fld.addChild(new MovieClip()), ii, 0x80ffff, rc, [5,5,5,-5,-5,-5,-5,5,5,5]);
  68.                     }
  69.                 }
  70.                 mv(e);
  71.             }
  72.             function _x(e:Event) : void {   // explosion
  73.                 if ((e.target.alpha = (--e.target.cnt)*0.03) == 0) kill(e.target);
  74.                 else {
  75.                     e.target.v.x *= 0.95;
  76.                     e.target.v.y *= 0.95;
  77.                     mv(e);
  78.                 }
  79.             }
  80.             function mv(e:Event) : void {   // common motion
  81.                 e.target.rotation += e.target.r;
  82.                 e.target.x += e.target.v.x;
  83.                 e.target.y += e.target.v.y;
  84.                 if (e.target.x<-400 || e.target.y<-400 || e.target.x>400 || e.target.y>400) kill(e.target);
  85.             }
  86.             function hit(t:*, list:Array, r2:Number) : Boolean {   // hit evaluation
  87.                 for (i=0; i<list.length; i++)
  88.                     if ((list[i].x-t.x)*(list[i].x-t.x)+(list[i].y-t.y)*(list[i].y-t.y) < r2) {
  89.                         kill(list[i]);
  90.                         if (--t.l <= 0) return true;
  91.                     }
  92.                 return false;
  93.             }
  94.             function kill(mc:*) : void {   // kill object
  95.                 mc.parent.removeChild(mc).removeEventListener("enterFrame", mc.fn);
  96.                 if (mc.ar) mc.ar = mc.ar.splice(mc.ar.indexOf(mc), 1);
  97.             }
  98.         }
  99.     }
  100. }
[ 此帖被貘良了在2009-12-13 07:16重新编辑 ]
评价一下你浏览此帖子的感受

精彩

感动

搞笑

开心

愤怒

无聊

灌水
zapper 离线
级别: ④见习
显示用户信息 
1  发表于: 2010-03-15   
不知是dropbox叉了还是我叉了...点开是无法显示该页
笔没水了,暂时无法签名
bluewait 离线
级别: ①新人
显示用户信息 
2  发表于: 2010-06-20   
这个很强大,绝对的as高手
deathdark 离线
级别: 总版主
显示用户信息 
3  发表于: 2015-08-24   
很喜欢做飞行射击的游戏 我参考下
欢迎有爱的新人老鸟来空想场所畅谈!
点击进入
描述
快速回复

认证码:

验证问题:
12-5=?,答案:7 正确答案:7
按"Ctrl+Enter"直接提交