Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have downloaded a snake game project from the internet, initially this project contains three java files namely

1. Engine.java 
2. Snake.java 
3. Gameoard.java


when i runs Engine.java, game starts running .

Now i have done some modification in it to improve the interactivity of the game, i.e i have added two JDialog named
1. PlayGame.java

2.RulesDialog.java


PlayGame.java has three JButtons :- Play button, Rules Button , Exit


-when play button gets clicked game should start

-when rules button gets clicked RulesDialog.java JDialog appears and 

-when exit button gets clicked application or game should be closed.


Action of button is set in the
actionPerformed()
method of individual buttons.

Now problem i am facing is when i <pre lang="text">click Play button game window is visible on the screen but snake is not moving


and i am not getting to point how to resolve this problem ..

So plz help me out
Posted

1 solution

My recommendation would be to do have actionPerformed call sepperate methods for each button:

Java
public class Demo
      extends JFrame
      implements ActionListener
{

    private JButton first;
    private JButton second;
    private JButton third;
    public Demo() {

        first = new JButton("first");
        first.addActionListener(this);
        calcFrame.add(first);

        second = new JButton("second");
        second.addActionListener(this);
        calcFrame.add(second);

        third = new JButton("third");
        third.addActionListener(this);
        calcFrame.add(third);
    }

    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == first)
        {
            actionFirst();
        } else if(e.getSource() == second)
        {
            actionSecond();
        } else if(e.getSource() == third)
        {
            actionThird();
        }
    }  
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900