最近买了 android ,在电车上挺无聊的,给android做了个小 游戏 玩玩,顺便弄了个flash版
游戏画面如下:

这个游戏实现起来很简单,代码也很少,首先需要几个碎图:

因为游戏简单,直接把相应的图做成MC来的比较快
一共以下几个MC
游戏精灵,普通地板,可旋转地板,左移地板,右移地板,弹跳地板,减HP地板
在各个MC内部添加几行代码

/*********可旋转地板**************/
//是否可站立,游戏精灵存在,则开始播放,在第五贞设定为false,并且开始翻转,表示不可站立
var st:Boolean = true;
var fun:Function;
stop();
/*********左移地板**************/
import flash.display.Sprite;

var st:Boolean = true;
var charaIsMove:Boolean = true;
//游戏精灵
var chara:Sprite;
onMove();
function onMove():void{
if(chara != null){
//游戏精灵不为空的时候,将游戏精灵向左移
if(chara.x < this.x || chara.x > this.x + this.width){
chara = null;
st = false;
}else{
chara.x -= 2;
}
}
}
/*********右移地板**************/
import flash.display.Sprite;

var st:Boolean = true;
var charaIsMove:Boolean = true;
//游戏精灵
var chara:Sprite;
onMove();
function onMove():void{
if(chara != null){
//游戏精灵不为空,将精灵向右移
if(chara.x < this.x || chara.x > this.x + this.width){
chara = null;
st = false;
}else{
chara.x += 2;
}
}
}
/*********弹跳地板**************/
import flash.display.Sprite;

var st:Boolean = true;
var charaIsMove:Boolean = true;
var chara:Object;
stop();
function onJump():void{
if(chara != null){
//游戏精灵不为空,则弹跳
chara.jumpSpeed = -15;
chara.jump = true;
chara = null;
}
}
/*********减HP地板**************/
//表示可站立
var st:Boolean = true;
//减HP
var minsHp:Boolean = true;

