flash游戏开发-as3实现的俄罗斯方块

作者:袖梨 2022-07-02

  1. package
  2. {
  3. import flash">flash.display.Sprite;
  4. import flash.events.Event;
  5. import flash.events.KeyboardEvent;
  6. import flash.events.MouseEvent;
  7. import flash.events.TimerEvent;
  8. import flash.geom.Point;
  9. import flash.text.TextField;
  10. import flash.ui.Keyboard;
  11. import flash.utils.Timer;
  12. import flash.net.*;
  13. /**
  14. * ...
  15. * @author sliz
  16. * @qq:405628079
  17. */
  18. [SWF(, , backgroundColor="0x000000", frameRate="60")]
  19. public class Main extends Sprite
  20. {
  21. private var deadBoxs:Array = new Array();
  22. private var liveBoxs:Array = new Array();
  23. private var xNum:int = 20;
  24. private var yNum:int = 40;
  25. private var w:Number = 10;
  26. private var world:Sprite = new Sprite();
  27. private var liveWrapper:Sprite = new Sprite();

  28. private var keyRight :Boolean = false;
  29. private var keyLeft :Boolean = false;
  30. private var keyUp:Boolean = false;
  31. private var keyRollLeft:Boolean = false;
  32. private var keyRollRight:Boolean = false;

  33. private var lives:Array = new Array();
  34. private var livesLength:int = 5;

  35. public function Main():void
  36. {
  37. init();
  38. }

  39. private function init():void
  40. {
  41. ////////////////////////////
  42. var lable1:TextField = new TextField();
  43. addChild(lable1);
  44. lable1.textColor = 0xffffff;
  45. lable1.text = "上,下,左,右,空格";
  46. var lable2:TextField = new TextField();
  47. addChild(lable2);
  48. lable2.textColor = 0xffffff;
  49. lable2.text = "sliz";
  50. lable2.;
  51. lable2.
  52. lable2.addEventListener(MouseEvent.CLICK, clickHandler);
  53. lable2.x = stage.stageWidth - lable2.width;
  54. lable2.y = stage.stageHeight - lable2.height;
  55. ///////////////////////////
  56. var timer:Timer = new Timer(1000);
  57. var timer2:Timer = new Timer(100);
  58. addChild(world);
  59. world.x = stage.stageWidth / 2 - xNum * w / 2;
  60. world.y = stage.stageHeight / 2 - yNum * w / 2
  61. addChild(liveWrapper);
  62. for (var i:int = 0; i
  63. deadBoxs = new Array();
  64. for (var j:int = 0; j
  65. deadBoxs[j] = 0;
  66. }
  67. }
  68. lives[0] = 1;
  69. for (i = 0; i
  70. initLive();
  71. }
  72. update();
  73. stage.addEventListener(MouseEvent.CLICK, clickHandler);
  74. stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
  75. stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
  76. stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
  77. timer.addEventListener(TimerEvent.TIMER, timeHandler);
  78. timer2.addEventListener(TimerEvent.TIMER, timeHandler2);
  79. timer2.start();
  80. timer.start();
  81. }
  82. private function initLive():void {
  83. var r:Number = Math.random();
  84. if (r
  85. lives[lives[0]] = 0;
  86. liveBoxs[0]={ x:1, y:0 } ;
  87. liveBoxs[1]={ x:0, y:0} ;
  88. liveBoxs[2]={ x:2, y:0 } ;
  89. liveBoxs[3] = { x:3, y:0 } ;
  90. }else if (r
  91. lives[lives[0]] = 1;
  92. liveBoxs[0]={ x:1, y:0 } ;
  93. liveBoxs[1]={ x:0, y:0} ;
  94. liveBoxs[2]={ x:1, y:1 } ;
  95. liveBoxs[3]={ x:2, y:1 } ;
  96. }else if (r
  97. lives[lives[0]] = 2;
  98. liveBoxs[0]={ x:1, y:0 } ;
  99. liveBoxs[1]={ x:0, y:1} ;
  100. liveBoxs[2]={ x:1, y:1 } ;
  101. liveBoxs[3]={ x:2, y:0 } ;
  102. }else if (r
  103. lives[lives[0]] = 3;
  104. liveBoxs[0]={ x:0, y:0 } ;
  105. liveBoxs[1]={ x:0, y:1} ;
  106. liveBoxs[2]={ x:1, y:1 } ;
  107. liveBoxs[3]={ x:1, y:0 } ;
  108. }else {
  109. lives[lives[0]] = 4;
  110. liveBoxs[0]={ x:1, y:1 } ;
  111. liveBoxs[1]={ x:1, y:0} ;
  112. liveBoxs[2]={ x:0, y:1 } ;
  113. liveBoxs[3]={ x:2, y:1 } ;
  114. }
  115. move((int)(xNum / 2), 1);
  116. if (lives[0] == livesLength) {
  117. lives[0] = 1;
  118. }else {
  119. lives[0]++;
  120. }
  121. }
  122. private function liveToDead():void {
  123. for (var i:int = 0; i
  124. deadBoxs[liveBoxs.y][liveBoxs.x] = 1;
  125. }
  126. }
  127. private function clear():void {
  128. for (var i:int = 0; i
  129. var counter:int = 0;
  130. for (var j:int = 0; j
  131. if (deadBoxs[j] != 1) {
  132. break;
  133. }
  134. counter++;
  135. }
  136. if (counter==xNum) {
  137. for (j = 0; j
  138. deadBoxs[j] = 0;
  139. }
  140. for (var ii:int = i; ii > 0;ii-- ) {
  141. for (j = 0; j
  142. deadBoxs[ii][j] = deadBoxs[ii-1][j];
  143. }
  144. }
  145. for (j = 0; j
  146. deadBoxs[0][j] = 0;
  147. }
  148. }
  149. }
  150. }
  151. private function isBottom():Boolean {
  152. var ret:Boolean = false;
  153. for (var i:int = 0; i
  154. if (liveBoxs.y ==yNum-1) {
  155. ret = true;
  156. break
  157. }
  158. for (var ii:int = 0; ii
  159. for (var j:int = 0; j
  160. if ((deadBoxs[ii][j]==1)&&(liveBoxs.y==ii-1)&&(liveBoxs.x==j)) {
  161. ret = true;
  162. break
  163. }
  164. }
  165. }
  166. }
  167. return ret;
  168. }
  169. private function isLive():Boolean {
  170. var ret:Boolean = true;
  171. for (var i:int = 0; i
  172. if (
  173. (liveBoxs.x
  174. ||(liveBoxs.x> ( xNum - 1))
  175. ||(liveBoxs.y
  176. ||(liveBoxs.y>(yNum-1))
  177. ) {
  178. ret = false;
  179. break
  180. }
  181. for (var ii:int = 0; ii
  182. for (var j:int = 0; j
  183. if ((deadBoxs[ii][j]==1)&&(liveBoxs.x==j)&&(liveBoxs.y==ii)) {
  184. ret = false;
  185. break
  186. }
  187. }
  188. }
  189. }
  190. return ret;
  191. }
  192. private function move(x:int=1, y:int=0):void {
  193. var liveBoxsShadow:Array = new Array();
  194. for (var i:int = 0; i
  195. liveBoxsShadow = {x:liveBoxs.x,y:liveBoxs.y};
  196. liveBoxs.x += x;
  197. liveBoxs.y += y;
  198. }
  199. if (!isLive()) {
  200. for (i = 0; i
  201. liveBoxs = {x:liveBoxsShadow.x,y:liveBoxsShadow.y};
  202. }
  203. }
  204. }
  205. private function roll(b:Boolean = true):void {
  206. var liveBoxsShadow:Array = new Array();
  207. for (var i:int = 0; i
  208. liveBoxsShadow = {x:liveBoxs.x,y:liveBoxs.y};
  209. }
  210. if (b) {
  211. for (i = 1; i
  212. var tempIX:int=liveBoxs.x;
  213. liveBoxs.x = liveBoxs[0].x+liveBoxs.y-liveBoxs[0].y;
  214. liveBoxs.y = liveBoxs[0].y -tempIX +liveBoxs[0].x;
  215. }
  216. }else {
  217. for (i= 1; i
  218. tempIX=liveBoxs.x;
  219. liveBoxs.x = liveBoxs[0].x-liveBoxs.y+liveBoxs[0].y;
  220. liveBoxs.y = liveBoxs[0].y +tempIX -liveBoxs[0].x;
  221. }
  222. }
  223. if (!isLive()) {
  224. for (i = 0; i
  225. liveBoxs = {x:liveBoxsShadow.x,y:liveBoxsShadow.y};
  226. }
  227. }
  228. }
  229. private function update():void {
  230. world.graphics.clear();
  231. world.graphics.lineStyle(1, 0xffffff);
  232. world.graphics.lineTo(xNum * w , 0);
  233. world.graphics.lineTo(xNum * w ,yNum * w);
  234. world.graphics.lineTo(0, yNum * w );
  235. world.graphics.lineTo(0,0);
  236. world.graphics.lineStyle();

  237. for (var i:int = 0; i
  238. for (var j:int = 0; j
  239. if (deadBoxs[j] == 1) {
  240. world.graphics.beginFill(0x123456);
  241. world.graphics.drawRect(j * w, i * w, w, w);
  242. }
  243. }
  244. }
  245. var color:Number;
  246. if (lives[0]==4) {
  247. color = 0x00ffff;
  248. }else if (lives[0]==1) {
  249. color = 0xff0000;
  250. }else if (lives[0]==2) {
  251. color = 0x00ff00;
  252. }else if (lives[0]==3) {
  253. color=0x0000ff
  254. }else {
  255. color=0xffff00
  256. }
  257. for (i = 0; i
  258. world.graphics.beginFill(color,0.9);
  259. world.graphics.drawRect(liveBoxs.x*w,liveBoxs.y*w,w,w);
  260. }
  261. world.graphics.lineStyle(1, 0xffffff, 0.5);
  262. for (i = 0; i
  263. world.graphics.moveTo(w * (i + 1), 0);
  264. world.graphics.lineTo(w * (i + 1), w*yNum);
  265. }
  266. for (i = 0; i
  267. world.graphics.moveTo(0, w*(i+1));
  268. world.graphics.lineTo(w * xNum , w*(i+1));
  269. }
  270. }
  271. private function clickHandler(e:MouseEvent):void {
  272. navigateToURL( new URLRequest ("http://*spa**ce.flash8.net/space/?534614" ) );
  273. }
  274. private function timeHandler2(e:TimerEvent):void {
  275. if (keyLeft) {
  276. move(-1, 0);
  277. }
  278. if (keyRight) {
  279. move(1, 0);
  280. }
  281. if (keyRollLeft) {
  282. roll();
  283. }
  284. if (keyRollRight) {
  285. roll(false);
  286. }
  287. }
  288. private function timeHandler(e:TimerEvent):void {
  289. move(0, 1);
  290. if (isBottom()) {
  291. liveToDead();
  292. clear();
  293. initLive();
  294. }
  295. }
  296. private function enterFrameHandler(e:Event):void {
  297. if (keyUp) {
  298. while (!isBottom()) {
  299. move(0, 1);
  300. }
  301. liveToDead();
  302. clear();
  303. initLive();
  304. keyUp = false;
  305. }
  306. update();
  307. }
  308. private function keyDownHandler(e:KeyboardEvent):void {
  309. switch(e.keyCode){
  310. case Keyboard.LEFT:
  311. keyLeft = true;
  312. keyRight = false;
  313. break;
  314. case Keyboard.RIGHT:
  315. keyRight = true;
  316. keyLeft = false;
  317. break;
  318. case Keyboard.SPACE:
  319. keyUp = true;
  320. break;
  321. case Keyboard.UP:
  322. keyRollLeft = true;
  323. keyRollRight = false;
  324. break;
  325. case Keyboard.DOWN:
  326. keyRollRight = true;
  327. keyRollLeft = false;
  328. break;
  329. }
  330. }
  331. private function keyUpHandler(e:KeyboardEvent):void {
  332. switch(e.keyCode){
  333. case Keyboard.LEFT:
  334. keyLeft = false;
  335. break;
  336. case Keyboard.RIGHT:
  337. keyRight = false;
  338. break;
  339. case Keyboard.UP:
  340. keyRollLeft = false;
  341. break;
  342. case Keyboard.DOWN:
  343. keyRollRight = false;
  344. break;
  345. }
  346. }
  347. }
  348. }

相关文章

精彩推荐