WEB开发网
开发学院手机开发Android 开发 android平台俄罗斯方块游戏完整代码 阅读

android平台俄罗斯方块游戏完整代码

 2012-12-02 14:31:58 来源:WEB开发网   
核心提示: 帮助类,helpActivity.java 代码为:package com.example.eluosifangkuai;import android.app.Activity;import android.os.Bundle;import android.view.Window;import android.vie
 
帮助类,helpActivity.java 代码为:
package com.example.eluosifangkuai;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class helpActivity extends Activity{
 
 public void onCreate(Bundle help)
 {
 super.onCreate(help);
  requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置无标题,全面模式
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
 setContentView(R.layout.help);
 }
}
 
接下来就游戏的核心类了,所有游戏运行都在这两个类中,首先是一个游戏视图类,包括了游戏运行的核心代码。在以后我会慢慢解释这些代码的含义,希望大家支持。
GameView.java 具体代码为:
 
package com.example.eluosifangkuai;
import java.util.Random;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
public class GameView extends View{
 
 public final static int of_Width = 24;  // 游戏运行的长度
 public final static int of_Height = 10;  // 游戏运行的宽度
 int gradeCourt = 0;   //  分数、等级、 消行数
 int levl = 1;
 int line = 0;
 private int[][] m_screen = new int[of_Width][of_Height];  //屏幕数组
 private int[][] m_color = new int[of_Width][of_Height];  // 颜色方块数组
 private int[] log_color = new int[of_Height];     // 消行数时记录颜色值
 private int k = 0,a = 0,state = 0,statenext = 0;   // 随即数和随即方块
 private int of_x = 0,of_y = 0;     //  下落位置
 private int of_x_x = 0;      // 阴影离下方物的距离
 private int add_dis_y = 0;    // 震动距离
 public int log_X = 0;  // 升级位图绘制标志
 public  int timeTask = 1000;  // 等级级数相关参数
 private boolean buttonLeft = false;   //按键变换颜色值
 private boolean buttonRight = false;
 public boolean buttonRotate = false;
 public boolean buttonDown = false;
 public boolean buttonFastDown = false;
 public boolean buttonPause = false;
  ImageButton m_ButtonLeft = null;  //图像按钮
  ImageButton m_ButtonLeft_a = null;
  ImageButton m_ButtonRight = null;
  ImageButton m_ButtonRight_a = null;
  ImageButton m_ButtonDown = null;
  ImageButton m_ButtonDown_a = null;
  ImageButton m_ButtonFastDown = null;
  ImageButton m_ButtonFastDown_a = null;
  ImageButton m_ButtonRotate = null;
  ImageButton m_ButtonRotate_a = null;
  ImageButton m_ButtonPause = null;
  ImageButton m_ButtonPause_a = null;
  Canvas m_Canvas = null;
 GameResources m_GameResources;
 Random m_Random = null;
 
 public GameView(Context context)  // 视图类构造函数 负责一些变量的初始化
 {
 super(context);
 m_GameResources = new GameResources(context);  // 初始化位图资源变量
 inItButton(context);     //  初始化图像按钮
 clean();     // 清除游戏开始方块数组值
 m_Random = new Random();   // 产生随即值函数
 state = Math.abs(m_Random.nextInt()%7);   //当前方块颜色值
 statenext = Math.abs(m_Random.nextInt()%7); //下一个方块颜色值
 k = Math.abs(m_Random.nextInt()%28);   // 当前方块
 a = Math.abs(m_Random.nextInt()%28);  //  下一个方块
 for(int i=0;i<4;i++)      // 初始化方块
 {
  for(int j=0;j<4;j++)
  {
  if(0 != statefang.state[k][i][j])
  {
   m_screen[i][j+3] = statefang.state[k][i][j];
   m_color[i][j+3] = state;
  } 
  }
 }
 of_y = 3;   //  出现方块的位置值
 fastDown(1);    //  获取下方透明方块的距离
 setFocusable(true);    //  获取按键焦点,可以响应触摸事件
 setFocusableInTouchMode(true); 
 }
 