游戏实现部分,只需要一个类Main,下面是所有代码

  1. package game {
  2. import flash.display.Sprite;
  3. import flash.utils.getDefinitionByName;
  4. import flash.events.Event;
  5. import flash.events.MouseEvent;
  6. import flash.text.TextField;
  7. import flash.events.KeyboardEvent;
  8. import flash.display.Shape;
  9. import flash.display.DisplayObjectContainer;
  10. import flash.system.fscommand;
  11. //import flash.desktop.NativeApplication;
  12. public class Main extends Sprite{
  13. private var _chara:chara = new chara();
  14. private var _wall:Sprite = new Sprite();
  15. private var _bak:Sprite = new Sprite();
  16. private var _bakImg:bak = new bak();
  17. private var _mai:mai = new mai();
  18. private var _ue:ue = new ue();
  19. private var _hp:hp = new hp();
  20. private var _chara_ctrl:Boolean = false;
  21. private var _sectend:int = 0;
  22. private var _speed:int = 2;
  23. private var _mcList:Array = new Array();
  24. private var _leftCtrl:Boolean = false;
  25. private var _rightCtrl:Boolean = false;
  26. private var _mcIndex:int = 0;
  27. private var _mcArray:Array = new Array();
  28. private var _txt:TextField = new TextField();
  29. private var _hpIndex:int = 1;
  30. private var _charaOld:int = 1;
  31. private var _shape:Shape = new Shape();
  32. private var _showText:Sprite = new Sprite();
  33. private var _gameStart:Boolean = false;
  34. private var _sound:jiao = new jiao();
  35. private var _hpAdd:int = 0;
  36. //加入地板
  37. private function getMC(my:int = 480,mx:int = -1):void{
  38. var i:int = Math.floor(Math.random()*_mcArray.length);
  39. var ClassReference:Class;
  40. ClassReference = getDefinitionByName(_mcArray[i]) as Class;
  41. var nmc:Sprite = new ClassReference as Sprite;
  42. nmc.y = my - this._wall.y;
  43. if(mx < 0){
  44. nmc.x = Math.floor(Math.random()*270) - 50;
  45. }else{
  46. nmc.x = mx;
  47. }
  48. this._mcList.push(nmc);
  49. this._bak.addChild(nmc);
  50. }
  51. public function Main() {
  52. //将各种地板加入数组
  53. //普通地板
  54. _mcArray.push("mc");
  55. //可旋转地板
  56. _mcArray.push("toumc");
  57. //左移地板
  58. _mcArray.push("leftMc");
  59. //右移地板
  60. _mcArray.push("rightMc");
  61. //弹跳地板
  62. _mcArray.push("tan");
  63. //减HP地板
  64. _mcArray.push("sMc");
  65. //添加各个画板层
  66. this._bak.addChild(_bakImg);
  67. this._wall.addChild(this._bak);
  68. this._wall.addChild(this._chara);
  69. this._chara.x = 160;
  70. this._chara.y = 100;
  71. this.addChild(this._wall);
  72. this.addChild(_mai);
  73. this.addChild(_ue);
  74. _hp.x = 20;
  75. _hp.y = 460;
  76. this.addChild(_hp);
  77. this.addChild(_shape);
  78. _txt.selectable = false;
  79. _txt.x = 20;
  80. _txt.y = 430;
  81. _txt.width = 200;
  82. _txt.height = 200;
  83. this.addChild(_txt);
  84. this.addChild(_showText);
  85. this.addEventListener(MouseEvent.MOUSE_DOWN,onDown);
  86. this.addEventListener(MouseEvent.MOUSE_UP,onUp);
  87. stage.addEventListener(KeyboardEvent.KEY_DOWN,isKeyDown,false,0,true);
  88. stage.addEventListener(KeyboardEvent.KEY_UP,isKeyUp,false,0,true);
  89. stage.showDefaultContextMenu = false
  90. //fscommand("showmenu", "false");
  91. init();
  92. }
  93. //游戏初始化
  94. private function init():void{
  95. this._bak.y = 0;
  96. this._wall.y = 0;
  97. var i:int;
  98. for(i=0;i<_mcList.length;i++){
  99. this._bak.removeChild(_mcList[i]);
  100. }
  101. _mcList.splice(0,_mcList.length);
  102. this._chara.x = 160;
  103. this._chara.y = 100;
  104. //添加游戏说明框
  105. _shape.graphics.lineStyle(5, 0x000000,0.8);
  106. _shape.graphics.beginFill(0x999999,0.6);
  107. _shape.graphics.drawRoundRect(50,100,220,300,20,20);
  108. _shape.graphics.lineStyle(5, 0x000000,0.8);
  109. _shape.graphics.beginFill(0x999999,0.6);
  110. _shape.graphics.drawRoundRect(70,300,180,40,10,10);
  111. _shape.graphics.lineStyle(5, 0x000000,0.8);
  112. _shape.graphics.beginFill(0x999999,0.6);
  113. _shape.graphics.drawRoundRect(70,350,180,40,10,10);
  114. var txt:TextField = new TextField();
  115. txt.selectable = false;
  116. txt.width = 200;
  117. txt.height = 400;
  118. txt.x = 60;
  119. txt.y = 110;
  120. txt.htmlText = "<font color='#FFFFFF' size='20'><b>" + "  100階にチャレンジ/n/nあなたができますか?/n/n"+
  121. "操作:/n</b></font>" +
  122. "<font color='#FFFFFF' size='15'><b>「←」キーをクリック:/n      ・左に移動/n" +
  123. "<font color='#FFFFFF' size='15'><b>「→」キーをクリック:/n      ・右に移動/n" +
  124. "</b></font>";
  125. _showText.addChild(txt);
  126. txt = new TextField();
  127. txt.selectable = false;
  128. txt.x = 120;
  129. txt.y = 305;
  130. txt.htmlText = "<font color='#FFFFFF' size='20'><b>" + "スタート"+ "</b></font>";
  131. _showText.addChild(txt);
  132. txt = new TextField();
  133. txt.selectable = false;
  134. txt.x = 120;
  135. txt.y = 355;
  136. txt.htmlText = "<font color='#FFFFFF' size='20'><b>" + " 閉じる"+ "</b></font>";
  137. _showText.addChild(txt);
  138. //gameStart();
  139. }
  140. //游戏结束
  141. private function gameOver():void{
  142. this._chara.jump = false;
  143. _leftCtrl = false;
  144. _rightCtrl = false;
  145. _sound.play();
  146. _hpIndex = 1;
  147. _hp.gotoAndStop(_hpIndex);
  148. _gameStart = false;
  149. _sectend = 0;
  150. this.removeEventListener(Event.ENTER_FRAME,onFrame);
  151. this._bak.y = 0;
  152. this._wall.y = 0;
  153. this._bakImg.y = 0;
  154. var i:int;
  155. for(i=0;i<_mcList.length;i++){
  156. this._bak.removeChild(_mcList[i]);
  157. }
  158. _mcList.splice(0,_mcList.length);
  159. this._chara.x = 160;
  160. this._chara.y = 100;
  161. _shape.graphics.lineStyle(5, 0x000000,0.8);
  162. _shape.graphics.beginFill(0x999999,0.6);
  163. _shape.graphics.drawRoundRect(50,100,220,300,20,20);
  164. _shape.graphics.lineStyle(5, 0x000000,0.8);
  165. _shape.graphics.beginFill(0x999999,0.6);
  166. _shape.graphics.drawRoundRect(70,300,180,40,10,10);
  167. _shape.graphics.lineStyle(5, 0x000000,0.8);
  168. _shape.graphics.beginFill(0x999999,0.6);
  169. _shape.graphics.drawRoundRect(70,350,180,40,10,10);
  170. var txt:TextField = new TextField();
  171. txt.selectable = false;
  172. txt.width = 200;
  173. txt.height = 400;
  174. txt.x = 60;
  175. txt.y = 110;
  176. txt.htmlText = "<font color='#FFFFFF' size='20'><b>" + "       GAME OVER/n/n"+
  177. "階数:" + _txt.text +
  178. "/n称号:" + getName(int(_txt.text)) +
  179. "/n</b></font>";
  180. _showText.addChild(txt);
  181. txt = new TextField();
  182. txt.selectable = false;
  183. txt.x = 120;
  184. txt.y = 305;
  185. txt.htmlText = "<font color='#FFFFFF' size='20'><b>" + "スタート"+ "</b></font>";
  186. _showText.addChild(txt);
  187. txt = new TextField();
  188. txt.selectable = false;
  189. txt.x = 120;
  190. txt.y = 355;
  191. txt.htmlText = "<font color='#FFFFFF' size='20'><b>" + " 閉じる"+ "</b></font>";
  192. _showText.addChild(txt);
  193. }
  194. //游戏称号
  195. private function getName(cnt:int):String{
  196. if(cnt < 10){
  197. return "赤ちゃん";
  198. }else if(cnt < 20){
  199. return "子供";
  200. }else if(cnt < 30){
  201. return "小学生";
  202. }else if(cnt < 40){
  203. return "中学生";
  204. }else if(cnt < 50){
  205. return "高校生";
  206. }else if(cnt < 60){
  207. return "大学生";
  208. }else if(cnt < 70){
  209. return "大学院生";
  210. }else if(cnt < 80){
  211. return "専門家";
  212. }else if(cnt < 90){
  213. return "高級専門家";
  214. }else{
  215. return "神";
  216. }
  217. }
  218. //游戏开始
  219. private function gameStart():void{
  220. //在画板上添加3个地板
  221. getMC(300,160);
  222. getMC(350);
  223. getMC(400);
  224. this.addEventListener(Event.ENTER_FRAME,onFrame,false,0,true);
  225. }
  226. //键盘按下
  227. private function isKeyDown(event:KeyboardEvent):void{
  228. if(!_gameStart){
  229. return;
  230. }
  231. if(event.keyCode == 37){
  232. if(_leftCtrl){
  233. return;
  234. }
  235. _leftCtrl = true;
  236. _rightCtrl = false;
  237. this._chara.gotoAndPlay("left");
  238. }else if(event.keyCode == 39){
  239. if(_rightCtrl){
  240. return;
  241. }
  242. _rightCtrl = true;
  243. _leftCtrl = false;
  244. this._chara.gotoAndPlay("right");
  245. }
  246. }
  247. //键盘弹起
  248. private function isKeyUp(event:KeyboardEvent):void{
  249. if(event.keyCode == 37){
  250. _leftCtrl = false;
  251. }else if(event.keyCode == 39){
  252. _rightCtrl = false;
  253. }
  254. }
  255. //鼠标按下
  256. private function onDown(event:MouseEvent):void{
  257. var intX:Number = event.currentTarget.mouseX;
  258. var intY:Number = event.currentTarget.mouseY;
  259. if(_gameStart){
  260. if(intX <= 160){
  261. if(_leftCtrl){
  262. return;
  263. }
  264. _leftCtrl = true;
  265. _rightCtrl = false;
  266. this._chara.gotoAndPlay("left");
  267. }else{
  268. if(_rightCtrl){
  269. return;
  270. }
  271. _rightCtrl = true;
  272. _leftCtrl = false;
  273. this._chara.gotoAndPlay("right");
  274. }
  275. }else if(intX > 70 && intX < 250 && intY > 300 && intY < 340){
  276. _gameStart = true;
  277. _shape.graphics.clear();
  278. this.removeAllChildren(_showText);
  279. gameStart();
  280. }else if(intX > 70 && intX < 250 && intY > 350 && intY < 390){
  281. fscommand("quit");
  282. //stage.nativeWindow.close();
  283. //NativeApplication.nativeApplication.exit();
  284. }
  285. stage.focus = this;
  286. }
  287. //删除地板方法
  288. public function removeAllChildren(
  289. container:DisplayObjectContainer ):void {
  290. var count:int = container.numChildren;
  291. for ( var i:int = 0; i < count; i++ ) {
  292. container.removeChildAt( 0 );
  293. }
  294. }
  295. //鼠标弹起
  296. private function onUp(event:MouseEvent):void{
  297. var intX:Number = event.currentTarget.mouseX;
  298. if(intX <= 160){
  299. _leftCtrl = false;
  300. }else{
  301. _rightCtrl = false;
  302. }
  303. }
  304. //逻辑部分
  305. private function onFrame(event:Event):void{
  306. var speedNum:Number = 0.03;
  307. var downlength:Number;
  308. var _child:Object;
  309. //找到画面外可删除地板,将其删除
  310. if(_mcList.length > 0 && _mcList[0].y - this._wall.y < 40){
  311. _child = _mcList[0] as Object;
  312. if(_child.charaIsMove != null && _child.chara != null){
  313. _child.chara = null;
  314. }
  315. this._bak.removeChild(_mcList[0]);
  316. _mcList.splice(0,1);
  317. }
  318. var i:int;
  319. this._wall.y -= _speed;
  320. //游戏精灵上升下降,以及上升下降幅度控制
  321. if(!_chara_ctrl){
  322. this._chara.y += _speed;
  323. downlength = speedNum*_sectend*_sectend;
  324. if(downlength > 37){
  325. downlength = 37;
  326. }
  327. this._chara.y += downlength;
  328. _sectend++;
  329. }else if(this._chara.jump){
  330. this._chara.y += _speed;
  331. this._chara.y -= 0.06*this._chara.jumpSpeed*this._chara.jumpSpeed;
  332. //charaOld = this._chara.y;
  333. this._chara.jumpSpeed++;
  334. if(this._chara.jumpSpeed >= 0){
  335. this._chara.jump = false;
  336. }
  337. }
  338. //是否处在弹跳阶段
  339. if(!this._chara.jump){
  340. _chara_ctrl = false;
  341. }else{
  342. if(this._chara.jumpSpeed >= 0){
  343. this._chara.jump = false;
  344. }
  345. }
  346. //循环地板数组
  347. for(i=0;!this._chara.jump && i<this._mcList.length;i++){
  348. _child = _mcList[i] as Object;
  349. //找到精灵接触的地板,这里也可以用hitTestObject来判断
  350. if(this._chara.y >= _child.y &&
  351. this._chara.x >= _child.x && _child.st &&
  352. this._chara.x <= _child.x + _child.width && _charaOld <= _child.y+1){
  353. if(_hpAdd > 0 && _charaOld != this._chara.y){
  354. _hpAdd--;
  355. if(_hpAdd == 0 && _hpIndex > 1){
  356. _hpIndex--;
  357. _hp.gotoAndStop(_hpIndex);
  358. }
  359. }
  360. if(_child.charaIsMove != null && _child.charaIsMove){
  361. _child.chara = this._chara;
  362. }
  363. if(_child.minsHp != null && _child.minsHp){
  364. _child.minsHp = false;
  365. _hpIndex++;
  366. _hpAdd = 5;
  367. _hp.gotoAndStop(_hpIndex);
  368. }
  369. this._chara.y = _child.y +1;
  370. _sectend = 1;
  371. _chara_ctrl = true;
  372. _child.play();
  373. break;
  374. }
  375. }
  376. //是否接触到上方的刺针
  377. if(this._chara.hitTestObject(_ue)){
  378. _chara_ctrl = false;
  379. this._chara.jump = false;
  380. this._chara.y += 30;
  381. _sectend = 3;
  382. _hpIndex++;
  383. _hpAdd = 5;
  384. _hp.gotoAndStop(_hpIndex);
  385. }
  386. //循环背景图片,达到连贯显示
  387. if(this._wall.y % 130 == 0){
  388. _bakImg.y += 130;
  389. }
  390. //精灵移动
  391. if(_leftCtrl){
  392. this._chara.x -= _speed*2;
  393. }
  394. if(_rightCtrl){
  395. this._chara.x += _speed*2;
  396. }
  397. if(this._chara.x<25){
  398. this._chara.x = 25;
  399. }
  400. if(this._chara.x>295){
  401. this._chara.x = 295;
  402. }
  403. if(_mcIndex <= 0){
  404. this.getMC();
  405. _mcIndex = Math.floor(Math.random()*60) + 30;
  406. }
  407. _mcIndex--;
  408. //显示楼层
  409. _txt.htmlText = "<font color='#FFFFFF' size='20'>" + Math.floor((-this._wall.y)/480) + "</font>";
  410. _charaOld = this._chara.y;
  411. //判断游戏是否结束
  412. if(_hpIndex >= 4){
  413. gameOver();
  414. }else if(this._chara.y + this._wall.y > 540){
  415. gameOver();
  416. }
  417. }
  418. }
  419. }

