先建一个mXn矩阵,然后随机埋k个地雷,接着没有雷的地方数周边地雷个数,形成地雷矩阵。
最后贴图,设置胜利条件,失败触发事件。
主要用途:贴图
Texture texture;
texture = new Texture(Gdx.files.internal("android.jpg"));
batch.begin();
batch.draw(texture, Gdx.graphics.getWidth() / 4, 0, Gdx.graphics.getWidth() / 2, Gdx.graphics.getWidth() / 2);
batch.end();
文字标签,通常加在Stage(舞台)上显示。
使用时需要皮肤Skin,可以自己写skin,也可以下载(3个文件)
Label label;
Stage stage;
label=showArr(arr,a,b);
stage=new Stage();
stage.addActor(label);
stage.act();
stage.draw();
本文创建label写了个函数,显示地雷矩阵
public void showWon(){
Skin skin=new Skin();
BitmapFont font=new BitmapFont();
font.scale(5);
skin.add("default",new Label.LabelStyle(font,Color.BLACK));
Label label=new Label("you won!",skin);
label.setPosition(500,800);
stage.addActor(label);
}
舞台演员,需要重写,之后加入舞台。
MineActor重写了演员类,实现点击后改变图片并且计数,计数到一定数值后提示你赢了,点到地雷后会退出游戏。
public class MineActor extends Actor
{
@Override
public void act(float delta)
{
// TODO: Implement this method
super.act(delta);
if(down) clearListeners();
}
@Override
public void draw(Batch batch, float parentAlpha)
{
// TODO: Implement this method
super.draw(batch, parentAlpha);
batch.draw(region,getX(),getY(),getOriginX(),getOriginY(),getWidth(),getHeight(),getScaleX(),getScaleY(),getRotation());
}
}
可以截取Texture一部分
TextureRegion region;
region=new TextureRegion(new Texture(Gdx.files.internal("null.png")));
加监听
主函数一定要加:
Gdx.input.setInputProcessor(stage);
演员里可加:
addListener(new InputListener(){
@Override
public boolean touchDown(InputEvent event,float x,float y,int pointer,int button){
MyGdxGame.SSS--;
region.setTexture(texture);
if(rrr) Gdx.app.exit();
down=true;
return true;
}});
地雷矩阵的埋雷函数:
public int[][] setMine(int a,int b,int n){
int[][] arr=new int[a][b];
int i,j,c=0;
Random r=new Random();
for(i=0;i
地雷矩阵设置数字:
public int[][] setMatrix(int[][] arr,int a,int b){
int i,j;
for(i=1;i
设置演员位置和底图数字:
public MineActor[][] setActor(int[][] arr,int a,int b){
MineActor[][] actor=new MineActor[a][b];
int i,j;
for(i=0;i
设一个全局变量SSS记录点击方格数:
public static int SSS;
MyGdxGame.SSS–;
MyGdxGame文件:
package com.mycompany.mygame;
import com.badlogic.gdx.*;
import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.graphics.g2d.*;
import java.util.*;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.scenes.scene2d.*;
public class MyGdxGame implements ApplicationListener
{
Texture texture;
SpriteBatch batch;
Label label;
Stage stage;
int[][] arr;
MineActor[][] mine;
int a=5,b=8,n=10;
public static int SSS;
@Override
public void create()
{
texture = new Texture(Gdx.files.internal("android.jpg"));
batch = new SpriteBatch();
SSS=a*b-n;
arr=setMine(a,b,n);
arr=setMatrix(arr,a,b);
label=showArr(arr,a,b);
mine=setActor(arr,a,b);
stage=new Stage();
stage.addActor(label);
int i,j;
for(i=0;i<a;i++){
for(j=0;j<b;j++){
stage.addActor(mine[i][j]);
}
}
Gdx.input.setInputProcessor(stage);
}
@Override
public void render()
{
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(texture, Gdx.graphics.getWidth() / 4, 0,
Gdx.graphics.getWidth() / 2, Gdx.graphics.getWidth() / 2);
batch.end();
stage.act();
stage.draw();
if(SSS==0) {showWon();}
}
@Override
public void dispose()
{
}
@Override
public void resize(int width, int height)
{
}
@Override
public void pause()
{
}
@Override
public void resume()
{
}
public int[][] setMine(int a,int b,int n){
int[][] arr=new int[a][b];
int i,j,c=0;
Random r=new Random();
for(i=0;i<a;i++){
for(j=0;j<b;j++){
arr[i][j]=-1;
}
}
while(true){
for(i=0;i<a;i++){
for(j=0;j<b;j++){
if(arr[i][j]==-1){
if(r.nextInt(a*b)<n){
arr[i][j]=-2;
c++;
}
}
if(c==n){return arr;}
}
}
}
}
public int[][] setMatrix(int[][] arr,int a,int b){
int i,j;
for(i=1;i<a-1;i++){
for(j=1;j<b-1;j++){
if(arr[i][j]==-1){
int num=0;
if(arr[i-1][j]==-2) num++;
if(arr[i+1][j]==-2) num++;
if(arr[i][j-1]==-2) num++;
if(arr[i][j+1]==-2) num++;
if(arr[i-1][j-1]==-2) num++;
if(arr[i-1][j+1]==-2) num++;
if(arr[i+1][j-1]==-2) num++;
if(arr[i+1][j+1]==-2) num++;
arr[i][j]=num;
}
}
}
for(i=1;i<a-1;i++){
if(arr[i][0]==-1){
int num=0;
if(arr[i-1][0]==-2) num++;
if(arr[i+1][0]==-2) num++;
if(arr[i-1][1]==-2) num++;
if(arr[i][1]==-2) num++;
if(arr[i+1][1]==-2) num++;
arr[i][0]=num;
}
if(arr[i][b-1]==-1){
int num=0;
if(arr[i-1][b-1]==-2) num++;
if(arr[i+1][b-1]==-2) num++;
if(arr[i-1][b-2]==-2) num++;
if(arr[i][b-2]==-2) num++;
if(arr[i+1][b-2]==-2) num++;
arr[i][b-1]=num;
}
}
for(j=1;j<b-1;j++){
if(arr[0][j]==-1){
int num=0;
if(arr[0][j-1]==-2) num++;
if(arr[0][j+1]==-2) num++;
if(arr[1][j-1]==-2) num++;
if(arr[1][j]==-2) num++;
if(arr[1][j+1]==-2) num++;
arr[0][j]=num;
}
if(arr[a-1][j]==-1){
int num=0;
if(arr[a-1][j-1]==-2) num++;
if(arr[a-1][j+1]==-2) num++;
if(arr[a-2][j-1]==-2) num++;
if(arr[a-2][j]==-2) num++;
if(arr[a-2][j+1]==-2) num++;
arr[a-1][j]=num;
}
}
if(arr[0][0]==-1){
int num=0;
if(arr[0][1]==-2) num++;
if(arr[1][1]==-2) num++;
if(arr[1][0]==-2) num++;
arr[0][0]=num;
}
if(arr[a-1][0]==-1){
int num=0;
if(arr[a-1][1]==-2) num++;
if(arr[a-2][1]==-2) num++;
if(arr[a-2][0]==-2) num++;
arr[a-1][0]=num;
}
if(arr[0][b-1]==-1){
int num=0;
if(arr[0][b-2]==-2) num++;
if(arr[1][b-1]==-2) num++;
if(arr[1][b-2]==-2) num++;
arr[0][b-1]=num;
}
if(arr[a-1][b-1]==-1){
int num=0;
if(arr[a-2][b-1]==-2) num++;
if(arr[a-2][b-2]==-2) num++;
if(arr[a-1][b-2]==-2) num++;
arr[a-1][b-1]=num;
}
return arr;
}
public MineActor[][] setActor(int[][] arr,int a,int b){
MineActor[][] actor=new MineActor[a][b];
int i,j;
for(i=0;i<a;i++){
for(j=0;j<b;j++){
actor[i][j]=new MineActor(arr[i][j],i*200,j*200,200);
}
}
return actor;
}
public Label showArr(int[][] arr,int a,int b){
Skin skin=new Skin();
BitmapFont font=new BitmapFont();
String aaa="";int i,j;
font.scale(2);
skin.add("default",new Label.LabelStyle(font,Color.BLACK));
Label label=new Label(aaa,skin);
label.setPosition(20,Gdx.graphics.getHeight()-200);
for(i=0;i<a;i++){
for(j=0;j<b;j++){
aaa=aaa+arr[i][j]+", ";
}
aaa=aaa+"\n";
}
label.setText(aaa);
return label;
}
public void showWon(){
Skin skin=new Skin();
BitmapFont font=new BitmapFont();
font.scale(5);
skin.add("default",new Label.LabelStyle(font,Color.BLACK));
Label label=new Label("you won!",skin);
label.setPosition(500,800);
stage.addActor(label);
}
}
MineActor文件:
package com.mycompany.mygame;
import com.badlogic.gdx.scenes.scene2d.*;
import com.badlogic.gdx.graphics.g2d.*;
import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.*;
public class MineActor extends Actor
{
TextureRegion region;
Texture texture;
int num;boolean rrr=false,down=false;
public MineActor(int num,int x,int y,int size){
this.num=num;
if(num==-2) rrr=true;
region=new TextureRegion(new Texture(Gdx.files.internal("null.png")));
texture=new Texture(Gdx.files.internal(num+".png"));
setBounds(x,y,size,size);
addListener(new InputListener(){
@Override
public boolean touchDown(InputEvent event,float x,float y,int pointer,int button){
MyGdxGame.SSS--;
region.setTexture(texture);
if(rrr) Gdx.app.exit();
down=true;
return true;
}});
}
@Override
public void act(float delta)
{
// TODO: Implement this method
super.act(delta);
if(down) clearListeners();
}
@Override
public void draw(Batch batch, float parentAlpha)
{
// TODO: Implement this method
super.draw(batch, parentAlpha);
batch.draw(region,getX(),getY(),getOriginX(),getOriginY(),getWidth(),getHeight(),getScaleX(),getScaleY(),getRotation());
}
}