android俄罗斯方块完整代码

这篇博客分享了作者自己编写的Android版俄罗斯方块的完整代码,包括形状存储、音乐播放类、资源播放类和图片按钮类的详细实现。通过10个Java文件实现了游戏逻辑,涉及游戏状态、资源加载、音乐控制等方面。作者强调了自学的重要性,并鼓励读者交流和改进代码。
摘要由CSDN通过智能技术生成

这段时间一直没有更新自己的博客,很多东西不经常看看会生疏了许多。在这里我就把自己前段时间编写的android俄罗斯方块代码贴出来。 

                                                                          转载请注明出处~~~~~~~~~~~~~~~~谢谢!

                                                                        http://blog.csdn.net/wu10045125/article/details/8248356

 

再贴出几张图片:

整个游戏我分为10个java文件:

先是俄罗斯方块的形状存储statefang.java,代码如下:

 

package com.example.eluosifangkuai;

public class statefang {  //方块的逻辑类
 
 public static int [][][]  state = new int[][][] {
  {// I
   { 0, 0, 1, 0 }, { 0, 0, 1, 0 }, { 0, 0, 1, 0 }, { 0, 0, 1, 0 } }, {// I1
   { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 1, 1, 1, 1 } }, {// I2
   { 0, 0, 1, 0 }, { 0, 0, 1, 0 }, { 0, 0, 1, 0 }, { 0, 0, 1, 0 } }, {// I3
   { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 1, 1, 1, 1 } }, {// I4
   { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 1, 1, 0 }, { 0, 1, 1, 0 } }, {// O5
   { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 1, 1, 0 }, { 0, 1, 1, 0 } }, {// O6
   { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 1, 1, 0 }, { 0, 1, 1, 0 } }, {// O7
   { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 1, 1, 0 }, { 0, 1, 1, 0 } }, {// O8
   { 0, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 1, 0 } }, {// L9
   { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 1, 1, 1, 0 }, { 1, 0, 0, 0 } }, {// L10
   { 0, 0, 0, 0 }, { 0, 1, 1, 0 }, { 0, 0, 1, 0 }, { 0, 0, 1, 0 } }, {// L11
   { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 1, 0 }, { 1, 1, 1, 0 } }, {// L12
   { 0, 0, 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 1, 0 }, { 0, 1, 1, 0 } }, {// J13
   { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 1, 0, 0, 0 }, { 1, 1, 1, 0 } }, {// J14
   { 0, 0, 0, 0 }, { 0, 1, 1, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 } }, {// J15
   { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 1, 1, 1, 0 }, { 0, 0, 1, 0 } }, {// J16
   { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 1, 0, 0 }, { 1, 1, 1, 0 } }, {// T17
   { 0, 0, 0, 0 }, { 0, 0, 1, 0 }, { 0, 1, 1, 0 }, { 0, 0, 1, 0 } }, {// T18
   { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 1, 1, 1, 0 }, { 0, 1, 0, 0 } }, {// T19   
   { 0, 0, 0, 0 }, { 1, 0, 0, 0 }, { 1, 1, 0, 0 }, { 1, 0, 0, 0 } }, {// T20
   { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 1, 1, 0 }, { 1, 1, 0, 0 } }, {// S21
   { 0, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 1, 0 }, { 0, 0, 1, 0 } }, {// S22
   { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 1, 1, 0 }, { 1, 1, 0, 0 } }, {// S23
   { 0, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 1, 0 }, { 0, 0, 1, 0 } }, {// Z24
   { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 1, 1, 0, 0 }, { 0, 1, 1, 0 } }, {// Z25
   { 0, 0, 0, 0 }, { 0, 0, 1, 0 }, { 0, 1, 1, 0 }, { 0, 1, 0, 0 } }, {// Z26
   { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 1, 1, 0, 0 }, { 0, 1, 1, 0 } }, {// Z27
   { 0, 0, 0, 0 }, { 0, 0, 1, 0 }, { 0, 1, 1, 0 }, { 0, 1, 0, 0 } } // 28
  
 };

}

 

我们当然还要编写音乐播放类,资源播放类了等等。。。。。。

我把所有的图片资源编写在一个类里面,叫做GameResources.java,具体代码如下:

 

package com.example.eluosifangkuai;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Bitmap.Config;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;

public class GameResources {
 
 Resources m_Resources;  // 资源类
 Canvas m_Canvas;    // 画布
 Bitmap m_Bitmaphc = null;  // 缓冲位图
 Bitmap m_Bitmap01 = null;  // 图像位图
 Bitmap [] m_Bitmaps = new Bitmap[8];  //精灵位图
 Bitmap score;  // 分数位图
 Bitmap Play;   // 开始位图
 Bitmap Level;
 public GameResources(Context context)  // 初始化 装载位图
 {
  m_Resources = context.getResources();
 for(int i=0;i<7;i++)
 {
  m_Bitmaps[i] = createImage(m_Resources.getDrawable(R.drawable.cube_960_011+i),18,18);
 }
 m_Bitmap01 = createImage(m_Resources.getDrawable(R.drawable.bgcatcher),320,480);
 m_Bitmaps[7] = createImage(m_Resources.getDrawable(R.drawable.main11),320,402);
 
 score = createImage(m_Resources.getDrawable(R.drawable.score),87,150);
 Play = createImage(m_Resources.getDrawable(R.drawable.b7),320,480);
 Level = createImage(m_Resources.getDrawable(R.drawable.levelup),139,88);
 m_Bitmaphc = Bitmap.createBitmap(320,480, Config.ARGB_8888);
 m_Canvas = new Canvas(m_Bitmaphc);
 bitmapB();
 }
 
