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

Need to add the menu with \"Start\" for Snake Game, Here is 2 classes code: ----

ID: 3838247 • Letter: N

Question

Need to add the menu with "Start" for Snake Game, Here is 2 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

#include <iostream>
#include <conio.h>
void run();
void printMap();
void initMap();
void move(int dx, int dy);
void update();
void changeDirection(char key);
void clearScreen();
void generateFood();
char getMapValue(int value);
const int mapwidth = 10;
const int mapheight = 10;
const int size = mapwidth * mapheight;
int map[size];
int headxpos;
int headypos;
int direction;
int food = 3;
bool running;
int main()
{
run();
return 0;
}
void run()
{
initMap();
running = true;
while (running) {
if (kbhit())
changeDirection(getch());
}
}
std::cout << " !!!Game over!" << std::endl << " Your score is: " << food;
std::cin.ignore();
}
void changeDirection(char key) {
switch (key) {
case 'w':
if (direction != 2) direction = 0;
break;
case 'd':
if (direction != 3) direction = 1;
break;
case 's':
if (direction != 4) direction = 2;
break;
case 'a':
if (direction != 5) direction = 3;
break;
}
}
void move(int dx, int dy) {
int newx = headxpos + dx;
int newy = headypos + dy;
if (map[newx + newy * mapwidth] == -2) {
food++;
generateFood();
}
else if (map[newx + newy * mapwidth] != 0) {
running = false;
}
headxpos = newx;
headypos = newy;
map[headxpos + headypos * mapwidth] = food + 1;

}
void clearScreen() {
system("cls");
}
void generateFood() {
int x = 0;
int y = 0;
do {
x = rand() % (mapwidth - 2) + 1;
y = rand() % (mapheight - 2) + 1;
} while (map[x + y * mapwidth] != 0);
map[x + y * mapwidth] = -2;
}
void update() {
// Move in direction indicated
switch (direction) {
case 0: move(-1, 0);
break;
case 1: move(0, 1);
break;
case 2: move(1, 0);
break;
case 3: move(0, -1);
break;
}
for (int i = 0; i < size; i++) {
if (map[i] > 0) map[i]--;
}
}
void initMap()
{
headxpos = mapwidth / 2;
headypos = mapheight / 2;
map[headxpos + headypos * mapwidth] = 1;
for (int x = 0; x < mapwidth; ++x) {
map[x] = -1;
map[x + (mapheight - 1) * mapwidth] = -1;
}
for (int y = 0; y < mapheight; y++) {
map[0 + y * mapwidth] = -1;
map[(mapwidth - 1) + y * mapwidth] = -1;
}
generateFood();
}
void printMap()
{
for (int x = 0; x < mapwidth; ++x) {
for (int y = 0; y < mapheight; ++y) {
std::cout << getMapValue(map[x + y * mapwidth]);
}
std::cout << std::endl;
}
}
char getMapValue(int value)
{
if (value > 0) return 'o';

switch (value) {
case -1: return 'X';
case -2: return 'O';
}
}

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