JAVA,编一个游戏小游戏,比如扫雷,这个程序大概的代码是什么,谁能教教我我比较笨,但是我认学。

JAVA,编一个游戏小游戏,比如扫雷,这个程序大概的代码是什么,谁能教教我我比较笨,但是我认学。,第1张

import javaxswing;

import javaawt;

import javaawtevent;

public class Main

{

public static void main(String[] argus)

{

Landmine Zhang = new Landmine();

}

}

//

// Landmine类 主界面

class Landmine extends JFrame

{

static Resources resources = new Resources();

Playing listener = new Playing(this); //主要监听者,监听地雷面板的动作

Help helpListener = new Help(this); //辅助监听者,监听“帮助”、“关于”

JPanel landminePanel = new JPanel(); //创建地雷面板

JPanel topPanel = new JPanel(); //创建顶部面板

JPanel lowerPanel = new JPanel(); //创建底部面板

public static MyButton [][] lei; //主区按钮

public static int numberOfUnflaged ; //剩余的雷数,显示在topPanel上,用于提示用户

public static int numberOfClicked; //已经翻开的格子数,当数字数字到"总格子数—雷数"时,即胜利

public static int usedTime; //已用时间

public static JLabel numberOfUnflagedLabel = new JLabel(); //创建剩雷数标签

public static JLabel timeLabel = new JLabel();//创建时间标签

public static Timer timer; //创建计时

Keylistener keyListener = new Keylistener(this);

public Landmine()

{

super("扫雷__12版__小老头"); //标题

setBounds(300,90,800,800); //设置窗口位置和大小

setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);//最大化、最小化、关闭按钮

BorderLayout ff = new BorderLayout(); //创建布局管理器

setLayout(ff); //关联布局管理器

setResizable(false); //禁止改变窗口大小

/初始化一些数据/

numberOfClicked = 0;

numberOfUnflaged = 40;

usedTime = 0;

/设置顶部面板/

numberOfUnflagedLabelsetText("剩余雷数:"+numberOfUnflaged);//显示剩余雷数

numberOfUnflagedLabelsetFont(resourcesfontOne);//设置剩雷数标签字体

numberOfUnflagedLabelsetIcon(resourcesbombIconForLabel);//剩雷数标签图标(地雷形)

topPaneladd(numberOfUnflagedLabel); //剩雷数标签加入topPanel

timeLabelsetText("用时:" + usedTime); //显示剩余时间

timeLabelsetFont(resourcesfontOne); //设置时间标签字体

timeLabelsetIcon(resourcesclockIcon); //设置时间标签图标

topPaneladd(timeLabel); //时间标签加入topPanel

add(topPanel,BorderLayoutNORTH); //加入主面板上部

timer = new Timer(1000,new TimerListener());//计算器注册监听者

/设置底部面板/

JButton aboutJB = new JButton("关于"); //创建“关于”按钮

JButton helpJB = new JButton("求救"); //创建“求救”按钮

helpJBaddActionListener(helpListener); //"求救"按钮加入监听者

aboutJBaddActionListener(helpListener);//"关于"按钮加入监听者

helpJBaddKeyListener(keyListener);

aboutJBaddKeyListener(keyListener); //注册按键监听

lowerPaneladd(aboutJB); //“关于”按钮加入lowerPanel

lowerPaneladd(helpJB); //“帮助”按钮加入lowerPanel

add(lowerPanel,BorderLayoutSOUTH);

/设置地雷面板/

GridLayout dd = new GridLayout(16,16);

landminePanelsetLayout(dd); //布局管理

lei = new MyButton[18][18];

for(int i=0; i<18; ++i)

{//创建下标0—17的按钮,1818矩阵

for(int j=0; j<18; ++j)

{

lei[i][j] = new MyButton(i,j);

}

}

for(int i=1; i<17; ++i)

{//将下标1-16的按钮,加入面板、设置图标、翻开标记为假、加入监听者

for(int j=1; j<17; ++j)

{

landminePaneladd(lei[i][j]); //按钮加入地雷面板

lei[i][j]setIcon(resourcessmallIcon); //设置按钮图标

lei[i][j]isClicked = false; //翻开标记设置为 假lei[i][j]setIcon(dead);

lei[i][j]addActionListener(listener); //加入监听者

lei[i][j]addMouseListener(listener); //加入鼠标事件监听者

lei[i][j]addKeyListener(keyListener); //按钮注册按键监听,当焦点在按钮上是能监听按键

}

}

add(landminePanel,BorderLayoutCENTER); //landminePanel加入主框架中央

addLandmine(); //布雷

timerstart(); //启动计时器

setVisible(true);//显示之

}

/布雷/

public static void addLandmine()

{//随机将40的按钮的是否为雷的标记isBomb设为真

for(int count = 0; count<40; /blank/)

{

int i = (int)(Mathrandom()100 % 16 +1 ) ;

int j = (int)(Mathrandom()100 % 16 +1 ) ;

if(lei[i][j]isBomb == false)

{

lei[i][j]isBomb = true;

count++;

}

}

}

class TimerListener implements ActionListener

{//内部类,时间监听

public void actionPerformed(ActionEvent e)

{

usedTime++;

timeLabelsetText("用时:" + usedTime);

}

}

}

//

// Playing类 执行主要游戏 *** 作

class Playing implements ActionListener,MouseListener