 public void bitmapB()
 {
  Paint m_Paint = new Paint();
  m_Paint.setAntiAlias(true);
  m_Paint.setAlpha(220);
  m_Canvas.drawBitmap(m_Bitmap01, 0,0,null);    
 }
 public static Bitmap createImage(Drawable tile, int w, int h) {  // 双缓冲 加载位图资源
  Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
  Canvas canvas = new Canvas(bitmap);
  tile.setBounds(0, 0, w, h);
  tile.draw(canvas);
  return bitmap;
 }
 
}

 

音乐播放类,MusicPlay.java 具体代码如下:

package com.example.eluosifangkuai;


import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;

public class MusicPlay {
 
  public static MediaPlayer m_MediaPlay ; // 背景播放器
  public static MediaPlayer m_MenuPlay ;
  public static SoundPool soundPool;//声明 音效播放器
  public MediaPlayer m_FastDown ;
 
  private static boolean musicSwitch = true;//音乐开关
  private static boolean soundSwitch = true;//音效开关
  private static Map<Integer,Integer> soundMap; //音效资源id与加载过后的音源id的映射关系表
  private static Context context;
 
  public static void inItMusicPlay(Context c){
   context = c;
  }
  public static void inItMenuMusicPlay(Context c){
   context = c;
  }
  //初始化背景播放器
 public static void BgMediaplayer()
 {
   m_MediaPlay = MediaPlayer.create(context, R.raw.gamebg);
   m_MediaPlay.setLooping(true);
 }
 public static void menuMusic()
 {
  m_MenuPlay = MediaPlayer.create(context, R.raw.menubg);
  m_MenuPlay.setLooping(true);
 }
    public static void pauseMusic()
    {
   if(m_MediaPlay.isPlaying())
   {
    m_MediaPlay.pause();
   }
 }
    public static void pauseMenuMusic()
    {
   if(m_MenuPlay.isPlaying())
   {
    m_MenuPlay.pause();
   }
 }
    public static void startMusic(){
  if(musicSwitch){
   m_MediaPlay.start();
  }
 } 
    public static void startMenuMusic(){
  if(musicSwitch){
   m_MenuPlay.start();
  }
 } 
    public static void releaseMusic(){
  if(m_MediaPlay != null){
   m_MediaPlay.release();
  }
 }
    public static void releaseMenuMusic(){
  if(m_MenuPlay != null)
  {
   m_MenuPlay.release();
  }
 }
 
  //设置音乐开关
   public static void setMusicSwitch(boolean musicSwitch){
    MusicPlay.musicSwitch = musicSwitch;
    if(MusicPlay.musicSwitch){
     m_MediaPlay.start();
    }
    else{
     m_MediaPlay.stop();
    }
   }
   public static void inItSound()
   {
    soundPool = new SoundPool(8,AudioManager.STREAM_MUSIC,0);
    soundMap = new HashMap<Integer, Integer>();
  //将音效资源加入 soundPool,并做成soundMap 映射
  soundMap.put(R.raw.action,soundPool.load(context, R.raw.action, 1));
  soundMap.put(R.raw.fastdown,soundPool.load(context, R.raw.fastdown, 1));
  soundMap.put(R.raw.rotation,soundPool.load(context, R.raw.rotation, 1));
  soundMap.put(R.raw.down,soundPool.load(context, R.raw.down, 1));
  soundMap.put(R.raw.delete1,soundPool.load(context, R.raw.delete1, 1));
  soundMap.put(R.raw.delete2,soundPool.load(context, R.raw.delete2, 1));
  soundMap.put(R.raw.delete3,soundPool.load(context, R.raw.delete3, 1));
  soundMap.put(R.raw.delete4,soundPool.load(context, R.raw.delete4, 1));
   }
      public static int playSound(int resId,int loop)
      {
  
  if(!soundSwitch){
   return 0;
  }
  
  Integer soundId = soundMap.get(resId);
        if(soundId != null)
        {
          return soundPool.play(soundId, 1, 1, 1, loop, 1);
        }
        else
        {
         return 0;
        }
     }
      public static void releaseSound(){
   if(soundPool != null){
    soundPool.release();
   }
  }
}

 

另外我还编写了一个图片按钮类,名为ImageButton.java 具体代码如下:

package com.example.eluosifangkuai;

import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;

public class ImageButton {
 
     //  按钮图片
     private Bitmap mBitButton = null;
    
     //图片绘制的XY坐标
     private int mPosX =0;
     private int mPosY =0;
     //图片绘制的宽高
     private int mWidth =0;
     private int mHeight =0;
  
     public ImageButton(Context context, int frameBitmapID, int x, int y) {
  mBitButton = ReadBitMap(context,frameBitmapID);
  mPosX = x;
  mPosY = y;
  mWidth = mBitButton.getWidth();
  mHeight = mBitButton.getHeight();
     }

     //绘制图片按钮
     public void DrawImageButton(Canvas canvas, Paint paint) {
  canvas.drawBitmap(mBitButton, mPosX, mPosY, paint);
     }
    
   
    //  判断是否点中图片按钮
     public boolean IsClick(int x, int y) {
  boolean isClick = false;
  if (x >= mPosX && x <= mPosX + mWidth && y >= mPosY
   && y <= mPosY

  • 18
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 72
    评论
评论 72
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值