整個游戲我分為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()