當前位置:編程學習大全網 - 編程語言 - 如何用java寫壹個小遊戲?

如何用java寫壹個小遊戲?

包mybase.programe

/*

*連連看整體算法思路:由兩個按鈕決定。如果這兩個按鈕的數量相等,開始尋找它們連接的路徑。找到穿過這裏的路。

*有三種情況:(從下面三種情況可以知道需要三次測試,這三次測試分別檢測壹條直路。所以會有

*三條路。如果三條路都是空按鈕,那麽連接兩個按鈕的只是三種直線(兩個轉折點)

* 1.鄰近的

*

* 2.如果它們不相鄰,首先在第壹個按鈕的對等體中找到壹個空按鈕。1).找到後,再橫向看第二個按鈕到這個空按鈕。

*列中是否有按鈕。2).如果沒有,看第壹個按鈕和與之同行的空按鈕之間是否有按鈕。3).如果不是,那麽從

*與第壹個按鈕同行的空按鈕與第二個按鈕同行垂直,看是否有按鈕。如果沒有,路就通了,可以淘汰。

*

* 3.如果2失敗,在與第壹個按鈕相同的列中找到壹個空按鈕。1).找到後,看第二個按鈕是否垂直於這個空按鈕所在的線。

* 2).如果沒有,看同壹列第壹個按鈕和空按鈕之間是否有按鈕。3).如果不是,則與第壹個按鈕在同壹列。

*空按鈕與第二個按鈕水平同列,看是否有按鈕。如果沒有,路就通了,可以淘汰。

*

*如果上述三個步驟都失敗,這兩個按鈕就無法擦除。

*/

導入javax . swing . *;

導入Java . awt . *;

導入Java . awt . event . *;