{

static Resources resources = new Resources();

public static Landmine gui;

public Playing(Landmine in )

{

gui = in;

}

public void actionPerformed(ActionEvent event)

{

MyButton receive = (MyButton)eventgetSource();

if(receiveisBomb)

{//如果翻到了雷。。

for(int i=1; i<17; ++i)

{//将所有的雷图标设为 “地雷”

for(int j=1; j<17; ++j)

{

if(guilei[i][j]isBomb)

guilei[i][j]setIcon(resourcesbombIcon);

}

}

receivesetIcon(resourcesdeadIcon);//将踩到的地雷图标设为 “衰”

guitimerstop(); //停止计时器

JOptionPaneshowMessageDialog(null,"小朋友,你挂了…","失败!",

JOptionPaneINFORMATION_MESSAGE,

resourcesdeadIcon);//提示失败

int yourChose = JOptionPaneshowConfirmDialog(null,"你可能是一不小心点错了,再来一局?" );

if(yourChose == JOptionPaneOK_OPTION)

{//点击“是”时

replay();

}

else

{//点击 “否” 或 “取消” 时退出程序

Systemexit(0);

}

}

else if(receiveisClicked ==false)

{//未翻到雷

showBombNumber(receive);

}

}

public static void showBombNumber(MyButton in)

{//翻开点击的按钮

int numberOfLandmine = 0;//记录雷的个数

inisClicked = true; //翻开标记设为真

/检测周围8个方块是否为雷/

if(guilei[innum_x-1][innum_y-1]isBomb == true) numberOfLandmine++;//左上

if(guilei[innum_x][innum_y-1]isBomb == true) numberOfLandmine++; //上

if(guilei[innum_x+1][innum_y-1]isBomb == true) numberOfLandmine++;//右上

if(guilei[innum_x+1][innum_y]isBomb == true) numberOfLandmine++; //右

if(guilei[innum_x+1][innum_y+1]isBomb == true) numberOfLandmine++;//右下

if(guilei[innum_x][innum_y+1]isBomb == true) numberOfLandmine++; //下

if(guilei[innum_x-1][innum_y+1]isBomb == true) numberOfLandmine++;//左下

if(guilei[innum_x-1][innum_y]isBomb == true) numberOfLandmine++; //左

insetIcon(new ImageIcon("images/"+numberOfLandmine+"png"));//根据周围的雷数显示数字图标

guinumberOfClicked++;//翻开格子数+1

if(guinumberOfClicked==216)

{//翻开216个格子时游戏成功,用户选择是否再来一局

int yourChoice = JOptionPaneshowConfirmDialog(null,"恭喜你成功了!再来一盘吗?");

if(yourChoice == JOptionPaneOK_OPTION)

replay();

else

Systemexit(0);

}

if(numberOfLandmine==0)

{//如果周围无雷,则将周围未翻开格子的全部翻开

if(guilei[innum_x-1][innum_y-1]isClicked == false)

showBombNumber(guilei[innum_x-1][innum_y-1]);

if(guilei[innum_x][innum_y-1]isClicked == false)

showBombNumber(guilei[innum_x][innum_y-1]);

if(guilei[innum_x+1][innum_y-1]isClicked == false)

showBombNumber(guilei[innum_x+1][innum_y-1]);

if(guilei[innum_x+1][innum_y]isClicked == false)

showBombNumber(guilei[innum_x+1][innum_y]);

if(guilei[innum_x+1][innum_y+1]isClicked == false)

showBombNumber(guilei[innum_x+1][innum_y+1]);

if(guilei[innum_x][innum_y+1]isClicked == false)

showBombNumber(guilei[innum_x][innum_y+1]);

if(guilei[innum_x-1][innum_y+1]isClicked == false)

showBombNumber(guilei[innum_x-1][innum_y+1]);

if(guilei[innum_x-1][innum_y]isClicked == false)

showBombNumber(guilei[innum_x-1][innum_y]);

}

}

public static void replay()

{//重新开始

guidispose(); //释放框架资源

guitimerstop(); //终止计时器

Landmine ff = new Landmine();//重新创建一个主类的实例

//这几条语句实现了重新开始————关闭上一个窗口,重新开启一个

//但是这种方法会造成内存的浪费,一个改进的方法是不关闭当年窗口,而是将当前窗口重新初始化

}

public void mousePressed(MouseEvent e)

{//当鼠标右键点击时自动调用此函数

int mods = egetModifiers();

MyButton receive = (MyButton)egetSource();

if((mods & InputEventBUTTON3_MASK) != 0)

{//鼠标右键

if(receiveisClicked == false)

{

receiveisRight = receiveisRight false : true;//改变receiveisRight的值

if(receiveisRight)

{//如果添加标记,则剩余雷数-1,设置标签为“旗帜”

guinumberOfUnflaged--;

receivesetIcon(resourcesflagIcon);

}

else

{//如果清除标记,则剩余雷数+1,设置标签为“未翻开”

guinumberOfUnflaged++;

receivesetIcon(resourcessmallIcon);

}

guinumberOfUnflagedLabelsetText("剩余雷数:"+guinumberOfUnflaged);

//更新剩余雷数标签

}

}

}

public void mouseReleased(MouseEvent e){}

public void mouseExited(MouseEvent e) {}

public void mouseClicked(MouseEvent e){}

public void mouseEntered(MouseEvent e){}

}

//

// Help类,响应“关于”、“求救”

class Help implements ActionListener

