import [Link].
KeyEvent;
import [Link];
public class Snake
{
static int dots;
static int difficulty;
static int orangeNum;
public Orange[] oranges;
static int score = 0;
static String printScore = "Score: ";
static Font c = new Font("Calibri", [Link], 12);
static Font endGame = new Font("Calibri", [Link], 16);
static int left = KeyEvent.VK_LEFT;
static int up = KeyEvent.VK_UP;
static int down = KeyEvent.VK_DOWN;
static int right = KeyEvent.VK_RIGHT;
static boolean dirRight = true;
static boolean dirLeft = false;
static boolean dirUp = false;
static boolean dirDown = false;
static boolean firstMove = true;
static boolean inGame = true;
static
static
static
static
static
static
static
int
int
int
int
int
WIDTH = 500;
HEIGHT = 500;
DOTSIZE = 5;
NUMBEROFSPACES = 900; //doubt
MIDDLE = 200; //doubt
int[] x = new int[NUMBEROFSPACES];
int[] y = new int[NUMBEROFSPACES];
public Snake()
{
// Create window for Snake game.
//[Link](1,1,"[Link]");
[Link]([Link]);
[Link](0, WIDTH);
[Link](0, HEIGHT);
[Link]([Link]);
// Create border around black screen.
[Link](MIDDLE, MIDDLE, MIDDLE);
menuSection();
}
public void menuSection()
{
[Link]([Link]);
boolean selected = false;
int option = 0;
while (!selected)
{
// Draw menu for splash screen.
[Link](250, 250, "1. Start");
[Link](250, 220, "2. Instructions");
[Link](250, 190, "3. Exit");
[Link]();
// Take in input for splash screen menu.
if ([Link](KeyEvent.VK_S) ||
[Link](KeyEvent.VK_1))
{
option = 1;
selected = true;
}
else if ([Link](KeyEvent.VK_I)||
[Link](KeyEvent.VK_2))
{
option = 2;
selected = true;
}
else if ([Link](KeyEvent.VK_E)||
[Link](KeyEvent.VK_3))
{
option = 3;
selected = true;
}
}
// Go to screen respective to option selected
if (option == 1)
{
startMenu();
}
else if (option == 2)
{
displayInstructions();
}
else if (option == 3)
{
exitScreen();
}
}
// Menu Section
public void startMenu()
{
gameStart();
}
// Instructions to play game
public void displayInstructions()
{
[Link]([Link]);
boolean selected = false;
while (!selected)
{
[Link](150, 150, "[Link]");
}
menuSection();
}
// Display exit screen.
public void exitScreen()
{
}
public void gameStart()
{
// Clear screen.
[Link]([Link]);
dots = 1;
// Put head at the middle of board.
for (int i = 0; i < dots; i++) {
x[i] = MIDDLE; //doubt
y[i] = MIDDLE; //doubt
}
orangeNum = 1;
oranges = new Orange[orangeNum];
// Create oranges.
for (int i = 0; i < orangeNum; i++)
{
oranges[i] = new Orange(difficulty);
}
while (firstMove)
{
// Calculates frames per second.
[Link](1000 /25);
[Link]([Link]);
checks();
}
}
// Method to update screen after each move.
public void drawOrange()
{
[Link](c);
// Draw oranges on board.
for (int i = 0;i<orangeNum; i++)
{
[Link](oranges[i].getX(),
oranges[i].getY(),"[Link]"); //doubt (can we change this line without
using getX and getY
// actually professor didn't discussed about getX and getY in class)
}
// Draw head followed by body.
for (int i = 0; i < dots; i++)
{
if (i == 0)
{
[Link](x[i], y[i], "[Link]", 10, 10);
}
else
{
[Link](x[i], y[i], "[Link]", 10, 10);
}
}
}
// Checks tick through game.
public void checks()
{
checkKey();
move();
drawOrange(); //doubt
checkOrange();
collisionDetect();
// Check to see if orange needs to be replaced.
for (int i = 0; i < orangeNum; i++)
{
if ([Link]() >= oranges[i].getStartTime()+
oranges[i].getEndTime()) //doubt
{
oranges[i] = new Orange(difficulty);
}
}
}
// Check to see if a key was pressed and change direction relative to
key pressed.
public void checkKey()
{
if ([Link](down))
{
dirDown = true;
dirUp
= false;
dirRight = false;
dirLeft = false;
}
if ([Link](up))
{
dirUp = true;
dirDown = false;
dirRight = false;
dirLeft = false;
}
if ([Link](right))
{
dirRight = true;
dirLeft=false;
dirDown = false;
dirUp = false;
}
if ([Link](left))
{
dirLeft = true;
dirRight= false;
dirDown = false;
dirUp = false;
}
}
// Move body in array depending on direction selected.
public void move()
{
for (int i = dots; i > 0; i--)
{
x[i] = x[(i - 1)];
y[i] = y[(i - 1)];
}
if (dirDown && !dirUp)
{
y[0] -= DOTSIZE;
}
else if (dirUp && !dirDown)
{
y[0] += DOTSIZE;
}
else if (dirRight && !dirLeft)
{
x[0] += DOTSIZE;
}
else if (dirLeft && !dirRight)
{
x[0] -= DOTSIZE;
}
}
public void collisionDetect()
{
// Check to see if head overlaps body
for (int i = dots; i > 0; i--)
{
if ((i > 1) && (x[0] == x[i]) && (y[0] == y[i]))
{
inGame = false;
gameOver();
}
}
// See if head hits borders of game board
if (y[0] >= HEIGHT)
{
inGame = false;
gameOver();
}
if (y[0] <= 0)
{
inGame = false;
gameOver();
}
if (x[0] <= 0)
{
inGame = false;
gameOver();
}
if (x[0] >= WIDTH)
{
inGame = false;
gameOver();
}
}
// Check to see if the head hits an orange
public void checkOrange()
{
for (int i = 0; i < orangeNum; i++)
{
if ((x[0] == oranges[i].getX()) &&
(y[0] == oranges[i].getY()))
{
dots++;
score++;
oranges[i] = new Orange(difficulty);
drawOrange();
}
}
}
//Display game over screen
public void gameOver()
{
[Link]([Link]);
[Link]([Link]);
[Link](endGame);
if (score >= 1)
{
[Link](250, 220, "YOU WIN!");
[Link](250, 170, "Score: " + score);
[Link]();
}
else
{
[Link](250, 250, "ouch..! Game Over");
[Link](250, 190, "Score: " + score);
[Link]();
}
}
public static void main(String[] args)
{
new Snake();
}
}