Ⅰ 用java eclipse能設計出怎樣簡易的小游戲
用java的swing就可以設計出像:拼圖游戲、推箱子、五子棋、象棋、坦克大戰、超級瑪麗、飛機大戰等游戲。
Ⅱ java猜數字小游戲。用eclipse寫的
importjava.util.Scanner;
/**
*Java命令行版猜數字游戲
*@authorkaifang
*/
publicclassGuessNum{
publicstaticvoidmain(String[]args)
{
System.out.println("======猜數字游戲====== ");
intanswer=(int)(Math.random()*200+1);
Scannersr=newScanner(System.in);
while(true){
System.out.print("請輸入你猜的數字(1-200):");
intin=sr.nextInt();
if(in>answer){
System.out.println("猜大了! ");
}elseif(in<answer){
System.out.println("猜小了! ");
}else{
System.out.println("恭喜你,才猜對了!!! ");
break;
}
}
sr.close();
}
}
Ⅲ 高分求一個運行在Eclipse環境下的java 掃雷游戲的初級代碼 越小越好 越短越好 運行就好,就是初級就好了,
import java.awt.Button;
import java.util.Set;
// 每一個小方塊類
public class Diamond extends Button {
private Diamond[] diamonds;
// 該小方塊周圍的八個方向上的小方塊
private Diamond east;
private Diamond north;
private Diamond northEast;
private Diamond northWest;
private Diamond south;
private Diamond southEast;
private Diamond southWest;
private Diamond west;
private boolean isBomb;// 是否是雷
private boolean isChange;// 又沒有被翻過
private int no;// 產生的方塊的編號
// 持有所有小方塊的引用,方便進行操作
public Diamond(Diamond[] diamonds) {
this.diamonds = diamonds;
}
// 按鍵時方塊發生改變
public boolean change() {
this.isChange = true;// 說明已經翻過了
if(isBomb) {// 觸雷
//this.setBackground(Color.red);
return true;
} else {// 不是雷,就顯示周圍雷的數目
//this.setLabel(this.getNearBombNo() + "");
this.setLabel(this.getNearBombNo() + "");
//if(this.getNearBombNo() == 0) {
// this.moveon();
//}
return false;
}
}
// 獲得該小方塊周圍雷的數量
public int getNearBombNo() {
int no = 0;
if(this.northWest != null && this.northWest.isBomb) no++;
if(this.north != null && this.north.isBomb) no++;
if(this.northEast != null && this.northEast.isBomb) no++;
if(this.east != null && this.east.isBomb) no++;
if(this.southEast != null && this.southEast.isBomb) no++;
if(this.south != null && this.south.isBomb) no++;
if(this.southWest != null && this.southWest.isBomb) no++;
if(this.west != null && this.west.isBomb) no++;
return no;
}
// 獲得該小方塊周圍的小方塊
public Diamond getNearDimaond(int i) {
int index = -1;
switch (i) {
case 1:// 1表示西北,2,表示北,以此類推
index = no - 10;
if(index < 1 || no == 19 || no == 28 || no == 37 || no == 46 || no == 55 || no == 64 || no == 73) {
return null;
} else {
return diamonds[index];
}
case 2:
index = no - 9;
if(index < 1) {
return null;
} else {
return diamonds[index];
}
case 3:
index = no - 8;
if(index < 1 || no == 9 || no == 18 || no == 27 || no == 36 || no == 45 || no == 54 || no == 63 || no == 72) {
return null;
} else {
return diamonds[index];
}
case 4:
index = no + 1;
if(no == 9 || no == 18 || no == 27 || no == 36 || no == 45 || no == 54 || no == 63 || no == 72 || no == 81) {
return null;
} else {
return diamonds[index];
}
case 5:
index = no + 10;
if(index >= 81 ||no == 9 || no == 18 || no == 27 || no == 36 || no == 45 || no == 54 || no == 63 || no == 72 || no == 81) {
return null;
} else {
return diamonds[index];
}
case 6:
index = no + 9;
if(index > 81) {
return null;
} else {
return diamonds[index];
}
case 7:
index = no + 8;
if(index >= 81 || no==1 || no == 10 || no == 19 || no == 28 || no == 37 || no == 46 || no == 55 || no == 64 || no == 73) {
return null;
} else {
return diamonds[index];
}
case 8:
index = no - 1;
if(no==1 || no==10 || no == 19 || no == 28 || no == 37 || no == 46 || no == 55 || no == 64 || no == 73) {
return null;
} else {
return diamonds[index];
}
}
return null;
}
// 遞歸,set是用來裝已經翻過的小方塊的,不然會死循環,為什麼用set,因為set是不重復的
public void moveon(Set<Diamond> set) {
set.add(this);// 先把自己加上
if(this.getNorthWest() != null && this.getNorthWest().isBomb == false) {
this.getNorthWest().change();
if(this.getNorthWest().getNearBombNo() == 0) {
if(set.contains(this.getNorthWest()) == false)
this.getNorthWest().moveon(set);
}
set.add(this.getNorthWest());
}
if(this.getNorth() != null && this.getNorth().isBomb == false) {
this.getNorth().change();
if(this.getNorth().getNearBombNo() == 0) {
if(set.contains(this.getNorth()) == false)
this.getNorth().moveon(set);
}
set.add(this.getNorth());
}
if(this.getNorthEast() != null && this.getNorthEast().isBomb == false) {
this.getNorthEast().change();
if(this.getNorthEast().getNearBombNo() == 0) {
if(set.contains(this.getNorthEast()) == false)
this.getNorthEast().moveon(set);
}
set.add(this.getNorthEast());
}
if(this.getEast() != null && this.getEast().isBomb == false) {
this.getEast().change();
if(this.getEast().getNearBombNo() == 0) {
if(set.contains(this.getEast()) == false)
this.getEast().moveon(set);
}
set.add(this.getEast());
}
if(this.getSouthEast() != null && this.getSouthEast().isBomb == false) {
this.getSouthEast().change();
if(this.getSouthEast().getNearBombNo() == 0) {
if(set.contains(this.getSouthEast()) == false)
this.getSouthEast().moveon(set);
}
set.add(this.getSouthEast());
}
if(this.getSouth() != null && this.getSouth().isBomb == false) {
this.getSouth().change();
if(this.getSouth().getNearBombNo() == 0) {
if(set.contains(this.getSouth()) == false)
this.getSouth().moveon(set);
}
set.add(this.getSouth());
}
if(this.getSouthWest() != null && this.getSouthWest().isBomb == false) {
this.getSouthWest().change();
if(this.getSouthWest().getNearBombNo() == 0) {
if(set.contains(this.getSouthWest()) == false)
this.getSouthWest().moveon(set);
}
set.add(this.getSouthWest());
}
if(this.getWest() != null && this.getWest().isBomb == false) {
this.getWest().change();
if(this.getWest().getNearBombNo() == 0) {
if(set.contains(this.getWest()) == false)
this.getWest().moveon(set);
}
set.add(this.getWest());
}
}
/*public Diamond[] getDiamonds() {
return diamonds;
}*/
public Diamond getEast() {
return east;
}
public int getNo() {
return no;
}
public Diamond getNorth() {
return north;
}
public Diamond getNorthEast() {
return northEast;
}
public Diamond getNorthWest() {
return northWest;
}
public Diamond getSouth() {
return south;
}
public Diamond getSouthEast() {
return southEast;
}
public Diamond getSouthWest() {
return southWest;
}
public Diamond getWest() {
return west;
}
public boolean isBomb() {
return isBomb;
}
public boolean isChange() {
return isChange;
}
public void setBomb(boolean isBomb) {
this.isBomb = isBomb;
}
public void setChange(boolean isChange) {
this.isChange = isChange;
}
public void setDiamonds(Diamond[] diamonds) {
this.diamonds = diamonds;
}
public void setEast(Diamond east) {
this.east = east;
}
public void setNo(int no) {
this.no = no;
}
public void setNorth(Diamond north) {
this.north = north;
}
public void setNorthEast(Diamond northEast) {
this.northEast = northEast;
}
public void setNorthWest(Diamond northWest) {
this.northWest = northWest;
}
public void setSouth(Diamond south) {
this.south = south;
}
public void setSouthEast(Diamond southEast) {
this.southEast = southEast;
}
public void setSouthWest(Diamond southWest) {
this.southWest = southWest;
}
public void setWest(Diamond west) {
this.west = west;
}
}
Ⅳ 運行在Eclipse環境下的java掃雷游戲的初級代碼是什麼
import java.awt.Button;x0dx0aimport java.util.Set;x0dx0a// 每一個小方塊類x0dx0apublic class Diamond extends Button {x0dx0aprivate Diamond[] diamonds;x0dx0ax0dx0a// 該小方塊周圍的八個方向上的小方塊x0dx0aprivate Diamond east;x0dx0aprivate Diamond north;x0dx0aprivate Diamond northEast;x0dx0aprivate Diamond northWest;x0dx0aprivate Diamond south;x0dx0aprivate Diamond southEast;x0dx0aprivate Diamond southWest;x0dx0aprivate Diamond west;x0dx0ax0dx0aprivate boolean isBomb;// 是否是雷x0dx0aprivate boolean isChange;// 又沒有被翻過x0dx0aprivate int no;// 產生的方塊的編號x0dx0ax0dx0a// 持有所有小方塊的引用,方便進行操作x0dx0apublic Diamond(Diamond[] diamonds) {x0dx0athis.diamonds = diamonds;x0dx0a}x0dx0ax0dx0a// 按鍵時方塊發生改變x0dx0apublic boolean change() {x0dx0athis.isChange = true;// 說明已經翻過了x0dx0aif(isBomb) {// 觸雷x0dx0a//this.setBackground(Color.red);x0dx0areturn true;x0dx0a} else {// 不是雷,就顯示周圍雷的數目x0dx0a//this.setLabel(this.getNearBombNo() + "");x0dx0athis.setLabel(this.getNearBombNo() + "");x0dx0a//if(this.getNearBombNo() == 0) {x0dx0a//this.moveon();x0dx0a//}x0dx0areturn false;x0dx0a}x0dx0a}x0dx0ax0dx0a// 獲得該小方塊周圍雷的數量x0dx0apublic int getNearBombNo() {x0dx0aint no = 0;x0dx0aif(this.northWest != null && this.northWest.isBomb) no++;x0dx0aif(this.north != null && this.north.isBomb) no++;x0dx0aif(this.northEast != null && this.northEast.isBomb) no++;x0dx0aif(this.east != null && this.east.isBomb) no++;x0dx0aif(this.southEast != null && this.southEast.isBomb) no++;x0dx0aif(this.south != null && this.south.isBomb) no++;x0dx0aif(this.southWest != null && this.southWest.isBomb) no++;x0dx0aif(this.west != null && this.west.isBomb) no++;x0dx0ax0dx0areturn no;x0dx0a}x0dx0ax0dx0a// 獲得該小方塊周圍的小方塊x0dx0apublic Diamond getNearDimaond(int i) {x0dx0aint index = -1;x0dx0aswitch (i) {x0dx0acase 1:// 1表示西北,2,表示北,以此類推x0dx0aindex = no - 10;x0dx0aif(index < 1 || no == 19 || no == 28 || no == 37 || no == 46 || no == 55 || no == 64 || no == 73) {x0dx0areturn null;x0dx0a} else {x0dx0areturn diamonds[index];x0dx0a}x0dx0acase 2:x0dx0aindex = no - 9;x0dx0aif(index < 1) {x0dx0areturn null;x0dx0a} else {x0dx0areturn diamonds[index];x0dx0a}x0dx0acase 3:x0dx0aindex = no - 8;x0dx0aif(index < 1 || no == 9 || no == 18 || no == 27 || no == 36 || no == 45 || no == 54 || no == 63 || no == 72) {x0dx0areturn null;x0dx0a} else {x0dx0areturn diamonds[index];x0dx0a}x0dx0acase 4:x0dx0aindex = no + 1;x0dx0aif(no == 9 || no == 18 || no == 27 || no == 36 || no == 45 || no == 54 || no == 63 || no == 72 || no == 81) {x0dx0areturn null;x0dx0a} else {x0dx0areturn diamonds[index];x0dx0a}x0dx0acase 5:x0dx0aindex = no + 10;x0dx0aif(index >= 81 ||no == 9 || no == 18 || no == 27 || no == 36 || no == 45 || no == 54 || no == 63 || no == 72 || no == 81) {x0dx0areturn null;x0dx0a} else {x0dx0areturn diamonds[index];x0dx0a}x0dx0acase 6:x0dx0aindex = no + 9;x0dx0aif(index > 81) {x0dx0areturn null;x0dx0a} else {x0dx0areturn diamonds[index];x0dx0a}x0dx0acase 7:x0dx0aindex = no + 8;x0dx0aif(index >= 81 || no==1 || no == 10 || no == 19 || no == 28 || no == 37 || no == 46 || no == 55 || no == 64 || no == 73) {x0dx0areturn null;x0dx0a} else {x0dx0areturn diamonds[index];x0dx0a}x0dx0acase 8:x0dx0aindex = no - 1;x0dx0aif(no==1 || no==10 || no == 19 || no == 28 || no == 37 || no == 46 || no == 55 || no == 64 || no == 73) {x0dx0areturn null;x0dx0a} else {x0dx0areturn diamonds[index];x0dx0a}x0dx0a}x0dx0areturn null;x0dx0a}x0dx0ax0dx0a// 遞歸,set是用來裝已經翻過的小方塊的,不然會死循環,為什麼用set,因為set是不重復的x0dx0apublic void moveon(Set
Ⅳ 急需基於eclipse的JAVA小游戲源代碼!!!
單人版五子棋,不用導入,直接新建一個mywindow類就行,然後把一下代碼粘貼就Ok了。或者,直接用dos就可以了。。
---------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class mypanel extends Panel implements MouseListener
{
int chess[][] = new int[11][11];
boolean Is_Black_True;
mypanel()
{
Is_Black_True = true;
for(int i = 0;i < 11;i++)
{
for(int j = 0;j < 11;j++)
{
chess[i][j] = 0;
}
}
addMouseListener(this);
setBackground(Color.BLUE);
setBounds(0, 0, 360, 360);
setVisible(true);
}
public void mousePressed(MouseEvent e)
{
int x = e.getX();
int y = e.getY();
if(x < 25 || x > 330 + 25 ||y < 25 || y > 330+25)
{
return;
}
if(chess[x/30-1][y/30-1] != 0)
{
return;
}
if(Is_Black_True == true)
{
chess[x/30-1][y/30-1] = 1;
Is_Black_True = false;
repaint();
Justisewiner();
return;
}
if(Is_Black_True == false)
{
chess[x/30-1][y/30-1] = 2;
Is_Black_True = true;
repaint();
Justisewiner();
return;
}
}
void Drawline(Graphics g)
{
for(int i = 30;i <= 330;i += 30)
{
for(int j = 30;j <= 330; j+= 30)
{
g.setColor(Color.WHITE);
g.drawLine(i, j, i, 330);
}
}
for(int j = 30;j <= 330;j += 30)
{
g.setColor(Color.WHITE);
g.drawLine(30, j, 330, j);
}
}
void Drawchess(Graphics g)
{
for(int i = 0;i < 11;i++)
{
for(int j = 0;j < 11;j++)
{
if(chess[i][j] == 1)
{
g.setColor(Color.BLACK);
g.fillOval((i + 1) * 30 - 8, (j + 1) * 30 - 8, 16, 16);
}
if(chess[i][j] == 2)
{
g.setColor(Color.WHITE);
g.fillOval((i + 1) * 30 - 8, (j + 1) * 30 - 8, 16, 16);
}
}
}
}
void Justisewiner()
{
int black_count = 0;
int white_count = 0;
int i = 0;
for(i = 0;i < 11;i++)//橫向判斷
{
for(int j = 0;j < 11;j++)
{
if(chess[i][j] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋勝利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[i][j] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋勝利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
for(i = 0;i < 11;i++)//豎向判斷
{
for(int j = 0;j < 11;j++)
{
if(chess[j][i] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋勝利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[j][i] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋勝利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
for(i = 0;i < 7;i++)//左向右斜判斷
{
for(int j = 0;j < 7;j++)
{
for(int k = 0;k < 5;k++)
{
if(chess[i + k][j + k] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋勝利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[i + k][j + k] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋勝利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
}
for(i = 4;i < 11;i++)//右向左斜判斷
{
for(int j = 6;j >= 0;j--)
{
for(int k = 0;k < 5;k++)
{
if(chess[i - k][j + k] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋勝利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[i - k][j + k] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋勝利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
}
}
void Clear_Chess()
{
for(int i=0;i<11;i++)
{
for(int j=0;j<11;j++)
{
chess[i][j]=0;
}
}
repaint();
}
public void paint(Graphics g)
{
Drawline(g);
Drawchess(g);
}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
}
class myframe extends Frame implements WindowListener
{
mypanel panel;
myframe()
{
setLayout(null);
panel = new mypanel();
add(panel);
panel.setBounds(0,23, 360, 360);
setTitle("單人版五子棋");
setBounds(200, 200, 360, 383);
setVisible(true);
addWindowListener(this);
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowDeactivated(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
}
public class mywindow
{
public static void main(String argc [])
{
myframe f = new myframe();
}
}
Ⅵ 大神們 急求基於eclipse的java小游戲程序的源碼,程序不要多復雜啊。像坦克大戰,五子棋,掃雷之類的謝謝
import java.util.Scanner;
public class Wuziqi {
/**
* 棋盤
*/
private final int[][] qipan;
/**
* 步數
*/
private int bushu;
/**
* 構造方法,設置棋盤規格
* @param x
* @param y
*/
public Wuziqi(int x, int y) {
if (x < 1 || y < 1) {
System.out.println("棋盤規格應不小於1,使用默認規格");
qipan = new int[9][9];
} else {
qipan = new int[y][x];
}
}
/**
* 游戲開始
*/
public void play() {
int[] zuobiao = null;
//如果游戲沒有結束
while (!end(zuobiao)) {
//落子,並取得坐標
zuobiao = luozi();
//輸出棋盤
out();
}
}
/**
* 輸出棋盤和棋子
*/
private void out() {
for (int i = 0; i < qipan.length; i++) {
for (int j = 0; j < qipan[i].length; j++) {
if (qipan[i][j] == 0) {
System.out.print(" +");
}else if (qipan[i][j] == -1) {
System.out.print(" 白");
}else if (qipan[i][j] == 1) {
System.out.print(" 黑");
}
}
System.out.println(" ");
}
}
/**
* 落子
*/
private int[] luozi() {
int[] zuobiao;
bushu++;
if (bushu % 2 == 1) {
System.out.println("請黑方落子");
zuobiao = input();
qipan[zuobiao[1]][zuobiao[0]] = 1;
}else {
System.out.println("請白方落子");
zuobiao = input();
qipan[zuobiao[1]][zuobiao[0]] = -1;
}
return zuobiao;
}
/**
* 輸入坐標
* @return
*/
private int[] input() {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入x軸坐標");
String x = sc.next();
System.out.println("請輸入y軸坐標");
String y = sc.next();
//如果沒有通過驗證,則再次執行input(),遞歸演算法
if (!validate(x, y)) {
return input();
}
int int_x = Integer.valueOf(x);
int int_y = Integer.valueOf(y);
return new int[] {int_x, int_y};
}
/**
* 校驗數據
* @param x
* @param y
* @return
*/
private boolean validate(String x, String y) {
Integer int_x = null;
Integer int_y = null;
//異常處理的方式判斷字元串是否是一個整數
try {
int_x = Integer.valueOf(x);
int_y = Integer.valueOf(y);
} catch (NumberFormatException e) {
System.out.println("坐標格式錯誤,坐標應為整數");
return false;
}
if (int_x < 0 || int_y < 0 || int_x >= qipan[0].length || int_y >= qipan.length) {
System.out.println("坐標越界");
return false;
}
if (qipan[int_y][int_x] == 0) {
return true;
} else {
System.out.println("坐標上已有棋子");
}
return false;
};
/**
* 結束條件
* @return
*/
private boolean end(int[] zuobiao) {
if (zuobiao == null) {
return false;
}
//計數器
//表示棋盤上經過最近落子坐標的4條線上的連續(和最近落子顏色相同的)棋子的個數
//如果某條線上連續的棋子大於等於4(加上最近落子本身,大於等於5),則游戲結束,符合五子棋規則
int[] jieguo = new int[4];
int x = zuobiao[0];
int y = zuobiao[1];
//定義八個方向
final int[][] fangxiang = {{-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}};
//最近落子的坐標上的棋子顏色
int number = qipan[y][x];
//搜索最近落子坐標為中心最遠4的距離
for (int i = 1; i <= 4; i++) {
//每次搜索不同的距離都搜索八個方向
for (int j = 0; j < fangxiang.length; j++) {
//約定如果某個方向為null時,不再搜索這個方向。關鍵字continue是跳過本次(一次)循環的意思
if (fangxiang[j] == null) {
continue;
}
int mubiao_x = x + i * fangxiang[j][0];
int mubiao_y = y + i * fangxiang[j][1];
//如果搜索坐標相對於棋盤越界,則不再搜索這個方向
if (mubiao_y >= qipan.length || mubiao_y < 0 || mubiao_x >= qipan[0].length || mubiao_x < 0) {
fangxiang[j] = null;
continue;
}
//如果最近落子坐標上的值等於目標坐標上的值(顏色相同),則計數器上某條線加1
//否則認為這個方向沒有棋子或有別的顏色的棋子,不再搜索這個方向
if (number == qipan[mubiao_y][mubiao_x]) {
jieguo[j % 4]++;
}else {
fangxiang[j] = null;
}
}
}
//查看計數器上是否有比3更大的數(查看是否有一方勝出)
for (int i : jieguo) {
if (i > 3) {
System.out.println("游戲結束");
if (bushu % 2 == 1) {
System.out.println("黑方勝");
} else {
System.out.println("白方勝");
}
return true;
}
}
//沒有勝出者的情況下,查看棋盤上是否還有空位置,如果有,則游戲可以繼續
for (int[] arr : qipan) {
for (int i : arr) {
if (i == 0) {
return false;
}
}
}
//如果沒有空位置,則平局
System.out.println("游戲結束,平局");
return true;
}
}