{

static Resources resources = new Resources();

public static Landmine gui;

public Help(Landmine in)

{

gui = in ;

}

public void actionPerformed(ActionEvent event)

{

if(eventgetActionCommand()=="关于")

JOptionPaneshowMessageDialog(null,"扫雷12版。。小老头出品");

if(eventgetActionCommand()=="求救")

help();

}

public static void help()

{//求救

int stopNumber = (int)(Mathrandom() guinumberOfUnflaged + 1 );

int count = 0;

for(int i=1; i<17;++i )

{

for(int j=1; j<17; ++j)

{

if( guilei[i][j]isBomb && !guilei[i][j]isClicked && !guilei[i][j]isRight )

{

count++;

}

if(count == stopNumber)

{

guilei[i][j]setIcon(resourcesbadIcon);

return;

}

}

}

}

}

//

// Keylistener类,响应键盘事件

class Keylistener implements KeyListener

{

static Resources resources = new Resources();

Landmine gui;

public Keylistener(Landmine in)

{

gui = in;

}

public void keyPressed(KeyEvent e)

{//有键按下时自动执行该方法

if(egetKeyCode() == KeyEventVK_UP)

{//按键为 向上 时,将所有未标记的地雷显示出

for(int i=1; i<17; ++i)

{

for(int j=1; j<17; ++j)

{

if(guilei[i][j]isBomb && !guilei[i][j]isRight)

guilei[i][j]setIcon(resourcesbadIcon);

}

}

}

if(egetKeyCode() == KeyEventVK_DOWN)

{//按键为 向下 时,将所有未标记的地雷恢复为未点击的图标

for(int i=1; i<17; ++i)

{

for(int j=1; j<17; ++j)

{

if(guilei[i][j]isBomb && !guilei[i][j]isRight)

guilei[i][j]setIcon(resourcessmallIcon);

}

}

}

}

public void keyReleased(KeyEvent e){}

public void keyTyped(KeyEvent e){}

}

//

// 按钮类 MyBtton

class MyButton extends JButton

{

public int num_x,num_y; //第几号方块

public boolean isBomb; //是否为雷

public boolean isClicked; //是否被点击

public int BombFlag; //探雷标记

public boolean isRight; //是否点击右键

public MyButton(int x, int y)

{

BombFlag = 0;

num_x = x;

num_y = y;

isBomb = false;

isClicked = true;

isRight = false;

}

}

//

// 资源类 其他类中用到的图标,字体等

class Resources

{

public static ImageIcon deadIcon;

public static ImageIcon smallIcon;

public static ImageIcon clockIcon;

public static ImageIcon bombIcon;

public static ImageIcon flagIcon;

public static ImageIcon badIcon;

public static ImageIcon bombIconForLabel;

public static Font fontOne;

public Resources()

{

deadIcon = new ImageIcon("images/deadgif");

smallIcon = new ImageIcon("images/smallIconpng");

clockIcon = new ImageIcon("images/clock2png");

bombIcon = new ImageIcon("images/bombpng");

flagIcon = new ImageIcon("images/flag_2png");

badIcon = new ImageIcon("images/badgif");

bombIconForLabel = new ImageIcon("images/bombForLabelgif");

fontOne = new Font("null",FontBOLD,20);

}

}

这应该有很多种算法把。。如果不考虑效率的话穷举法是肯定可以的。。。也就是说试过所有的排列组合。。然后计算每次排列的剩余防御卡的价值和。。和最大的排列就是你要的匹配。。当然这个算法 当N很大的时候非常没效率。。但你的问题只要给出算法然后说明正确性。。所以穷举是肯定可行的。。

下面是模拟代码

import javautilArrayList;

public class Test {

//攻击卡数组

private int [] a = {1,1,3,4};

//匹配结果

private ArrayList<int []> result = new ArrayList<int []>();

//防御卡数组

private D [] d = {new D(1,5),new D(2,2),new D(2,8),new D(3,4)};

//最大防御卡价值初始化为0

private int value = 0;

public static void main(String []args){

int [] input = {1,2,3,4};

Test t = new Test();

tprocess(input, inputlength, 0);

ArrayList<int []> r = tgetResult();

int size = rsize();

Systemoutprintln("可能的组合有"+size+"种 ");

for(int i=0;i<size;i++){

int resultSize = rget(i)length;

Systemoutprint("第"+(i+1)+"种 为:");

for(int j=0;j<resultSize;j++){

Systemoutprint(rget(i)[j]);

}

}

Systemoutprintln();

Systemoutprintln("Value = "+tgetValue());

Systemoutprintln("结束");

}

public void process(int[] input, int n, int k) {

if (n == k) {

int count = 0;

int size = inputlength;

int [] out = new int [size];

for (int i = 0; i < size; i++) {

if(d[input[i]-1]getX()>a[i]){

count += d[input[i]-1]getY();

}

out [i] = input[i];

}

//如果排列价值和大于之前的最大防御价值改变最优匹配

if(count > value){

if(resultsize() != 0){

resultclear();

}

resultadd(out);

value = count;

for(int in:out){

Systemoutprint(in);

}

Systemoutprintln();

}

//如果价值和一样并且没有这个组合增加到结果list

if(count == value && !thisinTheResult(input)){

resultadd(out);

for(int in:out){

Systemoutprint(in);

}

Systemoutprintln();

}

} else {

for (int i = k; i < n; i++) {

swap(input, k, i);

process(input, n, k + 1);

swap(input, i, k);

}

}

}

public boolean inTheResult(int[]a){

int size = thisresultsize();

for(int i=0;i<size;i++){

int s = thisresultget(i)length;

for(int j=0;j<s;j++){

if(a[j] != thisresultget(i)[j]){

return false;

}

}

}

return true;

}

public ArrayList<int[]> getResult(){

return thisresult;

}

public int getValue(){

return thisvalue;

}

public void swap(int[] a, int x, int y) {

int temp = a[x];

a[x] = a[y];

a[y] = temp;

}

//防御卡的类 x是di y是wi

public class D{

// x = di y = wi

private int x,y;

D (int x,int y){

thisx = x;

thisy = y;

}

public int getX(){

return x;

}

public int getY(){

return y;

}

}

}

