美文网首页
android 俄罗斯方块

android 俄罗斯方块

作者: 葫芦岛iv | 来源:发表于2019-02-07 12:57 被阅读0次
android 俄罗斯方块

方向键按钮用 ImageView 或者 ImageButton
不能用 Button 按钮图片和声音文件自己加
程序代码只有两百行

/**
 *葫芦岛.iv 2019 02 02
 */
import android.app.*;
import android.graphics.*;
import android.graphics.Bitmap.*;
import android.media.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import java.util.Random;
public class MainActivity extends Activity  {
  private int 当前图形 = -1, 下一图形, 平移, 平移1,下降, 行, i,移动x,移动y, ax, ay;
  private int 行数=31,列数=16,格宽=50,下降速度=400,得分数, 级别数, 消层数;
  private int [] 方块x = new int [4],方块y = new int [4], 旋转x = new int [4],旋转y = new int [4];
  private int [] 颜色={Color.BLACK ,Color.CYAN,Color.YELLOW,Color.BLUE,Color.GREEN,Color.WHITE,Color.MAGENTA,Color.RED};
  private int [][] 网格点 = new int [行数][列数];
  private int [][] 方块组X = { { 0, 1, 0, 1 }, { -1, 0, 1, 2 }, { -1, 0, 0, 1 }, { -1, 0, 0, 1 }, { -1, 0, 0, 1 }, { -1, -1, 0, 1 }, { -1, 0, 1, 1 } };
  private int [][] 方块组Y = { { -1, -1, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, -1, -1 }, { -1, -1, 0, 0 }, { 0, 0, -1, 0 }, { 0, -1, 0, 0 }, { 0, 0, 0, -1 } };
  private SoundPool 播放声音=new SoundPool(3, AudioManager.STREAM_SYSTEM, 5);
  private Random random = new Random();
  private ImageView 界面,上,下,左,右;
  private TextView 级别,层数,分数;
  private Paint p = new Paint();
  private Bitmap bitmap;
  private Canvas g; 
  private Handler handler = new Handler();
  private Runnable update_thread = new Runnable() {
    public void run () {
      if(下降速度>0){
      运行();    
      handler.postDelayed(update_thread, 下降速度);
      }
    } };
  
