模拟Linux系统自带的扫雷游戏,代码无素材,可直接运行
BUG:
没写自定义窗口界面(扩展很容易),暂停按钮也没写功能,也没有时钟
运行效果:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
class GamePane extends GridPane{
int graph[][];
int size[]=new int[2];
int num;
Rectangle bg[][],cover[][];
int route[][]={{0,1},{1,0},{0,-1},{-1,0},{1,-1},{-1,1},{-1,-1},{1,1}};
public GamePane(int x,int y,int num,int PaneHeight){
bg=new Rectangle[x][y];
this.num=num;
cover=new Rectangle[x][y];
graph=new int[x][y];
setPadding(new Insets(10,10,10,10));
size[0]=x;size[1]=y;
setVgap(2);
setHgap(2);
setLayoutX(5);
setLayoutY(5);
while(num!=0){
int a=(int)(Math.random()*x),b=(int)(Math.random()*y);
if(graph[a][b]==0){
graph[a][b]=-1;
num--;
}
}
for(int i=0;i<x;i++){
for(int j=0;j<y;j++){
int count=0;
for(int k=0;k<8;k++){
if((i+route[k][0]<x)&&(i+route[k][0]>=0)&&(j+route[k][1]<y)&&(j+route[k][1]>=0)&&graph[i+route[k][0]][j+route[k][1]]==-1)
count++;
}
if(graph[i][j]!=-1)
graph[i][j]=count;
}
}
for(int i=0;i<x;i++){
for(int j=0;j<y;j++){
bg[i][j]=new Rectangle(PaneHeight/y,PaneHeight/y);
bg[i][j].setArcHeight(15);
bg[i][j].setArcWidth(15);
bg[i][j].setFill(Color.rgb(230,230,230));
add(bg[i][j],i,j);
if(graph[i][j]>0){
Label tmp=new Label(String.valueOf(graph[i][j]));
tmp.setMinSize(PaneHeight/y,PaneHeight/y);
tmp.setFont(Font.font("Mangal",FontWeight.BOLD,PaneHeight/y/2));
tmp.setAlignment(Pos.CENTER);
add(tmp,i,j);
}
else if(graph[i][j]==-1){
Pane tmp=new Pane(new Circle(PaneHeight/y/2,PaneHeight/y/2,PaneHeight/y/2.5));
add(tmp,i,j);
}
cover[i][j]=new Rectangle(PaneHeight/y,PaneHeight/y);
cover[i][j].setFill(Color.rgb(200,200,200));
cover[i][j].setArcHeight(15);
cover[i][j].setArcWidth(15);
int a=i,b=j;
cover[i][j].setOnMouseEntered(e-> cover[a][b].setFill(Color.rgb(150,180,200)));
cover[i][j].setOnMouseExited(e-> cover[a][b].setFill(Color.rgb(200,200,200)));
cover[i][j].setOnMouseClicked(e->{
getChildren().remove(cover[a][b]);
if(graph[a][b]==-1){
Line left=new Line(0,0,PaneHeight/y-2,PaneHeight/y-2),right=new Line(PaneHeight/y-2,0,0,PaneHeight/y-2);
left.setStrokeWidth(2);
left.setStroke(Color.RED);
right.setStrokeWidth(2);
right.setStroke(Color.RED);
Pane tmp=new Pane(left,right);
add(tmp,a,b);
overClear();
}
else if(graph[a][b]==0)
Remove(a, b);
});
add(cover[i][j],i,j);
}
}
}
public void overClear(){
for(int i=0;i<size[0];i++)
for(int j=0;j<size[1];j++)
if(getChildren().contains(cover[i][j]))
getChildren().remove(cover[i][j]);
}
public void Remove(int x,int y){
for(int i=0;i<4;i++){
if(x+route[i][0]>=0&&x+route[i][0]<size[0]&&y+route[i][1]>=0&&y+route[i][1]<size[1]&&graph[x + route[i][0]][y + route[i][1]]!=-1&&getChildren().contains(cover[x + route[i][0]][y + route[i][1]])) {
getChildren().remove(cover[x + route[i][0]][y + route[i][1]]);
if(graph[x + route[i][0]][y + route[i][1]]==0)
Remove(x + route[i][0], y + route[i][1]);
}
}
}
}
class ChooseBt extends Button{
public ChooseBt(String str){
setText(str);
setMinWidth(170);
setMinHeight(170);
setFont(Font.font("System", FontWeight.BOLD,20));
Rectangle rectangle=new Rectangle(170,170);
rectangle.setArcWidth(20);
rectangle.setArcHeight(20);
setShape(rectangle);
}
}
class MenuBt extends Button{
public MenuBt(String text){
setText(text);
setMaxWidth(150);
setMaxHeight(10);
Rectangle rectangle=new Rectangle(150,10);
rectangle.setArcWidth(20);
rectangle.setArcHeight(20);
setShape(rectangle);
setFont(Font.font("DFKai-SB",20));
setPadding(new Insets(20,20,20,20));
}
}
public class Mine extends Application{
GridPane ChoosePane=new GridPane();
GamePane gamePane=new GamePane(8,8,10,400);
GridPane pane=new GridPane();
MenuBt btReplay=new MenuBt("重开一局"),btChange=new MenuBt("改变难度"),btPause=new MenuBt("暂停");
GridPane menu=new GridPane();
Scene Choose =new Scene(ChoosePane,400,400),Game=new Scene(pane);
ChooseBt eight=new ChooseBt(" 8*8\n10个雷"),sixteen=new ChooseBt("16*16\n40个雷"),thirty=new ChooseBt("30*15\n99个雷"),custom=new ChooseBt("自定义");
public void start(Stage root) {
pane.setHgap(20);pane.setVgap(20);
pane.setAlignment(Pos.CENTER_LEFT);
pane.add(gamePane,0,0);
pane.add(menu,1,0);
menu.setVgap(20);
menu.setHgap(20);
menu.setAlignment(Pos.CENTER);
menu.setPadding(new Insets(10,10,10,10));
menu.add(btReplay,0,8);
menu.add(btChange,0,9);
menu.add(btPause,0,10);
btChange.setOnMouseClicked(e-> root.setScene(Choose));
btReplay.setOnMouseClicked(e->{
pane.getChildren().remove(gamePane);
gamePane=new GamePane(gamePane.size[0],gamePane.size[1],gamePane.num,400);
pane.add(gamePane,0,0);
});
eight.setOnMouseClicked(e->{pane.getChildren().remove(gamePane);gamePane=new GamePane(8,8,10,400); pane.add(gamePane,0,0);root.setScene(Game);});
sixteen.setOnMouseClicked(e->{pane.getChildren().remove(gamePane);gamePane=new GamePane(16,16,40,400); pane.add(gamePane,0,0);root.setScene(Game);});
thirty.setOnMouseClicked(e->{pane.getChildren().remove(gamePane);gamePane=new GamePane(30,15,99,400); pane.add(gamePane,0,0);root.setScene(Game);});
ChoosePane.setVgap(20);
ChoosePane.setHgap(20);
ChoosePane.setAlignment(Pos.CENTER);
ChoosePane.add(eight,0,0);
ChoosePane.add(sixteen,1,0);
ChoosePane.add(thirty,0,1);
ChoosePane.add(custom,1,1);
root.setScene(Game);
root.setMinWidth(400);
root.setResizable(false);
root.setTitle("扫雷-Italink");
root.show();
}
}