运行结果

1234

1243

1324

1342

2314

2341

3214

3241

3412

3421

4321

4312

可能的组合有4种

第1种 为:3412第2种 为:3421第3种 为:4321第4种 为:4312

Value = 12

结束

貌似测试了下应该是正确的

其实这个问题挺有意思的建议你不要看我的代码根据我的提示自己做次

用JAVA编写一个龟兔赛跑的GUI界面小游戏,其功能说明如下:

当程序启动后,主窗体上有两个,一幅乌龟,一幅是兔子,并列放置在窗体的左边,主窗体下面有两个按钮,一个是跑,一个是退出

当点击跑按钮时,就开始往右边移动,谁先移动到右边,就是胜利者,在窗体的标题栏上提示是谁赢。

当点击退出按钮时,退出当前程序

import javaxswing;

import javaawt;

import javaawtevent;

public class Game1 extends javaxswingJFrame {

public Game1() {

initComponents();

}

private void initComponents() {

jButton1 = new javaxswingJButton();

jButton2 = new javaxswingJButton();

jButton3 = new javaxswingJButton();

jButton4 = new javaxswingJButton();

jButton5 = new javaxswingJButton();

jButton6 = new javaxswingJButton();

jButton7 = new javaxswingJButton();

setDefaultCloseOperation(javaxswingWindowConstantsEXIT_ON_CLOSE);

jButton1setIcon(new javaxswingImageIcon("C:\\Users\\Administrator\\Desktop\\游戏jpg"));

jButton1setText("jButton1");

jButton1addActionListener(new javaawteventActionListener() {

public void actionPerformed(javaawteventActionEvent evt) {

jButton1ActionPerformed(evt);

}

});

jButton2setIcon(new javaxswingImageIcon("C:\\Users\\Administrator\\Desktop\\游戏jpg"));

jButton2setText("jButton2");

jButton2addActionListener(new javaawteventActionListener() {

public void actionPerformed(javaawteventActionEvent evt) {

jButton2ActionPerformed(evt);

}

});

jButton3setIcon(new javaxswingImageIcon("C:\\Users\\Administrator\\Desktop\\游戏jpg"));

jButton3setText("jButton3");

jButton3addActionListener(new javaawteventActionListener() {

public void actionPerformed(javaawteventActionEvent evt) {

jButton3ActionPerformed(evt);

}

});

jButton4setIcon(new javaxswingImageIcon("C:\\Users\\Administrator\\Desktop\\游戏2jpg"));

jButton4setText("jButton4");

jButton4addActionListener(new javaawteventActionListener() {

public void actionPerformed(javaawteventActionEvent evt) {

jButton4ActionPerformed(evt);

}

});

jButton5setIcon(new javaxswingImageIcon("C:\\Users\\Administrator\\Desktop\\游戏1jpg")); // NOI18N

jButton5setText("jButton5");

jButton5addActionListener(new javaawteventActionListener() {

public void actionPerformed(javaawteventActionEvent evt) {

jButton5ActionPerformed(evt);

}

});

jButton6setIcon(new javaxswingImageIcon("C:\\Users\\Administrator\\Desktop\\游戏1jpg")); // NOI18N

jButton6setText("jButton6");

jButton6addActionListener(new javaawteventActionListener() {

public void actionPerformed(javaawteventActionEvent evt) {

jButton6ActionPerformed(evt);

}

});

jButton7setIcon(new javaxswingImageIcon("C:\\Users\\Administrator\\Desktop\\游戏1jpg")); // NOI18N

jButton7setText(" ");

jButton7addActionListener(new javaawteventActionListener() {

public void actionPerformed(javaawteventActionEvent evt) {

jButton7ActionPerformed(evt);

}

});

javaxswingGroupLayout layout = new javaxswingGroupLayout(getContentPane());

getContentPane()setLayout(layout);

layoutsetHorizontalGroup(

layoutcreateParallelGroup(javaxswingGroupLayoutAlignmentLEADING)

addGroup(layoutcreateSequentialGroup()

addGap(132, 132, 132)

addGroup(layoutcreateParallelGroup(javaxswingGroupLayoutAlignmentTRAILING, false)

addComponent(jButton1, javaxswingGroupLayoutAlignmentLEADING, 0, 0, ShortMAX_VALUE)

addComponent(jButton2, javaxswingGroupLayoutAlignmentLEADING, 0, 0, ShortMAX_VALUE)

addComponent(jButton3, javaxswingGroupLayoutAlignmentLEADING, 0, 0, ShortMAX_VALUE)

addComponent(jButton4, javaxswingGroupLayoutAlignmentLEADING, 0, 0, ShortMAX_VALUE)

addComponent(jButton5, javaxswingGroupLayoutAlignmentLEADING, 0, 0, ShortMAX_VALUE)

addComponent(jButton6, 0, 0, ShortMAX_VALUE)

addComponent(jButton7, javaxswingGroupLayoutPREFERRED_SIZE, 69, ShortMAX_VALUE))

addContainerGap(199, ShortMAX_VALUE))

);

layoutsetVerticalGroup(

layoutcreateParallelGroup(javaxswingGroupLayoutAlignmentLEADING)

addGroup(layoutcreateSequentialGroup()

addGap(101, 101, 101)

addComponent(jButton7)

addGap(29, 29, 29)

addComponent(jButton6)

addGap(30, 30, 30)

addComponent(jButton5)

addPreferredGap(javaxswingLayoutStyleComponentPlacementRELATED, javaxswingGroupLayoutDEFAULT_SIZE, ShortMAX_VALUE)

addComponent(jButton4)

addGap(18, 18, 18)

addComponent(jButton3)

addGap(18, 18, 18)

addComponent(jButton2)

addGap(18, 18, 18)

addComponent(jButton1))

);

pack();

}// </editor-fold>

private void jButton1ActionPerformed(javaawteventActionEvent evt) {

if(a==0)

{

if(a!=b)

{

if(b==2)

{

jButton2setIcon(f1);

jButton1setIcon(f3);

a=2;

b=0;

}

else

{

if(c==2)

{

jButton3setIcon(f1);

jButton1setIcon(f3);

c=0;

a=2;

}

}

}

}

}

private void jButton2ActionPerformed(javaawteventActionEvent evt) {

if(b==0)

{

if(b!=c)

{

if(c==2)

{

jButton3setIcon(f1);

jButton2setIcon(f3);

b=2;

c=0;

}

else

{

if(d==2)

{

jButton4setIcon(f1);

jButton2setIcon(f3);

d=0;

b=2;

}

}

}

}

if(b==1)

{

if(a==2)

{

jButton1setIcon(f2);

jButton2setIcon(f3);

b=2;

a=1;

}

}

}

private void jButton3ActionPerformed(javaawteventActionEvent evt) {

if(c==0)

{

if(c!=d)

{

if(d==2)

{

jButton4setIcon(f1);

jButton3setIcon(f3);

c=2;

d=0;

}

else

{

if(e==2)

{

jButton5setIcon(f1);

jButton3setIcon(f3);

e=0;

c=2;

}

}

}

}

if(c==1)

{

if(b==0&&a==2)

{

jButton1setIcon(f2);

jButton3setIcon(f3);

c=2;

a=1;

}

if(b==2)

{

jButton2setIcon(f2);

jButton3setIcon(f3);

c=2;

b=1;

}

}

}

private void jButton4ActionPerformed(javaawteventActionEvent evt) {

if(d==0)

{

if(d!=e)

{

if(e==2)

{

jButton5setIcon(f1);

jButton4setIcon(f3);

d=2;

e=0;

}

else

{

if(f==2)

{

jButton6setIcon(f1);

jButton4setIcon(f3);

f=0;

d=2;

}

}

}

}

if(d==1)

{

if(c==0&&b==2)

{

jButton2setIcon(f2);

jButton4setIcon(f3);

d=2;

b=1;

}

if(c==2)

{

jButton3setIcon(f2);

jButton4setIcon(f3);

d=2;

c=1;

}

}

}

private void jButton5ActionPerformed(javaawteventActionEvent evt) {

if(e==0)

{

if(e!=f)

{

if(f==2)

{

jButton6setIcon(f1);

jButton5setIcon(f3);

e=2;

f=0;

}

else

{

if(g==2)

{

jButton7setIcon(f1);

jButton5setIcon(f3);

g=0;

e=2;

}

}

}

}

if(e==1)

{

if(d==0&&c==2)

{

jButton3setIcon(f2);

jButton5setIcon(f3);

e=2;

c=1;

}

if(d==2)

{

jButton4setIcon(f2);

jButton5setIcon(f3);

e=2;

d=1;

}

}}

private void jButton6ActionPerformed(javaawteventActionEvent evt) {

if(f==0&&g==2)

{

jButton7setIcon(f1);

jButton6setIcon(f3);

f=2;

g=0;

}

if(f==1&&e==2)

{

jButton5setIcon(f2);

jButton6setIcon(f3);

f=2;

e=1;

}

if(f==1&&e==0&&d==2)

{

jButton4setIcon(f2);

jButton6setIcon(f3);

d=1;

f=2;

}

}

private void jButton7ActionPerformed(javaawteventActionEvent evt) {

if(g==1&&f==2)

{

jButton6setIcon(f2);

jButton7setIcon(f3);

g=2;

f=1;

}

if(g==1&&f==0&&e==2)

{

jButton5setIcon(f2);

jButton7setIcon(f3);

g=2;

e=1;

}

}

public static void main(String args[]) {

javaawtEventQueueinvokeLater(new Runnable() {

public void run() {

new Game1()setVisible(true);

}

});

}

// Variables declaration - do not modify

private javaxswingJButton jButton1;

private javaxswingJButton jButton2;

private javaxswingJButton jButton3;

private javaxswingJButton jButton4;

private javaxswingJButton jButton5;

private javaxswingJButton jButton6;

private javaxswingJButton jButton7;

// End of variables declaration

int a=0;int b=0;int c=0;int d=2;int e=1;int f=1;int g=1;

String s="C:\\Users\\Administrator\\Desktop\\游戏jpg";

String s1="C:\\Users\\Administrator\\Desktop\\游戏1jpg";

String s2="C:\\Users\\Administrator\\Desktop\\游戏2jpg";

ImageIcon f1=new ImageIcon("C:\\Users\\Administrator\\Desktop\\游戏jpg");

ImageIcon f2=new ImageIcon("C:\\Users\\Administrator\\Desktop\\游戏1jpg");

ImageIcon f3=new ImageIcon("C:\\Users\\Administrator\\Desktop\\游戏2jpg");

}//我对“点击按钮要隔一个按钮移,必须两个相邻按钮箭头相对”的理解是两个箭头相对的按钮后要有空位才可以移动,不知道是不是对的,而且我不会把按钮做成圆形,你可以用你的界面,如果我理解不对,里面的方法思想应该可以解决移动问题,还有,向下的按钮图案我存为