公共類連連看實現ActionListener {

JFrame主機;//主面板

容器thisContainer

JPanel centerPanel,southPanel,northPanel//子面板

JButton diamonds button[][]= new JButton[6][5];//遊戲按鈕數組

JButton exitButton,resetButton,newlyButton//退出、重新排序和重新啟動按鈕

JLabel fractional able = new JLabel(" 0 ");//分數標簽

JButton firstButton,secondButton//分別記錄兩次選中的按鈕。

//存儲遊戲按鈕的位置(其實這裏只有6行5列。但是我們使用了8行7列。等於圍繞這個面板按鈕。

//壹層按鈕為0,這樣可以消除靠近面板邊緣的兩個按鈕)

int grid[][]= new int[8][7];

靜態布爾表達式信息=假;//判斷按鈕是否被選中。

int x0 = 0,y0 = 0,x = 0,y = 0,fristMsg = 0,secondMsg = 0,validateLV//遊戲按鈕的位置坐標

int i,j,k,n;//消除方法控制

public void init() {

MainFrame = new JFrame(“JKJ連連看”);

this container = mainframe . getcontentpane();

this container . set layout(new BorderLayout());

center panel = new JPanel();

south panel = new JPanel();

north panel = new JPanel();

thisContainer.add(centerPanel," Center ");

thisContainer.add(southPanel," South ");

thisContainer.add(northPanel," North ");

center panel . set layout(new GridLayout(6,5));

for(int cols = 0;cols & lt6;cols++) {

for(int rows = 0;行數& lt5;rows++) {

diamondsButton[cols][rows] =新的JButton(String

。value of(grid[cols+1][rows+1]);

菱形按鈕[列][行]。addActionListener(this);

center panel . add(diamonds button[cols][rows]);

}

}

exit button = new JButton(" exit ");

exit button . addactionlistener(this);

reset button = new JButton(" regroup ");

reset button . addactionlistener(this);

NewlyButton = new JButton("再來壹局");

newly button . addactionlistener(this);

south panel . add(exit button);

south panel . add(reset button);

south panel . add(newly button);

fraction lable . settext(string . value of(integer . parse int(fraction lable

。getText()))));

north panel . add(fractional able);

mainFrame.setBounds(280,100,500,450);

mainframe . set visible(true);

mainframe . setdefaultcloseoperation(JFrame。EXIT _ ON _ CLOSE);

}

public void randomBuild() {

int randoms,cols,rows

for(int twins = 1;雙胞胎& lt= 15;Twins++) {// a * * 15雙紐扣,30。

randoms =(int)(math . random()* 25+1);//按鈕上的數字

for(int like = 1;相似& lt= 2;like++) {

cols =(int)(math . random()* 6+1);

rows =(int)(math . random()* 5+1);

while (grid[cols][rows]!= 0) {//等於0表示這個空間有按鈕。

cols =(int)(math . random()* 6+1);

rows =(int)(math . random()* 5+1);

}

this . grid[cols][rows]= randoms;

}

}

}

公共空隙率(){

fraction lable . settext(string . value of(integer . parse int(fraction lable

。getText())+100));

}

公共void reload() {

int save[]= new int[30];

int n = 0,cols,rows

int grid[][]= new int[8][7];

for(int I = 0;我& lt= 6;i++) {

for(int j = 0;j & lt= 5;j++) {

if (this.grid[i][j]!= 0) {

save[n]= this . grid[I][j];//記下每個按鈕的編號。

n++;//有幾個按鈕沒有消除。

}

}

}

n = n-1;

this.grid = grid

while(n & gt;= 0) {//再放壹次沒有消除的按鈕。

cols =(int)(math . random()* 6+1);

rows =(int)(math . random()* 5+1);

while (grid[cols][rows]!= 0) {

cols =(int)(math . random()* 6+1);

rows =(int)(math . random()* 5+1);

}

this . grid[cols][rows]= save[n];

n-;

}

mainframe . set visible(false);

pressInformation = false//此處按鈕點擊信息必須歸類為初始。

init();

for(int I = 0;我& lt6;i++) {

for(int j = 0;j & lt5;j++) {

if(grid[I+1][j+1]= = 0)

菱形按鈕[i][j]。set visible(false);

}

}

}

public void estimate even(int placeX,int placeY,JButton bz) {

if (pressInformation == false) {

x = placeX

y = placeY

second msg = grid[x][y];

secondButton = bz

pressInformation = true

}否則{

x0 = x;

y0 = y;

fristMsg = secondMsg

firstButton = secondButton

x = placeX

y = placeY

second msg = grid[x][y];

secondButton = bz

if(frist msg = = second msg & amp;& amp秒按鈕!= firstButton) {

肖();

}

}

}

Public void xiao() {//相同情況下可以消除嗎?仔細分析,不是筆記。

if((x0 = = x & amp;& amp(y0 == y + 1 || y0 == y - 1))

| |((x0 = = x+1 | | x0 = = x-1)& amp;& amp(y0 = = y)){///判斷它們是否相鄰。

移除();

}否則{

for(j = 0;j & lt7;j++) {

If (grid[x0][j] == 0) {//確定哪個按鈕與第壹個按鈕壹起為空。

//如果發現空按鈕,根據列值的三種情況,比較第二個按鈕的位置和空按鈕的位置。

如果(y & gtJ) {//第二個按鈕在空按鈕的右邊。

for(I = y-1;我& gt= j;I-){//檢測從第二個按鈕的水平左側到空格所在的列是否存在所有空格。

if (grid[x][i]!= 0) {

k = 0;

打破;//如果有非空格,退出。這個退出在k==2的情況下是不可能的,所以會到底部215行會顯示相同的判斷柱。

}否則{

k = 1;

} // K=1表示所有空格都通過了第壹次驗證,即從第二個按鈕到空格所在的列,所有空格都被留下。

}

if (k == 1) {

line passone();//進入第二次驗證,即判斷第壹個按鈕與其對等按鈕之間的空格。

}

}

if(y & lt;J) {//第二個按鈕在空按鈕的左邊。

for(I = y+1;我& lt= j;I++) {//檢測從第二個按鈕的水平右側到空格所在的列是否存在所有空格。

if (grid[x][i]!= 0) {

k = 0;

打破;

}否則{

k = 1;

}

}

if (k == 1) {

line passone();

}

}

If (y == j) {//第二個按鈕與空按鈕在同壹列。

line passone();

}

}

//第三次檢查第J列確定為空的按鈕是否與第二個按鈕垂直,看是否有按鈕。

if (k == 2) {

If (x0 == x) {//第壹個和第二個按鈕在同壹行。

移除();

}

如果(x0 & ltX) {//第壹個按鈕在第二個按鈕下面。

for(n = x0;n & lt= x-1;N++) {//從空按鈕垂直到第二個按鈕所在行是否有按鈕。

if (grid[n][j]!= 0) {

k = 0;

打破;

}

//沒有按鈕,表示這條路會通。

if(grid[n][j]= = 0 & amp;& ampn == x - 1) {

移除();

}

}

}

如果(x0 & gtX) {//第壹個按鈕在第二個按鈕上面。

for(n = x0;n & gt= x+1;n - ) {

if (grid[n][j]!= 0) {

k = 0;

打破;

}

if(grid[n][j]= = 0 & amp;& ampn == x + 1) {

移除();

}

}

}

}

}// - for

//當上面未能檢測到空白按鈕與第壹個按鈕同行(找不到與第二個按鈕的連接路徑)時,執行下面的執行。

//檢測與第壹個按鈕在同壹列的空白按鈕。

for(I = 0;我& lt8;i++) {

If (grid[i][y0] == 0) {//確定與第壹個按鈕在同壹列的哪個按鈕是空的。

if(x & gt;I) {//第二個按鈕在這個空按鈕下面。

for(j = x-1;j & gt= I;j - ) {

if (grid[j][y]!= 0) {

k = 0;

打破;

}否則{

k = 1;

}

}

if (k == 1) {

rowPassOne();

}

}

if(x & lt;I) {//第二個按鈕在這個空按鈕的上面。

for(j = x+1;j & lt= I;j++) {

if (grid[j][y]!= 0) {

k = 0;

打破;

}否則{

k = 1;

}

}

if (k == 1) {

rowPassOne();

}

}

If (x == i) {//第二個按鈕配這個空按鈕。

rowPassOne();

}

}

if (k == 2) {

If (y0 == y) {//第二個按鈕與第壹個按鈕在同壹列。

移除();

}

如果(y0 & ltY) {//第二個按鈕在第壹個按鈕的右邊。

for(n = y0;n & lt= y-1;n++) {

if (grid[i][n]!= 0) {

k = 0;

打破;

}

if(grid[I][n]= = 0 & amp;& ampn == y - 1) {

移除();

}

}

}

如果(y0 & gtY) {//第二個按鈕在第壹個按鈕的左邊。

for(n = y0;n & gt= y+1;n - ) {

if (grid[i][n]!= 0) {

k = 0;

打破;

}

if(grid[I][n]= = 0 & amp;& ampn == y + 1) {

移除();

}

}

}

}

}// - for

}// - else

}// -肖

公共void linePassOne() {

如果(y0 & gtJ) {//第壹個按鈕在同壹行,空按鈕在左邊。

for(I = y0-1;我& gt= j;I-){//判斷第壹個按鈕和左側空按鈕之間是否有按鈕。

if(網格[x0][i]!= 0) {

k = 0;

打破;

}否則{

k = 2;

} // K=2表示第二次驗證已經通過。

}

}

如果(y0 & ltJ) {//第壹個按鈕在同壹行,空按鈕在右邊。

for(I = y0+1;我& lt= j;i++) {

if(網格[x0][i]!= 0) {

k = 0;

打破;

}否則{

k = 2;

}

}

}

}

public void rowPassOne() {

如果(x0 & gtI) {//第壹個按鈕在與其同列的空白按鈕下面。

for(j = x0-1;j & gt= I;j - ) {

if (grid[j][y0]!= 0) {

k = 0;

打破;

}否則{

k = 2;

}

}

}

如果(x0 & ltI) {//第壹個按鈕在同壹列的空白按鈕之上。

for(j = x0+1;j & lt= I;j++) {

if (grid[j][y0]!= 0) {

k = 0;

打破;

}否則{

k = 2;

}

}

}

}

公共void remove() {

first button . set visible(false);

second button . set visible(false);

分數();

pressInformation = false

k = 0;

網格[x0][y0]= 0;

grid[x][y]= 0;

}

public void action performed(action event e){

if(e . getsource()= = newly button){

int grid[][]= new int[8][7];

this.grid = grid

random build();

mainframe . set visible(false);

pressInformation = false

init();

}

if (e.getSource() == exitButton)

system . exit(0);

if (e.getSource() == resetButton)

重載();

for(int cols = 0;cols & lt6;cols++) {

for(int rows = 0;行數& lt5;rows++) {

if(e . getsource()= = diamonds button[cols][rows])

estimateEven(列+ 1,行+ 1,diamonds button[列][行]);

}

}

}

公共靜態void main(String[] args) {

連連看llk =新連連看();

llk . randombuild();

llk . init();

}

}

  • 上一篇:鹽城師範學院代碼
  • 下一篇:陶行知的學前教育思想論文(2)
  • copyright 2024編程學習大全網