Java 为什么我的simon游戏不起作用?我需要做什么才能起作用
我正在做一个西蒙游戏,我不知道该怎么做。我得到了声音和所有的好东西工作,但至于其他一切,我不知道我在做什么。我需要一些帮助使按钮按正确的顺序工作和闪烁。(评论是失败的尝试)非常感谢您的帮助Java 为什么我的simon游戏不起作用?我需要做什么才能起作用,java,bluej,Java,Bluej,我正在做一个西蒙游戏,我不知道该怎么做。我得到了声音和所有的好东西工作,但至于其他一切,我不知道我在做什么。我需要一些帮助使按钮按正确的顺序工作和闪烁。(评论是失败的尝试)非常感谢您的帮助 import java.io.*; import javax.swing.*; import java.awt.event.*; import java.awt.event.ActionEvent; import java.awt.*; import javax.swing.border.*; import
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.*;
import javax.swing.border.*;
import java.util.Random;
import java.applet.*;
import java.util.ArrayList;
public class Game extends JFrame
{
JButton button1, button2, button3, button4, button5;
JLabel label1, label2, label3;
JPanel panel1, panel2;
File wavFile1 = new File("NewRing1.wav");
File wavFile2 = new File("NewRing2.wav");
File wavFile3 = new File("NewRing3.wav");
File wavFile4 = new File("NewRing4.wav");
AudioClip sound1;
AudioClip sound2;
AudioClip sound3;
AudioClip sound4;
int sequence1;
int[] anArray;
public Game()
{
anArray = new int[1000];
GridLayout grid = new GridLayout(3, 2);
this.getContentPane().setLayout(grid);
Container theContainer = this.getContentPane();
EtchedBorder edge = new EtchedBorder(EtchedBorder.RAISED);
button1 = new JButton();
button1.setForeground(Color.BLACK);
button1.setBackground(Color.RED);
button2 = new JButton();
button2.setForeground(Color.BLACK);
button2.setBackground(Color.BLUE);
button3 = new JButton();
button3.setForeground(Color.BLACK);
button3.setBackground(Color.GREEN);
button4 = new JButton();
button4.setForeground(Color.BLACK);
button4.setBackground(Color.YELLOW);
button5 = new JButton("Begin");
label1 = new JLabel("Score");
label2 = new JLabel("High Score");
label3 = new JLabel("Follow The Pattern");
panel1 = new JPanel();
panel1.add(label1);
panel1.add(label2);
panel2 = new JPanel();
panel2.add(label3);
panel2.add(button5);
button1.setBorder(edge);
button2.setBorder(edge);
button3.setBorder(edge);
button4.setBorder(edge);
label1.setBorder(edge);
label2.setBorder(edge);
panel1.setBorder(edge);
panel2.setBorder(edge);
label3.setBorder(edge);
theContainer.add(panel1);
theContainer.add(panel2);
theContainer.add(button1);
theContainer.add(button2);
theContainer.add(button3);
theContainer.add(button4);
Button1Handler handleButton1 = new Button1Handler();
button1.addActionListener(handleButton1);
Button2Handler handleButton2 = new Button2Handler();
button2.addActionListener(handleButton2);
Button3Handler handleButton3 = new Button3Handler();
button3.addActionListener(handleButton3);
Button4Handler handleButton4 = new Button4Handler();
button4.addActionListener(handleButton4);
Button5Handler handleButton5 = new Button5Handler();
button5.addActionListener(handleButton5);
try{sound1 = Applet.newAudioClip(wavFile1.toURL());}
catch(Exception e){e.printStackTrace();}
try{sound2 = Applet.newAudioClip(wavFile2.toURL());}
catch(Exception e){e.printStackTrace();}
try{sound3 = Applet.newAudioClip(wavFile3.toURL());}
catch(Exception e){e.printStackTrace();}
try{sound4 = Applet.newAudioClip(wavFile4.toURL());}
catch(Exception e){e.printStackTrace();}
setVisible(true);
}
public class Button1Handler implements ActionListener
{
public void actionPerformed(ActionEvent actionEvent)
{
sound1.play();
}
}
public class Button2Handler implements ActionListener
{
public void actionPerformed(ActionEvent actionEvent)
{
sound2.play();
}
}
public class Button3Handler implements ActionListener
{
public void actionPerformed(ActionEvent actionEvent)
{
sound3.play();
}
}
public class Button4Handler implements ActionListener
{
public void actionPerformed(ActionEvent actionEvent)
{
sound4.play();
}
}
/*
public static int[] buttonClicks(int[] anArray)
{
return anArray;
}
*/
public class Button5Handler implements ActionListener
{
public void actionPerformed(ActionEvent actionEvent)
{
for (int i = 1; i <= 159; i++)
{
Random rand = new Random();
int randomNum = rand.nextInt(40) % 4 + 1;
anArray[i] = randomNum;
System.out.println("Element at index: "+ i + " Is: " + anArray[i]);
}
buttonClicks(anArra[3]);
/*
for (int i = 1; i <= 100; i++)
{
Random rand = new Random();
int randomNum = rand.nextInt((4 - 1) + 1) + 1;
if(randomNum == 1)
{
button1.doClick();
sequence1 = 1;
System.out.println(sequence1);
}
else if(randomNum == 2)
{
button2.doClick();
sequence1 = 2;
System.out.println(sequence1);
}
else if(randomNum == 3)
{
button3.doClick();
sequence1 = 3;
System.out.println(sequence1);
}
else
{
button4.doClick();
sequence1 = 4;
System.out.println(sequence1);
}
}
*/
}
}
import java.io.*;
导入javax.swing.*;
导入java.awt.event.*;
导入java.awt.event.ActionEvent;
导入java.awt.*;
导入javax.swing.border.*;
导入java.util.Random;
导入java.applet.*;
导入java.util.ArrayList;
公共类游戏扩展JFrame
{
b按钮1、按钮2、按钮3、按钮4、按钮5;
JLabel标签1、标签2、标签3;
JPanel小组1、小组2;
文件wavFile1=新文件(“NewRing1.wav”);
File wavFile2=新文件(“NewRing2.wav”);
文件wavFile3=新文件(“NewRing3.wav”);
文件wavFile4=新文件(“NewRing4.wav”);
音频剪辑声音1;
音频剪辑声音2;
音频剪辑声音3;
音频剪辑声音4;
int序列1;
国际[]无秩序;
公共游戏()
{
anArray=新整数[1000];
GridLayout grid=新的GridLayout(3,2);
此.getContentPane().setLayout(网格);
容器容器=this.getContentPane();
蚀刻边框边缘=新的蚀刻边框(蚀刻边框。凸起);
button1=新的JButton();
按钮1.设置前景(颜色:黑色);
按钮1.后退背景(颜色:红色);
button2=新的JButton();
按钮2.设置前景(颜色:黑色);
按钮2.背景(颜色:蓝色);
button3=新的JButton();
按钮3.设置前景(颜色:黑色);
按钮3.后退背景(颜色:绿色);
button4=新的JButton();
按钮4.设置前景(颜色:黑色);
按钮4.立根背景(颜色:黄色);
按钮5=新的JButton(“开始”);
标签1=新的JLabel(“分数”);
label2=新的JLabel(“高分”);
label3=新的JLabel(“遵循模式”);
panel1=新的JPanel();
面板1.添加(标签1);
面板1.添加(标签2);
panel2=新的JPanel();
面板2.添加(标签3);
面板2.添加(按钮5);
按钮1.设置顺序(边缘);
按钮2.收进顺序(边缘);
按钮3.收进顺序(边缘);
按钮4.收进顺序(边缘);
标签1.立根顺序(边缘);
标签2.立根顺序(边缘);
镶板1.立根顺序(边缘);
镶板2.立根顺序(边缘);
标签3.立根顺序(边缘);
容器。添加(面板1);
添加(面板2);
容器。添加(按钮1);
容器。添加(按钮2);
容器。添加(按钮3);
container.add(按钮4);
Button1Handler handleButton1=新Button1Handler();
按钮1.addActionListener(把手按钮1);
Button2Handler handleButton2=新Button2Handler();
按钮2.addActionListener(把手按钮2);
按钮3手柄按钮3=新按钮3手柄();
按钮3.addActionListener(手柄按钮3);
按钮4手柄按钮4=新按钮4手柄();
按钮4.addActionListener(把手按钮4);
Button5Handler handleButton5=新的Button5Handler();
按钮5.addActionListener(把手按钮5);
试试{sound1=Applet.newAudioClip(wavFile1.tour());}
catch(异常e){e.printStackTrace();}
试试{sound2=Applet.newAudioClip(wavFile2.tour());}
catch(异常e){e.printStackTrace();}
试试{sound3=Applet.newAudioClip(wavFile3.tour());}
catch(异常e){e.printStackTrace();}
试试{sound4=Applet.newAudioClip(wavFile4.tour());}
catch(异常e){e.printStackTrace();}
setVisible(真);
}
公共类Button1Handler实现ActionListener
{
已执行的公共无效操作(ActionEvent ActionEvent)
{
声音1.播放();
}
}
公共类Button2Handler实现ActionListener
{
已执行的公共无效操作(ActionEvent ActionEvent)
{
声音2.播放();
}
}
公共类Button3Handler实现ActionListener
{
已执行的公共无效操作(ActionEvent ActionEvent)
{
声音3.播放();
}
}
公共类Button4Handler实现ActionListener
{
已执行的公共无效操作(ActionEvent ActionEvent)
{
声音4.播放();
}
}
/*
公共静态int[]按钮链接(int[]数组)
{
返回混乱;
}
*/
公共类Button5Handler实现ActionListener
{
已执行的公共无效操作(ActionEvent ActionEvent)
{
对于(int i=1;i下面是一些让您开始的代码。序列的播放是在一个单独的线程中执行的,因为您需要在两者之间使用延迟。这段代码绝对不是理想的。您应该使用更好的变量名称和重构来尝试创建更好、更封装的游戏模型。也许您可以利用这个机会了解这个问题吗
import java.io.*;
导入javax.swing.*;
导入java.awt.event.*;
导入java.awt.event.ActionEvent;
导入java.awt.*;
导入javax.swing.border.*;
导入java.util.Random;
导入java.applet.*;
导入java.util.logging.Level;
导入java.util.logging.Logger;
公共类游戏扩展JFrame{
b按钮1、按钮2、按钮3、按钮4、按钮5;
JLabel标签1、标签2、标签3;
JPanel小组1、小组2;
文件wavFile1=新文件(“NewRing1.wav”);
File wavFile2=新文件(“NewRing2.wav”);
文件wavFile3=新文件(“NewRing3.wav”);
文件wavFile4=新文件(“NewRing4.wav”);
音频剪辑声音1;
音频剪辑声音2;
音频剪辑声音3;
音频剪辑声音4;
智力水平;
智力得分;
国际[]无秩序;
int currentArrayPosition;
公共游戏(){
GridLayout grid=新的GridLayout(3,2);
此.getContentPane().setLayout(网格);
容器容器=this.getContentPane();
蚀刻边框边缘=新的蚀刻边框(蚀刻边框。凸起);
级别=0;
得分=0;
button1=新的JButton();
按钮1.设置前景(颜色:黑色);
按钮1.后退背景(颜色:红色);
button2=新的JButton();
按钮2.设置前景(颜色:黑色);
按钮2.背景(颜色:蓝色);
button3=新的JButton();
按钮3.设置前景(颜色:黑色);
按钮3.后退背景(颜色:绿色);
button4=新的JButton()
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.*;
import javax.swing.border.*;
import java.util.Random;
import java.applet.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Game extends JFrame {
JButton button1, button2, button3, button4, button5;
JLabel label1, label2, label3;
JPanel panel1, panel2;
File wavFile1 = new File("NewRing1.wav");
File wavFile2 = new File("NewRing2.wav");
File wavFile3 = new File("NewRing3.wav");
File wavFile4 = new File("NewRing4.wav");
AudioClip sound1;
AudioClip sound2;
AudioClip sound3;
AudioClip sound4;
int level;
int score;
int[] anArray;
int currentArrayPosition;
public Game() {
GridLayout grid = new GridLayout(3, 2);
this.getContentPane().setLayout(grid);
Container theContainer = this.getContentPane();
EtchedBorder edge = new EtchedBorder(EtchedBorder.RAISED);
level = 0;
score = 0;
button1 = new JButton();
button1.setForeground(Color.BLACK);
button1.setBackground(Color.RED);
button2 = new JButton();
button2.setForeground(Color.BLACK);
button2.setBackground(Color.BLUE);
button3 = new JButton();
button3.setForeground(Color.BLACK);
button3.setBackground(Color.GREEN);
button4 = new JButton();
button4.setForeground(Color.BLACK);
button4.setBackground(Color.YELLOW);
button5 = new JButton("Begin");
label1 = new JLabel("Score: " + String.valueOf(score));
label2 = new JLabel("High Score");
label3 = new JLabel("Follow The Pattern");
panel1 = new JPanel();
panel1.add(label1);
panel1.add(label2);
panel2 = new JPanel();
panel2.add(label3);
panel2.add(button5);
button1.setBorder(edge);
button2.setBorder(edge);
button3.setBorder(edge);
button4.setBorder(edge);
label1.setBorder(edge);
label2.setBorder(edge);
panel1.setBorder(edge);
panel2.setBorder(edge);
label3.setBorder(edge);
theContainer.add(panel1);
theContainer.add(panel2);
theContainer.add(button1);
theContainer.add(button2);
theContainer.add(button3);
theContainer.add(button4);
Button1Handler handleButton1 = new Button1Handler();
button1.addActionListener(handleButton1);
Button2Handler handleButton2 = new Button2Handler();
button2.addActionListener(handleButton2);
Button3Handler handleButton3 = new Button3Handler();
button3.addActionListener(handleButton3);
Button4Handler handleButton4 = new Button4Handler();
button4.addActionListener(handleButton4);
Button5Handler handleButton5 = new Button5Handler();
button5.addActionListener(handleButton5);
try {
sound1 = Applet.newAudioClip(wavFile1.toURL());
} catch (Exception e) {
e.printStackTrace();
}
try {
sound2 = Applet.newAudioClip(wavFile2.toURL());
} catch (Exception e) {
e.printStackTrace();
}
try {
sound3 = Applet.newAudioClip(wavFile3.toURL());
} catch (Exception e) {
e.printStackTrace();
}
try {
sound4 = Applet.newAudioClip(wavFile4.toURL());
} catch (Exception e) {
e.printStackTrace();
}
setVisible(true);
}
public class Button1Handler implements ActionListener {
public void actionPerformed(ActionEvent actionEvent) {
sound1.play();
buttonClicked(button1);
}
}
public class Button2Handler implements ActionListener {
public void actionPerformed(ActionEvent actionEvent) {
sound2.play();
buttonClicked(button2);
}
}
public class Button3Handler implements ActionListener {
public void actionPerformed(ActionEvent actionEvent) {
sound3.play();
buttonClicked(button3);
}
}
public class Button4Handler implements ActionListener {
public void actionPerformed(ActionEvent actionEvent) {
sound4.play();
buttonClicked(button4);
}
}
private void buttonClicked(JButton clickedButton) {
if (isCorrectButtonClicked(clickedButton)) {
currentArrayPosition++;
addToScore(1);
if (currentArrayPosition == anArray.length) {
playNextSequence();
} else {
}
} else {
JOptionPane.showMessageDialog(this, String.format("Your scored %s points", score));
score = 0;
level = 0;
label1.setText("Score: " + String.valueOf(score));
}
}
private boolean isCorrectButtonClicked(JButton clickedButton) {
int correctValue = anArray[currentArrayPosition];
if (clickedButton.equals(button1)) {
return correctValue == 1;
} else if (clickedButton.equals(button2)) {
return correctValue == 2;
} else if (clickedButton.equals(button3)) {
return correctValue == 3;
} else if (clickedButton.equals(button4)) {
return correctValue == 4;
} else {
return false;
}
}
public class Button5Handler implements ActionListener {
public void actionPerformed(ActionEvent actionEvent) {
playNextSequence();
}
}
private void playNextSequence() {
level++;
currentArrayPosition = 0;
anArray = createSequence(level);
(new Thread(new SequenceButtonClicker())).start();
}
private int[] createSequence(int sequenceLength) {
int[] sequence = new int[sequenceLength];
for (int i = 0; i < sequenceLength; i++) {
Random rand = new Random();
int randomNum = rand.nextInt(40) % 4 + 1;
sequence[i] = randomNum;
}
return sequence;
}
private JButton getButtonFromInt(int sequenceNumber) {
switch (sequenceNumber) {
case 1:
return button1;
case 2:
return button2;
case 3:
return button3;
case 4:
return button4;
default:
return button1;
}
}
private void flashButton(JButton button) throws InterruptedException {
Color originalColor = button.getBackground();
button.setBackground(new Color(255, 255, 255, 200));
Thread.sleep(250);
button.setBackground(originalColor);
}
private void soundButton(JButton button) {
if (button.equals(button1)) {
sound1.play();
} else if (button.equals(button2)) {
sound2.play();
} else if (button.equals(button3)) {
sound3.play();
} else if (button.equals(button4)) {
sound4.play();
}
}
private void addToScore(int newPoints) {
score += newPoints;
label1.setText("Score: " + String.valueOf(score));
}
private class SequenceButtonClicker implements Runnable {
public void run() {
for (int i = 0; i < anArray.length; i++) {
try {
JButton nextButton = getButtonFromInt(anArray[i]);
soundButton(nextButton);
flashButton(nextButton);
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Game.class.getName()).log(Level.SEVERE, "Interrupted", ex);
}
}
}
}
}