艺虎动画 > flash AS3游戏源码制作:3D俄罗斯方块游戏

flash AS3游戏源码制作:3D俄罗斯方块游戏

翼虎动漫   2011-1-17

http://img.flashzhizuo.net.cn/us/2011117182947990.swf

需要pv3d 引擎的支持:

  1. package { 
  2.     import flash.display.Sprite; 
  3.     import flash.text.TextField; 
  4.     import flash.events.Event; 
  5.     import flash.events.KeyboardEvent; 
  6.     import flash.utils.getTimer; 
  7.  
  8.     import org.papervision3d.materials.*; 
  9.     import org.papervision3d.materials.utils.*; 
  10.     import org.papervision3d.materials.shadematerials.FlatShadeMaterial; 
  11.     import org.papervision3d.lights.PointLight3D; 
  12.     import org.papervision3d.objects.*; 
  13.     import org.papervision3d.objects.primitives.* 
  14.     import org.papervision3d.view.*; 
  15.     import org.papervision3d.scenes.Scene3D; 
  16.  
  17.     [SWF(backgroundColor = "0x000000", frameRate = "24")] 
  18.      
  19.     public class Tetris extends BasicView { 
  20.         private const W:int = 10
  21.         private const H:int = 20
  22.         private const UNIT:int = 16
  23.         private const COLOR:Array =[0x0000000x00FFFF0xFFFF000x22FF22
    0xFF22220x4444FF0xFF88440xFF22FF]; 
  24.         private const PAT:Array =[ 
  25.             [[1111]], 
  26.             [[020], [222]], 
  27.             [[330], [033]], 
  28.             [[044], [440]], 
  29.             [[55], [50], [50]], 
  30.             [[66], [06], [06]], 
  31.             [[77], [77]] 
  32.         ]; 
  33.         private const SPEED:Array = [3020105]; 
  34.  
  35.         private const VK_H:int = 72// h 
  36.         private const VK_J:int = 74// j 
  37.         private const VK_K:int = 75// k 
  38.         private const VK_L:int = 76// l 
  39.         private const VK_SPC:int = 32// space 
  40.  
  41.         private var field:Array = []; 
  42.         private var piece:Array; 
  43.         private var next:Array; 
  44.         private var text:TextField = new TextField(); 
  45.         private var keytable:Array = []; 
  46.         private var count:int = 0
  47.         private var step:int = 0
  48.         private var px:int
  49.         private var py:int
  50.  
  51.         private var light:PointLight3D; 
  52.  
  53.         public function Tetris() { 
  54.             Wonderfl.capture_delay(25); 
  55.  
  56.             light = new PointLight3D(false); 
  57.             light.x = 500
  58.             light.y = -750
  59.             light.z = 1500
  60.  
  61.             text.x = 300
  62.             text.textColor = 0xFFFFFF
  63.             text.text="Next:"
  64.             addChild(text); 
  65.             var t:TextField = new TextField(); 
  66.             t.textColor = 0xFFFFFF
  67.             t.text = "Keys: H, J, K, L, SPACE"
  68.             t.scaleX = t.scaleY = 1.25
  69.             t.width = 250
  70.  
  71.             addChild(t); 
  72.             field = new Array(H).map(function():*{ 
  73.                     return new Array(W).map(function():*{ 
  74.                             return 0
  75.                         }) 
  76.                 }); 
  77.             keytable[VK_H] = function():void {px -= space(px-1, py, piece)}; 
  78.             keytable[VK_J] = function():void {rotate(true)}; 
  79.             keytable[VK_K] = function():void {rotate(false)}; 
  80.             keytable[VK_L] = function():void {px += space(px+1, py, piece)}; 
  81.             keytable[VK_SPC] = function():void {drop(); pick();}; 
  82.             stage.addEventListener(KeyboardEvent.KEY_DOWN, function(e:KeyboardEvent):void { 
  83.                     if (keytable[e.keyCode]) { 
  84.                         var oldpx:int = px; 
  85.                         var oldpy:int = py; 
  86.                         keytable[e.keyCode](); 
  87.                         repaint(); 
  88.                     } 
  89.                 }); 
  90.             pick(); 
  91.             pick(); 
  92.             addEventListener(Event.ENTER_FRAME, function(e:Event):void { 
  93.                     if (--step < 0) { 
  94.                         step=SPEED[int(count/10)]; 
  95.                         if (space(px, py+1, piece)) { 
  96.                             ++py; 
  97.                             repaint(); 
  98.                         } else { 
  99.                             drop(); 
  100.                             pick(); 
  101.                         } 
  102.                     } 
  103.                     //repaint(); 
  104.                 }); 
  105.         } 
  106.  
  107.         private function rotate(clock:Boolean):void { 
  108.             var r:Array = new Array(piece[0].length).map(function():*{return [];}); 
  109.             for (var j:int = 0; j<piece.length; ++j) 
  110.             for (var i:int = 0; i < r.length; ++i) 
  111.             if (clock) 
  112.             r[i][piece.length-1-j] = piece[j][i]; 
  113.             else 
  114.             r[r.length-1-i][j] = piece[j][i]; 
  115.             if (space(px, py, r)) 
  116.             piece = r; 
  117.         } 
  118.  
  119.         private function repaint():void { 
  120.             // TODO: 
  121.             // Reuse Scene3D and Cube objects. 
  122.             // Don’t recreate on every repaint. 
  123.              
  124.             scene = new Scene3D; 
  125.  
  126.             for( var x:int = -1; x <= W; x++ ) { 
  127.                 scene.addChild( cube( x, -10x444444 ) ); 
  128.                 scene.addChild( cube( x, H, 0x444444 ) ); 
  129.             } 
  130.             for( var y:int = -1; y <= H; y++ ) { 
  131.                 scene.addChild( cube( -1, y, 0x444444 ) ); 
  132.                 scene.addChild( cube( W, y, 0x444444 ) ); 
  133.             } 
  134.  
  135.             //graphics.clear(); 
  136.             for (var j:int = 0; j < H; ++j)  
  137.             for (var i:int = 0; i < W; ++i) { 
  138.                 var g:int = 0
  139.                 if (py <= j && j < (py+piece.length) && px <= i && i < (px+piece[0].length)) 
  140.                 g = piece[j-py][i-px]; 
  141.                 if (g == 0
  142.                 g = field[j][i]; 
  143.                 //graphics.beginFill(COLOR[g]); 
  144.                 //graphics.drawRect(i*UNIT, j*UNIT, UNIT, UNIT); 
  145.  
  146.                 if(g) scene.addChild( cube( i, j, COLOR[g] ) ); 
  147.             } 
  148.  
  149.             for (j = 0; j < next.length; ++j) 
  150.             for (i = 0; i < next[j].length; ++i) { 
  151.                 g = next[j][i]; 
  152.                 //graphics.beginFill(COLOR[ci]); 
  153.                 //graphics.drawRect((i+W+1)*UNIT, (j+2)*UNIT, UNIT, UNIT); 
  154.  
  155.                 if(g) scene.addChild( cube( i+W , j-H/2+1, COLOR[g], 33 ) ); 
  156.             } 
  157.  
  158.             camera.z = 300
  159.             camera.y = -120
  160.             renderer.renderScene( scene, camera, viewport ); 
  161.         } 
  162.  
  163.         private function cube( x:int, y:int, c:uint, w:int = W, h:int = H ):Cube { 
  164.          var mlist:MaterialsList = new MaterialsList( { all : new FlatShadeMaterial(light,c) } );             
  165.          var ret:Cube = new Cube( mlist, UNIT, UNIT, UNIT ); 
  166.          ret.x = -(x - w/2) * UNIT * 1.1
  167.          ret.y = -(y - h/2) * UNIT * 1.1
  168.          return ret; 
  169.         } 
  170.          
  171.  
  172.         private function space(x:int, y:int, p:Array):int { 
  173.             for (var j:int = 0; j < p.length; ++j) { 
  174.                 if (0 > (y+j) || (y+j) >= H) 
  175.                 return 0
  176.                 for (var i:int = 0; i < p[j].length; ++i) { 
  177.                     if (0 > (x+i) || (x+i) >= W) 
  178.                     return 0
  179.                     if (p[j][i] && field[y+j][x+i]) 
  180.                     return 0
  181.                 } 
  182.             } 
  183.             return 1
  184.         } 
  185.  
  186.         private function drop():void { 
  187.             for (; space(px, py+1, piece); py++) 
  188.             ; 
  189.             for (var j:int = 0; j < piece.length; ++j) 
  190.             for (var i:int = 0; i < piece[j].length; ++i) 
  191.             if (piece[j][i]) 
  192.             field[py+j][px+i] = piece[j][i]; 
  193.             for (j=0; j<H; ++j) 
  194.             if (field[j].indexOf(0) < 0) { 
  195.                 field.splice(j, 1); 
  196.                 field.unshift([]); 
  197.                 for (i=0; i<W; ++i) 
  198.                 field[0][i] = 0
  199.             } 
  200.  
  201.             count++; 
  202.             if (count/10 >= SPEED.length) 
  203.             count = 0
  204.         } 
  205.  
  206.         private function pick():void { 
  207.             piece = next; 
  208.             if (piece != null) { 
  209.                 px = (W-piece[0].length)/2
  210.                 py = 0
  211.                 if (!space(px, py, piece)) 
  212.                 text.text="GAME OVER"
  213.             } 
  214.             next = PAT[int(Math.random()*PAT.length)]; 
  215.         } 
  216.     } 
  217. }