网络资源模板--Android Studio 实现俄罗斯方块游戏App

目录

一、项目演示

二、项目测试环境

三、项目详情

四、完整的项目源码 


一、项目演示

网络资源模板--基于Android studio 实现的俄罗斯方块游戏

二、项目测试环境

三、项目详情

首页

这段代码实现了一个Android应用中的选择活动(`SelectActivity`)。以下是功能总结:

1. **布局和视图**:在`onCreate`方法中,设置了活动的布局,并初始化了五个`ImageView`(`grade1`至`grade5`),它们代表不同的等级选择。

2. **点击监听**:为每个`ImageView`设置了点击监听器,所有等级图标的点击都会调用`onClick`方法。

3. **启动新活动**:在`onClick`方法中,根据点击的视图ID,将相应的等级信息(1到5)通过`Intent`传递给`MainActivity`,然后启动这个活动。

总的来说,这个活动让用户可以选择不同的等级,并将选择结果传递到主活动中。

package com.example.tetris;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class SelectActivity extends AppCompatActivity implements View.OnClickListener {
    ImageView grade1;
    ImageView grade2;
    ImageView grade3;
    ImageView grade4;
    ImageView grade5;
    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_select);
        this.grade1 = (ImageView) findViewById(R.id.grade1);
        this.grade2 = (ImageView) findViewById(R.id.grade2);
        this.grade3 = (ImageView) findViewById(R.id.grade3);
        this.grade4 = (ImageView) findViewById(R.id.grade4);
        this.grade5 = (ImageView) findViewById(R.id.grade5);
        this.grade1.setOnClickListener(this);
        this.grade2.setOnClickListener(this);
        this.grade3.setOnClickListener(this);
        this.grade4.setOnClickListener(this);
        this.grade5.setOnClickListener(this);
    }

    public void onClick(View view) {
        Intent intent = new Intent(this, MainActivity.class);
        switch (view.getId()) {
            case R.id.grade1:
                intent.putExtra("grade", 1);
                break;
            case R.id.grade2:
                intent.putExtra("grade", 2);
                break;
            case R.id.grade3:
                intent.putExtra("grade", 3);
                break;
            case R.id.grade4:
                intent.putExtra("grade", 4);
                break;
            case R.id.grade5:
                intent.putExtra("grade", 5);
                break;
        }
        startActivity(intent);
    }
}

这段代码主要实现了一个简单的Tetris(俄罗斯方块)游戏的一些核心功能。以下是主要功能总结:

1. **清空下一个方块列表**:
   - `nextTetrisList` 被清空,然后根据下一个方块的形状,将对应的颜色添加到列表中。如果位置为空,则添加0表示空白。

2. **适配器设置**:
   - 创建 `BlockAdapter`,将 `nextTetrisList` 作为数据源并设置给下一个方块的视图,用于显示下一个即将出现的方块。

3. **计时器管理**:
   - 创建并管理计时器,定期发送更新消息以更新游戏状态。在暂停和继续游戏时,能够启动和停止计时器。

4. **游戏暂停/继续功能**:
   - `pause()` 方法实现了切换暂停状态,更新按钮文本和禁用/启用控制按钮。

5. **方块向下移动及消行逻辑**:
   - `stopDown()` 方法处理方块向下移动,更新方块状态,并检查行是否满。如果满行,则增加分数,向上移动下方行,并清空顶部行。

6. **游戏结束逻辑**:
   - 检查顶部行是否为空,若不为空则结束游戏,并更新最高分记录。

7. **随机方块状态更新**:
   - 生成下一个随机方块和颜色,并调用方法更新下一个方块的显示。

这段代码实现了俄罗斯方块游戏的基本功能,但可以考虑提高可读性、增加错误处理、模块化设计和增强用户界面反馈等改进建议。

// 清空下一个方块列表
        this.nextTetrisList.clear();
        for (int i3 = 0; i3 < 4; i3++) {
            for (int i4 = 0; i4 < 4; i4++) {
                // 根据下一个方块的形状添加颜色到列表
                if (((1 << i4) & StateFang.shape[this.nextRand][i3]) != 0) {
                    this.nextTetrisList.add(Integer.valueOf(this.nextRandColor)); // 添加颜色
                } else {
                    this.nextTetrisList.add(0); // 空白位置
                }
            }
        }

// 创建下一个方块的适配器,并设置给下一个方块的视图
        BlockAdapter blockAdapter3 = new BlockAdapter(this, this.nextTetrisList, R.layout.item_adapter);
        this.nextTetrisAdapter = blockAdapter3;
        this.nextTetrisView.setAdapter((ListAdapter) blockAdapter3); // 设置适配器

// 打印当前随机形状的日志
        String str2 = this.TAG;
        Log.i(str2, this.rand + "");

