Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need to add the menu for this GAME press Enter to play game and background pictu

ID: 3840866 • Letter: N

Question

Need to add the menu for this GAME press Enter to play game and background picture, please be specific where can i put the code. i asked same question beofre but no one did it, Here is 2 JAVA classes code:

-------------

*****snake.java

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.Timer;
public class Snake implements ActionListener, KeyListener
{

   public static Snake snake;
   public JFrame jframe;
   public RenderPanel renderPanel;
   public Timer timer = new Timer(20, this);
   public ArrayList snakeParts = new ArrayList();
   public static final int UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3, SCALE = 10;
   public int ticks = 0, direction = DOWN, score, tailLength = 10, time;
   public Point head, cherry;
   public Random random;
   public boolean over = false, paused;
   public Dimension dim;

   public Snake()
   {
       dim = Toolkit.getDefaultToolkit().getScreenSize();
       jframe = new JFrame("Snake");
       jframe.setVisible(true);
       jframe.setSize(805, 700);
       jframe.setResizable(false);
       jframe.setLocation(dim.width / 2 - jframe.getWidth() / 2, dim.height / 2 - jframe.getHeight() / 2);
       jframe.add(renderPanel = new RenderPanel());
       jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       jframe.addKeyListener(this);
       startGame();
   }

   public void startGame()
   {
       over = false;
       paused = false;
       time = 0;
       score = 0;
       tailLength = 20;
       ticks = 0;
       direction = DOWN;
       head = new Point(0, -1);
       random = new Random();
       snakeParts.clear();
       cherry = new Point(random.nextInt(79), random.nextInt(66));
       timer.start();
   }

   @Override
   public void actionPerformed(ActionEvent arg0)
   {
       renderPanel.repaint();
       ticks++;

       if (ticks % 2 == 0 && head != null && !over && !paused)
       {
           time++;

           snakeParts.add(new Point(head.x, head.y));

           if (direction == UP)
           {
               if (head.y - 1 >= 0 && noTailAt(head.x, head.y - 1))
               {
                   head = new Point(head.x, head.y - 1);
               }
               else
               {
                   over = true;

               }
           }

           if (direction == DOWN)
           {
               if (head.y + 1 < 67 && noTailAt(head.x, head.y + 1))
               {
                   head = new Point(head.x, head.y + 1);
               }
               else
               {
                   over = true;
               }
           }

           if (direction == LEFT)
           {
               if (head.x - 1 >= 0 && noTailAt(head.x - 1, head.y))
               {
                   head = new Point(head.x - 1, head.y);
               }
               else
               {
                   over = true;
               }
           }

           if (direction == RIGHT)
           {
               if (head.x + 1 < 80 && noTailAt(head.x + 1, head.y))
               {
                   head = new Point(head.x + 1, head.y);
               }
               else
               {
                   over = true;
               }
           }

           if (snakeParts.size() > tailLength)
           {
               snakeParts.remove(0);

           }

           if (cherry != null)
           {
               if (head.equals(cherry))
               {
                   score += 10;
                   tailLength++;
                   cherry.setLocation(random.nextInt(79), random.nextInt(66));
               }
           }
       }
   }

   public boolean noTailAt(int x, int y)
   {
       for (Point point : snakeParts)
       {
           if (point.equals(new Point(x, y)))
           {
               return false;
           }
       }
       return true;
   }

   public static void main(String[] args)
   {
       snake = new Snake();
   }

   @Override
   public void keyPressed(KeyEvent e)
   {
       int i = e.getKeyCode();

       if ((i == KeyEvent.VK_A || i == KeyEvent.VK_LEFT) && direction != RIGHT)
       {
           direction = LEFT;
       }

       if ((i == KeyEvent.VK_D || i == KeyEvent.VK_RIGHT) && direction != LEFT)
       {
           direction = RIGHT;
       }

       if ((i == KeyEvent.VK_W || i == KeyEvent.VK_UP) && direction != DOWN)
       {
           direction = UP;
       }

       if ((i == KeyEvent.VK_S || i == KeyEvent.VK_DOWN) && direction != UP)
       {
           direction = DOWN;
       }

       if (i == KeyEvent.VK_SPACE)
       {
           if (over)
           {
               startGame();
           }
           else
           {
               paused = !paused;
           }
       }
   }
  