游戏jpg;向上的我存为游戏1jpg;空位按钮我存为游戏2jpg

前天写的猜数字游戏,yong i控制猜测次数,有详细解析,用黑窗口可以直接运行,

我试验过了,没问题

import javaxswingIcon;

import javaxswingJOptionPane;

public class CaiShuZi4JOptionPane {

/

@param args

/

public static void main(String[] args) {

Icon icon = null;

boolean bl = false;

int put = 0;

int c = (int) (((Mathrandom())100)+1); //获取一个1-100的随机数

Systemoutprintln("你获取的随机数是:"+c); //打印你的随机数字

String str1 = (String) JOptionPaneshowInputDialog(null,"请输入你的猜测数字(1-100):\n","猜数字游戏",JOptionPanePLAIN_MESSAGE,icon,null,"在这输入"); //第一次输入你的猜测数字

if(str1==null){

JOptionPaneshowMessageDialog(null, "你已经取消了本次游戏"); //如果你点取消那么本次游戏结束

}else{

bl = num(str1); //判断是输入的是不是数字或者是整数

if(true==bl){ //如果是数字的话进入与随机数比较的程序

Systemoutprintln("你输入的数字是:"+str1); //打印你输入的数字

put = IntegervalueOf(str1);

for(int i = 4;i > 0;i--){ //i是你可以猜测的次数

if(put==c){

JOptionPaneshowMessageDialog(null, "恭喜你猜对了,正确答案是:"+c+"。"); //如果你猜对了就直接结束循环

break;

}else if(put>c){ //如果输大了就让你再次从新输入

str1 = (String) JOptionPaneshowInputDialog(null,"你的输入过大。你还有"+i+"次机会,请重新输入:\n","猜数字游戏",JOptionPanePLAIN_MESSAGE,icon,null,"在这输入");

if(str1==null){

JOptionPaneshowMessageDialog(null, "你已经取消了本次输入");

break;

}else{

bl =num(str1);

if(true==bl){

put = IntegervalueOf(str1);

}else{

JOptionPaneshowMessageDialog(null, "你的输入不正确,请重新输入");

}

}

}else if(put<c){ //如果你输小了也让你从新输入

str1 = (String) JOptionPaneshowInputDialog(null,"你的输入过小。你还有"+i+"次机会,请重新输入:\n","猜数字游戏",JOptionPanePLAIN_MESSAGE,icon,null,"在这输入");

if(str1==null){

JOptionPaneshowMessageDialog(null, "你已经取消了本次输入");

break;

}else{

bl =num(str1);

if(true==bl){

put = IntegervalueOf(str1);

}else{

JOptionPaneshowMessageDialog(null, "你的输入不正确,请重新输入");

}

}

}

}

}else if(bl==false){ //这个 是你第一次如果填写的不是数字的话也会结束本次游戏

JOptionPaneshowMessageDialog(null, "请您下次按要求填写。本次游戏结束");

}

if(true==bl && c!=put){ //如果你i次都没猜对,那么就直接告诉你这个数十什么

JOptionPaneshowMessageDialog(null, "很遗憾你没能猜对,这个数字是:"+c+"");

}

}

}

public static boolean num(String value){ //一个静态方法,判断你输入的是不是数字

try {

IntegerparseInt(value);

return true;

} catch (Exception e) {

return false;

}

}

}

