Android PlayGame

[b][size=large][align=center]PushBox[/align][/size][/b]
这里介绍一款经典小游戏:PushBox,共分四部分介绍。在编写代码之前,应先设计游戏的框架,了解各个类之间的关系,游戏的流程,前期的准备是做出游戏的必备条件。
[list]
[*][b]欢迎动画界面的设计与实现 [/b]
[/list]

当玩家运行游戏时,首先看到的是欢迎界面,欢迎界面是整个游戏的门面,良好的欢迎界面会增加玩家对游戏的视觉体验。下面将开始对欢迎动画界面的开发进行介绍。


[1].[b]PushBoxActivity类框架的搭建 [/b]

public class PushBoxActivity extends Activity{ //主类
WelcomeView welcomeView = null;//欢迎界面
WelcomeViewGoThread welcomeViewGoThread = null;//欢迎界面中的移动线程
MenuView menuView = null;
MenuViewGoThread menuViewGoThread = null;//菜单界面中的移动线程
GameView gameView = null;

boolean isSound = true;//是否播放声音
MediaPlayer pushBoxSound;//推箱子声音
MediaPlayer backSound;//背景音乐
MediaPlayer winSound;//胜利的音乐
MediaPlayer startSound;//开始和菜单时的音乐

int map1[][];
int map2[][];//当前游戏的地图
int selectMap = 0;//选中的地图

MySprite mySprite;//精灵

KeyThread kt;//键盘监听线程
int action = 0;//键盘的状态,二进制表示 从左往右表示上下左右

Handler myHandler = new Handler(){//用来更新UI线程中的控件
public void handleMessage(Message msg) {
if(msg.what == 1){//收到WelcomeViewGoThread/Welcome发来的消息 有各个Thread发来msg
welcomeViewGoThread.setFlag(false);
if(welcomeView != null){
welcomeView = null;
}
initAndToMenuView();
}
else if(msg.what == 2){//收到MenuView发来的消息
if(menuView != null){
menuView = null;
}
initAndToGameView();
}
else if(msg.what == 3){
if(gameView != null){
gameView = null;
}
initAndToMenuView();
}
else if(msg.what == 4){//收到GameView来的消息,进入下一关
if(selectMap+1<MapList.map1.length){
selectMap = selectMap+1;
initAndToGameView();
}
else {
selectMap = 0;
initAndToGameView();
}
}
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//全屏
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN ,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

pushBoxSound = MediaPlayer.create(this, R.raw.sound2);//推箱子的声音
winSound = MediaPlayer.create(this, R.raw.winsound);//胜利的声音
backSound = MediaPlayer.create(this, R.raw.sound1);//背景音乐
backSound.setLooping(true); //设置循环
startSound = MediaPlayer.create(this, R.raw.sound3);
startSound.setLooping(true);
initAndToWelcomeView();
}

public void initAndToWelcomeView(){
welcomeView = new WelcomeView(this);
if(isSound){
startSound.start();
}
this.setContentView(welcomeView);
welcomeViewGoThread = new WelcomeViewGoThread(this);
welcomeViewGoThread.start();
}

public void initAndToMenuView(){
menuView = new MenuView(this);
this.setContentView(menuView);
menuViewGoThread = new MenuViewGoThread(this);
menuViewGoThread.start();
}

public void initAndToGameView(){
map1 = new int[MapList.map1[selectMap].length][MapList.map1[selectMap][0].length];
for(int i=0; i<MapList.map1[selectMap].length; i++){
for(int j=0; j<MapList.map1[selectMap][i].length; j++){
map1[i][j] = MapList.map1[selectMap][i][j];//初始化第一层
}
}
map2 = new int[MapList.map2[selectMap].length][MapList.map2[selectMap][0].length];
for(int i=0; i<MapList.map2[selectMap].length; i++){
for(int j=0; j<MapList.map2[selectMap][i].length; j++){
map2[i][j] = MapList.map2[selectMap][i][j];//初始化第二层
}
}
gameView = new GameView(this);
mySprite = new MySprite(this);
kt = new KeyThread(this);//添加键盘监听
kt.start();
if(isSound){
backSound.start();//播放声音
}
this.setContentView(gameView);
}

public boolean onKeyUp(int keyCode, KeyEvent event) {//键盘抬起
if(keyCode == 19){//上
action = action & 0x37;
}
if(keyCode == 20){//下
action = action & 0x3B;
}
if(keyCode == 21){//左
action = action & 0x3D;
}
if(keyCode == 22){//右
action = action & 0x3E;
}
return false;
}

public boolean onKeyDown(int keyCode, KeyEvent event){//键盘按下监听
if(keyCode == 19){//上
action = action | 0x08;
}
if(keyCode == 20){//下
action = action | 0x04;
}
if(keyCode == 21){//左
action = action | 0x02;
}
if(keyCode == 22){//右
action = action | 0x01;
}
return false;
}
}


PushBoxActivity作为各个部分之间跳转的桥梁,承担找纽带作用。

这个类中Handler方法起到接受各个类的消息,并作出相应的动作。

Handler myHandler = new Handler(){//用来更新UI线程中的控件
public void handleMessage(Message msg) {
if(msg.what == 1){//收到WelcomeViewGoThread/Welcome发来的消息 有各个Thread发来msg
welcomeViewGoThread.setFlag(false);
if(welcomeView != null){
welcomeView = null;
}
initAndToMenuView();
}
else if(msg.what == 2){//收到MenuView发来的消息
if(menuView != null){
menuView = null;
}
initAndToGameView();
}
else if(msg.what == 3){
if(gameView != null){
gameView = null;
}
initAndToMenuView();
}
else if(msg.what == 4){//收到GameView来的消息,进入下一关
if(selectMap+1<MapList.map1.length){
selectMap = selectMap+1;
initAndToGameView();
}
else {
selectMap = 0;
initAndToGameView();
}
}
}
};


当Welcome动作完成之后,相应的会发出一个Message,比如:
if(msg.what == 1){//收到WelcomeViewGoThread/Welcome发来的消息 有各个Thread发来msg
welcomeViewGoThread.setFlag(false);
if(welcomeView != null){
welcomeView = null;
}
initAndToMenuView();
}
受到这个消息后,进入初始化菜单的界面initAndToMenuView()。

[2].[b]WelcomeView[/b]

public class WelcomeView extends SurfaceView implements SurfaceHolder.Callback, OnClickListener{
PushBoxActivity pushBoxActivity;
WelcomeViewDrawThread welcomeViewDrawThread = null;
Bitmap bitmap;
Bitmap wallLeft;//左面的墙
Bitmap wallRight;//右面的墙
Bitmap iron;//铁门
Bitmap woodLeft;//左面的木门
Bitmap woodRight;//右面的木门
Bitmap background;
Bitmap mountain;//背景的山

int wallLeftX = 15;//墙的坐标
int wallLeftY = 10;
int wallRightX = 150;
int wallRightY = 10;

int ironX = 15;//铁门的坐标
int ironY = 10;

int woodLeftX = 15;//木门的坐标
int woodLeftY = 10;
int woodRightX = 150;
int woodRightY = 10;
public WelcomeView(PushBoxActivity pushBoxActivity) {//构造器
super(pushBoxActivity);
this.pushBoxActivity = pushBoxActivity;
getHolder().addCallback(this);
welcomeViewDrawThread = new WelcomeViewDrawThread(this,getHolder());
wallRight = BitmapFactory.decodeResource(getResources(), R.drawable.wallright);
wallLeft = BitmapFactory.decodeResource(getResources(), R.drawable.wallleft);
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image4);
iron = BitmapFactory.decodeResource(getResources(), R.drawable.image2);
woodLeft = BitmapFactory.decodeResource(getResources(), R.drawable.image33);
woodRight = BitmapFactory.decodeResource(getResources(), R.drawable.image3);
background = BitmapFactory.decodeResource(getResources(), R.drawable.background);//背景的水
mountain = BitmapFactory.decodeResource(getResources(), R.drawable.mountain2);
}
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);//背景白色
canvas.drawBitmap(background, 0, 0, new Paint());//绘制背景
canvas.drawBitmap(mountain, 0, 0, new Paint());//后面的山图片
canvas.drawBitmap(wallLeft, wallLeftX, wallLeftY,new Paint());//墙的左面
canvas.drawBitmap(wallRight, wallRightX, wallRightY,new Paint());//墙的右面
canvas.drawBitmap(iron, ironX, ironY,new Paint());//铁门
canvas.drawBitmap(woodLeft, woodLeftX, woodLeftY,new Paint());//木头门左面
canvas.drawBitmap(woodRight, woodRightX, woodRightY,new Paint());//木头门右面
canvas.drawBitmap(bitmap, 0, 0, new Paint());
this.setOnClickListener(this);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
public void surfaceCreated(SurfaceHolder holder) {
welcomeViewDrawThread.setFlag(true);
welcomeViewDrawThread.start();//启动刷帧线程
}
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
welcomeViewDrawThread.setFlag(false);//停止刷帧线程
while (retry) {
try {
welcomeViewDrawThread.join();//等待刷帧线程结束
retry = false;
}
catch (InterruptedException e) {//不断地循环,直到等待的线程结束
}
}
}
public void onClick(View v) {
pushBoxActivity.myHandler.sendEmptyMessage(1);
}
}

这个类主要对View初始化,
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值