求助:一个java小游戏编程实例,主要的界面做出来,但是游戏的中按钮怎么走得方法不知道怎么写。

这个游戏取之于《机械迷城》中的一个mini游戏如图

一共六个按钮,向下三个在上,向上三个在下,游戏结果要向按钮右边所示才算成功,
游戏规则如按钮左边所示,①点击按钮可移向相邻空位(不过向上箭头的只能向上移,向下箭头只能向下移),②点击按钮要隔一个按钮移,必须两个相邻按钮箭头相对,这样一直移动直至右边所示的结果,才算成功。
求这个游戏的方法怎么编写,只要按钮移动的方法,用java编写,
谢谢各位大侠了!!!!如果回答的好,我可以加分,我还有很多分。。。。

第1个回答  2011-12-31
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Game1 extends javax.swing.JFrame {

public Game1() {
initComponents();
}

private void initComponents() {

jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setIcon(new javax.swing.ImageIcon("C:\\Users\\Administrator\\Desktop\\游戏.jpg"));
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setIcon(new javax.swing.ImageIcon("C:\\Users\\Administrator\\Desktop\\游戏.jpg"));
jButton2.setText("jButton2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

jButton3.setIcon(new javax.swing.ImageIcon("C:\\Users\\Administrator\\Desktop\\游戏.jpg"));
jButton3.setText("jButton3");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});

jButton4.setIcon(new javax.swing.ImageIcon("C:\\Users\\Administrator\\Desktop\\游戏2.jpg"));
jButton4.setText("jButton4");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});

jButton5.setIcon(new javax.swing.ImageIcon("C:\\Users\\Administrator\\Desktop\\游戏1.jpg")); // NOI18N
jButton5.setText("jButton5");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});

jButton6.setIcon(new javax.swing.ImageIcon("C:\\Users\\Administrator\\Desktop\\游戏1.jpg")); // NOI18N
jButton6.setText("jButton6");
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});

jButton7.setIcon(new javax.swing.ImageIcon("C:\\Users\\Administrator\\Desktop\\游戏1.jpg")); // NOI18N
jButton7.setText(" ");
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(132, 132, 132)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
.addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
.addComponent(jButton3, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
.addComponent(jButton4, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
.addComponent(jButton5, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
.addComponent(jButton6, 0, 0, Short.MAX_VALUE)
.addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 69, Short.MAX_VALUE))
.addContainerGap(199, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(101, 101, 101)
.addComponent(jButton7)
.addGap(29, 29, 29)
.addComponent(jButton6)
.addGap(30, 30, 30)
.addComponent(jButton5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_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(java.awt.event.ActionEvent evt) {
if(a==0)
{
if(a!=b)
{
if(b==2)
{
jButton2.setIcon(f1);
jButton1.setIcon(f3);
a=2;
b=0;
}
else
{
if(c==2)
{
jButton3.setIcon(f1);
jButton1.setIcon(f3);
c=0;
a=2;
}

}
}
}

}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
if(b==0)
{
if(b!=c)
{
if(c==2)
{
jButton3.setIcon(f1);
jButton2.setIcon(f3);
b=2;
c=0;
}
else
{
if(d==2)
{
jButton4.setIcon(f1);
jButton2.setIcon(f3);
d=0;
b=2;
}
}
}
}
if(b==1)
{

if(a==2)
{
jButton1.setIcon(f2);
jButton2.setIcon(f3);
b=2;
a=1;
}
}
}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
if(c==0)
{
if(c!=d)
{
if(d==2)
{
jButton4.setIcon(f1);
jButton3.setIcon(f3);
c=2;
d=0;
}
else
{
if(e==2)
{
jButton5.setIcon(f1);
jButton3.setIcon(f3);
e=0;
c=2;
}
}
}
}
if(c==1)
{
if(b==0&&a==2)
{
jButton1.setIcon(f2);
jButton3.setIcon(f3);
c=2;
a=1;
}
if(b==2)
{
jButton2.setIcon(f2);
jButton3.setIcon(f3);
c=2;
b=1;
}
}
}

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
if(d==0)
{
if(d!=e)
{
if(e==2)
{
jButton5.setIcon(f1);
jButton4.setIcon(f3);
d=2;
e=0;
}
else
{
if(f==2)
{
jButton6.setIcon(f1);
jButton4.setIcon(f3);
f=0;
d=2;
}
}
}
}
if(d==1)
{
if(c==0&&b==2)
{
jButton2.setIcon(f2);
jButton4.setIcon(f3);
d=2;
b=1;
}
if(c==2)
{
jButton3.setIcon(f2);
jButton4.setIcon(f3);
d=2;
c=1;
}
}
}

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
if(e==0)
{
if(e!=f)
{
if(f==2)
{
jButton6.setIcon(f1);
jButton5.setIcon(f3);
e=2;
f=0;
}
else
{
if(g==2)
{
jButton7.setIcon(f1);
jButton5.setIcon(f3);
g=0;
e=2;
}
}
}
}
if(e==1)
{
if(d==0&&c==2)
{
jButton3.setIcon(f2);
jButton5.setIcon(f3);
e=2;
c=1;
}
if(d==2)
{
jButton4.setIcon(f2);
jButton5.setIcon(f3);
e=2;
d=1;
}
}}
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
if(f==0&&g==2)
{
jButton7.setIcon(f1);
jButton6.setIcon(f3);
f=2;
g=0;
}
if(f==1&&e==2)
{
jButton5.setIcon(f2);
jButton6.setIcon(f3);
f=2;
e=1;
}
if(f==1&&e==0&&d==2)
{
jButton4.setIcon(f2);
jButton6.setIcon(f3);
d=1;
f=2;
}
}

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
if(g==1&&f==2)
{
jButton6.setIcon(f2);
jButton7.setIcon(f3);
g=2;
f=1;
}
if(g==1&&f==0&&e==2)
{
jButton5.setIcon(f2);
jButton7.setIcon(f3);
g=2;
e=1;
}

}

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Game1().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton 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\\游戏1.jpg";
String s2="C:\\Users\\Administrator\\Desktop\\游戏2.jpg";
ImageIcon f1=new ImageIcon("C:\\Users\\Administrator\\Desktop\\游戏.jpg");
ImageIcon f2=new ImageIcon("C:\\Users\\Administrator\\Desktop\\游戏1.jpg");
ImageIcon f3=new ImageIcon("C:\\Users\\Administrator\\Desktop\\游戏2.jpg");

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

非常感谢!
我得慢慢看看,还有能不能把按钮的图片发给我

追答

那按钮图片就是你发的图我用截图工具截下来的图片

追问

哦,那谢谢,不过我自己的思路跟你得有点不一样,看你得有点看不懂。
点击按钮要隔一个按钮移,必须两个相邻按钮箭头相对”的理解是两个箭头相对的按钮后要有空位才可以移动,你得理解是对的,我没有说清楚。
这个步骤,我的思路是怎样判断它相邻的按钮的方向是与之相反的,然后在隔一个跳。我自己写得代码可以给你。

本回答被提问者采纳
第2个回答  2011-12-31
这个好像不太容易啊
第3个回答  2011-12-30
你可以使它摇晃
相似回答