写了一下,结果输出到桌面上,你把文件输出路径改成你的桌面路径就可以了,不知道你要不要最终结果

代码:

package comhmall;

import javaioBufferedWriter;

import javaioFile;

import javaioFileWriter;

import javaioIOException;

import javautilArrayList;

import javautilList;

import javautilRandom;

import javautilScanner;

/

@ClassName Mora

@Description TODO

@Author dell

@Date 2019/7/1 13:03

@Version 10

/

public class Mora {

private static String calculate(String input) {

List<String> list = new ArrayList<>();

listadd("拳头");

listadd("剪刀");

listadd("布");

Random r = new Random();

Integer random = rnextInt(3);

String temp = listget(random);

if (tempequals(input)) {

return "您出了" + input + ",电脑出了" + temp + ",结果为平局";

} else {

if (inputequals("拳头")) {

if (tempequals("剪刀")) {

return "您出了" + input + ",电脑出了" + temp + ",结果为您赢了";

} else {

return "您出了" + input + ",电脑出了" + temp + ",结果为您输了";

}

} else if (inputequals("剪刀")) {

if (tempequals("拳头")) {

return "您出了" + input + ",电脑出了" + temp + ",结果为您输了";

} else {

return "您出了" + input + ",电脑出了" + temp + ",结果为您赢了";

}

} else if (inputequals("布")) {

if (tempequals("拳头")) {

return "您出了" + input + ",电脑出了" + temp + ",结果为您赢了";

} else {

return "您出了" + input + ",电脑出了" + temp + ",结果为您输了";

}

} else {

return "输入数据有误";

}

}

}

public static void main(String[] args) {

Scanner scanner = new Scanner(Systemin);

Integer count;

while (true) {

Systemoutprintln("请输入比赛场次(不能小于5):");

count = scannernextInt();

if (count >= 5) {

Systemoutprintln("比赛开始");

break;

}

Systemoutprintln("输入小于5,请重新输入");

}

String input;

File f = new File("C:\\Users\\dell\\Desktop\\resulttxt");

try {

BufferedWriter bw = new BufferedWriter(new FileWriter(f));

for (int i = 0; i < count; i++) {

Systemoutprintln("请输入您的选择:");

input = scannernext();

String result = calculate(input);

bwwrite(result);

bwnewLine();

Systemoutprintln(result);

}

bwclose();

} catch (IOException e) {

eprintStackTrace();

}

}

}