 public void inItButton(Context context)   //  初始化图像按钮
 {
 m_ButtonLeft = new ImageButton(context,R.drawable.control_left1,0,354);
 m_ButtonLeft_a = new ImageButton(context,R.drawable.control_left2,0,354);
 m_ButtonRight = new ImageButton(context,R.drawable.control_right1,80,354);
 m_ButtonRight_a = new ImageButton(context,R.drawable.control_right2,80,354); 
 m_ButtonRotate = new ImageButton(context,R.drawable.control_rotate1,160,354);
 m_ButtonRotate_a = new ImageButton(context,R.drawable.control_rotate2,160,354);
 m_ButtonDown = new ImageButton(context,R.drawable.control_down1,240,354);
 m_ButtonDown_a = new ImageButton(context,R.drawable.control_down2,240,354);
 m_ButtonFastDown = new ImageButton(context,R.drawable.control_drop1,230,300);
 m_ButtonFastDown_a = new ImageButton(context,R.drawable.control_drop2,230,300);
 }
 public void clean()
 {
 for(int i = 0;i<of_Width;i++)
 {
  for(int j=0;j<of_Height;j++)
  {
  m_screen[i][j] = 0;
  m_color[i][j] = -1;
  }
 }
 }
 public void onDraw(Canvas canvas)  // 画图函数 ,主线程中实现不停刷新  此函数响应刷新
 {
 super.onDraw(canvas);
 Paint m_Paint = new Paint();    //  新建画笔
 m_Paint.setAntiAlias(true);  // 设置抗锯齿
 m_Paint.setFakeBoldText(true); //true为粗体,false为非粗体
 m_Paint.setColor(Color.RED);
 m_Paint.setAlpha(100);  // 设置透明度
 this.setKeepScreenOn(true);  //  保存屏幕常亮
 Log.i("onDraw","执行了");    
 canvas.drawBitmap(m_GameResources.m_Bitmaphc,0,0,null);   // 画出双缓冲中的图片
 canvas.drawBitmap(m_GameResources.m_Bitmaps[7],0,add_dis_y,null);
 canvas.drawBitmap(m_GameResources.score,230,150+add_dis_y,null);
 
 
 PaintTm(canvas,m_Paint);  //  绘制下方透明方块
 Paintnext(canvas);    // 绘制下一个产生方块
 paintCourt(canvas);   // 绘制方块本身,在屏幕数组的位置
 PaintButton(canvas,m_Paint);  // 绘制下方方向导航按键
 canvas.drawText(" "+levl,256,185+add_dis_y,m_Paint);  // 绘制等级、分数、消行数
 canvas.drawText(" "+gradeCourt,255,185+add_dis_y+50,m_Paint);
 canvas.drawText(" "+line,255,185+add_dis_y+100,m_Paint);
 add_dis_y = 0;   // 调整震动原始值
 m_Canvas = canvas; 
 }
 public void PaintButton(Canvas canvas,Paint m_Paint)// 绘制下方方向导航按键和升级位图
 {
 if(timeTask == 800 || timeTask == 500 || timeTask == 200)
 {
  if(log_X == 0)
    {
  canvas.drawBitmap(m_GameResources.Level,35,200, m_Paint);
  log_X = 1;
    }
 }
 if(buttonLeft)
 {
  m_ButtonLeft_a.DrawImageButton(m_Canvas, m_Paint);
 }
 else
 {
  m_ButtonLeft.DrawImageButton(canvas, m_Paint);
 }
 
 buttonLeft = false;
 
 if(buttonRight)
 {
  m_ButtonRight_a.DrawImageButton(canvas, m_Paint);
 }
 else
 {
  m_ButtonRight.DrawImageButton(canvas, m_Paint);
 }
 
 buttonRight = false;
 
 if(buttonRotate)
 {
  m_ButtonRotate_a.DrawImageButton(canvas, m_Paint);
 }
 else
 {
  m_ButtonRotate.DrawImageButton(canvas, m_Paint);
 }
 
 buttonRotate = false;
 
 if(buttonDown)
 {
  m_ButtonDown_a.DrawImageButton(canvas, m_Paint);
 }
 else
 {
  m_ButtonDown.DrawImageButton(canvas, m_Paint);
 }
 
 buttonDown = false;
 
 if(buttonFastDown)
 {
  m_ButtonFastDown_a.DrawImageButton(canvas, m_Paint);
 }
 else
 {
  m_ButtonFastDown.DrawImageButton(canvas, m_Paint);
 }
 
 buttonFastDown = false;
 
 }
 public void paintCourt(Canvas canvas)
 {
 for(int i=0;i<of_Width;i++)
 {
  for(int j=0;j<of_Height;j++)
  {
  if(0 != m_screen[i][j] && i > 3)
  {
   canvas.drawBitmap(m_GameResources.m_Bitmaps[m_color[i][j]],j*18+17,(i-4)*18+11+add_dis_y,null);   
  }
  }
 }
 }
 
 
 public void colorstate(int x,int y,int stateColor)  // 颜色的状态值
 {
 for(int i=0;i<4;i++)
 {
  for(int j=0;j<4;j++)
  {
  if(statefang.state[k][i][j] != 0)
  m_color[x+i][y+j] = stateColor;
  }
 }
 }
 public void PaintTm(Canvas canvas,Paint paint) // 绘制下方半透明方块 
 {
 for(int i=0;i<4;i++)
 {
  for(int j=0;j<4;j++)
  {
  int num = ((of_x_x-4+i)*18+11+add_dis_y);
  
  if(0 != statefang.state[k][i][j] && num >= 11)
  {
   canvas.drawBitmap(m_GameResources.m_Bitmaps[state],(of_y+1+j)*18,(of_x_x-4+i)*18+11+add_dis_y,paint);
   
  }
  }
 }
 }
   public void Paintnext(Canvas canvas)
   {
    for(int i=0;i<4;i++)
 {
  for(int j=0;j<4;j++)
  {
  if(0 != statefang.state[a][i][j])
  {
   canvas.drawBitmap(m_GameResources.m_Bitmaps[statenext],j*15+225,60+i*15+add_dis_y,null);   
  }
  }
 }
 }
  
 public void idKeyDown(int key,KeyEvent keyevent)   //对按键的响应
 {
 if(key == KeyEvent.KEYCODE_DPAD_DOWN && moveDownOn())//&& of_x < of_Width-4){
 {
  Down();
  fastDown(1);
  buttonDown = false;
 } 
 if(key == KeyEvent.KEYCODE_DPAD_RIGHT && moveRightOn())
 {
    Right();
    fastDown(1);
    buttonRight = false;
   }
 if(key == KeyEvent.KEYCODE_DPAD_LEFT && moveLeftOn() )//&& of_y > 0)
 {
  Left();
  fastDown(1);
  buttonLeft = false;
 }
 if(key == KeyEvent.KEYCODE_DPAD_UP&&rotateOnCourt())
 {
  rate();
  fastDown(1);
  buttonRotate = false;
 }
 if(key == KeyEvent.KEYCODE_DPAD_CENTER && moveDownOn())
 {
  FastDown();
  fastDown(1);
  buttonFastDown = false;
 }
  
 }

上一页  3 4 5 6 7 8 9 10 11 12 13  下一页

Tags:android 平台 俄罗斯

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接