复制代码

flash版小游戏:是男人就下100层相关推荐

  1. Pytho制作小游戏——是男人就下100层

    前言 今天给大家介绍一个Python制作的小游戏:是男人就下100层 先给大家看看效果图 部分代码如下: import pygame import game from random import ch ...

  2. OpenGL2D小游戏——是男人就下100层

    2019独角兽企业重金招聘Python工程师标准>>> 是男人就下100层想必大家一定都玩过,在这里给大家简单介绍一下游戏规则. 游戏规则: 游戏人物从屏幕上方按一定速率下落,同时台 ...

  3. 好玩小游戏---是男人就下100层

    200多KB, 紧张刺激, 适合长期编程人员放松以下. 也很简单, 直接操作鼠标往下跳即可. 楼主最高记录也就30多层, 不知道有没有高手出现. 下载地址: http://traffic189.com ...

  4. cocos2d-x 是男人就下100层 附源码

    1.效果图: 玩法: 一个不断下降的小人,点击屏幕的left或者right控制小人的移动方向,尽可能生存久些.为什么要搞这个游戏呢?因为在2012年的8月份,我完成它的android版本,见<自 ...

  5. 是男人就下100层【第五层】——换肤版2048游戏

    ---------------------------------------------------------------------------------------------------- ...

  6. Cocos2d-x 版本小游戏 《是男人就下100层》 项目开源

    Cocos2d-x 版本小游戏 <是男人就下100层> 项目开源 原文:Cocos2d-x 版本小游戏 <是男人就下100层> 项目开源 这个是很久就开始动手写的一个小游戏了, ...

  7. 自己写的第一个android 游戏《是男人就下100层》

    自己开发的第一个android 游戏<是男人就下100层>,注意是安卓游戏,不是IPhone的.截图如下:            这个有游戏是用重力感应来控制的,所以要晃动手机来控制人物移 ...

  8. 是男人就下100层【第五层】——2048游戏从源代码到公布市场

    上一篇<是男人就下100层[第五层]--换肤版2048游戏>中阳光小强对2048游戏用自己的方式进行了实现,并分享了核心源码,这一篇阳光小强打算将该项目的全部源码公开并结合这个实例在这篇文 ...

  9. Unity经典游戏教程之:是男人就下100层

    版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...

  10. 是男人就下100层【第五层】——2048游戏从源码到发布市场

    上一篇<是男人就下100层[第五层]--换肤版2048游戏>中阳光小强对2048游戏用自己的方式进行了实现,并分享了核心源码,这一篇阳光小强打算将该项目的所有源代码公开并结合这个实例在这篇 ...