结果:

Java不能直接运行Flash游戏。Flash是使用Adobe Flash开发的技术,而Java是使用Java语言和Java虚拟机(JVM)开发的技术。不过,您可以在Java应用程序中嵌入Flash游戏。这需要使用一些第三方库和工具来实现。

以下是在Java应用程序中嵌入Flash游戏的步骤:

1 下载并安装Swing浏览器:Swing浏览器是一个基于Java的浏览器,可以在Java应用程序中加载网页。您可以从GitHub上下载Swing浏览器,并根据文档进行安装和配置。

2 在Java应用程序中添加Swing浏览器依赖库:Swing浏览器提供了一个依赖库,您需要将其添加到您的Java项目中。

3 引入SWT组件:SWT是一个基于本机 *** 作系统GUI控件的工具包,可以帮助您在Java应用程序中嵌入Flash游戏。

4 创建一个SWT浏览器控件:使用SWT组件创建一个WebBrowser对象。

5 将WebBrowser对象添加到窗口并设置大小、位置等属性。

6 加载Flash游戏URL:调用WebBrowser对象的setUrl方法并传递Flash游戏的URL地址即可将其加载到控件中。

以上是大致的步骤,具体实现细节需要根据您的具体情况进行调整。

<Style Type="Text/CSS">

<!--

Input{}

blokje{

Height:48px;

Width:48px;

Font-Size:16pt;

Font-Weight:900;

Font-Family:Comic Sans MS,Arial,Verdana;

Text-Align:Center;

}

knopje{

Width:144px;

}

-->

</Style>

<Center>

<Form>

<Input Type="Button" title="Click here to play again" Name="beurten" Width="164px" Class="knopje" Value="Player 1" onClick="wie = 'Player 1'; initieer()">

<Table CellPadding="0" CellSpacing="0" Border="0">

<Script Language="JavaScript">

<!--//

for (i = 1; i < 10; i++) {

if (i % 3 == 1) {documentwrite('<Tr>');}

documentwrite('<Td><Input Type="Button" Name="' + i + '" Value=" " Class="blokje" onClick="wijzig(thisform,this)"></Td>');

if (i % 3 == 0) {documentwrite('</Tr>');}

}

//-->

</Script>

</Table>

<Input Type="CheckBox" title="Click here to change from 2 players or computergame" Name="automaat" Value="2 players" Id="autmat" onClick="if (thisvalue == 'Computer') {thisvalue = '2 players'; auto = 0} else {thisvalue = 'Computer'; auto = 1}; wie = 'Player 1'; initieer();"><Label For="autmat">与电脑对玩?</Label>

<Table CellPadding="0" CellSpacing="0" Border="1">

<Tr BgColor="Silver">

<Td Align="Center"> 玩家 1 胜</Td>

<Td Align="Center"> 玩家 2 胜</Td>

<Td Align="Center"> 平局 </Td>

</Tr>

<Tr BgColor="Silver">

<Td Align="Center"><Input Style="BackGround-Color:Transparent;Border:Solid 0px;Text-Align:Center" Type="Text" Size="3" ReadOnly title="Player 1" Name="speler1" Value="0"></Td>

<Td Align="Center"><Input Style="BackGround-Color:Transparent;Border:Solid 0px;Text-Align:Center" Type="Text" Size="3" ReadOnly title="Player 2" Name="speler2" Value="0"></Td>

<Td Align="Center"><Input Style="BackGround-Color:Transparent;Border:Solid 0px;Text-Align:Center" Type="Text" Size="3" ReadOnly title="Remise" Name="remise" Value="0"></Td>

</Tr>

</Table>

</Form>

</Center>

<Script Language="JavaScript">

<!--//

timerID = xtal = ytal = ztal = auto = 0;

wie = 'Player 1';

function initieer(){

clearTimeout(timerID);

cel = new Array(0,0,0,0,0,0,0,0,0,0);

aanv = new Array(); strat = verd = leeg = aanv;

beurt = wissel = 1; wint = keren = 0;

documentforms[0]speler1value = xtal;

documentforms[0]speler2value = ytal;

documentforms[0]remisevalue = ztal;

if (wie != 'Start') {

for (i in cel) {

cel[i] = 0; if (i > 0) {documentforms[0]elements[i]value = ''};

}

}

documentforms[0]beurtenvalue = wie;

}

function zetten() {

clearTimeout(timerID);

if (aanv[0] > 0) {

slag = aanv[Mathfloor(Mathrandom() 10) % aanvlength]

} else if (verd[0] > 0) {

slag = verd[Mathfloor(Mathrandom() 10) % verdlength];

} else if (strat[0] > 0) {

slag = strat[Mathfloor(Mathrandom() 10) % stratlength];

} else {

slag = leeg[Mathfloor(Mathrandom() 10) % leeglength];

}

if (beurt == 0 && slag > 0) {documentforms[0]elements[slag]click()};

}

