Ⅰ 用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;
}
}