  @Override
  protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);   
    setContentView(R.layout.main);
    上 = (ImageView)findViewById(R.id.上id);
    下 = (ImageView)findViewById(R.id.下id);
    左 = (ImageView)findViewById(R.id.左id);
    右 = (ImageView)findViewById(R.id.右id);  
    分数 = (TextView)findViewById(R.id.分数id);
    级别 = (TextView)findViewById(R.id.级别id);
    层数 = (TextView)findViewById(R.id.层数id);
    界面 = (ImageView)findViewById(R.id.界面id);
    bitmap = Bitmap.createBitmap(800, 1550, Config.ARGB_4444);
    g = new Canvas(bitmap);
    界面.setImageBitmap(bitmap); 
    播放声音.load(this, R.drawable.xcs, 1);   
    播放声音.load(this, R.drawable.sjs, 1);   
    播放声音.load(this, R.drawable.sbs, 1);  
    上.setOnTouchListener(new OnTouchListener(){
        @Override
        public boolean onTouch (View p1, MotionEvent p2) {
          if(p2.getAction() == MotionEvent.ACTION_DOWN && 当前图形 > 0) {
            for(i = 0; i < 4; i++) {
              旋转x[i] = 方块x[i]; 
              旋转y[i] = 方块y[i]; 
              行 = 旋转y[i]; 
              旋转y[i] = 旋转x[i]; 
              旋转x[i] = 行 * -1;
              ax = 移动x + 旋转x[i] + 平移;
              ay = 移动y + 旋转y[i] + 下降;
              if(ax < 0 || ax > 列数 || ay > 30 || 网格点[ay][ax] > 0)
                return true;
            } 
            移动方块(true);}       
          return true;
        } });
    下.setOnTouchListener(new OnTouchListener(){
        @Override
        public boolean onTouch (View p1, MotionEvent p2) {
          if(p2.getAction() == MotionEvent.ACTION_DOWN) {运行();下降速度 = 30;}
          if(p2.getAction() == MotionEvent.ACTION_UP) 下降速度 = 400 - 级别数 * 40;
          return true;
        } });  
    左.setOnTouchListener(new OnTouchListener(){
        @Override
        public boolean onTouch (View p1, MotionEvent p2) {
          if(p2.getAction() == MotionEvent.ACTION_DOWN) {平移1 = -1;运行();下降速度 = 80;}
          if(p2.getAction() == MotionEvent.ACTION_UP) {
            下降速度 = 400 - 级别数 * 40; 平移1 = 0; 下降 = 1;}
          return true;
        } });  
    右.setOnTouchListener(new OnTouchListener(){
        @Override
        public boolean onTouch (View p1, MotionEvent p2) {
          if(p2.getAction() == MotionEvent.ACTION_DOWN) {平移1 = 1;运行();下降速度 = 80;}
          if(p2.getAction() == MotionEvent.ACTION_UP) {
            下降速度 = 400 - 级别数 * 40; 平移1 = 0; 下降 = 1;}
          return true;
        } }); p.setTextSize(160); 
        handler.post(update_thread);//启动    
  }

  private void 运行 () {    
    if(当前图形 == -1) {//产生下一个图形    
      当前图形 = 下一图形; 
      下一图形 = random.nextInt(7);// 随机产生下一图形
      移动x = 7;
      移动y = 3;//回到起始点
      p.setColor(Color.BLACK);
      g.drawRect(300, 0, 500, 100, p);
      p.setColor(颜色[下一图形 + 1]);
      for(i = 0; i < 4; i++) {    
        ax = 方块组X[下一图形][i] * 格宽 + 350; 
        ay = 方块组Y[下一图形][i] * 格宽 + 格宽;
        g.drawRoundRect(new RectF(ax, ay, ax + 格宽, ay + 格宽), 10, 10, p);
        方块x[i] = 方块组X[当前图形][i]; 
        方块y[i] = 方块组Y[当前图形][i];
      }
    }
    平移 = 平移1;下降 = 下降 == 1 && 平移 != 0 ? 0 : 1; // 减慢下降速度
    for(i = 0; i < 4; i++) {// 是否可以移动方块
      ax = 移动x + 方块x[i] + 平移; ay = 移动y + 方块y[i] + 下降;
      if(ax < 0 || ax > 15 || ay < 行数 && 网格点[ay][ax] > 0) {
        ax -= 平移; 平移 = 0;//平移时横向超出游戏区||网格点上有方块 平移归零
      }
      if(ay >= 行数 || 网格点[ay][ax - 平移] > 0)
        break;//下降到最底层||网格点上有方块就不能再移动了
    }  
    if(i == 4) {if(下降速度 != 80)移动方块(false);下降速度++; return; }// 能够移动方块就不执行下面的代码
    下降速度 = 400 - 级别数 * 40; 
    for(i = 0; i < 4; i++)  // 不能移动方块 就给网格点赋值
      网格点[移动y + 方块y[i]][移动x + 方块x[i]] = 当前图形 + 1;
    行 = 0;// 消行
    for(ay = 30; ay > 1; ay--)
      for(i = 0, ax = 0; ax < 列数; ax++) {
        网格点[ay + 行][ax] = 网格点[ay][ax]; //数组消行
        if(网格点[ay][ax] != 0)
          行 = ++i == 列数 ? 行 + 1 : 行;  //消行条件
      }
    if(行 > 0) {
      得分数 += (int)Math.pow(行, 2) * 10;
      消层数 += 行;  
      for(ay = 0; ay < 行数; ay++)
        for(ax = 0; ax < 列数; ax++) {
          p.setColor(颜色[网格点[ay][ax]]);
          g.drawRoundRect(new RectF(ax * 格宽, ay * 格宽, ax * 格宽 + 格宽 , ay * 格宽 + 格宽), 10, 10, p);
        }
      if(得分数 > 级别数 * 640) {
        if(++级别数 == 8) {
          下降速度 = 400;
          g.drawText("恭喜,通关", 0, 600, p); 
        } 
        播放声音.play(2, 1, 1, 0, 0, 1);
      } 
     else 播放声音.play(1, 1, 1, 0, 0, 1); 
      分数.setText("分数:" + 得分数);   
      级别.setText("级别:" + 级别数);    
      层数.setText("层数:" + 消层数);  
    } else if(移动y < 5) {// Game Over
      播放声音.play(3, 1, 1, 0, 0, 1); 
      g.drawText("Game Over", 0, 600, p);
      下降速度=-1;
    } 
    当前图形 = -1;//开启图形开关 
    界面.invalidate();//刷新    
  }

  private void 移动方块 (boolean 转) {
    p.setColor(Color.BLACK);
    for(i = 0; i < 4; i++) {// 清除方块
      ax = (移动x + 方块x[i]) * 格宽;
      ay = (移动y + 方块y[i]) * 格宽;
      g.drawRect(ax, ay, ax + 格宽, ay + 格宽, p);
    } 
    移动x += 平移;
    移动y += 下降;
    p.setColor(颜色[当前图形 + 1]);
    for(i = 0; i < 4; i++) {// 绘制方块
      if(转) {方块x[i] = 旋转x[i]; 方块y[i] = 旋转y[i];}
      ax = (移动x + 方块x[i]) * 格宽; 
      ay = (移动y + 方块y[i]) * 格宽;
      g.drawRoundRect(new RectF(ax, ay, ax + 格宽 , ay + 格宽), 10, 10, p);// 绘制方块
    }
    界面.invalidate();//刷新   
  }
  public void 开始Click (View v) {
    if(下降速度 == -1)handler.post(update_thread);//启动    
    网格点 = new int [行数][列数]; 
    得分数 = 级别数 = 消层数 = 平移 = 0;
    当前图形 = -1;下降 = 1;下降速度 = 400;
    g.drawColor(Color.BLACK);
  }
  public void 暂停Click (View v) {
    下降速度 = -下降速度;
    if(下降速度 > 0) 
     handler.postDelayed(update_thread, 下降速度);
  }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:gravity="bottom">

        <ImageView
            android:layout_height="1550px"
            android:background="#000000"
            android:layout_width="800px"
            android:id="@+id/界面id"
            android:onClick="暂停Click"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="分数:"
                android:id="@+id/分数id"
                android:textSize="20sp"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="级别:"
                android:id="@+id/级别id"
                android:layout_marginTop="30dp"
                android:textSize="20sp"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="层数:"
                android:id="@+id/层数id"
                android:layout_marginTop="30dp"
                android:textSize="20sp"/>

            <Button
                android:layout_height="100dp"
                android:layout_width="match_parent"
                android:text="开始"
                android:layout_marginTop="50dp"
                android:layout_marginBottom="78dp"
                android:onClick="开始Click"
                android:id="@+id/开始id"/>

            <ImageView
                android:layout_height="51dp"
                android:layout_width="51dp"
                android:src="@android:drawable/ic_delete"
                android:id="@+id/上id"/>

        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:gravity="right">

        <ImageView
            android:layout_height="51dp"
            android:layout_width="51dp"
            android:src="@android:drawable/ic_delete"
            android:id="@+id/左id"/>

        <ImageView
            android:layout_height="51dp"
            android:layout_width="51dp"
            android:src="@android:drawable/ic_delete"
            android:id="@+id/下id"/>

        <ImageView
            android:layout_height="51dp"
            android:layout_width="51dp"
            android:src="@android:drawable/ic_delete"
            android:id="@+id/右id"/>

    </LinearLayout>

</LinearLayout>

相关文章

网友评论

      本文标题:android 俄罗斯方块

      本文链接:https://www.haomeiwen.com/subject/xlnwsqtx.html