// 创建计时器,定时发送消息以更新游戏状态
        Timer timer2 = new Timer();
        this.timer = timer2;
        timer2.schedule(new TimerTask() {
            public void run() {
                MainActivity.this.handler.sendEmptyMessage(0); // 定时发送更新消息
            }
        }, 0, (long) this.timeInterval); // 设置定时任务,开始时立即执行,每隔 timeInterval 毫秒执行一次
    }

    private void pause() {
        // 切换暂停状态
        boolean z = !this.isPause;
        this.isPause = z;
        if (z) { // 如果现在是暂停状态
            stopTimer(); // 停止计时器
            this.pausebtn.setText("继续"); // 将按钮文本改为“继续”
            this.leftMove.setEnabled(false); // 禁用左移按钮
            this.rightMove.setEnabled(false); // 禁用右移按钮
            this.rotateMove.setEnabled(false); // 禁用旋转按钮
            this.downMove.setEnabled(false); // 禁用下移按钮
            return;
        }
        startTimer(); // 启动计时器
        this.pausebtn.setText("暂停"); // 将按钮文本改为“暂停”
        this.leftMove.setEnabled(true); // 启用左移按钮
        this.rightMove.setEnabled(true); // 启用右移按钮
        this.rotateMove.setEnabled(true); // 启用旋转按钮
        this.downMove.setEnabled(true); // 启用下移按钮
    }

    private void startTimer() {
        // 如果计时器为空,初始化计时器
        if (this.timer == null) {
            this.timer = new Timer();
        }
        // 安排一个计时任务
        this.timer.schedule(new TimerTask() {
            public void run() {
                MainActivity.this.handler.sendEmptyMessage(0); // 发送消息以更新游戏状态
            }
        }, 0, (long) this.timeInterval); // 从0毫秒开始,每隔timeInterval毫秒执行
    }

    private void stopTimer() {
        // 停止计时器
        Timer timer2 = this.timer;
        if (timer2 != null) {
            timer2.cancel(); // 取消计时器
            this.timer = null; // 将计时器设置为null
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopTimer(); // 在销毁活动时停止计时器
    }

    public void stopDown() {
        // 向下移动方块,处理消行逻辑
        int i = 3; // 从第三行开始
        while (true) {
            if (i < 0) {
                break; // 如果行数小于0,退出循环
            }
            int i2 = this.position[0] + i; // 计算方块的当前位置
            if (i2 >= 0 && StateFang.shape[this.rand][i] != 0) { // 如果当前行有效且方块形状不为空
                int[] iArr = this.allBlock; // 获取所有方块的数组
                iArr[i2] = iArr[i2] + leftMath(StateFang.shape[this.rand][i], this.position[1]); // 更新当前行的状态
                for (int i3 = 0; i3 < this.xSize; i3++) {
                    // 更新当前行的颜色
                    if (((1 << i3) & leftMath(StateFang.shape[this.rand][i], this.position[1])) != 0) {
                        this.blockColor[i2][i3] = this.randColor; // 设置颜色
                    }
                }
            }
            i--; // 行数递减
        }

        int i4 = this.ySize - 1; // 从最后一行开始检查
        while (i4 >= 0) {
            if (this.allBlock[i4] == 1023) { // 如果该行满了
                this.score++; // 增加分数
                this.scoreTextView.setText("分数:" + this.score); // 更新分数显示
                for (int i5 = i4 - 1; i5 >= 0; i5--) {
                    // 向上移动方块
                    int[] iArr2 = this.allBlock;
                    int i6 = i5 + 1;
                    iArr2[i6] = iArr2[i5]; // 移动行
                    for (int i7 = 0; i7 < this.xSize; i7++) {
                        // 更新颜色
                        int[][] iArr3 = this.blockColor;
                        iArr3[i6][i7] = iArr3[i5][i7];
                    }
                }
                this.allBlock[0] = 0; // 清空顶部行
                for (int i8 = 0; i8 < this.xSize; i8++) {
                    this.blockColor[0][i8] = 0; // 清空颜色
                }
            } else {
                i4--; // 行数递减
            }
        }

        if (this.allBlock[0] != 0) { // 如果顶部行不为空
            if (this.score > this.highestScore) { // 如果当前分数高于最高分
                this.cacheUtils.getValue("highestScore" + this.grade, this.score + ""); // 更新最高分
                this.highestScore = this.score; // 更新最高分变量
                this.maxScoreTextView.setText("最高分:" + this.highestScore); // 更新显示
                this.scoreTextView.setText("分数:" + this.score); // 更新分数显示
            }
            gameOver(); // 结束游戏
        }

        // 更新随机方块状态
        this.rand = this.nextRand;
        this.position[0] = StateFang.initPosition[this.rand][1]; // 设置新方块的位置
        this.position[1] = StateFang.initPosition[this.rand][0];
        this.randColor = this.nextRandColor; // 设置新方块的颜色
        this.nextRand = this.random.nextInt(19); // 生成下一个随机方块
        this.nextRandColor = this.random.nextInt(5) + 1; // 生成下一个随机颜色
        nextTetrisShow(); // 更新下一个方块的显示
        Log.i(this.TAG, this.rand + ""); // 日志记录当前方块
    }

四、完整的项目源码 

👇👇👇👇👇快捷获取方式👇👇👇👇👇

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程乐学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值