android平台俄罗斯方块游戏代码.docx
《android平台俄罗斯方块游戏代码.docx》由会员分享,可在线阅读,更多相关《android平台俄罗斯方块游戏代码.docx(37页珍藏版)》请在冰豆网上搜索。
![android平台俄罗斯方块游戏代码.docx](https://file1.bdocx.com/fileroot1/2022-11/17/cd502080-09ad-4453-b161-10e67c311dc3/cd502080-09ad-4453-b161-10e67c311dc31.gif)
android平台俄罗斯方块游戏代码
整个游戏我分为10个java文件:
先是俄罗斯方块的形状存储statefang.java,代码如下:
package.example.eluosifangkuai;publicclassstatefang{//方块的逻辑类publicstaticint[][][]state=newi
整个游戏我分为10个java文件:
先是俄罗斯方块的形状存储statefang.java,代码如下:
package.example.eluosifangkuai;
publicclassstatefang{//方块的逻辑类
publicstaticint[][][]state=newint[][][]{
{//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.example.eluosifangkuai;
importandroid.content.Context;
importandroid.content.res.Resources;
importandroid.graphics.Bitmap;
importandroid.graphics.Canvas;
importandroid.graphics.Bitmap.Config;
importandroid.graphics.Paint;
importandroid.graphics.drawable.Drawable;
publicclassGameResources{
Resourcesm_Resources;//资源类
Canvasm_Canvas; //画布
Bitmapm_Bitmaphc=null;//缓冲位图
Bitmapm_Bitmap01=null;//图像位图
Bitmap[]m_Bitmaps=newBitmap[8];//精灵位图
Bitmapscore;//分数位图
BitmapPlay; //开始位图
BitmapLevel;
publicGameResources(Contextcontext)//初始化装载位图
{
m_Resources=context.getResources();
for(inti=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=newCanvas(m_Bitmaphc);
bitmapB();
}
publicvoidbitmapB()
{
Paintm_Paint=newPaint();
m_Paint.setAntiAlias(true);
m_Paint.setAlpha(220);
m_Canvas.drawBitmap(m_Bitmap01,0,0,null);
}
publicstaticBitmapcreateImage(Drawabletile,intw,inth){//双缓冲加载位图资源
Bitmapbitmap=Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
Canvascanvas=newCanvas(bitmap);
tile.setBounds(0,0,w,h);
tile.draw(canvas);
returnbitmap;
}
}
音乐播放类,MusicPlay.java具体代码如下:
package.example.eluosifangkuai;
importjava.util.HashMap;
importjava.util.Map;
importandroid.content.Context;
importandroid.media.AudioManager;
importandroid.media.MediaPlayer;
importandroid.media.SoundPool;
publicclassMusicPlay{
publicstaticMediaPlayerm_MediaPlay;//背景播放器
publicstaticMediaPlayerm_MenuPlay;
publicstaticSoundPoolsoundPool;//声明音效播放器
publicMediaPlayerm_FastDown;
privatestaticbooleanmusicSwitch=true;//音乐开关
privatestaticbooleansoundSwitch=true;//音效开关
privatestaticMapsoundMap;//音效资源id与加载过后的音源id的映射关系表
privatestaticContextcontext;
publicstaticvoidinItMusicPlay(Contextc){
con