   @Override
   public void keyReleased(KeyEvent e)
   {
   }

   @Override
   public void keyTyped(KeyEvent e)
   {
   }

}

--------------------------------------------------------------------------------------------------------------------------------------------

***RenderPanel.java

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import javax.swing.JPanel;

@SuppressWarnings("serial")

public class RenderPanel extends JPanel
{

   public static final Color GREEN = new Color(1666073);

   @Override
   protected void paintComponent(Graphics g)
   {
       super.paintComponent(g);
       Snake snake = Snake.snake;
       g.setColor(Color.BLACK);
       g.fillRect(0, 0, 800, 700);
       g.setColor(Color.WHITE);
       for (Point point : snake.snakeParts)
       {
           g.fillRect(point.x * Snake.SCALE, point.y * Snake.SCALE, Snake.SCALE, Snake.SCALE);
       }
      
       g.fillRect(snake.head.x * Snake.SCALE, snake.head.y * Snake.SCALE, Snake.SCALE, Snake.SCALE);
       g.setColor(Color.RED);
       g.fillRect(snake.cherry.x * Snake.SCALE, snake.cherry.y * Snake.SCALE, Snake.SCALE, Snake.SCALE);
       String string = "Score: " + snake.score + ", Length: " + snake.tailLength + ", Time: " + snake.time / 20;
       g.setColor(Color.white);
       g.drawString(string, (int) (getWidth() / 2 - string.length() * 2.5f), 10);
       string = "Game Over!";

       if (snake.over)
       {
           g.drawString(string, (int) (getWidth() / 2 - string.length() * 2.5f), (int) snake.dim.getHeight() / 4);
       }

       string = "Game Paused!";

       if (snake.paused && !snake.over)
       {
           g.drawString(string, (int) (getWidth() / 2 - string.length() * 2.5f), (int) snake.dim.getHeight() / 4);
       }
   }
}

Explanation / Answer

You can add this code by making a different class in the same package or you add the code where you have started the code to be precise

public void startGame()

{

//add the code here

}

The menu is not a big deal it is just an addition to the UI to your game and can be interactive as it increases the intercommunication with the user which is good and will let him explore about the game with more deep understanding.

So coming back to the code I have made one menu code for you to add in the places I have instructed you if it doesn't work for you please comment on the answer so that I would assure you with my best guidance

Snake(){
this.setResizable(false);
Menu = new JMenuBar();
Play = new JMenuItem("Play");
cntrl = new JMenu("cntrl");
res = new JMenuItem("res");
instruct = new JMenuItem("instruct");
res.setMnemonic(KeyEvent.VK_R);
res.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_MASK));
res.setMnemonic(KeyEvent.VK_I);
instruct.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, InputEvent.CTRL_MASK));
Play.setMnemonic(KeyEvent.VK_P);
Play.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.CTRL_MASK));
cntrl.add(Play);
cntrl.add(res);
cntrl.add(instruct);
Menu.add(cntrl);
this.setJMenuBar(Menu);
//GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this);
panel = new snake();
this.setLayout(new BorderLayout());
this.setMinimumSize(new Dimension(800, 595));
panel.dim = this.getSize();
this.add(panel, BorderLayout.CENTER);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Play.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent eee){Play();}});
res.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent ee){res();}});
instruct.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent ee){instruct();}});
this.addKeyListener(new KeyAdapter(){public void keyPressed(KeyEvent e){panel.keyPress(e);}});
this.setVisible(true);
this.pack();
}

Hope this helps you to improve your UI and enjoy the game.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote