You are on page 1of 7

import java.awt.event.

KeyEvent;
import java.awt.Font;
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", Font.ITALIC, 12);
static Font endGame = new Font("Calibri", Font.ITALIC, 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.
//StdDraw.picture(1,1,"snakebg.jpg");
StdDraw.clear(StdDraw.BLACK);
StdDraw.setXscale(0, WIDTH);
StdDraw.setYscale(0, HEIGHT);
StdDraw.setPenColor(StdDraw.WHITE);
// Create border around black screen.
StdDraw.square(MIDDLE, MIDDLE, MIDDLE);
menuSection();
}
public void menuSection()
{
StdDraw.clear(StdDraw.BLACK);
boolean selected = false;
int option = 0;

while (!selected)
{
// Draw menu for splash screen.
StdDraw.text(250, 250, "1. Start");
StdDraw.text(250, 220, "2. Instructions");
StdDraw.text(250, 190, "3. Exit");
StdDraw.show();
// Take in input for splash screen menu.
if (StdDraw.isKeyPressed(KeyEvent.VK_S) ||
StdDraw.isKeyPressed(KeyEvent.VK_1))
{
option = 1;
selected = true;
}
else if (StdDraw.isKeyPressed(KeyEvent.VK_I)||
StdDraw.isKeyPressed(KeyEvent.VK_2))
{
option = 2;
selected = true;
}
else if (StdDraw.isKeyPressed(KeyEvent.VK_E)||
StdDraw.isKeyPressed(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()
{
StdDraw.clear(StdDraw.BLACK);
boolean selected = false;
while (!selected)

{
StdDraw.picture(150, 150, "Instructions.jpg");
}
menuSection();
}
// Display exit screen.
public void exitScreen()
{
}
public void gameStart()
{
// Clear screen.
StdDraw.clear(StdDraw.WHITE);
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.
StdDraw.show(1000 /25);
StdDraw.clear(StdDraw.WHITE);
checks();
}
}
// Method to update screen after each move.
public void drawOrange()
{
StdDraw.setFont(c);
// Draw oranges on board.
for (int i = 0;i<orangeNum; i++)
{
StdDraw.picture(oranges[i].getX(),
oranges[i].getY(),"orange.jpg"); //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)
{
StdDraw.picture(x[i], y[i], "snake.jpg", 10, 10);
}
else
{
StdDraw.picture(x[i], y[i], "snake.jpg", 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 (System.currentTimeMillis() >= 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 (StdDraw.isKeyPressed(down))
{
dirDown = true;
dirUp
= false;
dirRight = false;
dirLeft = false;
}
if (StdDraw.isKeyPressed(up))
{
dirUp = true;
dirDown = false;
dirRight = false;
dirLeft = false;
}
if (StdDraw.isKeyPressed(right))

{
dirRight = true;
dirLeft=false;
dirDown = false;
dirUp = false;
}
if (StdDraw.isKeyPressed(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()
{
StdDraw.clear(StdDraw.WHITE);
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setFont(endGame);
if (score >= 1)
{
StdDraw.text(250, 220, "YOU WIN!");
StdDraw.text(250, 170, "Score: " + score);
StdDraw.show();
}
else

{
StdDraw.text(250, 250, "ouch..! Game Over");
StdDraw.text(250, 190, "Score: " + score);
StdDraw.show();
}
}
public static void main(String[] args)
{
new Snake();
}
}

You might also like