安卓游戏开发一(超级玛丽) 您所在的位置:网站首页 超级马里奥游戏开发 安卓游戏开发一(超级玛丽)

安卓游戏开发一(超级玛丽)

#安卓游戏开发一(超级玛丽)| 来源: 网络整理| 查看: 265

超级玛丽(1)

前言:想做安卓游戏开发,首先你需要先掌握一些安卓的基础知识,然后学会使用surfaceview绘制一些简单的bitmap,学会了这些,你就具备了游戏开发的最基本条件。本文主要适合一些对surfaceview有一定了解的朋友,整个游戏都是使用android的原生代码编写,未使用第三方的任何东西,属于作者我原创,希望读者朋友尊重原创。

先上视频链接:游戏效果视频

github地址(三个完整项目):https://github.com/nihuoLT/myGameRepository.git

里面还有其它俩个游戏,博主将他们开源,只是为了在成都找个工作,有公司的大佬看到,觉得博主适合您公司的职位或则对Android2d游戏开发有兴趣的朋友

都可以联系博主,至于游戏效果是不是我说的这么好,大家,下载看看就知道了

废话不多说,今天我们先把马里奥的移动做出来。首先定义一个接口类GameInterFace.java:

package com.example.newgame_1; import android.graphics.Bitmap; public interface GameInterFace { int getX(); int getY(); int getWidth(); int getHeight(); Bitmap getBitmap(); } 然后定义我们的马里奥类Maliao.java让它实现接口,然后写它的构造方法:

public JumpTest(int x,int y,GameView gameView){ this.gameView=gameView;//拿到主Surfaceview this.source=BitmapFactory.decodeStream(gameView.getResources() .openRawResource(R.drawable.allmali));//获取马里奥的图片 this.dead=BitmapFactory.decodeStream(gameView.getResources() .openRawResource(R.drawable.mario13));//获取马里奥死亡图片 this.x=x; this.width=source.getWidth()/6; this.height=source.getHeight(); this.cache=Bitmap.createBitmap(width, height, Config.ARGB_8888);//创建缓存图片 this.y=y-height; this.speed=20; sprites.add(Bitmap.createBitmap(source,0,0,width-2,height));//切割图片 sprites.add(Bitmap.createBitmap(source,width,0,width,height)); sprites.add(Bitmap.createBitmap(source,width*2,0,width,height)); sprites.add(Bitmap.createBitmap(source,width*3+1,0,width-1,height)); sprites.add(Bitmap.createBitmap(source,width*4,0,width,height)); sprites.add(Bitmap.createBitmap(source,width*5,0,width,height)); }

然后在getBitmap方法里这样写:

public Bitmap getBitmap(){ if(IsMove){//是否移动标志 bitmap=sprites.get(index);//这里实现帧动画 if(count==5){//每5秒切换一帧 index++; if(index==size){ index=first; } count=0; } count++; if(XdirectionFlag){//移动方向标志 if(x0) x-=5; } }else{ if(XdirectionFlag){//没有移动时切换为相应方向的帧 bitmap=sprites.get(0); }else{ bitmap=sprites.get(3); } } return bitmap; }

在Activity中左右按钮控制方向:

public boolean onTouch(View v, MotionEvent event) {       switch (event.getAction()) {         case MotionEvent.ACTION_DOWN: switch (v.getId()) {   case R.id.left: gameView.jumpTest.IsMove=true; gameView.jumpTest.XdirectionFlag=false; gameView.jumpTest.first=2;             gameView.jumpTest.index=gameView.jumpTest.first;             gameView.jumpTest.size=4;             left.setBackgroundResource(R.drawable.zuo_r);   break;              case R.id.right:             gameView.jumpTest.IsMove=true;             gameView.jumpTest.XdirectionFlag=true;             gameView.jumpTest.first=0;             gameView.jumpTest.index=gameView.jumpTest.first;             gameView.jumpTest.size=2;             right.setBackgroundResource(R.drawable.you_r);   break;      }     break;  case MotionEvent.ACTION_UP:           switch (v.getId()) {   case R.id.left:     eft.setBackgroundResource(R.drawable.zuo_b); gameView.jumpTest.IsMove=false;   break;              case R.id.right:             right.setBackgroundResource(R.drawable.you_b);             gameView.jumpTest.IsMove=false;   break:           } break;     } return true; }

最后在surfaceview里绘制出来:

for(GameInterFace gameInterFace:(List)mys.clone()){    c.drawBitmap(gameInterFace.getBitmap(), gameInterFace.getX(),gameInterFace.getY(), paint); }

今天就先讲到这里下面是效果图:



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有