function win() {

if ( cel[1] + cel[2] + cel[3] == 3 ||

cel[4] + cel[5] + cel[6] == 3 ||

cel[7] + cel[8] + cel[9] == 3 ||

cel[1] + cel[4] + cel[7] == 3 ||

cel[2] + cel[5] + cel[8] == 3 ||

cel[3] + cel[6] + cel[9] == 3 ||

cel[1] + cel[5] + cel[9] == 3 ||

cel[3] + cel[5] + cel[7] == 3

) {

if (confirm("玩家一: 你赢了一局 !!\n还想要继续玩吗"))

{

wint = 1;

xtal++; wie = 'Player 1'; timerID = setTimeout('initieer()',800)

} else {

xtal++; wie = 'Start'; timerID = setTimeout('initieer()',800)

};

}

if ( cel[1] + cel[2] + cel[3] == 30 ||

cel[4] + cel[5] + cel[6] == 30 ||

cel[7] + cel[8] + cel[9] == 30 ||

cel[1] + cel[4] + cel[7] == 30 ||

cel[2] + cel[5] + cel[8] == 30 ||

cel[3] + cel[6] + cel[9] == 30 ||

cel[1] + cel[5] + cel[9] == 30 ||

cel[3] + cel[5] + cel[7] == 30

) {

if (confirm(((auto == 1) ('电脑赢啦!!!') : ('玩家二: 你赢了一局 !!')) + "!\n还想要继续玩吗")) {

ytal++; wie = 'Player 1'; timerID = setTimeout('initieer()',800)

} else {

ytal++; wie = 'Start'; timerID = setTimeout('initieer()',800)

};

}

}

function wisselen(beurt){

if (wissel == 1) {

if (beurt == 0) {

beurt = 1;

wie = "Player " + 1

}

else {

if (auto == 0) {beurt = 0;

wie = "Player " + 2} else {beurt = 0; wie = "Computer";}

}

if (keren == 9

) {

if (confirm("打平了!!!\n\n还想要继续玩吗")) {

ztal++; wie = 'Player 1'; timerID = setTimeout('initieer()',800)} else

{ztal++; wie = 'Start'; timerID = setTimeout('initieer()',800)};

}

}

else {

beurt = beurt;

}

wissel = 1;

return(beurt);

}

function wijziging(klik) {

plek = cel[klik]; geklikt = klik;

if (plek == 0){

if (beurt == 0){

xo = "O";

plek = 10;

} else {

xo = "X";

plek = 1;

}

cel[klik] = plek; keren++;

}

else {

if (plek == 10){

xo = "O";}

if (plek == 1){

xo = "X";}

wissel = 0

}

return(xo);

}

function verdedig() {leeg = new Array(); verd = new Array();

for (i = 1; i < 10; i++) { oud = cel[i]; cel[i] = 1;

if ( (cel[1] + cel[2] + cel[3] == 3 ||

cel[4] + cel[5] + cel[6] == 3 ||

cel[7] + cel[8] + cel[9] == 3 ||

cel[1] + cel[4] + cel[7] == 3 ||

cel[2] + cel[5] + cel[8] == 3 ||

cel[3] + cel[6] + cel[9] == 3 ||

cel[1] + cel[5] + cel[9] == 3 ||

cel[3] + cel[5] + cel[7] == 3) && oud == 0

) { verd[verdlength] = i}

cel[i] = oud;

if (cel[i] == 0) {

if (keren != 1) {leeg[leeglength] = i} else

if (cel[5] == 0) {leeg[0] = 5; leeg[1] = 10 - geklikt} else

if (i % 2 != 0) {leeg[leeglength] = i}

};

}

}

function aanval() {aanv = new Array();

for (i = 1; i < 10; i++) { oud = cel[i]; cel[i] = 10;

if ( (cel[1] + cel[2] + cel[3] == 30 ||

cel[4] + cel[5] + cel[6] == 30 ||

cel[7] + cel[8] + cel[9] == 30 ||

cel[1] + cel[4] + cel[7] == 30 ||

cel[2] + cel[5] + cel[8] == 30 ||

cel[3] + cel[6] + cel[9] == 30 ||

cel[1] + cel[5] + cel[9] == 30 ||

cel[3] + cel[5] + cel[7] == 30) && oud == 0

) { aanv[aanvlength] = i}

cel[i] = oud;

}

}

function strategie() {strat = new Array();

for (i = 1; i < 10; i++) { oud = cel[i]; cel[i] = 10;

if ( (cel[1] + cel[2] + cel[3] == 20 ||

cel[4] + cel[5] + cel[6] == 20 ||

cel[7] + cel[8] + cel[9] == 20 ||

cel[1] + cel[4] + cel[7] == 20 ||

cel[2] + cel[5] + cel[8] == 20 ||

cel[3] + cel[6] + cel[9] == 20 ||

cel[1] + cel[5] + cel[9] == 20 ||

cel[3] + cel[5] + cel[7] == 20) && oud == 0

) {

if (keren != 3) {strat[stratlength] = i} else if (i % 2 != 0) {strat[stratlength] = i}

}

cel[i] = oud;

}

}

function wijzig(form,element){

wijziging(elementname);

elementvalue = xo;

beurt = wisselen(beurt);

formbeurtenvalue = wie;

verdedig(); aanval(); strategie(); win();

if (auto == 0 || wint == 1) {return} else {

timerID = setTimeout('zetten()',600)

}

}

initieer();

//-->

</Script>

这是一个3子棋的游戏,不太大,把上面这段代码加入<body></body>中。

以上就是关于JAVA,编一个游戏小游戏,比如扫雷,这个程序大概的代码是什么,谁能教教我我比较笨,但是我认学。全部的内容,包括:JAVA,编一个游戏小游戏,比如扫雷,这个程序大概的代码是什么,谁能教教我我比较笨,但是我认学。、java编写:一个游戏程序、用JAVA编写一个龟兔赛跑的GUI界面小游戏,其功能说明如下:等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/9736605.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-01
下一篇2023-05-01

发表评论

登录后才能评论

评论列表(0条)

    保存