flash版小游戏:是男人就下100层
最近买了 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,下面是所有代码
- package game {
- import flash.display.Sprite;
- import flash.utils.getDefinitionByName;
- import flash.events.Event;
- import flash.events.MouseEvent;
- import flash.text.TextField;
- import flash.events.KeyboardEvent;
- import flash.display.Shape;
- import flash.display.DisplayObjectContainer;
- import flash.system.fscommand;
- //import flash.desktop.NativeApplication;
- public class Main extends Sprite{
- private var _chara:chara = new chara();
- private var _wall:Sprite = new Sprite();
- private var _bak:Sprite = new Sprite();
- private var _bakImg:bak = new bak();
- private var _mai:mai = new mai();
- private var _ue:ue = new ue();
- private var _hp:hp = new hp();
- private var _chara_ctrl:Boolean = false;
- private var _sectend:int = 0;
- private var _speed:int = 2;
- private var _mcList:Array = new Array();
- private var _leftCtrl:Boolean = false;
- private var _rightCtrl:Boolean = false;
- private var _mcIndex:int = 0;
- private var _mcArray:Array = new Array();
- private var _txt:TextField = new TextField();
- private var _hpIndex:int = 1;
- private var _charaOld:int = 1;
- private var _shape:Shape = new Shape();
- private var _showText:Sprite = new Sprite();
- private var _gameStart:Boolean = false;
- private var _sound:jiao = new jiao();
- private var _hpAdd:int = 0;
- //加入地板
- private function getMC(my:int = 480,mx:int = -1):void{
- var i:int = Math.floor(Math.random()*_mcArray.length);
- var ClassReference:Class;
- ClassReference = getDefinitionByName(_mcArray[i]) as Class;
- var nmc:Sprite = new ClassReference as Sprite;
- nmc.y = my - this._wall.y;
- if(mx < 0){
- nmc.x = Math.floor(Math.random()*270) - 50;
- }else{
- nmc.x = mx;
- }
- this._mcList.push(nmc);
- this._bak.addChild(nmc);
- }
- public function Main() {
- //将各种地板加入数组
- //普通地板
- _mcArray.push("mc");
- //可旋转地板
- _mcArray.push("toumc");
- //左移地板
- _mcArray.push("leftMc");
- //右移地板
- _mcArray.push("rightMc");
- //弹跳地板
- _mcArray.push("tan");
- //减HP地板
- _mcArray.push("sMc");
- //添加各个画板层
- this._bak.addChild(_bakImg);
- this._wall.addChild(this._bak);
- this._wall.addChild(this._chara);
- this._chara.x = 160;
- this._chara.y = 100;
- this.addChild(this._wall);
- this.addChild(_mai);
- this.addChild(_ue);
- _hp.x = 20;
- _hp.y = 460;
- this.addChild(_hp);
- this.addChild(_shape);
- _txt.selectable = false;
- _txt.x = 20;
- _txt.y = 430;
- _txt.width = 200;
- _txt.height = 200;
- this.addChild(_txt);
- this.addChild(_showText);
- this.addEventListener(MouseEvent.MOUSE_DOWN,onDown);
- this.addEventListener(MouseEvent.MOUSE_UP,onUp);
- stage.addEventListener(KeyboardEvent.KEY_DOWN,isKeyDown,false,0,true);
- stage.addEventListener(KeyboardEvent.KEY_UP,isKeyUp,false,0,true);
- stage.showDefaultContextMenu = false
- //fscommand("showmenu", "false");
- init();
- }
- //游戏初始化
- private function init():void{
- this._bak.y = 0;
- this._wall.y = 0;
- var i:int;
- for(i=0;i<_mcList.length;i++){
- this._bak.removeChild(_mcList[i]);
- }
- _mcList.splice(0,_mcList.length);
- this._chara.x = 160;
- this._chara.y = 100;
- //添加游戏说明框
- _shape.graphics.lineStyle(5, 0x000000,0.8);
- _shape.graphics.beginFill(0x999999,0.6);
- _shape.graphics.drawRoundRect(50,100,220,300,20,20);
- _shape.graphics.lineStyle(5, 0x000000,0.8);
- _shape.graphics.beginFill(0x999999,0.6);
- _shape.graphics.drawRoundRect(70,300,180,40,10,10);
- _shape.graphics.lineStyle(5, 0x000000,0.8);
- _shape.graphics.beginFill(0x999999,0.6);
- _shape.graphics.drawRoundRect(70,350,180,40,10,10);
- var txt:TextField = new TextField();
- txt.selectable = false;
- txt.width = 200;
- txt.height = 400;
- txt.x = 60;
- txt.y = 110;
- txt.htmlText = "<font color='#FFFFFF' size='20'><b>" + " 100階にチャレンジ/n/nあなたができますか?/n/n"+
- "操作:/n</b></font>" +
- "<font color='#FFFFFF' size='15'><b>「←」キーをクリック:/n ・左に移動/n" +
- "<font color='#FFFFFF' size='15'><b>「→」キーをクリック:/n ・右に移動/n" +
- "</b></font>";
- _showText.addChild(txt);
- txt = new TextField();
- txt.selectable = false;
- txt.x = 120;
- txt.y = 305;
- txt.htmlText = "<font color='#FFFFFF' size='20'><b>" + "スタート"+ "</b></font>";
- _showText.addChild(txt);
- txt = new TextField();
- txt.selectable = false;
- txt.x = 120;
- txt.y = 355;
- txt.htmlText = "<font color='#FFFFFF' size='20'><b>" + " 閉じる"+ "</b></font>";
- _showText.addChild(txt);
- //gameStart();
- }
- //游戏结束
- private function gameOver():void{
- this._chara.jump = false;
- _leftCtrl = false;
- _rightCtrl = false;
- _sound.play();
- _hpIndex = 1;
- _hp.gotoAndStop(_hpIndex);
- _gameStart = false;
- _sectend = 0;
- this.removeEventListener(Event.ENTER_FRAME,onFrame);
- this._bak.y = 0;
- this._wall.y = 0;
- this._bakImg.y = 0;
- var i:int;
- for(i=0;i<_mcList.length;i++){
- this._bak.removeChild(_mcList[i]);
- }
- _mcList.splice(0,_mcList.length);
- this._chara.x = 160;
- this._chara.y = 100;
- _shape.graphics.lineStyle(5, 0x000000,0.8);
- _shape.graphics.beginFill(0x999999,0.6);
- _shape.graphics.drawRoundRect(50,100,220,300,20,20);
- _shape.graphics.lineStyle(5, 0x000000,0.8);
- _shape.graphics.beginFill(0x999999,0.6);
- _shape.graphics.drawRoundRect(70,300,180,40,10,10);
- _shape.graphics.lineStyle(5, 0x000000,0.8);
- _shape.graphics.beginFill(0x999999,0.6);
- _shape.graphics.drawRoundRect(70,350,180,40,10,10);
- var txt:TextField = new TextField();
- txt.selectable = false;
- txt.width = 200;
- txt.height = 400;
- txt.x = 60;
- txt.y = 110;
- txt.htmlText = "<font color='#FFFFFF' size='20'><b>" + " GAME OVER/n/n"+
- "階数:" + _txt.text +
- "/n称号:" + getName(int(_txt.text)) +
- "/n</b></font>";
- _showText.addChild(txt);
- txt = new TextField();
- txt.selectable = false;
- txt.x = 120;
- txt.y = 305;
- txt.htmlText = "<font color='#FFFFFF' size='20'><b>" + "スタート"+ "</b></font>";
- _showText.addChild(txt);
- txt = new TextField();
- txt.selectable = false;
- txt.x = 120;
- txt.y = 355;
- txt.htmlText = "<font color='#FFFFFF' size='20'><b>" + " 閉じる"+ "</b></font>";
- _showText.addChild(txt);
- }
- //游戏称号
- private function getName(cnt:int):String{
- if(cnt < 10){
- return "赤ちゃん";
- }else if(cnt < 20){
- return "子供";
- }else if(cnt < 30){
- return "小学生";
- }else if(cnt < 40){
- return "中学生";
- }else if(cnt < 50){
- return "高校生";
- }else if(cnt < 60){
- return "大学生";
- }else if(cnt < 70){
- return "大学院生";
- }else if(cnt < 80){
- return "専門家";
- }else if(cnt < 90){
- return "高級専門家";
- }else{
- return "神";
- }
- }
- //游戏开始
- private function gameStart():void{
- //在画板上添加3个地板
- getMC(300,160);
- getMC(350);
- getMC(400);
- this.addEventListener(Event.ENTER_FRAME,onFrame,false,0,true);
- }
- //键盘按下
- private function isKeyDown(event:KeyboardEvent):void{
- if(!_gameStart){
- return;
- }
- if(event.keyCode == 37){
- if(_leftCtrl){
- return;
- }
- _leftCtrl = true;
- _rightCtrl = false;
- this._chara.gotoAndPlay("left");
- }else if(event.keyCode == 39){
- if(_rightCtrl){
- return;
- }
- _rightCtrl = true;
- _leftCtrl = false;
- this._chara.gotoAndPlay("right");
- }
- }
- //键盘弹起
- private function isKeyUp(event:KeyboardEvent):void{
- if(event.keyCode == 37){
- _leftCtrl = false;
- }else if(event.keyCode == 39){
- _rightCtrl = false;
- }
- }
- //鼠标按下
- private function onDown(event:MouseEvent):void{
- var intX:Number = event.currentTarget.mouseX;
- var intY:Number = event.currentTarget.mouseY;
- if(_gameStart){
- if(intX <= 160){
- if(_leftCtrl){
- return;
- }
- _leftCtrl = true;
- _rightCtrl = false;
- this._chara.gotoAndPlay("left");
- }else{
- if(_rightCtrl){
- return;
- }
- _rightCtrl = true;
- _leftCtrl = false;
- this._chara.gotoAndPlay("right");
- }
- }else if(intX > 70 && intX < 250 && intY > 300 && intY < 340){
- _gameStart = true;
- _shape.graphics.clear();
- this.removeAllChildren(_showText);
- gameStart();
- }else if(intX > 70 && intX < 250 && intY > 350 && intY < 390){
- fscommand("quit");
- //stage.nativeWindow.close();
- //NativeApplication.nativeApplication.exit();
- }
- stage.focus = this;
- }
- //删除地板方法
- public function removeAllChildren(
- container:DisplayObjectContainer ):void {
- var count:int = container.numChildren;
- for ( var i:int = 0; i < count; i++ ) {
- container.removeChildAt( 0 );
- }
- }
- //鼠标弹起
- private function onUp(event:MouseEvent):void{
- var intX:Number = event.currentTarget.mouseX;
- if(intX <= 160){
- _leftCtrl = false;
- }else{
- _rightCtrl = false;
- }
- }
- //逻辑部分
- private function onFrame(event:Event):void{
- var speedNum:Number = 0.03;
- var downlength:Number;
- var _child:Object;
- //找到画面外可删除地板,将其删除
- if(_mcList.length > 0 && _mcList[0].y - this._wall.y < 40){
- _child = _mcList[0] as Object;
- if(_child.charaIsMove != null && _child.chara != null){
- _child.chara = null;
- }
- this._bak.removeChild(_mcList[0]);
- _mcList.splice(0,1);
- }
- var i:int;
- this._wall.y -= _speed;
- //游戏精灵上升下降,以及上升下降幅度控制
- if(!_chara_ctrl){
- this._chara.y += _speed;
- downlength = speedNum*_sectend*_sectend;
- if(downlength > 37){
- downlength = 37;
- }
- this._chara.y += downlength;
- _sectend++;
- }else if(this._chara.jump){
- this._chara.y += _speed;
- this._chara.y -= 0.06*this._chara.jumpSpeed*this._chara.jumpSpeed;
- //charaOld = this._chara.y;
- this._chara.jumpSpeed++;
- if(this._chara.jumpSpeed >= 0){
- this._chara.jump = false;
- }
- }
- //是否处在弹跳阶段
- if(!this._chara.jump){
- _chara_ctrl = false;
- }else{
- if(this._chara.jumpSpeed >= 0){
- this._chara.jump = false;
- }
- }
- //循环地板数组
- for(i=0;!this._chara.jump && i<this._mcList.length;i++){
- _child = _mcList[i] as Object;
- //找到精灵接触的地板,这里也可以用hitTestObject来判断
- if(this._chara.y >= _child.y &&
- this._chara.x >= _child.x && _child.st &&
- this._chara.x <= _child.x + _child.width && _charaOld <= _child.y+1){
- if(_hpAdd > 0 && _charaOld != this._chara.y){
- _hpAdd--;
- if(_hpAdd == 0 && _hpIndex > 1){
- _hpIndex--;
- _hp.gotoAndStop(_hpIndex);
- }
- }
- if(_child.charaIsMove != null && _child.charaIsMove){
- _child.chara = this._chara;
- }
- if(_child.minsHp != null && _child.minsHp){
- _child.minsHp = false;
- _hpIndex++;
- _hpAdd = 5;
- _hp.gotoAndStop(_hpIndex);
- }
- this._chara.y = _child.y +1;
- _sectend = 1;
- _chara_ctrl = true;
- _child.play();
- break;
- }
- }
- //是否接触到上方的刺针
- if(this._chara.hitTestObject(_ue)){
- _chara_ctrl = false;
- this._chara.jump = false;
- this._chara.y += 30;
- _sectend = 3;
- _hpIndex++;
- _hpAdd = 5;
- _hp.gotoAndStop(_hpIndex);
- }
- //循环背景图片,达到连贯显示
- if(this._wall.y % 130 == 0){
- _bakImg.y += 130;
- }
- //精灵移动
- if(_leftCtrl){
- this._chara.x -= _speed*2;
- }
- if(_rightCtrl){
- this._chara.x += _speed*2;
- }
- if(this._chara.x<25){
- this._chara.x = 25;
- }
- if(this._chara.x>295){
- this._chara.x = 295;
- }
- if(_mcIndex <= 0){
- this.getMC();
- _mcIndex = Math.floor(Math.random()*60) + 30;
- }
- _mcIndex--;
- //显示楼层
- _txt.htmlText = "<font color='#FFFFFF' size='20'>" + Math.floor((-this._wall.y)/480) + "</font>";
- _charaOld = this._chara.y;
- //判断游戏是否结束
- if(_hpIndex >= 4){
- gameOver();
- }else if(this._chara.y + this._wall.y > 540){
- gameOver();
- }
- }
- }
- }
复制代码
flash版小游戏:是男人就下100层相关推荐
- Pytho制作小游戏——是男人就下100层
前言 今天给大家介绍一个Python制作的小游戏:是男人就下100层 先给大家看看效果图 部分代码如下: import pygame import game from random import ch ...
- OpenGL2D小游戏——是男人就下100层
2019独角兽企业重金招聘Python工程师标准>>> 是男人就下100层想必大家一定都玩过,在这里给大家简单介绍一下游戏规则. 游戏规则: 游戏人物从屏幕上方按一定速率下落,同时台 ...
- 好玩小游戏---是男人就下100层
200多KB, 紧张刺激, 适合长期编程人员放松以下. 也很简单, 直接操作鼠标往下跳即可. 楼主最高记录也就30多层, 不知道有没有高手出现. 下载地址: http://traffic189.com ...
- cocos2d-x 是男人就下100层 附源码
1.效果图: 玩法: 一个不断下降的小人,点击屏幕的left或者right控制小人的移动方向,尽可能生存久些.为什么要搞这个游戏呢?因为在2012年的8月份,我完成它的android版本,见<自 ...
- 是男人就下100层【第五层】——换肤版2048游戏
---------------------------------------------------------------------------------------------------- ...
- Cocos2d-x 版本小游戏 《是男人就下100层》 项目开源
Cocos2d-x 版本小游戏 <是男人就下100层> 项目开源 原文:Cocos2d-x 版本小游戏 <是男人就下100层> 项目开源 这个是很久就开始动手写的一个小游戏了, ...
- 自己写的第一个android 游戏《是男人就下100层》
自己开发的第一个android 游戏<是男人就下100层>,注意是安卓游戏,不是IPhone的.截图如下: 这个有游戏是用重力感应来控制的,所以要晃动手机来控制人物移 ...
- 是男人就下100层【第五层】——2048游戏从源代码到公布市场
上一篇<是男人就下100层[第五层]--换肤版2048游戏>中阳光小强对2048游戏用自己的方式进行了实现,并分享了核心源码,这一篇阳光小强打算将该项目的全部源码公开并结合这个实例在这篇文 ...
- Unity经典游戏教程之:是男人就下100层
版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...
- 是男人就下100层【第五层】——2048游戏从源码到发布市场
上一篇<是男人就下100层[第五层]--换肤版2048游戏>中阳光小强对2048游戏用自己的方式进行了实现,并分享了核心源码,这一篇阳光小强打算将该项目的所有源代码公开并结合这个实例在这篇 ...
最新文章
- 在Elasticsearch中对 text 类型的字段进行聚合异常Fielddata is disabled,Set fielddata=true
- Hibernate HQL的update方法详解
- Jupyter notebook的内核是什么?Ipython
- 登录方式1:MySQL自带客户端
- 重装系统后sqlserver安装失败_Windows 10八月更新再遇尴尬:安装失败 或安装后随机重启...
- 等价类测试与决策表测试
- aws rds监控慢sql_AWS RDS SQL Server恢复模型,备份和还原
- C语言atoi()函数:将字符串转换成int(整数)
- 软件观念革命:交互设计精髓_“被催债”的设计推荐书单
- 窝囊同事做测试三年未涨工资,被开当天,bat全部大佬门口迎接!
- 【转】弹出USB大容量存储设备时出问题的解决方法
- 有趣且有意义的数字,你想到了什么?请不吝留言
- 2016年计算机b级考试试题,2016年全国计算机等级考试模拟试题一级B.doc
- 毕业设计So Easy:JSP+layui+MySQL实现Web端图书管理系统
- 基于python的自媒体和官媒数据爬取、可视化分析及云词图制作
- dockers 的简单使用
- matlab练习(11.7)
- STM32定时器中断实验
- 工程电磁场第四章总结
- ios全局点击空白隐藏keyboard