android 扫雷小游戏
先看效果图:
初始游戏界面:
翻开块和标记块界面:
游戏结束界面:
菜单界面:
更换难度界面:
查看游戏记录界面:
代码分析
Block.java
这部分代码实现的是游戏界面的板块
设置四个变量来记录当前块是否被翻开,当前块是否是地雷,是否把当前快标记为地雷(也就是插旗子),当前块周围的地雷数量。
关键部分代码:
//设置翻开状态
public void setNumberOfSurroundingMines(int number) {
this.setBackgroundResource(R.drawable.selected);//设置翻开背景图
updateNumber(number);//设置周围雷数
}
//添加雷块标识
public void setMineIcon() {
this.setBackgroundResource(R.drawable.dl);
}
//添加标记标识
public void setFlagIcon(boolean enabled) {
if (!enabled) {
this.setBackgroundResource(R.drawable.hq);
} else {
this.setTextColor(Color.BLACK);
}
}
//清除所有标记
public void clearAllIcons() {
this.setText("");
this.setTextColor(R.drawable.unselected);
}
private void setBoldFont() {
this.setTypeface(null, Typeface.BOLD);
}
//翻开方块
public void OpenBlock() {
if (!isCovered) {
return;
}
isCovered = false;
//如果为雷设置地雷标识
if (hasMine()) {
setMineIcon();
} else {
setNumberOfSurroundingMines(numberOfMinesInSurrounding);//根据周围雷数设置翻开状态及显示数字
}
}
LevelActivity
这部分把玩家选择的难度easy或hard传递给MenuActivity
package com.example.saolei;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class LevelActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level);
}
/*根据不同难度返回相应参数*/
public void Easy(View view) {
Intent data = new Intent();
data.putExtra("result", "easy");
setResult(2, data);
finish();
}
public void Hard(View view) {
Intent data = new Intent();
data.putExtra("result", "hard");
setResult(2, data);
finish();
}
public void Return(View view) {
Intent data = new Intent();
data.putExtra("result", "");
setResult(2, data);
finish();
}
}
MenuActivity
这部分是菜单页面,包括难度选择和游戏记录的查看
这里接收难度选择界面传回的参数并将其传回主界面处理
package com.example.saolei;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class MenuActivity extends AppCompatActivity {
String t = "easy";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
}
@Override
//接收难度界面传回的参数并将其传回主界面处理
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == 2) {
Intent level = new Intent();
t = data.getStringExtra("result");
level.putExtra("result", t);
setResult(1, data);
finish();
}
}
//点击新游戏按钮,将结果传回主界面
public void NewGame(View view) {
Intent data = new Intent();
data.putExtra("result", "newgame");
setResult(1, data);
finish();
}
//点击改变难度按钮,启动难度选择界面
public void ChangeLevel(View view) {
Intent level = new Intent(MenuActivity.this, LevelActivity.class);
startActivityForResult(level, 2);
}
//点击游戏记录按钮,显示记录
public void Record(View view) {
String filename = getExternalCacheDir().getAbsolutePath() + "/gamerecord.txt";//文件路径
File file = new File(filename);
Scanner inputStream = null;
FileInputStream fis = null;
BufferedReader br = null;
String str;
String message = " " + "级别" + " " + "胜负" + " " + "时间\n";
//若文件不存在,显示暂无记录
if (!file.exists()) {
new AlertDialog.Builder(this)
.setMessage("暂无记录!")
.setNegativeButton("确定", null)
.create().show();
}
else {
try {
//读取文件记录并生成对话框显示
inputStream = new Scanner(new FileInputStream(filename));
int i = 1;
fis = new FileInputStream(filename);
br = new BufferedReader(new InputStreamReader(fis));
while ((str = br.readLine()) != null) {
message = message + (i + ". " + str + "\n");
i++;
}
new AlertDialog.Builder(this)
.setMessage(message)
.setNegativeButton("确定", null)
.create().show();
fis.close();
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
inputStream.close();
}
}
}
//点击返回按钮,回传结果到主界面
public void Return1(View view) {
Intent data = new Intent();
data.putExtra("result", "start");
setResult(1, data);
finish();
}
}
MainActivity
这部分是主体内容,包括游戏初始化,雷区的创建,游戏胜负的判定,游戏结果的存档等
这部分比较长,就不贴代码了
最后附上完整项目链接
https://github.com/guyuanjunxi/saolei