近段时间在手游方面的学习主要集中在Cocos2d-x上。有引擎的确是好哇!不用再像过去费劲的使用Android上底效的SurfaceView或者用OpenGL ES一点一点去建立底层框架。今天以我在此前学Android时自已所策划编写的一个小工程来记念一下,告别那段时光。
这个游戏名叫“水果机”,80后不少同好都在街厅中玩过吧。还是喜欢下注“苹果”。在移植到Android上后,我增加了一些内容并提供了双人对战以丰富它的玩法。下面我来介绍一下:
玩家运行程序后。首先是主界面:
为了吸引人气,用了一张美眉(网上找的图自已PS的),宅男福利,我运行调试时也总要多看两眼。QQ群已经不在了。不必再加入。
主界面上有菜单四项:
“开始游戏”: 点击后进入游戏。一会儿讲。
“游戏设置”: 点击后进入设置界面。这里可以选择单人还是双人玩,在单人玩法中是否弹出打气球的随机奖励以及音效和音乐的设置。“打气球”是我在此基础上的一种创新吧,就是单人玩时每次轮盘经过最下面中间位置时会随机飞出来一些气球,可以选择相应水果位置飞出一些飞镖来击中它爆出彩蛋奖励!
“游戏帮助”: 点击后进入帮助界面。
“退出游戏”: 点击后关闭程序退出。
咱们直接选“开始游戏”,即进入游戏界面:
单人玩法:
这是一个单人游戏的游戏界面,最上方是筹码显示,初时都是10个。中间是轮盘,下方是下注区。现在玩家可以下注了,在下方水果下注区域(黄或绿区)中触屏,可以在相应水果档里下想要的注数。点击中间的手形按钮可以开始转动轮盘。
在轮盘每次经过最下方中间位置,会飞出一个气球,玩家可以点击下注区的水果以控制轮盘的相应水果位置向中间射出飞镖,击中有随机奖励:
双人对战玩法:
这是一个双人对战的游戏界面,最上方是两个人的筹码,初时都是10个。中间是轮盘,下方是下注区。现在1号玩家可以下注了,在下方水果下注区域(黄或绿区)中触屏,可以在相应水果档里下想要的注数。点击中间的手形按钮可以让二号玩家开始下注,都下完注后点击中间的手会开始转动轮盘。
中奖后会有提示继续。
好的,游戏介绍完了,咱们现在来学习一下如何进行开发:
工程共有11个源文件组成:
介绍一下:
WinnerActivity.java:主程序框架。
WinnerRecord.java:筹码计数器。
Apple.java:主游戏逻辑框架
AppThread.java:多线程更新逻辑。
AppleView.java:游戏视图。
DrawThread.java:多线程更新
FeiBiao.java:飞镖类
FeiBiaoThread.java:多线程更新飞镖逻辑。
QiQiu.java:气球类
QiQiuThread.java: 多线程更新气球逻辑。
Tile.java:轮盘的格子类。
咱们就按这个清单来讲解代码:
一. WinnerActivity.java:主程序框架。
package win.gam; import win.gam.AppleView; import android.app.Activity; import android.os.Bundle; import android.view.Window; import android.view.WindowManager; public class WinnerActivity extends Activity { /** Called when the activity is first created. */ AppleView appleview; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.main); requestWindowFeature(Window.FEATURE_NO_TITLE); //设置不显示标题 getWindow().setFlags( //设置为全屏模式 WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); appleview = new AppleView(this); //创建BallView对象 setContentView(appleview); //将屏幕设置为BallView对象 } }
二.WinnerRecord.java:筹码计数器。
package win.gam; import android.graphics.Color; public class WinnerRecord { int m_WinnerID; //ID int m_IconSize; //我的金币数量 int m_AppleRecord[]; //对应的水果下注数量 int m_Color; //色彩 //构造函数 public WinnerRecord(int vWinnerID) { m_WinnerID = vWinnerID ; switch(m_WinnerID) { case 0: m_Color = Color.YELLOW ; break; case 1: m_Color = Color.GREEN; break; case 2: m_Color = Color.BLUE; break; case 3: m_Color = Color.WHITE; break; } m_AppleRecord = new int[8]; for(int i = 0 ; i < 8 ; i++) { m_AppleRecord[i] = 0; } m_IconSize = 0; } //取得色彩 public int GetColor() { return m_Color; } //设置金币数量 public void SetIconSize(int vIconSize) { m_IconSize = vIconSize; } //取得金币数量 public int GetIconSize() { return m_IconSize; } //增加金币数量 public void AddIcon(int vAddIconSize) { m_IconSize += vAddIconSize; } //减少金币数量 public void DecIcon() { if(m_IconSize>0) { m_IconSize--; } } //清空金币 public void CleanIcon() { m_IconSize = 0; } //设置对应的水果的数量 public void SetRecord(int vAppleIndex,int vRecord) { m_AppleRecord[vAppleIndex] = vRecord; } public int GetRecord(int vAppleIndex) { return m_AppleRecord[vAppleIndex]; } //增加对应的水果的数量 public void AddRecord(int vAppleIndex) { if(m_IconSize > 0) { m_AppleRecord[vAppleIndex]++; m_IconSize--; } } //清空对应的水果数量 public void CleanRecord(int vAppleIndex ) { if(vAppleIndex >= 0) { m_AppleRecord[vAppleIndex] = 0; } else { for(int i = 0 ; i < 8 ; i++) { m_AppleRecord[i] = 0; } } } }
三.Apple.java:主游戏逻辑框架
package win.gam; import java.util.ArrayList; import java.util.HashMap; import java.util.Random; import win.gam.WinnerActivity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Typeface; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.SoundPool; import android.os.Handler; import android.os.Message; public class Apple { private static final Bitmap Bitmap = null; final int INITSTATE = 0; //初始状态 final int SETTINGSTATE = 1; //游戏设置 final int HELPSTATE = 2; //游戏帮助 final int GAMESTATE = 3; //游戏中 final int GAMEEXIT = 4; //游戏退出 Bitmap m_WelcomeUI = null; //欢迎界面 Bitmap m_BitMapArray[] = null; //位置 final int m_AppleBitMapSize = 17; //数量 int m_GameState = INITSTATE; //初始状态 float m_HelpTextMinTop = 70; //帮助状态时的最小文字位置 float m_HelpTextTop = m_HelpTextMinTop; //帮助状态时的文字Y位置 int m_Winsize[]; //大 WinnerActivity winnerActivity; //视窗 Tile appleTileArray[]; //水果格子 float m_ScreenWidth; //屏幕宽度 float m_ScreenHeight; //屏幕高度 int m_TileCows = 8; //列数 int m_TileRows = 7; //行数,不算头尾 int m_TileW = 30; //格子宽 int m_TileH = 30; //格子高 int m_Left = 0; //左上角X int m_Top = 30; //左上角Y int m_TileCount = 0; //格子总数 int m_AppleImageArray[]; //水果图ID数组 WinnerRecord m_WinnerRecord[]; //玩家 Paint m_WinnerPaint[]; //创建画笔对象 int m_WinnerSize = 1; //玩家数量 final int m_MaxWinnerSize = 2; //最大玩家数量 int m_CurrWinnerID = 0; //当前玩家ID int m_InitIconSize = 10; //初始金币数量 int m_ShowIconHeight= 60; //显示玩家下注的高度 int m_ShowIconTop = 0; //显示玩家下注时的位置 int m_ShowStartBtnX = 0; //显示GO按钮的位置X int m_ShowStartBtnY = 0; //显示GO按钮的位置Y double m_GameExitTime = 0; //获取当前时间,单位为纳秒 double m_currenttime = 0; //记录当前时间 double m_starttime = 0; //开始的时间 double m_TimeLength = 0; //间隔时间 int m_currentpathid = 0; //路径ID double m_Timer = 1000; //每隔多久前进一次 double m_DecTimer = 10; //加速度 double m_MinTimer = 20; //最小时间间隔 boolean m_bStartRun = false; //开始运动 Matrix m_BackGroundMatrix = new Matrix(); Matrix m_CenterMatrix = new Matrix(); float m_ScaleW = 1.0f; float m_ScaleH = 1.0f; int m_HandIndex_apple = -1; //手的显示索引_对应水果 int m_HandIndex_winner = -1; //手的显示索引_对应玩家 AppleThread m_appleThread = null; //水果线程 MediaPlayer m_BackMusic; //背景音乐 SoundPool m_SoundPool; //音效控制器 HashMap<Integer,Integer> m_SoundPoolMap; //音效容器 boolean m_SoundOpen = true; //音效开关 boolean m_MusicOpen = true; //声音天并 boolean m_QiQiuOpen = true; //是否弹出气球 int m_RunNum = 0; //运行次数 public QiQiu m_QiQiu = null; //气球 QiQiuThread m_QiQiuThread = null; //气球线程 public boolean m_QiQiuIsLive = false; //气球是否可用 public ArrayList<FeiBiao> m_FeiBiaoArray = new ArrayList<FeiBiao>(); //飞镖对象数组 FeiBiaoThread m_FeiBiaoThread = null; //飞镖线程 int m_ShowBaoWu = -1; //宝物 int m_WinnerAppleID = -1; //胜利者 boolean m_ShowWinner = true; //显示胜利者 Random random = new Random(); //随机数 int m_WinAllApple = 0; //0没意义,1所有水果都中奖,2中奖水果X2倍 final int m_WinAllAppleID = 777; //都中奖 Paint m_UITitle = new Paint(); //创建UI的标题画笔对象 int m_UITitleHeight = 0; //创建UI时的标题字体大小 Paint m_UIText = new Paint(); //创建UI的文字画笔对象t int m_UITextHeight = 0; //创建UI的文字画笔字体大小 //Paint m_UIText2 = new Paint(); //用来更新多线程的消息 Handler myHandler = new Handler() { public void handleMessage(Message msg) { if(msg.what == 1) { //收到放气球的消息 PutOutQiQiu(); } if(msg.what == 2) { //返回主界面 m_GameState = INITSTATE ; } } }; //构造函数 Apple(WinnerActivity Activity,int vScreenWidth,int vScreenHeight) { winnerActivity = Activity; m_ScreenWidth = vScreenWidth; //屏幕宽度 m_ScreenHeight = vScreenHeight; //屏幕高度 m_ScaleW = m_ScreenWidth/240.0f ; m_ScaleH = m_ScreenHeight/400.0f ; m_TileW = (int) (m_ScaleW * 30); //格子宽 m_TileH = (int) (m_ScaleH * 32); //格子高 m_Left = (int) (0 * m_ScaleW); //左上角X m_Top = (int) (30* m_ScaleH); //左上角Y m_HelpTextMinTop = 70* m_ScaleH; //帮助状态时的最小文字位置 m_HelpTextTop = m_HelpTextMinTop; //帮助状态时的文字Y位置 InitApple(m_InitIconSize); } //初始化 public void InitApple(int vIconSize) { m_BackMusic = MediaPlayer.create(winnerActivity, R.raw.back);//背景音乐 //m_IconSound = MediaPlayer.create(winnerActivity, R.raw.icon);//放金币的音乐 m_SoundPool = new SoundPool(4,AudioManager.STREAM_MUSIC,100); m_SoundPoolMap = new HashMap<Integer,Integer>(); m_SoundPoolMap.put(1, m_SoundPool.load(winnerActivity, R.raw.icon,1)); //加金币 m_SoundPoolMap.put(2, m_SoundPool.load(winnerActivity, R.raw.path,1)); //路径 m_SoundPoolMap.put(3, m_SoundPool.load(winnerActivity, R.raw.fly,1)); //飞镖 m_SoundPoolMap.put(4, m_SoundPool.load(winnerActivity, R.raw.bomb,1)); //打破气球 m_SoundOpen = true; m_MusicOpen = true; m_QiQiuOpen = true; //开始播放背景音乐 if(true == m_MusicOpen) { m_BackMusic.start(); } //玩家管理器 m_WinnerRecord = new WinnerRecord[m_MaxWinnerSize]; m_WinnerPaint = new Paint[m_MaxWinnerSize]; for(int i = 0 ; i < m_MaxWinnerSize ; i++) { //玩家 m_WinnerRecord[i] = new WinnerRecord(i); m_WinnerRecord[i].SetIconSize(vIconSize); //字体 m_WinnerPaint[i] = new Paint(); String familyName_Player = "宋体"; Typeface font_Player = Typeface.create(familyName_Player, Typeface.BOLD); m_WinnerPaint[i].setTypeface(font_Player); m_WinnerPaint[i].setColor(m_WinnerRecord[i].GetColor()); //为画笔设置颜色 m_WinnerPaint[i].setTextSize(16* m_ScaleH); //为画笔设置字体大小 m_WinnerPaint[i].setAntiAlias(true); //设置抗锯齿 } m_WelcomeUI = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.ui);//欢迎界面 int tBitMapSize = 50; m_BitMapArray = new Bitmap[tBitMapSize]; m_BitMapArray[0] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.a1);//苹果0 m_BitMapArray[1] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.a2);//桔子1 m_BitMapArray[2] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.a3);//橄榄2 m_BitMapArray[3] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.a4);//铃铛3 m_BitMapArray[4] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.a5);//西瓜4 m_BitMapArray[5] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.a6);//双星5 m_BitMapArray[6] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.a7);//双7 6 m_BitMapArray[7] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.a8);//黑板 7 m_BitMapArray[8] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.b1);//苹果 8 m_BitMapArray[9] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.b2);//桔子 9 m_BitMapArray[10] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.b3);//橄榄 10 m_BitMapArray[11] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.b4);//铃铛 11 m_BitMapArray[12] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.b5);//西瓜 12 m_BitMapArray[13] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.b6);//双星 13 m_BitMapArray[14] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.b7);//双7 14 m_BitMapArray[15] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.b8);//黑板 15 m_BitMapArray[16] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.a0);//招财进宝 16 m_BitMapArray[17] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.gp);//绿点 17 m_BitMapArray[18] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.rp);//红点 18 m_BitMapArray[19] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.c1);//苹果 19 m_BitMapArray[20] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.c2);//桔子 20 m_BitMapArray[21] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.c3);//橄榄 21 m_BitMapArray[22] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.c4);//铃铛22 m_BitMapArray[23] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.c5);//西瓜 23 m_BitMapArray[24] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.c6);//双星 24 m_BitMapArray[25] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.c7);//双7 25 m_BitMapArray[26] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.c8);//黑板26 //手的类型 m_BitMapArray[27] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.hand1);//黄手 m_BitMapArray[28] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.hand2);//绿手 m_BitMapArray[29] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.hand3);//蓝手 m_BitMapArray[30] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.hand4);//白手 //背景图 m_BitMapArray[31] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.background1);//背景 //GO按钮 m_BitMapArray[32] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.go);//开始 //金币 m_BitMapArray[33] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.icon);//金币 //气球 m_BitMapArray[34] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.qq1);//气球1 m_BitMapArray[35] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.qq2);//气球2 m_BitMapArray[36] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.qq3);//气球3 m_BitMapArray[37] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.qq4);//气球4 m_BitMapArray[38] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.qq5);//气球5 m_BitMapArray[39] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.qq6);//气球6 //箭头 m_BitMapArray[40] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.jt1);//箭头1右 m_BitMapArray[41] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.jt1_2);//箭头1左 m_BitMapArray[42] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.jt2);//箭头2右 m_BitMapArray[43] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.jt2_2);//箭头2左 //开始按钮 m_BitMapArray[44] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.start);//开始按钮 //设置按钮 m_BitMapArray[45] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.setting);//设置按钮 //帮助按钮 m_BitMapArray[46] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.help);//帮助按钮 //退出按钮 m_BitMapArray[47] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.exit);//退出按钮 //菜单按钮 m_BitMapArray[48] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.menu);//菜单按钮 //中奖钱袋 m_BitMapArray[49] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.money);//中奖钱袋 //胜数 m_Winsize = new int[m_AppleBitMapSize]; m_Winsize[0] = 5; m_Winsize[1] = 10; m_Winsize[2] = 15; m_Winsize[3] = 20; m_Winsize[4] = 20; m_Winsize[5] = 30; m_Winsize[6] = 40; m_Winsize[7] = 100; m_Winsize[8] = 2; m_Winsize[9] = 2; m_Winsize[10] = 2; m_Winsize[11] = 2; m_Winsize[12] = 2; m_Winsize[13] = 2; m_Winsize[14] = 2; m_Winsize[15] = 50; m_Winsize[16] = 0; m_bStartRun = false; //设置缩放 m_BackGroundMatrix.reset(); float bW1 = m_WelcomeUI.getWidth() ; float bH1 = m_WelcomeUI.getHeight() ; float bW2 = m_ScreenWidth ; float bH2 = m_ScreenHeight; float tScaleW = bW2/bW1; float tScaleH = bH2/bH1; m_BackGroundMatrix.postScale(tScaleW, tScaleH); String familyName = "宋体"; Typeface font = Typeface.create(familyName, Typeface.BOLD); m_UITitle.setTypeface(font); m_UITitle.setColor(Color.BLACK); //为画笔设置颜色 m_UITitleHeight = (int) (20 * m_ScaleH); m_UITitle.setTextSize(m_UITitleHeight); //为画笔设置字体大小 m_UITitle.setAntiAlias(true); //设置抗锯齿 String familyName2 = "宋体"; Typeface font2 = Typeface.create(familyName2, Typeface.BOLD); m_UIText.setTypeface(font2); m_UIText.setColor(Color.BLACK); //为画笔设置颜色 m_UITextHeight = (int) (16 * m_ScaleH); m_UIText.setTextSize(m_UITextHeight); //为画笔设置字体大小 m_UIText.setAntiAlias(true); //设置抗锯齿 //创建一个气球 m_QiQiu = new QiQiu(); //创建气球线程 m_QiQiuThread = new QiQiuThread(this); //创建飞标线程 m_FeiBiaoThread = new FeiBiaoThread(this); //创建线程 m_appleThread = new AppleThread(this); //启动线程 StartThread(); } //初始化游戏界面 public void InitGameUI() { if( 1 == m_WinnerSize ) { m_ShowIconHeight = (int) (60 * m_ScaleH) ; } else { m_ShowIconHeight = (int) (80 * m_ScaleH) ; } m_ShowIconTop = (int) (m_ScreenHeight - m_ShowIconHeight); //显示玩家下注的位置高度 m_CurrWinnerID = 0; m_InitIconSize = 10; //初始时10个金币 m_RunNum = 0; m_CenterMatrix.reset(); float bW1 = m_BitMapArray[31].getWidth() ; float bH1 = m_BitMapArray[31].getHeight() ; float bW2 = m_ScreenWidth ; float bH2 = m_ScreenHeight - m_ShowIconHeight; float tScaleW = bW2/bW1; float tScaleH = bH2/bH1; m_CenterMatrix.postScale(tScaleW, tScaleH); //计算格子行数与列数 m_TileCows = (int) (m_ScreenWidth / m_TileW); m_TileRows = (int) ((m_ScreenHeight - m_Top - m_ShowIconHeight) / m_TileH - 2); //上下两行的要去掉 //格子数量 m_TileCount = m_TileCows * 2 + m_TileRows * 2; m_AppleImageArray = new int[m_TileCount]; //先随机生成格子盘面 for(int i = 0 ; i < m_TileCount ; i++) { m_AppleImageArray[i] = random.nextInt(m_AppleBitMapSize); //起始位置 if(m_AppleImageArray[i] == 7 || m_AppleImageArray[i] == 15) { m_AppleImageArray[i] = 0; continue; } else if(m_AppleImageArray[i] == 6||m_AppleImageArray[i] == 14) { m_AppleImageArray[i] = 0; continue; } else if(m_AppleImageArray[i] == 5||m_AppleImageArray[i] == 13) { m_AppleImageArray[i] = 0; continue; } else if(m_AppleImageArray[i] == 4||m_AppleImageArray[i] == 12) { m_AppleImageArray[i] = 0; continue; } } //两边中央必须是招财进宝 int tTheTileIndex = m_TileCows + m_TileRows / 2; m_AppleImageArray[tTheTileIndex] = 16 ; tTheTileIndex = 2 * (m_TileCows + m_TileRows) - m_TileRows / 2; m_AppleImageArray[tTheTileIndex] = 16 ; //上边中央必须是大小黑板 tTheTileIndex = m_TileCows / 2; m_AppleImageArray[tTheTileIndex-1] = 15 ; m_AppleImageArray[tTheTileIndex] = 7 ; //下边中央必须是大小七 tTheTileIndex = 2 * m_TileCows + m_TileRows - m_TileCows / 2; m_AppleImageArray[tTheTileIndex+1] = 14 ; m_AppleImageArray[tTheTileIndex] = 6 ; //右上角下边必须是大小西瓜 tTheTileIndex = m_TileCows ; m_AppleImageArray[tTheTileIndex] = 4; m_AppleImageArray[tTheTileIndex+1] = 12 ; //左下角上边必须是大小星 tTheTileIndex = 2 * m_TileCows + m_TileRows ; m_AppleImageArray[tTheTileIndex] = 5; m_AppleImageArray[tTheTileIndex+1] = 13 ; //左上角是苹果 m_AppleImageArray[0] = 0 ; //左1是小苹果 m_AppleImageArray[1] = 8 ; //右上角是桔子 m_AppleImageArray[m_TileCows-1] = 1 ; m_AppleImageArray[m_TileCows-2] = 9 ; //左下角是橄榄 m_AppleImageArray[2 * m_TileCows + m_TileRows-1] = 2 ; m_AppleImageArray[2 * m_TileCows + m_TileRows-2] = 10 ; //右下角是铃铛 m_AppleImageArray[m_TileCows + m_TileRows] = 3 ; m_AppleImageArray[m_TileCows + m_TileRows+1] = 11 ; //生成格子 appleTileArray = new Tile[m_TileCount]; for(int i = 0 ; i < m_TileCount ; i++) { appleTileArray[i] = new Tile(); int tImageIndex = m_AppleImageArray[i]; appleTileArray[i].SetImage(tImageIndex,m_BitMapArray[tImageIndex]); } //上行 int tCount = 0; for(int i = 0 ; i < m_TileCows ; i++) { appleTileArray[tCount].SetTile(i*m_TileW+m_Left,m_Top); tCount++; } //右列 for(int i = 0 ; i < m_TileRows ; i++) { appleTileArray[tCount].SetTile((m_TileCows-1)*m_TileW+m_Left,(i+1)*m_TileH+m_Top); tCount++; } //下行 for(int i = m_TileCows-1 ; i >= 0 ; i--) { appleTileArray[tCount].SetTile(i*m_TileW+m_Left,(m_TileRows+1)*m_TileH+m_Top); tCount++; } //左列 for(int i = m_TileRows-1 ; i >= 0 ; i--) { appleTileArray[tCount].SetTile(m_Left,(i+1)*m_TileH+m_Top); tCount++; } m_starttime = System.nanoTime();//获取当前时间,单位为纳秒 m_Timer = 500; //每隔多久前进一次 m_DecTimer = 40; //加速度 m_MinTimer = 0; //最小时间间隔 m_currentpathid = random.nextInt(m_TileCount); //起始位置 } //退出释放 public void Release() { m_BackMusic.stop(); m_BackMusic = null; m_SoundPool.stop(m_SoundPoolMap.get(1)); m_SoundPool.stop(m_SoundPoolMap.get(2)); m_SoundPool.stop(m_SoundPoolMap.get(3)); m_SoundPool.stop(m_SoundPoolMap.get(4)); m_SoundPool = null; m_SoundOpen = false; m_MusicOpen = false; } //取得屏幕宽度 public float GetScreenWidth() { return m_ScreenWidth ; } //取得屏幕高度 public float GetScreenHeight() { return m_ScreenHeight; } //播放音效 public void PlaySound(int sound,int loop) { if(true == m_SoundOpen) { AudioManager mgr = (AudioManager)winnerActivity.getSystemService(Context.AUDIO_SERVICE); float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC); float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC); float volume = streamVolumeCurrent / streamVolumeMax; //播放音效 m_SoundPool.play(m_SoundPoolMap.get(sound), volume, volume, 1, loop, 1f); } } //设置玩家人数 public void SetWinnerSize(int vWinnerSize) { m_WinnerSize = vWinnerSize; } //打开气球 public void EnableQiQiu(boolean vQiQiuOpen) { m_QiQiuOpen = vQiQiuOpen ; } //打开音效 public void EnableSound(boolean vSoundOpen) { m_SoundOpen = vSoundOpen; } //打开音乐 public void EnableMusic(boolean vMusicOpen) { m_MusicOpen = vMusicOpen; if(true == m_MusicOpen) { m_BackMusic.start(); } else { m_BackMusic.pause(); } } //播放金币声音 public void PlayIconSound() { PlaySound(1,0); } //播放路径声音 public void PlayPathSound() { PlaySound(2,0); } //播放飞镖声音 public void PlayFeiBiaoSound() { PlaySound(3,0); } //播放打破气球声音 public void PlayBombSound() { PlaySound(4,0); } //改变窗口大小 public void ChangeWindowSize(int vScreenWidth,int vScreenHeight) { m_ScreenWidth = vScreenWidth; //屏幕宽度 m_ScreenHeight = vScreenHeight; //屏幕高度 //屏幕高度 m_ScaleW = m_ScreenWidth/240.0f ; m_ScaleH = m_ScreenHeight/400.0f ; m_TileW = (int) (m_ScaleW * 30); //格子宽 m_TileH = (int) (m_ScaleH * 32); //格子高 m_HelpTextMinTop = (int) (70* m_ScaleH); //帮助状态时的最小文字位置 m_HelpTextTop = m_HelpTextMinTop; //帮助状态时的文字Y位置 m_BackGroundMatrix.reset(); float bW1 = m_WelcomeUI.getWidth() ; float bH1 = m_WelcomeUI.getHeight() ; float bW2 = m_ScreenWidth ; float bH2 = m_ScreenHeight; float tScaleW = bW2/bW1; float tScaleH = bH2/bH1; m_BackGroundMatrix.postScale(tScaleW, tScaleH); m_CenterMatrix.reset(); bW1 = m_BitMapArray[31].getWidth() ; bH1 = m_BitMapArray[31].getHeight() ; bW2 = m_ScreenWidth ; bH2 = m_ScreenHeight - m_ShowIconHeight; tScaleW = bW2/bW1; tScaleH = bH2/bH1; m_CenterMatrix.postScale(tScaleW, tScaleH); } //开始线程 public void StartThread() { if(!m_appleThread.isAlive()) { m_appleThread.start(); } } //退出线程 public void ExitThread() { m_appleThread.Exit(); m_appleThread = null; //释放 Release(); } //更新 public void Update() { if(GAMEEXIT == m_GameState) { m_currenttime = System.nanoTime();//获取当前时间,单位为纳秒 m_TimeLength = (double)((m_currenttime - m_GameExitTime)/1000/1000);//毫秒 if(m_TimeLength > 3000) { winnerActivity.finish(); } } //游戏帮助 else if(HELPSTATE == m_GameState) { m_HelpTextTop -= 0.5f; } else if(GAMESTATE == m_GameState) { //游戏开始状态 if(true == m_bStartRun) { m_currenttime = System.nanoTime();//获取当前时间,单位为纳秒 m_TimeLength = (double)((m_currenttime - m_starttime)/1000/1000);//毫秒 //加速变化 if( m_TimeLength > m_Timer ) { m_Timer -= m_DecTimer ; //加速到终点 if(m_Timer < m_MinTimer) { m_DecTimer = -m_DecTimer; } else if(m_Timer >= 1000) { //减到到终点 m_DecTimer = -m_DecTimer; m_Timer = 1000; m_starttime = m_currenttime ; m_currentpathid++; m_currentpathid = (m_currentpathid)%m_TileCount; //播放放金币声音 PlayPathSound(); //计算胜利结果 CalculateWinner(); m_bStartRun = false; m_ShowBaoWu = -1; return ; } m_starttime = m_currenttime ; m_currentpathid++; m_currentpathid = (m_currentpathid)%m_TileCount; //如果是招财进宝,则放气球 int tCurrentImageId = appleTileArray[m_currentpathid].GetImageID() ; if( tCurrentImageId == 16) { myHandler.sendEmptyMessage(1);//发送放气球的消息 } } } else { if(m_WinnerAppleID >= 0) { m_currenttime = System.nanoTime();//获取当前时间,单位为纳秒 m_TimeLength = (double)((m_currenttime - m_starttime)/1000/1000);//毫秒 if( m_TimeLength > 500 ) { m_ShowWinner = !m_ShowWinner; m_starttime = m_currenttime ; } } } } } //渲染 public void Draw(Canvas canvas,Paint paint) { //退出状态 if(GAMEEXIT == m_GameState) { int centerBitmapW = m_BitMapArray[31].getWidth(); int centerBitmapH = m_BitMapArray[31].getHeight(); Bitmap centerBitmap = Bitmap.createBitmap(m_BitMapArray[31],0,0,centerBitmapW,centerBitmapH,m_BackGroundMatrix,true); canvas.save(); canvas.drawBitmap(centerBitmap, 0 , 0 , paint ); canvas.restore(); //=================="游戏人数设置==================================== String text = "正退出游戏。。。"; canvas.drawText(text, m_ScreenWidth /2 - 50*m_ScaleW, 40 * m_ScaleH, m_UITitle) ; return ; } //初始状态 if(INITSTATE == m_GameState) { int centerBitmapW = m_WelcomeUI.getWidth(); int centerBitmapH = m_WelcomeUI.getHeight(); Bitmap centerBitmap = Bitmap.createBitmap(m_WelcomeUI,0,0,centerBitmapW,centerBitmapH,m_BackGroundMatrix,true); canvas.save(); canvas.drawBitmap(centerBitmap, 0 , 0 , paint ); canvas.restore(); //按钮 int tBtnLeft = 20; int tBtnHeight = (int) (m_BitMapArray[ 44 ].getHeight()+3 * m_ScaleH); int tBtnTop = (int) (m_ScreenHeight - 4*(tBtnHeight+2)) ; for(int i = 0 ; i < 4 ; i++) { canvas.drawBitmap(m_BitMapArray[ 44 + i ], tBtnLeft + i * 40 * m_ScaleW , tBtnTop + i * tBtnHeight , paint ); } return ; } //SETTINGSTATE = 1; //游戏设置 if(SETTINGSTATE == m_GameState) { int centerBitmapW = m_BitMapArray[31].getWidth(); int centerBitmapH = m_BitMapArray[31].getHeight(); Bitmap centerBitmap = Bitmap.createBitmap(m_BitMapArray[31],0,0,centerBitmapW,centerBitmapH,m_BackGroundMatrix,true); canvas.save(); canvas.drawBitmap(centerBitmap, 0 , 0 , paint ); canvas.restore(); //=================="游戏人数设置==================================== String text = "游 戏 设 置"; canvas.drawText(text, m_ScreenWidth /2 - 50*m_ScaleW, 40 * m_ScaleH, m_UITitle) ; m_UIText.setColor(Color.BLACK); text = "游戏人数设置:"; canvas.drawText(text, 20*m_ScaleW, 70 * m_ScaleH, m_UITitle) ; int tShowConfig1 = (int) (100 * m_ScaleH); int tHalfUITextHeight = m_UITextHeight / 2 ; if( 1 == m_WinnerSize) { canvas.drawBitmap(m_BitMapArray[18], 40*m_ScaleW , tShowConfig1 , paint ); text = "单人"; canvas.drawText(text, 70*m_ScaleW, tShowConfig1 + tHalfUITextHeight , m_UIText) ; canvas.drawBitmap(m_BitMapArray[17], 120*m_ScaleW , tShowConfig1 , paint ); text = "双人"; canvas.drawText(text, 150*m_ScaleW, tShowConfig1 + tHalfUITextHeight, m_UIText) ; } else { canvas.drawBitmap(m_BitMapArray[17], 40*m_ScaleW , tShowConfig1 , paint ); text = "单人"; canvas.drawText(text, 70*m_ScaleW, tShowConfig1 + tHalfUITextHeight, m_UIText) ; canvas.drawBitmap(m_BitMapArray[18], 120*m_ScaleW , tShowConfig1 , paint ); text = "双人"; canvas.drawText(text, 150*m_ScaleW, tShowConfig1 + tHalfUITextHeight, m_UIText) ; } //================气球飞镖设置====================================== text = "弹出气球设置:"; canvas.drawText(text, 20*m_ScaleW, 140 * m_ScaleH, m_UITitle) ; int tShowConfig2 = (int) (170 * m_ScaleH); if( true == m_QiQiuOpen) { canvas.drawBitmap(m_BitMapArray[18], 40*m_ScaleW , tShowConfig2 , paint ); text = "开"; canvas.drawText(text, 70*m_ScaleW, tShowConfig2 + tHalfUITextHeight, m_UIText) ; canvas.drawBitmap(m_BitMapArray[17], 120*m_ScaleW , tShowConfig2 , paint ); text = "关"; canvas.drawText(text, 150*m_ScaleW, tShowConfig2 + tHalfUITextHeight, m_UIText) ; } else { canvas.drawBitmap(m_BitMapArray[17], 40*m_ScaleW , tShowConfig2 , paint ); text = "开"; canvas.drawText(text, 70*m_ScaleW, tShowConfig2 + tHalfUITextHeight, m_UIText) ; canvas.drawBitmap(m_BitMapArray[18], 120*m_ScaleW , tShowConfig2 , paint ); text = "关"; canvas.drawText(text, 150*m_ScaleW, tShowConfig2 + tHalfUITextHeight, m_UIText) ; } //=================游戏音效设置===================================== text = "游戏音效设置:"; canvas.drawText(text, 20*m_ScaleW, 210 * m_ScaleH, m_UITitle) ; int tShowConfig3 = (int) (240 * m_ScaleH); if( true == m_SoundOpen) { canvas.drawBitmap(m_BitMapArray[18], 40*m_ScaleW , tShowConfig3 , paint ); text = "开"; canvas.drawText(text, 70*m_ScaleW, tShowConfig3 + tHalfUITextHeight, m_UIText) ; canvas.drawBitmap(m_BitMapArray[17], 120*m_ScaleW , tShowConfig3 , paint ); text = "关"; canvas.drawText(text, 150*m_ScaleW, tShowConfig3 + tHalfUITextHeight, m_UIText) ; } else { canvas.drawBitmap(m_BitMapArray[17], 40*m_ScaleW , tShowConfig3 , paint ); text = "开"; canvas.drawText(text, 70*m_ScaleW, tShowConfig3 + tHalfUITextHeight, m_UIText) ; canvas.drawBitmap(m_BitMapArray[18], 120*m_ScaleW , tShowConfig3 , paint ); text = "关"; canvas.drawText(text, 150*m_ScaleW, tShowConfig3 + tHalfUITextHeight, m_UIText) ; } //================背景音乐设置====================================== text = "背景音乐设置:"; canvas.drawText(text, 20*m_ScaleW, 280 * m_ScaleH, m_UITitle) ; int tShowConfig4 = (int) (310 * m_ScaleH); if( true == m_MusicOpen) { canvas.drawBitmap(m_BitMapArray[18], 40*m_ScaleW , tShowConfig4 , paint ); text = "开"; canvas.drawText(text, 70*m_ScaleW, tShowConfig4 + tHalfUITextHeight, m_UIText) ; canvas.drawBitmap(m_BitMapArray[17], 120*m_ScaleW , tShowConfig4 , paint ); text = "关"; canvas.drawText(text, 150*m_ScaleW, tShowConfig4 + tHalfUITextHeight, m_UIText) ; } else { canvas.drawBitmap(m_BitMapArray[17], 40*m_ScaleW , tShowConfig4 , paint ); text = "开"; canvas.drawText(text, 70*m_ScaleW, tShowConfig4 + tHalfUITextHeight, m_UIText) ; canvas.drawBitmap(m_BitMapArray[18], 120*m_ScaleW , tShowConfig4 , paint ); text = "关"; canvas.drawText(text, 150*m_ScaleW, tShowConfig4 + tHalfUITextHeight, m_UIText) ; } int tShowConfig5 = (int) (m_ScreenHeight - 30); text = "返回主界面"; canvas.drawText(text, m_ScreenWidth /2 - 54*m_ScaleW, tShowConfig5, m_UITitle) ; return ; } //游戏帮助 if(HELPSTATE == m_GameState) { int centerBitmapW = m_BitMapArray[31].getWidth(); int centerBitmapH = m_BitMapArray[31].getHeight(); Bitmap centerBitmap = Bitmap.createBitmap(m_BitMapArray[31],0,0,centerBitmapW,centerBitmapH,m_BackGroundMatrix,true); canvas.save(); canvas.drawBitmap(centerBitmap, 0 , 0 , paint ); canvas.restore(); String text = "游 戏 帮 助"; canvas.drawText(text, m_ScreenWidth /2 - 50*m_ScaleW, 40*m_ScaleH, m_UITitle) ; m_UIText.setColor(Color.BLACK); //为画笔设置颜色 int tAreaHeight = (int) ( m_ScreenHeight - m_HelpTextMinTop + 80); float tHeight = m_HelpTextTop - (tAreaHeight - 20); tHeight += 90 * m_ScaleH ; { float tRealPosY = (m_ScreenHeight-90* m_ScaleH)+(int)(tHeight)%tAreaHeight; if(tRealPosY > m_HelpTextMinTop && tRealPosY < (m_ScreenHeight - 90* m_ScaleH)) { text = "进入游戏后开始下注, "; canvas.drawText(text, 70*m_ScaleW, tRealPosY, m_UIText) ; } } tHeight += 30* m_ScaleH; { float tRealPosY = (m_ScreenHeight-90* m_ScaleH)+(int)(tHeight)%tAreaHeight; if(tRealPosY > m_HelpTextMinTop && tRealPosY < (m_ScreenHeight - 90* m_ScaleH)) { text = "在最下方水果上点击进行下注,"; canvas.drawText(text, 5*m_ScaleW, tRealPosY, m_UIText) ; } } tHeight += 30* m_ScaleH; { float tRealPosY = (m_ScreenHeight-90* m_ScaleH)+(int)(tHeight)%tAreaHeight; if(tRealPosY > m_HelpTextMinTop && tRealPosY < (m_ScreenHeight - 90* m_ScaleH)) { text = "点击"; canvas.drawText(text, 5*m_ScaleW, tRealPosY, m_UIText) ; text = " 按钮可以开始游戏转动,"; canvas.drawText(text, 72*m_ScaleW, tRealPosY, m_UIText) ; canvas.drawBitmap(m_BitMapArray[32], 42*m_ScaleW, tRealPosY - 18* m_ScaleH ,paint ); } } tHeight += 30* m_ScaleH; { float tRealPosY = (m_ScreenHeight-90* m_ScaleH)+(int)(tHeight)%tAreaHeight; if(tRealPosY > m_HelpTextMinTop && tRealPosY < (m_ScreenHeight - 90* m_ScaleH)) { text = "金币用完后,点击"; canvas.drawText(text, 5*m_ScaleW, tRealPosY, m_UIText) ; text = " 可以投入"; canvas.drawText(text, 165*m_ScaleW, tRealPosY, m_UIText) ; canvas.drawBitmap(m_BitMapArray[33], 140*m_ScaleW, tRealPosY - 15* m_ScaleH ,paint ); } } tHeight += 30* m_ScaleH; { float tRealPosY = (m_ScreenHeight-90* m_ScaleH)+(int)(tHeight)%tAreaHeight; if(tRealPosY > m_HelpTextMinTop && tRealPosY < (m_ScreenHeight - 90* m_ScaleH)) { text = "一个金币继续游戏,转动时经过"; canvas.drawText(text, 5*m_ScaleW, tRealPosY, m_UIText) ; } } tHeight += 30* m_ScaleH; { float tRealPosY = (m_ScreenHeight-90* m_ScaleH)+(int)(tHeight)%tAreaHeight; if(tRealPosY > m_HelpTextMinTop && tRealPosY < (m_ScreenHeight - 90* m_ScaleH)) { text = "图块时会从底部放出气球"; canvas.drawText(text, 40*m_ScaleW, tRealPosY, m_UIText) ; canvas.drawBitmap(m_BitMapArray[16], 10*m_ScaleW, tRealPosY - 15* m_ScaleH ,paint ); } } tHeight += 30* m_ScaleH; { float tRealPosY = (m_ScreenHeight-90* m_ScaleH)+(int)(tHeight)%tAreaHeight; if(tRealPosY > m_HelpTextMinTop && tRealPosY < (m_ScreenHeight - 90* m_ScaleH)) { text = ",点击下注水果图片可以使左右两"; canvas.drawText(text, 5*m_ScaleW, tRealPosY, m_UIText) ; } } tHeight += 30* m_ScaleH; { float tRealPosY = (m_ScreenHeight-90* m_ScaleH)+(int)(tHeight)%tAreaHeight; if(tRealPosY > m_HelpTextMinTop && tRealPosY < (m_ScreenHeight - 90* m_ScaleH)) { text = "排相应水果释放 "; canvas.drawText(text, 5*m_ScaleW, tRealPosY, m_UIText) ; text = " ,击中"; canvas.drawText(text, 150*m_ScaleW, tRealPosY, m_UIText) ; canvas.drawBitmap(m_BitMapArray[40], 120*m_ScaleW, tRealPosY - 15* m_ScaleH ,paint ); canvas.drawBitmap(m_BitMapArray[34], 205*m_ScaleW, tRealPosY - 15* m_ScaleH ,paint ); } } tHeight += 30* m_ScaleH; //if(tHeight > m_HelpTextMinTop) { float tRealPosY = (m_ScreenHeight-90* m_ScaleH)+(int)(tHeight)%tAreaHeight; if(tRealPosY > m_HelpTextMinTop && tRealPosY < (m_ScreenHeight - 90* m_ScaleH)) { text = "会有意想不到的奖励哦。"; canvas.drawText(text, 5*m_ScaleW, tRealPosY, m_UIText) ; } } tHeight += 30* m_ScaleH; m_UIText.setColor(Color.BLUE); //为画笔设置颜色 //if(tHeight > m_HelpTextMinTop) { float tRealPosY = (m_ScreenHeight-90* m_ScaleH)+(int)(tHeight)%tAreaHeight; if(tRealPosY > m_HelpTextMinTop && tRealPosY < (m_ScreenHeight - 90* m_ScaleH)) { text = "开发者:红孩儿安卓游戏乐园"; canvas.drawText(text, 30*m_ScaleW, tRealPosY, m_UIText) ; } } tHeight += 30* m_ScaleH; //if(tHeight > m_HelpTextMinTop) { /* float tRealPosY = (m_ScreenHeight-90* m_ScaleH)+(int)(tHeight)%tAreaHeight; if(tRealPosY > m_HelpTextMinTop && tRealPosY < (m_ScreenHeight - 90* m_ScaleH)) { text = "QQ群:205100149"; canvas.drawText(text, 30*m_ScaleW, tRealPosY, m_UIText) ; } */ } int tShowConfig4 = (int) (m_ScreenHeight - 30*m_ScaleH); text = "返回主界面"; canvas.drawText(text, m_ScreenWidth /2 - 54*m_ScaleW, tShowConfig4, m_UITitle) ; m_UIText.setColor(Color.BLACK); return ; } //=========================游戏主界面显示=========================================== //背景图 //=========================================================== int centerBitmapW = m_BitMapArray[31].getWidth(); int centerBitmapH = m_BitMapArray[31].getHeight(); Bitmap centerBitmap = Bitmap.createBitmap(m_BitMapArray[ 31 ],0,0,centerBitmapW,centerBitmapH,m_CenterMatrix,true); canvas.save(); canvas.drawBitmap(centerBitmap, 0 , 0 , paint ); canvas.restore(); //=========================================================== //canvas.drawBitmap(m_BitMapArray[31], 0, 0 ,paint ); //显示玩家金币情况 for(int i = 0 ; i < m_WinnerSize ; i++) { canvas.drawBitmap(m_BitMapArray[33], 8 + i * 100, 5 ,paint ); int tIconSize = m_WinnerRecord[i].GetIconSize(); String text = ""+tIconSize; canvas.drawText(text,( 8 + i * 100 + 30) * m_ScaleW , 20 * m_ScaleH, m_WinnerPaint[i]) ; } //显示返回菜单 int menuBitMapW = m_BitMapArray[48].getWidth(); canvas.drawBitmap(m_BitMapArray[48],m_ScreenWidth - menuBitMapW - 5 , 2 * m_ScaleH ,paint ); //显示水果种类与胜利数量 Paint TextPaint = new Paint(); //创建画笔对象 String familyName = "宋体"; Typeface font = Typeface.create(familyName, Typeface.BOLD); TextPaint.setTypeface(font); TextPaint.setColor(Color.RED); //为画笔设置颜色 TextPaint.setTextSize(16* m_ScaleH); //为画笔设置字体大小 TextPaint.setAntiAlias(true); //设置抗锯齿 for(int i = 0 ; i < m_TileCows ; i++) { int index = m_TileCows - 1 - i; String text = ""+ m_Winsize[index]; if(i == m_TileCows - 1 ) { canvas.drawText(text, i * m_TileW + 5 * m_ScaleW + m_Left, m_ScreenHeight - m_ShowIconHeight + 10* m_ScaleH, TextPaint) ; } else { canvas.drawText(text, i * m_TileW + m_Left, m_ScreenHeight - m_ShowIconHeight + 10* m_ScaleH, TextPaint) ; } canvas.drawBitmap(m_BitMapArray[index], i * m_TileW +m_Left, m_ScreenHeight - m_ShowIconHeight + 10* m_ScaleH ,paint ); } //开始按钮 if(false == m_bStartRun) { int tBtnW = m_BitMapArray[32].getWidth(); int tBtnH = m_BitMapArray[32].getHeight(); m_ShowStartBtnX = (int) ((m_ScreenWidth - tBtnW)/2); //显示GO按钮的位置X m_ShowStartBtnY = (int) ((m_ScreenHeight - tBtnH)/2 + 20 * m_ScaleH); //显示GO按钮的位置Y canvas.drawBitmap(m_BitMapArray[32], m_ShowStartBtnX, m_ShowStartBtnY ,paint ); String familyName2 = "黑体"; Typeface font2 = Typeface.create(familyName2, Typeface.BOLD); Paint TextPaint2 = new Paint(); //创建画笔对象 TextPaint2.setTypeface(font2); TextPaint2.setColor(Color.BLACK); //为画笔设置颜色 TextPaint2.setTextSize(16* m_ScaleH); //为画笔设置字体大小 TextPaint2.setAntiAlias(true); //设置抗锯齿 if(m_WinnerSize == 1) { String text = "开始下注"; canvas.drawText(text, m_ShowStartBtnX - 26* m_ScaleW , m_ShowStartBtnY + 50* m_ScaleH, TextPaint2) ; } else { if( m_CurrWinnerID >= 0 ) { String text = "" + (m_CurrWinnerID + 1) + "号玩家下注"; canvas.drawText(text, m_ShowStartBtnX - 36* m_ScaleW , m_ShowStartBtnY + 60* m_ScaleH, TextPaint2) ; } } } //中间显示 //当前的图像ID int tCurrentImageId = appleTileArray[m_currentpathid].GetImageID() ; if( tCurrentImageId == 16) { int tShowCurrentImageW = m_BitMapArray[tCurrentImageId].getWidth(); int tShowCurrentImageH = m_BitMapArray[tCurrentImageId].getHeight(); int tShowCurrentImageX = (int) (m_ShowStartBtnX - tShowCurrentImageW); int tShowCurrentImageY = (int) (m_ShowStartBtnY - tShowCurrentImageH - 10 * m_ScaleH); canvas.drawBitmap(m_BitMapArray[tCurrentImageId], tShowCurrentImageX , tShowCurrentImageY, paint ); if( false == m_bStartRun ) { Paint TextPaint2 = new Paint(); //创建画笔对象 String familyName2 = "黑体"; Typeface font2 = Typeface.create(familyName2, Typeface.BOLD); TextPaint2.setTypeface(font2); TextPaint2.setColor(Color.RED); //为画笔设置颜色 TextPaint2.setTextSize(32* m_ScaleH); //为画笔设置字体大小 TextPaint2.setAntiAlias(true); //设置抗锯齿 String text = "X"+ m_Winsize[tCurrentImageId]; canvas.drawText(text, tShowCurrentImageX + tShowCurrentImageW + 10 * m_ScaleW , tShowCurrentImageY + tShowCurrentImageH, TextPaint2) ; } } else { int tShowCurrentImageW = m_BitMapArray[tCurrentImageId].getWidth(); int tShowCurrentImageH = m_BitMapArray[tCurrentImageId].getHeight(); int tShowCurrentImageX = (int) (m_ShowStartBtnX - tShowCurrentImageW); int tShowCurrentImageY = (int) (m_ShowStartBtnY - tShowCurrentImageH - 10 * m_ScaleH); int tGetIconSize = m_Winsize[tCurrentImageId] ; tCurrentImageId = tCurrentImageId % 8 ; canvas.drawBitmap(m_BitMapArray[tCurrentImageId], tShowCurrentImageX , tShowCurrentImageY , paint ); //普通图像 if( false == m_bStartRun ) { Paint TextPaint2 = new Paint(); //创建画笔对象 String familyName2 = "黑体"; Typeface font2 = Typeface.create(familyName2, Typeface.BOLD); TextPaint2.setTypeface(font2); TextPaint2.setColor(Color.RED); //为画笔设置颜色 TextPaint2.setTextSize(32* m_ScaleH); //为画笔设置字体大小 TextPaint2.setAntiAlias(true); //设置抗锯齿 String text = "X"+ tGetIconSize; canvas.drawText(text, tShowCurrentImageX + tShowCurrentImageW + 10 * m_ScaleW , tShowCurrentImageY + tShowCurrentImageH, TextPaint2) ; } } //显示奖励 if(m_ShowBaoWu >= 0) { Paint TextPaint2 = new Paint(); //创建画笔对象 String familyName2 = "黑体"; Typeface font2 = Typeface.create(familyName2, Typeface.BOLD); TextPaint2.setTypeface(font2); TextPaint2.setColor(Color.RED); //为画笔设置颜色 TextPaint2.setTextSize(20* m_ScaleH); //为画笔设置字体大小 TextPaint2.setAntiAlias(true); //设置抗锯齿 int tShowText_X = (int) (m_ScreenWidth / 2 ); int tShowText_Y = (int) (m_ScreenHeight/ 2 + 30 * m_ScaleH); switch(m_ShowBaoWu) { case 0://增加一个金币 { String text = "增加一个金币"; tShowText_X -= 70; canvas.drawText(text, tShowText_X , tShowText_Y, TextPaint2) ; } break; case 1://扣除一个金币 { String text = "扣除一个金币"; tShowText_X -= 70; canvas.drawText(text, tShowText_X , tShowText_Y, TextPaint2) ; } break; case 2://增加二个金币 { String text = "增加二个金币"; tShowText_X -= 70; canvas.drawText(text, tShowText_X , tShowText_Y, TextPaint2) ; } break; case 3://扣除二个金币 { String text = "扣除二个金币"; tShowText_X -= 70; canvas.drawText(text, tShowText_X , tShowText_Y, TextPaint2) ; } break; case 4://再持续一圈 { String text = "再发力转动"; tShowText_X -= 60; canvas.drawText(text, tShowText_X , tShowText_Y, TextPaint2) ; } break; case 5://停下来 { String text = "停下来"; tShowText_X -= 40; canvas.drawText(text, tShowText_X , tShowText_Y, TextPaint2) ; } break; case 6://所有水果都中奖 { String text = "所有水果都中奖"; tShowText_X -= 75; canvas.drawText(text, tShowText_X , tShowText_Y, TextPaint2) ; } break; case 7://中奖水果X2倍奖金 { String text = "中奖水果X2倍奖金"; tShowText_X -= 100; canvas.drawText(text, tShowText_X , tShowText_Y, TextPaint2) ; } break; } } //显示当前位置格子背景 appleTileArray[m_currentpathid].DrawBack(canvas,m_RunNum%4); //格子元素显示 for(int i = 0 ; i < m_TileCount ; i++) { appleTileArray[i].OnDraw(canvas,paint); } /* // ===============================小珠显示============================================ //左上角 canvas.drawBitmap(m_BitMapArray[17], 25+m_Left,25+m_Top, paint); //右上角 canvas.drawBitmap(m_BitMapArray[17], (m_TileCows-1)*m_TileW-10+m_Left,25+m_Top, paint); //左下角 canvas.drawBitmap(m_BitMapArray[17], 25+m_Left,(m_TileRows+1)*m_TileH - 12+m_Top, paint); //右下角 canvas.drawBitmap(m_BitMapArray[17], (m_TileCows-1)*m_TileW-10+m_Left,(m_TileRows+1)*m_TileH - 12+m_Top, paint); //其它 for(int i = 1 ; i < m_TileCows-1 ; i++) { //上行 canvas.drawBitmap(m_BitMapArray[17], i*m_TileW+5+m_Left,25+m_Top, paint); //下行 canvas.drawBitmap(m_BitMapArray[17], i*m_TileW+5+m_Left,(m_TileRows+1)*m_TileH - 12+m_Top , paint); } for(int i = 0 ; i < m_TileRows ; i++) { //右列 canvas.drawBitmap(m_BitMapArray[17],(m_TileCows-1) * m_TileW-5+m_Left,(i + 1)* m_TileH + 5+m_Top, paint); //左行 canvas.drawBitmap(m_BitMapArray[17], 20+m_Left,(i + 1) * m_TileH + 5+m_Top, paint); } //红珠 int tPathIndex = m_currentpathid; if(0 == tPathIndex) { //左上角 canvas.drawBitmap(m_BitMapArray[18], 25+m_Left,25+m_Top, paint); } else if(7 == tPathIndex) { //左上角 canvas.drawBitmap(m_BitMapArray[18], (m_TileCows-1)*m_TileW-10+m_Left,25+m_Top, paint); } else if(15 == tPathIndex) { //右下角 canvas.drawBitmap(m_BitMapArray[18], (m_TileCows-1)*m_TileW-10+m_Left,(m_TileRows+1)*m_TileH - 12+m_Top, paint); } else if(22 == tPathIndex) { //左下角 canvas.drawBitmap(m_BitMapArray[18], 25+m_Left,(m_TileRows+1)*m_TileH - 12+m_Top, paint); } else { if(0 < tPathIndex && tPathIndex < 7) { //上行 canvas.drawBitmap(m_BitMapArray[18], tPathIndex*m_TileW+5+m_Left,25+m_Top, paint); } else if(7 < tPathIndex && tPathIndex < 15) { //右列 int tIndex = tPathIndex - 8 ; canvas.drawBitmap(m_BitMapArray[18],(m_TileCows-1) * m_TileW-5+m_Left,(tIndex + 1)* m_TileH + 5+m_Top, paint); } else if(15 < tPathIndex && tPathIndex < 22) { //下行 int tIndex = (22 - tPathIndex); canvas.drawBitmap(m_BitMapArray[18], tIndex*m_TileW+5+m_Left,(m_TileRows+1)*m_TileH - 12+m_Top , paint); } else { //左行 int tIndex = (29 - tPathIndex) ; canvas.drawBitmap(m_BitMapArray[18], 20+m_Left,(tIndex + 1) * m_TileH + 5+m_Top, paint); } } // =========================================================================== */ if(true == m_QiQiuOpen) { //显示气球 if(true == m_QiQiuIsLive) { //气球显示 m_QiQiu.OnDraw(canvas,TextPaint); } //遍历飞镖列表,绘制每个飞镖对象 for( FeiBiao fb:m_FeiBiaoArray) { fb.OnDraw(canvas,TextPaint); } } //显示玩家下注情况 for(int i = 0 ; i < m_TileCows ; i++) { for(int j = 0 ; j < m_WinnerSize ; j++) { int tLeft = (int) (i * m_TileW + 1* m_ScaleW); int tTop = (int) (m_ShowIconTop + 40* m_ScaleH + j * 20* m_ScaleH) ; int tRight = (int) (tLeft + m_TileW - 1* m_ScaleW); int tBottom = (int) (tTop + 20* m_ScaleH - 1); //背景 canvas.drawRect(tLeft,tTop,tRight,tBottom, m_WinnerPaint[j]); int tappleIndex = m_TileCows-1-i; //取得是否有下注 int tRecordSize = m_WinnerRecord[j].GetRecord(tappleIndex); if( tRecordSize > 0 ) { if( (m_WinnerAppleID == tappleIndex || m_WinnerAppleID == m_WinAllAppleID )&& false == m_ShowWinner) { } else { //数字 String text = " " + tRecordSize; canvas.drawText(text, tLeft+4* m_ScaleW, tBottom-3* m_ScaleH, TextPaint) ; } } else { //数字 String text = " " + tRecordSize; canvas.drawText(text, tLeft+4* m_ScaleW , tBottom-3* m_ScaleH, TextPaint) ; } } } //显示钱袋 if( m_WinnerAppleID >= 0) { if(true == m_ShowWinner) { int tShowMoneW = m_BitMapArray[49].getWidth(); int tShowMoneH = m_BitMapArray[49].getHeight(); int tShowMoneyX = (int) ((m_ScreenWidth - tShowMoneW)/2); //显示钱袋的位置X int tShowMoneyY = (int) ((m_ScreenHeight - tShowMoneH)/2); //显示钱袋的位置Y canvas.drawBitmap(m_BitMapArray[49], tShowMoneyX, tShowMoneyY - 35* m_ScaleH ,paint ); } } /* //显示手 if(m_HandIndex_apple >= 0) { int tLeft = m_HandIndex_apple * m_TileW ; int tTop = m_ShowIconTop + m_HandIndex_winner * 20 + 40; canvas.drawBitmap(m_BitMapArray[27+m_HandIndex_winner], tLeft,tTop, paint); } */ } //设置游戏 public boolean GameConfig(float mouseX,float mouseY) { //如果是游戏设置状态 if(SETTINGSTATE == m_GameState) { if(mouseY < 120*m_ScaleH) { //单双人 if(mouseX < 120*m_ScaleW) { SetWinnerSize(1); } else { SetWinnerSize(2); } } else if(mouseY < 190*m_ScaleH) { //气球 if(mouseX < 120*m_ScaleW) { EnableQiQiu(true); } else { EnableQiQiu(false); } } else if(mouseY < 260*m_ScaleH) { //音效 if(mouseX < 120*m_ScaleW) { EnableSound(true); } else { EnableSound(false); } } else if(mouseY < 330*m_ScaleH) { //音乐 if(mouseX < 120*m_ScaleW) { EnableMusic(true); } else { EnableMusic(false); } } else { //返回游戏状态 myHandler.sendEmptyMessage(2);//发送返回主界面的消息 } return true; } else if(HELPSTATE == m_GameState) { //如果是帮助界面,返回游戏状态 if( mouseY > (m_ScreenHeight - 70*m_ScaleH)) { myHandler.sendEmptyMessage(2);//发送返回主界面的消息 } return true; } return false; } //重新开始 public void Restart(float mouseX,float mouseY) { //判断是否点击在"开始按钮"中 int tBtnW = m_BitMapArray[32].getWidth(); int tBtnH = m_BitMapArray[32].getHeight(); int tLeft = m_ShowStartBtnX - 4; int tTop = m_ShowStartBtnY - 4; int tRight = tLeft + tBtnW + 4; int tBottom = tTop + tBtnH + 4; if(mouseX < tLeft)return; if(mouseX > tRight)return; if(mouseY < tTop)return; if(mouseY > tBottom)return; if(true == IsReady()) { m_starttime = System.nanoTime(); //获取当前时间,单位为纳秒 m_Timer = 500 + random.nextInt(500); //每隔多久前进一次 m_DecTimer = 40 + random.nextInt(40); //加速度 m_MinTimer = 0; //最小时间间隔 //运行次数换图 int tBackImageIndex = m_RunNum % 4 ; switch(tBackImageIndex) { case 0: m_BitMapArray[31] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.background1);//背景 break; case 1: m_BitMapArray[31] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.background2);//背景 break; case 2: m_BitMapArray[31] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.background3);//背景 break; default: m_BitMapArray[31] = BitmapFactory.decodeResource(winnerActivity.getResources(), R.drawable.background4);//背景 break; } m_RunNum++; m_bStartRun = true; //开始 m_CurrWinnerID = 0; //当前玩家 } } //是否可以开始[检查是否有下注] public boolean IsReady() { boolean tPutIcon = false; if(false == m_bStartRun) { //如果是双人对战 if(m_WinnerSize == 2) { //查找是否有下注 for(int i = 0 ; i < m_TileCows ; i++) { for(int j = 0 ; j < m_WinnerSize ; j++) { if( m_WinnerRecord[m_CurrWinnerID].GetRecord(i) > 0 ) { tPutIcon = true; break; } } } if( true == tPutIcon ) { if(m_CurrWinnerID < 2) { m_CurrWinnerID++; } } if( m_CurrWinnerID == 2) { tPutIcon = true; m_CurrWinnerID = -1; } else { tPutIcon = false; } } else { for(int i = 0 ; i < m_TileCows ; i++) { for(int j = 0 ; j < m_WinnerSize ; j++) { if( m_WinnerRecord[j].GetRecord(i) > 0 ) { tPutIcon = true; break; } } } } } return tPutIcon; } //加币 public boolean AddIcon(float mouseX,float mouseY) { if(false == m_bStartRun) { //显示返回菜单 int tMaxRight = (int) (m_ScreenWidth - m_BitMapArray[48].getWidth()- 5); //显示玩家金币情况 for(int i = 0 ; i < m_WinnerSize ; i++) { int tIconSize = m_WinnerRecord[i].GetIconSize(); if( 0 == tIconSize) { int tLeft = i * 100 ; int tTop = 0 ; int tRight = tLeft + 100 ; if(tRight >= tMaxRight ) { tRight = tMaxRight - 1; } int tBottom = m_Top; if(mouseX < tLeft)continue; if(mouseX > tRight)continue; if(mouseY < tTop)continue; if(mouseY > tBottom)continue; m_WinnerRecord[i].AddIcon(1); //播放放金币声音 PlayIconSound(); return true; } } } return false; } //投注 public boolean DecIcon(float mouseX,float mouseY) { if(false == m_bStartRun) { if(m_WinnerAppleID >= 0) { //清空 for(int j = 0 ; j < m_WinnerSize ; j++) { m_WinnerRecord[j].CleanRecord(-1); } m_WinnerAppleID = -1; } for(int i = 0 ; i < m_TileCows ; i++) { //for(int j = 0 ; j < m_WinnerSize ; j++) //{ int tLeft = i * m_TileW ; int tTop = m_ShowIconTop ;//+ j * 20 ; int tRight = tLeft + m_TileW; int tBottom = (int) m_ScreenHeight; if(mouseX < tLeft)continue; if(mouseX > tRight)continue; if(mouseY < tTop)continue; if(mouseY > tBottom)continue; m_WinnerRecord[m_CurrWinnerID].AddRecord(m_TileCows-1-i); return true; //} } } return false; } //开始游戏 public boolean StartGame(float mouseX,float mouseY) { //如果是开始界面 if(INITSTATE == m_GameState) { //按钮 int tBtnLeft = (int) (20 * m_ScaleW); int tBtnWidth = m_BitMapArray[ 44 ].getWidth(); int tBtnHeight = (int) (m_BitMapArray[ 44 ].getHeight()+3 * m_ScaleH); int tBtnTop = (int) (m_ScreenHeight - 4*(tBtnHeight+2)) ; for(int i = 0 ; i < 4 ; i++) { int tLeft = (int) (tBtnLeft + i * 40 * m_ScaleW) ; int tTop = (int) (tBtnTop + i * tBtnHeight ) ; int tRight = tLeft + tBtnWidth; int tBottom = tTop + tBtnHeight; if(mouseX < tLeft)continue; if(mouseX > tRight)continue; if(mouseY < tTop)continue; if(mouseY > tBottom)continue; if( 0 == i ) { //进入游戏 InitGameUI(); m_GameState = GAMESTATE; } else if( 1 == i ) { //游戏设置 m_GameState = SETTINGSTATE ; } else if( 2 == i ) { //游戏帮助 m_GameState = HELPSTATE ; m_HelpTextTop = m_HelpTextMinTop; } else if( 3 == i ) { //退出游戏 m_GameState = GAMEEXIT ; m_GameExitTime = System.nanoTime();//获取当前时间,单位为纳秒 m_currenttime = System.nanoTime();//获取当前时间,单位为纳秒 //winnerActivity.finish(); //android.os.Process.killProcess(android.os.Process.myPid()); } } return true; } return false; } //检查是否点击了返回菜单 public void CheckEnterReturnMenu(float mouseX,float mouseY) { if( GAMESTATE == m_GameState ) { //显示返回菜单 int menuBitMapW = m_BitMapArray[48].getWidth(); int menuBitMapH = m_BitMapArray[48].getHeight(); int tLeft = (int) (m_ScreenWidth - menuBitMapW - 5 * m_ScaleW) ; int tTop = 0 ; int tRight = (int) (tLeft + m_TileW + 5 * m_ScaleW); int tBottom = (int) (tTop + menuBitMapH + 2 * m_ScaleH); if(mouseX < tLeft)return ; if(mouseX > tRight)return; if(mouseY < tTop)return; if(mouseY > tBottom)return; //重设状态 m_starttime = System.nanoTime();//获取当前时间,单位为纳秒 m_Timer = 500; //每隔多久前进一次 m_DecTimer = 40; //加速度 m_MinTimer = 0; //最小时间间隔 m_bStartRun = false; m_currentpathid = random.nextInt(m_TileCount); //起始位置 m_CurrWinnerID = 0; m_InitIconSize = 10; //初始时10个金币 for(int i = 0 ; i < m_MaxWinnerSize ; i++) { //玩家 m_WinnerRecord[i].SetIconSize(m_InitIconSize); m_WinnerRecord[i].CleanRecord(-1); } m_WinnerAppleID = -1; //返回菜单界面 m_GameState = INITSTATE ; } } //打气球 public void PutOutJianTou(float mouseX,float mouseY) { if( GAMESTATE == m_GameState ) { if( true == m_bStartRun && true == m_QiQiuOpen) { //如果没有气球可用,直接返回 if(false == m_QiQiuIsLive)return ; //取得点击的水果类型 int vAppleType = -1; for(int i = 0 ; i < m_TileCows ; i++) { int tLeft = i * m_TileW ; int tTop = m_ShowIconTop ; int tRight = tLeft + m_TileW; int tBottom = (int) m_ScreenHeight; if(mouseX < tLeft)continue; if(mouseX > tRight)continue; if(mouseY < tTop)continue; if(mouseY > tBottom)continue; vAppleType = m_TileCows - 1 -i; break; } if(-1 == vAppleType )return ; //如果已经有飞镖了,不能再发射了 if(m_FeiBiaoArray.size() > 0)return ; //清空飞镖 m_FeiBiaoArray.clear(); //左边 int tStartIndex = 2 * m_TileCows + m_TileRows ; for(int i = 0 ; i < m_TileRows ; i++) { int tImageID = appleTileArray[tStartIndex + i].GetImageID(); if(tImageID < 16) { tImageID = tImageID % 8 ; if( vAppleType == tImageID) { FeiBiao tFeiBiao = new FeiBiao(); int tPosX = appleTileArray[tStartIndex + i].GetTileX(); int tPosY = appleTileArray[tStartIndex + i].GetTileY(); float tSpeed = 0.5f; if(tImageID > 3) { tFeiBiao.Init(1, m_BitMapArray[42], 0, tPosX, tPosY, tSpeed); } else { tFeiBiao.Init(0, m_BitMapArray[40], 0, tPosX, tPosY, tSpeed); } m_FeiBiaoArray.add(tFeiBiao); } } } //右边 tStartIndex = m_TileCows ; for(int i = 0 ; i < m_TileRows ; i++) { int tImageID = appleTileArray[tStartIndex + i].GetImageID(); if(tImageID < 16) { tImageID = tImageID % 8 ; if( vAppleType == tImageID) { FeiBiao tFeiBiao = new FeiBiao(); int tPosX = appleTileArray[tStartIndex + i].GetTileX(); int tPosY = appleTileArray[tStartIndex + i].GetTileY(); float tSpeed = 0.5f; if(tImageID > 3) { tFeiBiao.Init(1, m_BitMapArray[43], 1, tPosX, tPosY, tSpeed); } else { tFeiBiao.Init(0, m_BitMapArray[41], 1, tPosX, tPosY, tSpeed); } m_FeiBiaoArray.add(tFeiBiao); } } } //播放飞镖声音 if(m_FeiBiaoArray.size() > 0) { PlayFeiBiaoSound(); } } } } //放一个气球 public void PutOutQiQiu() { if( GAMESTATE == m_GameState ) { if(false == m_QiQiuIsLive && true == m_QiQiuOpen) { int vQiQiuID = random.nextInt(6); //气球色彩 int vBaoWuID = random.nextInt(8); //宝物类型 float vSpeed = random.nextFloat()* 0.5f + 0.1f; int tImageW = m_BitMapArray[34 + vQiQiuID].getWidth(); //int tImageH = m_BitMapArray[34 + vQiQiuID].getHeight(); m_QiQiu.Init(vQiQiuID, m_BitMapArray[34 + vQiQiuID], (m_ScreenWidth - tImageW)/2, m_ScreenHeight, -vSpeed); m_QiQiu.SetBaoWu(vBaoWuID); m_QiQiuIsLive = true; } } } //飞镖碰撞气球 public void CollQiQiu() { if( GAMESTATE == m_GameState ) { if(true == m_QiQiu.IsLive() && true == m_QiQiuOpen) { int tBaoWuID = m_QiQiu.GetBaoWu(); m_QiQiu.Kill(); //播放打破气球的声音 PlayBombSound(); //刷币 RefreshBaoWu(tBaoWuID); } } } //处理刷宝 public void RefreshBaoWu(int vBaoWuID) { //播放放金币声音 PlayIconSound(); switch(vBaoWuID) { case 0://增加一个金币 m_WinnerRecord[0].AddIcon(1); break; case 1://扣除一个金币 m_WinnerRecord[0].DecIcon(); break; case 2://增加二个金币 m_WinnerRecord[0].AddIcon(2); break; case 3://扣除二个金币 m_WinnerRecord[0].DecIcon(); m_WinnerRecord[0].DecIcon(); break; case 4://再持续一圈 m_Timer = 1000; break; case 5://停下来 m_Timer = 2000; break; case 6://所有水果都中奖 m_WinAllApple = 1; break; case 7://中奖水果X2倍奖金 m_WinAllApple = 2; break; } m_ShowBaoWu = vBaoWuID ; } //鼠标 public void ShowHand(float mouseX,float mouseY) { /* if(false == m_bStartRun) { for(int i = 0 ; i < m_TileCows ; i++) { for(int j = 0 ; j < m_WinnerSize ; j++) { int tLeft = i * m_TileW ; int tTop = m_ShowIconTop + j * 20; int tRight = tLeft + m_TileW; int tBottom = tTop + 20; if(mouseX < tLeft)continue; if(mouseX > tRight)continue; if(mouseY < tTop)continue; if(mouseY > tBottom)continue; m_HandIndex_apple = i ; m_HandIndex_winner = j ; return ; } } } m_HandIndex_apple = -1 ; m_HandIndex_winner = -1; */ } //结算 public void CalculateWinner() { m_WinnerAppleID = -1; //所有水果都中奖 if( 1 == m_WinAllApple ) { int tCurrentImageId = appleTileArray[m_currentpathid].GetImageID() ; boolean tWin = false; if( tCurrentImageId < 16) { for(int i = 0 ; i < m_TileCows ; i++) { for(int j = 0 ; j < m_WinnerSize ; j++) { int tRecordSize = m_WinnerRecord[j].GetRecord(i); int tWinIconSisze = m_Winsize[i] * tRecordSize ; if( tWinIconSisze > 0) { m_WinnerRecord[j].AddIcon(tWinIconSisze) ; tWin = true; } } } m_WinnerAppleID = m_WinAllAppleID ; } if(false == tWin) { //清空 for(int j = 0 ; j < m_WinnerSize ; j++) { m_WinnerRecord[j].CleanRecord(-1); } } } else { //计算结果 int tCurrentImageId = appleTileArray[m_currentpathid].GetImageID() ; boolean tWin = false; if( tCurrentImageId < 16) { int tImageType = tCurrentImageId % 8 ; for(int j = 0 ; j < m_WinnerSize ; j++) { int tRecordSize = m_WinnerRecord[j].GetRecord(tImageType); //如果下注数大于0 if(tRecordSize > 0) { int tWinIconSisze = m_Winsize[tCurrentImageId] * tRecordSize ; //中奖水果X2 if( 2 == m_WinAllApple) { tWinIconSisze = tWinIconSisze * 2 ; } m_WinnerRecord[j].AddIcon(tWinIconSisze) ; m_WinnerAppleID = tImageType ; tWin = true; } } } if(false == tWin) { //清空 for(int j = 0 ; j < m_WinnerSize ; j++) { m_WinnerRecord[j].CleanRecord(-1); } } } //恢复 m_WinAllApple = 0; } }
四.AppThread.java:多线程更新逻辑。
package win.gam; public class AppleThread extends Thread { int sleepSpan = 20; //休眠时间 boolean flag = false; //线程执行标志位 Apple apple = null; public AppleThread(Apple apple) { this.apple = apple ; flag = true; //设置线程执行的标志位为true } public void Exit() { flag = false; } public void run() { while(flag) { apple.Update(); try{ Thread.sleep(sleepSpan); //休眠一段时间 } catch(Exception e){ e.printStackTrace(); } } } }
五.AppleView.java:游戏视图。
package win.gam; import win.gam.DrawThread; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.util.DisplayMetrics; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; public class AppleView extends SurfaceView implements SurfaceHolder.Callback { WinnerActivity winnerActivity; DisplayMetrics DMInfo; Apple apple; DrawThread dt; //后台屏幕绘制线程 String fps="FPS:N/A"; //用于显示帧速率的字符串,调试使用 boolean ButtonDown = false; public AppleView(Context activity) { super(activity); //调用父类构造器 winnerActivity = (WinnerActivity)activity; getHolder().addCallback(this); init(); dt = new DrawThread(this,getHolder()); //初始化重绘线程 } public void init() { DMInfo = new DisplayMetrics(); winnerActivity.getWindowManager().getDefaultDisplay().getMetrics(DMInfo); apple = new Apple(winnerActivity,DMInfo.widthPixels,DMInfo.heightPixels); } public boolean onTouchEvent(MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN) { //如果是设置游戏 if(true == apple.GameConfig(event.getX(), event.getY()))return true; //如果开始游戏 if(true == apple.StartGame(event.getX(), event.getY()))return true; //如果游戏中,看是否点击返回菜单 apple.CheckEnterReturnMenu(event.getX(), event.getY()); //如果游戏中,可以发射箭头 apple.PutOutJianTou(event.getX(), event.getY()); ButtonDown = true; //加分 if(false == apple.AddIcon(event.getX(), event.getY())) { //苹果机开始 if(false == apple.DecIcon(event.getX(), event.getY())) { apple.Restart(event.getX(), event.getY()); invalidate(); } } else { invalidate(); } } if(event.getAction() == MotionEvent.ACTION_UP) { ButtonDown = false; } //显示手 apple.ShowHand(event.getX(), event.getY()); return true; } public void doDraw(Canvas canvas) { Paint BackGraoundP = new Paint(); //创建画笔对象 BackGraoundP.setColor(Color.BLACK); //为画笔设置颜色 //刷背景色 RectF tRect = new RectF(0,0, this.getWidth(),this.getHeight()); canvas.drawRect(tRect, BackGraoundP); //画水果机 Paint paintApple = new Paint(); apple.Draw(canvas, paintApple); /* //画FPS Paint p = new Paint(); //创建画笔对象 p.setColor(Color.BLUE); //为画笔设置颜色 p.setTextSize(18); //为画笔设置字体大小 p.setAntiAlias(true); //设置抗锯齿 canvas.drawText(fps, 30, 30, p); //画出帧速率字符串 */ } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {//重写surfaceChanged方法 apple.ChangeWindowSize(width,height); } @Override public void surfaceCreated(SurfaceHolder holder) {//从写surfaceCreated方法 if(!dt.isAlive()) { //如果DrawThread没有启动,就启动这个线程 dt.start(); } } @Override public void surfaceDestroyed(SurfaceHolder holder) {//重写surfaceDestroyed方法 dt.flag = false; //停止线程的执行 dt = null; //将dt指向的对象声明为垃圾 apple.ExitThread(); } }
六.DrawThread.java:多线程更新
package win.gam; import win.gam.AppleView; import android.graphics.Canvas; import android.view.SurfaceHolder; public class DrawThread extends Thread { AppleView bv; //BallView对象引用 SurfaceHolder surfaceHolder;//SurfaceHolder对象引用 boolean flag=false; //线程执行标志位 int sleepSpan = 30; //休眠时间 long start = System.nanoTime(); //记录起始时间,该变量用于计算帧速率 int count=0; //记录帧数,该变量用于计算帧速率 //构造器 public DrawThread(AppleView bv,SurfaceHolder surfaceHolder){ this.bv = bv; //为BallView对象应用赋值 this.surfaceHolder = surfaceHolder; //为SurfaceHolder对象应用赋值 this.flag = true; //设置标志位 } //方法:线程的执行方法,用于绘制屏幕和计算帧速率 public void run(){ Canvas canvas = null;//声明一个Canvas对象 while(flag){ try{ canvas = surfaceHolder.lockCanvas(null);//获取BallView的画布 synchronized(surfaceHolder){ bv.doDraw(canvas); //调用BallView的doDraw方法进行绘制 } } catch(Exception e){ e.printStackTrace(); //捕获并打印异常 } finally{ if(canvas != null){ //如果canvas不为空 surfaceHolder.unlockCanvasAndPost(canvas);//surfaceHolder解锁并将画布对象传回 } } this.count++; if(count == 20){ //如果计满20帧 count = 0; //清空计数器 long tempStamp = System.nanoTime();//获取当前时间 long span = tempStamp - start; //获取时间间隔 start = tempStamp; //为start重新赋值 double fps = Math.round(100000000000.0/span*20)/100.0;//计算帧速率 bv.fps = "FPS:"+fps;//将计算出的帧速率设置到BallView的相应字符串对象中 } try{ Thread.sleep(sleepSpan); //线程休眠一段时间 } catch(Exception e){ e.printStackTrace(); //捕获并打印异常 } } } }
七.FeiBiao.java:飞镖类
package win.gam; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; public class FeiBiao { int m_FeiBiaoType = 0; //飞镖类型 int m_FangXiang = 0; //方向 0右 1左 float m_PosX = 0; //位置X float m_PosY = 0; //位置Y float m_Speed = -0.01f; //速度 boolean m_IsLive = false; //是否存活 Bitmap m_Image = null; //图像 FeiBiao() { } //设置气球 public void Init(int vFeiBiaoType,Bitmap Image,int vFangXiang ,int vPosX,int vPosY,float vSpeed) { m_FeiBiaoType = vFeiBiaoType; m_Image = Image ; m_FangXiang = vFangXiang ; m_PosX = vPosX; m_PosY = vPosY; m_Speed = vSpeed; m_IsLive = true; } //是否存活 public boolean IsLive() { return m_IsLive; } //取得气球类型 public int GetQiQiuType() { return m_FeiBiaoType; } //取得气球位置 public float GetPosX() { return m_PosX; } public float GetPosY() { return m_PosY; } //取得气球的宽度 public float GetImageWidth() { return m_Image.getWidth(); } //取得气球的高度 public float GetImageHeight() { return m_Image.getHeight(); } //更新 public void Update(float vScreenWidth,float vScreenHeight) { if(m_IsLive) { int tImageW = m_Image.getWidth(); //右 if(0 == m_FangXiang) { if( m_PosX > (vScreenWidth + tImageW)) { m_IsLive = false; } else { m_PosX += m_Speed ; } } else { //左 if( m_PosX < - tImageW) { m_IsLive = false; } else { m_PosX -= m_Speed ; } } } } //绘制 public void OnDraw(Canvas canvas,Paint paint) { if(m_IsLive) { canvas.drawBitmap(m_Image, m_PosX ,m_PosY, paint); } } }
八.FeiBiaoThread.java:多线程更新飞镖逻辑。
package win.gam; public class FeiBiaoThread extends Thread { int sleepSpan = 2; //休眠时间 boolean flag = false; //线程执行标志位 Apple apple = null; public FeiBiaoThread(Apple apple) { this.apple = apple ; flag = true; //设置线程执行的标志位为true this.start(); } //运行 public void run() { while(flag) { //更新飞镖 if( apple.m_FeiBiaoArray.size() > 0 ) { boolean IsLive = false; //位置 float tLeft = apple.m_QiQiu.GetPosX(); float tTop = apple.m_QiQiu.GetPosY(); float tRight = tLeft + apple.m_QiQiu.GetImageWidth(); float tBottom = tTop + apple.m_QiQiu.GetImageHeight(); //遍历飞镖列表,绘制每个飞镖对象 for( FeiBiao fb:apple.m_FeiBiaoArray) { fb.Update(apple.GetScreenWidth(),apple.GetScreenHeight()); if(true == fb.IsLive()) { IsLive = true; //位置 float tLeft2 = fb.GetPosX(); float tTop2 = fb.GetPosY(); float tRight2 = tLeft2 + fb.GetImageWidth(); float tBottom2 = tTop2 + fb.GetImageHeight(); if(true == CheckColl(tLeft,tTop,tRight,tBottom,tLeft2,tTop2,tRight2,tBottom2)) { apple.CollQiQiu(); } } } //如果都结束了退出线程 if(false == IsLive) { apple.m_FeiBiaoArray.clear(); } } try{ Thread.sleep(sleepSpan); //休眠一段时间 } catch(Exception e){ e.printStackTrace(); } } } //检查是否碰撞 public boolean CheckColl(float vLeft1,float vTop1,float vRight1,float vBottom1,float vLeft2,float vTop2,float vRight2,float vBottom2) { if(vLeft1 > vRight2 )return false; if(vTop1 > vBottom2 )return false; if(vRight1 < vLeft2 )return false; if(vBottom1 < vTop2 )return false; return true; } }
九.QiQiu.java:气球类
package win.gam; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; public class QiQiu { int m_QiQiuType = 0; //气球类型 int m_BaoWu = 0; //宝物 float m_PosX = 0; //位置X float m_PosY = 0; //位置Y float m_Speed = -0.1f; //速度 boolean m_IsLive = false; //是否存活 Bitmap m_Image = null; //图像 QiQiu() { } //设置气球 public void Init(int vQiQiuType,Bitmap Image,float vPosX,float vPosY,float vSpeed) { m_QiQiuType = vQiQiuType; m_Image = Image ; m_PosX = vPosX; m_PosY = vPosY; m_Speed = vSpeed; m_IsLive = true; } //是否存活 public boolean IsLive() { return m_IsLive; } //杀掉气球 public void Kill() { m_IsLive = false; } //取得气球类型 public int GetQiQiuType() { return m_QiQiuType; } //取得气球位置 public float GetPosX() { return m_PosX; } public float GetPosY() { return m_PosY; } //取得气球的宽度 public float GetImageWidth() { return m_Image.getWidth(); } //取得气球的高度 public float GetImageHeight() { return m_Image.getHeight(); } //设置宝物 public void SetBaoWu(int vBaoWu) { m_BaoWu = vBaoWu; } //取得宝物 public int GetBaoWu() { return m_BaoWu; } //更新 public void Update(float vScreenWidth,float vScreenHeight) { if(m_IsLive) { int tImageH = m_Image.getHeight(); if( m_PosY < - tImageH) { m_IsLive = false; } else { m_PosY += m_Speed ; } } } //绘制 public void OnDraw(Canvas canvas,Paint paint) { if(m_IsLive) { canvas.drawBitmap(m_Image, m_PosX ,m_PosY, paint); } } }
十.QiQiuThread.java: 多线程更新气球逻辑。
package win.gam; public class QiQiuThread extends Thread { int sleepSpan = 2; //休眠时间 boolean flag = false; //线程执行标志位 Apple apple = null; public QiQiuThread(Apple apple) { this.apple = apple ; flag = true; //设置线程执行的标志位为true this.start(); } //运行 public void run() { while(flag) { if(true == apple.m_QiQiu.IsLive()) { //更新气球 apple.m_QiQiu.Update(apple.GetScreenWidth(),apple.GetScreenHeight()); //如果气球挂了,则退出线程 if(false == apple.m_QiQiu.IsLive()) { apple.m_QiQiuIsLive = false; } } else { apple.m_QiQiuIsLive = false; } try{ Thread.sleep(sleepSpan); //休眠一段时间 } catch(Exception e){ e.printStackTrace(); } } } }
十一.Tile.java:轮盘的格子类。
package win.gam; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; public class Tile { /* static final int FX_RIGHTTOP = 0; static final int FX_RIGHT = 1; static final int FX_RIGHTBOTTOM = 2; static final int FX_BOTTOM = 3; static final int FX_LEFTBOTTOM = 4; static final int FX_LEFT = 5; static final int FX_LEFTTOP = 6; static final int FX_TOP = 7; */ int TileX = 0; //格子位置 int TileY = 0; //int FangXiang = FX_RIGHTTOP; //方向 // int ImageID = 0; //图像ID Bitmap Image;//图像 Tile() { } //设置位置 public void SetTile(int vTileX,int vTileY)//,int vFangXiang) { TileX = vTileX; TileY = vTileY; //FangXiang = vFangXiang ; } //取得位置X public int GetTileX() { return TileX; } //取得位置Y public int GetTileY() { return TileY; } //设置图像ID public void SetImage(int vBitMapID,Bitmap vBitMap) { ImageID = vBitMapID ; Image = vBitMap; } //取得图像ID public int GetImageID() { return ImageID; } //绘制背景 public void DrawBack(Canvas canvas,int vBackIndex) { int tWidth = Image.getWidth(); int tHeight = Image.getHeight(); int tBorderSize = 4 ; Paint tBackPaint = new Paint(); //创建画笔对象 switch(vBackIndex) { case 0: tBackPaint.setColor(Color.BLUE); //为画笔设置颜色 break; case 1: tBackPaint.setColor(Color.YELLOW); //为画笔设置颜色 break; case 2: tBackPaint.setColor(Color.CYAN); //为画笔设置颜色 break; case 3: tBackPaint.setColor(Color.RED); //为画笔设置颜色 break; } //绘制背景图 canvas.drawRect(TileX-tBorderSize,TileY-tBorderSize, TileX + tWidth+tBorderSize,TileY + tHeight+tBorderSize,tBackPaint); } //绘制 public void OnDraw(Canvas canvas,Paint paint) { canvas.drawBitmap(Image, TileX,TileY, paint); } }
代码讲解完了。原理应该很清楚了。但因为是使用SurfaceView,所以效率不太好,我正打算改成cocos2d-x版本,等改好后再教给大家如何使用Cocos2d-x来制做。好了,下课吧~
本游戏程序压缩包下载位置:
http://download.csdn.net/detail/honghaier/4976678