我是大学的一年级学生。大约一个月前,我收到了一份关于Java的任务,要求我创建一个带球的迷宫游戏,然后将球编程以便在迷宫中导航。在我完成整个GUI后,我开始编程球,但我遇到了一个问题。
在我的代码准备好之后,球需要正确移动,但无论我按哪个按钮,它都只向左移动。在我从rightX +=38
猜测这是导致的主要问题的rightY +=38
后,我看到这次改变之后的球根本没有移动。
我将在下面提供我的完整代码(有一些小错误,例如不存在的按钮因为我还没有删除它们)。球的移动代码从public boolean moveLeft1()
开始,一直持续到最后。
public class GMazeBall extends JFrame implements ActionListener{
private JButton button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11,
button12, buttonact, buttongame, button16, buttoncompass, button, buttonsand, button13, buttonLeft,buttonRight,
buttonUp, buttonDown;
private JPanel panel1, panel2, panel3;
private JTextField textoption, textsquare, textdirection, firsttext, secondtext, thirdtext;
private JLabel lsquare, ldirection, loption, l1, label2, ldigital, lspeed;
private ImageIcon gamereset, east, west, north, south, gameact, gamerun, white, sand;
private JSlider slidebar;
private JButton SandRoad;
private JButton SandStone;
private JButton GoldenBall;
ArrayList<JButton> myArray = new ArrayList<JButton>();
ImageIcon South = new ImageIcon("myimg/south.jpg");
ImageIcon West = new ImageIcon("myimg/west.jpg");
ImageIcon East = new ImageIcon("myimg/east.jpg");
ImageIcon North = new ImageIcon("myimg/noth.jpg");
public static void main(String[] args) {
GMazeBall frame = new GMazeBall();
frame.setSize(775, 665);
frame.createGUI();
frame.setVisible(true);
}
private void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(null);
panel1 = new JPanel();
panel1.setBounds(0, 0, 610, 500);
panel1.setBackground(Color.white);
panel1.setLayout(null);
window.add(panel1);
//public void createGoldenBall(int x, int y) {
Border border = BorderFactory.createEmptyBorder();
GoldenBall = new JButton(new ImageIcon("myimg/gold-ball.png"));
GoldenBall.setBounds(570, 0, 38, 38);
GoldenBall.setBorder(border);
GoldenBall.setOpaque(false);
GoldenBall.setContentAreaFilled(false);
GoldenBall.setBorderPainted(false);
panel1.add(GoldenBall);
createSandStone(0, 456);
Border newborder = BorderFactory.createEmptyBorder();
sand = new ImageIcon("myimg/sand.jpg");
for (int i = 0; i < 16; i++) {
buttonsand = new JButton(sand);
buttonsand.setBounds(i * 38, 0, 38, 38);
buttonsand.setBorder(newborder);
panel1.add(buttonsand);
myArray.add(buttonsand);
}
for (int i = 0; i < 16; i++) {
buttonsand = new JButton(sand);
buttonsand.setBounds(i * 38, 114, 38, 38);
buttonsand.setBorder(newborder);
panel1.add(buttonsand);
myArray.add(buttonsand);
}
for (int i = 0; i < 16; i++) {
buttonsand = new JButton(sand);
buttonsand.setBounds(i * 38, 228, 38, 38);
buttonsand.setBorder(newborder);
panel1.add(buttonsand);
myArray.add(buttonsand);
}
for (int i = 0; i < 16; i++) {
buttonsand = new JButton(sand);
buttonsand.setBounds(i * 38, 342, 38, 38);
buttonsand.setBorder(newborder);
panel1.add(buttonsand);
myArray.add(buttonsand);
}
for (int i = 0; i < 16; i++) {
buttonsand = new JButton(sand);
buttonsand.setBounds(i * 38, 456, 38, 38);
buttonsand.setBorder(newborder);
panel1.add(buttonsand);
myArray.add(buttonsand);
}
createSandRoad(38, 38);
createSandRoad(38, 76);
createSandRoad(190, 38);
createSandRoad(190, 76);
createSandRoad(342, 38);
createSandRoad(342, 76);
createSandRoad(76, 152);
createSandRoad(76, 190);
createSandRoad(228, 152);
createSandRoad(228, 190);
createSandRoad(418, 152);
createSandRoad(418, 190);
createSandRoad(38, 266);
createSandRoad(38, 304);
createSandRoad(190, 266);
createSandRoad(190, 304);
createSandRoad(456, 266);
createSandRoad(456, 304);
createSandRoad(76, 380);
createSandRoad(76, 418);
createSandRoad(228, 380);
createSandRoad(228, 418);
white = new ImageIcon("myimg/white32x32.jpg");
for (int y = 0; y < 13; y++) {
for (int x = 0; x < 16; x++) {
button = new JButton(white);
button.setBounds(x * 38, y * 38, 38, 38);
button.setContentAreaFilled(false);
button.setBorder(newborder);
panel1.add(button);
}
}
panel2 = new JPanel();
panel2.setBounds(605, 0, 170, 500);
panel2.setBackground(Color.LIGHT_GRAY);
panel2.setBorder(newborder);
window.add(panel2);
panel3 = new JPanel();
panel3.setBounds(0, 500, 775, 500);
panel3.setBackground(Color.LIGHT_GRAY);
panel3.setBorder(newborder);
window.add(panel3);
loption = new JLabel("option:");
panel2.add(loption);
textoption = new JTextField(5);
loption.setPreferredSize(new Dimension(70, 25));
textoption.setSize(20, 50);
panel2.add(textoption);
lsquare = new JLabel("square:");
panel2.add(lsquare);
textsquare = new JTextField(5);
lsquare.setPreferredSize(new Dimension(70, 25));
textsquare.setSize(20, 50);
panel2.add(textsquare);
ldirection = new JLabel("direction:");
panel2.add(ldirection);
textdirection = new JTextField(5);
ldirection.setPreferredSize(new Dimension(70, 25));
textdirection.setSize(20, 50);
panel2.add(textdirection);
ldigital = new JLabel(" DIGITAL TIMER");
ldigital.setPreferredSize(new Dimension(151, 25));
panel2.add(ldigital);
firsttext = new JTextField("00");
firsttext.setBackground(Color.black);
firsttext.setPreferredSize(new Dimension(35, 20));
panel2.add(firsttext);
l1 = new JLabel(":");
panel2.add(l1);
secondtext = new JTextField("00");
secondtext.setBackground(Color.black);
secondtext.setPreferredSize(new Dimension(35, 20));
panel2.add(secondtext);
label2 = new JLabel(":");
panel2.add(label2);
thirdtext = new JTextField("00");
thirdtext.setBackground(Color.black);
thirdtext.setPreferredSize(new Dimension(35, 20));
panel2.add(thirdtext);
button1 = new JButton("");
button1.setPreferredSize(new Dimension(41, 24));
panel2.add(button1);
buttonUp = new JButton("^");
buttonUp.setPreferredSize(new Dimension(41, 24));
buttonUp.addActionListener(this);
panel2.add(buttonUp);
button3 = new JButton("");
button3.setPreferredSize(new Dimension(41, 24));
panel2.add(button3);
buttonLeft = new JButton("<");
buttonLeft.setPreferredSize(new Dimension(41, 24));
buttonLeft.addActionListener(this);
panel2.add(buttonLeft);
button5 = new JButton("");
button5.setPreferredSize(new Dimension(41, 24));
panel2.add(button5);
buttonRight = new JButton(">");
buttonRight.setPreferredSize(new Dimension(41, 24));
buttonRight.addActionListener(this);
panel2.add(buttonRight);
button7 = new JButton("");
button7.setPreferredSize(new Dimension(41, 24));
panel2.add(button7);
buttonDown = new JButton("v");
buttonDown.setPreferredSize(new Dimension(41, 24));
buttonDown.addActionListener(this);
panel2.add(buttonDown);
button9 = new JButton("");
button9.setPreferredSize(new Dimension(41, 24));
panel2.add(button9);
button10 = new JButton("Opt.1");
panel2.add(button10);
button11 = new JButton("Opt.2");
panel2.add(button11);
button12 = new JButton("Opt.3");
panel2.add(button12);
button13 = new JButton(" Exit ");
panel2.add(button13);
east = new ImageIcon("myimg/north.jpg");
buttoncompass = new JButton(east);
panel2.add(buttoncompass);
buttoncompass.setPreferredSize(new Dimension(85, 80));
gameact = new ImageIcon("myimg/step.png");
buttonact = new JButton(gameact);
buttonact.setPreferredSize(new Dimension(70, 20));
panel3.add(buttonact);
gamerun = new ImageIcon("myimg/run.png");
buttongame = new JButton(gamerun);
buttongame.setPreferredSize(new Dimension(70, 20));
panel3.add(buttongame);
gamereset = new ImageIcon("myimg/reset.png");
button16 = new JButton(gamereset);
button16.setPreferredSize(new Dimension(70, 20));
panel3.add(button16);
lspeed = new JLabel(" speed: ");
panel3.add(lspeed);
slidebar = new JSlider(JSlider.HORIZONTAL, 200, 1000, 1000);
slidebar.setPreferredSize(new Dimension(182, 26));
panel3.add(slidebar);
}
public void createSandRoad(int x, int y) {
Border border = BorderFactory.createEmptyBorder();
SandRoad = new JButton(new ImageIcon("myimg/sand.jpg"));
SandRoad.setBounds(x, y, 38, 38);
SandRoad.setContentAreaFilled(false);
SandRoad.setBorder(border);
panel1.add(SandRoad);
}
public void createSandStone(int x, int y) {
Border border = BorderFactory.createEmptyBorder();
SandStone = new JButton(new ImageIcon("myimg/sandstone.jpg"));
SandStone.setBounds(x, y, 38, 38);
SandStone.setContentAreaFilled(false);
SandStone.setBorder(border);
panel1.add(SandStone);
}
public boolean moveLeft1() {
int leftX = GoldenBall.getX();
int leftY = GoldenBall.getY();
leftX -= 38;
for (int i = 0; i < myArray.size(); i++) {
if (myArray.get(i).getX() == leftX && myArray.get(i).getY() == leftY)
return true;
}
return false;
}
public boolean moveRight1() {
int rightX = GoldenBall.getX();
int rightY = GoldenBall.getY();
rightX += 38;
for (int i = 0; i < myArray.size(); i++) {
if (myArray.get(i).getX() == rightX && myArray.get(i).getY() == rightY)
return true;
}
return false;
}
public boolean moveUp1() {
int upX = GoldenBall.getX();
int upY = GoldenBall.getY();
upY -= 38;
for (int i = 0; i < myArray.size(); i++) {
if (myArray.get(i).getX() == upX && myArray.get(i).getY() == upY)
return true;
}
return false;
}
public boolean moveDown1() {
int downX = GoldenBall.getX();
int downY = GoldenBall.getY();
downY += 38;
for (int i = 0; i < myArray.size(); i++) {
if (myArray.get(i).getX() == downX && myArray.get(i).getY() == downY)
return true;
}
return false;
}
public void moveLeft2() {
int LeftX = GoldenBall.getX();
int LeftY = GoldenBall.getY();
LeftX -= 38;
GoldenBall.setBounds(LeftX, LeftY, 38, 38);
}
public void moveRight2() {
int RightX = GoldenBall.getX();
int RightY = GoldenBall.getY();
RightX += 38;
GoldenBall.setBounds(RightX, RightY, 38, 38);
}
public void moveUp2() {
int UpX = GoldenBall.getX();
int UpY = GoldenBall.getY();
UpY -= 38;
GoldenBall.setBounds(UpX, UpY, 38, 38);
}
public void moveDown2() {
int DownX = GoldenBall.getX();
int DownY = GoldenBall.getY();
DownY += 38;
GoldenBall.setBounds(DownX, DownY, 38, 38);
}
public void actionPerformed(ActionEvent movement) {
if (movement.getSource() == buttonLeft);
//buttoncompass.setIcon(west);
{
if (moveLeft1())
moveLeft2();
}
if (movement.getSource() == buttonRight);
//buttoncompass.setIcon(east);
{
if (moveRight1())
moveRight2();
}
if (movement.getSource() == buttonUp);
//buttoncompass.setIcon(north);
{
if (moveUp1())
moveUp2();
}
if (movement.getSource() == buttonDown);
//buttoncompass.setIcon(south);
{
if (moveDown1())
moveDown2();
}
}
}
答案 0 :(得分:0)
首先,请遵循主要问题的评论中的建议。你的代码是一个混乱(我不是在回顾,我只是说,无论你提出多少帮助,很少有人会这样做。)
如果你想避免只是向左移动&#34;问题,我会怎么做:
我会删除&#34;实现ActionListener&#34;从类中,用lambdas添加监听器。
buttonUp.addActionListener(e->{
System.out.println("I am moving up");
if (moveUp1())
moveUp2();
});
buttonLeft.addActionListener(e->{
System.out.println("I am moving left");
if (moveLeft1())
moveLeft2();
});
buttonRight.addActionListener(e->{
System.out.println("I am moving right.");
if (moveRight1())
moveRight2();
});
buttonDown.addActionListener(e->{
System.out.println("I am moving down");
if (moveDown1())
moveDown2();
});