最新文章

  1. 在Elasticsearch中对 text 类型的字段进行聚合异常Fielddata is disabled,Set fielddata=true
  2. Hibernate HQL的update方法详解
  3. Jupyter notebook的内核是什么?Ipython
  4. 登录方式1:MySQL自带客户端
  5. 重装系统后sqlserver安装失败_Windows 10八月更新再遇尴尬:安装失败 或安装后随机重启...
  6. 等价类测试与决策表测试
  7. aws rds监控慢sql_AWS RDS SQL Server恢复模型,备份和还原
  8. C语言atoi()函数:将字符串转换成int(整数)
  9. 软件观念革命:交互设计精髓_“被催债”的设计推荐书单
  10. 窝囊同事做测试三年未涨工资,被开当天,bat全部大佬门口迎接!
  11. 【转】弹出USB大容量存储设备时出问题的解决方法
  12. 有趣且有意义的数字,你想到了什么?请不吝留言
  13. 2016年计算机b级考试试题,2016年全国计算机等级考试模拟试题一级B.doc
  14. 毕业设计So Easy:JSP+layui+MySQL实现Web端图书管理系统
  15. 基于python的自媒体和官媒数据爬取、可视化分析及云词图制作
  16. dockers 的简单使用
  17. matlab练习(11.7)
  18. STM32定时器中断实验
  19. 工程电磁场第四章总结
  20. ios全局点击空白隐藏keyboard

热门文章

  1. android gc,Android 内存 - 垃圾回收(GC)机制
  2. 我是怎么招聘程序员的
  3. UI一揽子计划 3 (自定义视图、UIViewControllor、屏幕旋转判断和重布局)
  4. 银河麒麟系统飞腾CPU安装jdk和nacos
  5. 斐波那契数的数学封闭式表示
  6. 第十三讲 项目风险管理【2021年软考-高级信息系统项目管理师】
  7. oracle nvarchar2 substr,关于oracle中varchar2与nvarchar2的一点认识 - Qxun_dream
  8. 为什么我不看好婚礼直播市场
  9. 霍格沃兹测试开发进阶班16期
  10. Firefox版搜狗云输入法体验