import java.awt.*;
import javax.swing.*;
public class Snake extends Frame
{
Image offScreenImage;
Graphics offScreenGraphics;
int numOfPieces = 1;
int[] [] snake = new int [200] [2];
int xChange = 0;
int yChange = 0;
int score = 0;
int prizeX = (int) ((Math.random () * 18) + 1) * 20;
int prizeY = (int) ((Math.random () * 16) + 3) * 20;
Menu game = new Menu ("Game");
Menu about = new Menu ("About");
MenuItem newGame = new MenuItem ("New Game");
MenuItem exit = new MenuItem ("Exit");
MenuItem aboutIt = new MenuItem ("About");
MenuBar menu = new MenuBar ();
public Snake ()
{
super ("Snake");
setSize (400, 400);
setResizable (false);
snake [0] [0] = 20;
snake [0] [1] = 60;
game.add (newGame);
game.add (exit);
about.add (aboutIt);
menu.add (game);
menu.add (about);
setMenuBar (menu);
show ();
}
public static void drawGrid (Graphics g)
{
int x = 20;
int y = 60;
for (int count = 0 ; count < 380 ; count += 20)
{
g.drawLine (x + count, y, x + count, 380);
}
for (int count = 0 ; count < 380 ; count += 20)
{
g.drawLine (x, y + count, 380, y + count);
}
}
public static void delay (int time)
{
try
{
Thread.sleep (time);
}
catch (Exception e)
{
}
}
public boolean keyDown (Event evt, int key)
{
char keyChar = (char) key;
if (key == Event.LEFT)
{
xChange = -20;
yChange = 0;
repaint ();
}
if (key == Event.RIGHT)
{
xChange = 20;
yChange = 0;
repaint ();
}
if (key == Event.UP)
{
xChange = 0;
yChange = -20;
repaint ();
}
if (key == Event.DOWN)
{
xChange = 0;
yChange = 20;
repaint ();
}
return true;
}
public boolean action (Event evt, Object obj)
{
if (evt.target == exit)
System.exit (0);
if (evt.target == aboutIt)
JOptionPane.showMessageDialog (null, "About: Snake\nBy: Alex Mikhailov\nDescription: Move around and collect the pieces\nDon't hit the wall or yourself!");
if (evt.target == newGame)
{
numOfPieces = 1;
snake [0] [0] = 20;
snake [0] [1] = 60;
xChange = 0;
yChange = 0;
score = 0;
prizeX = (int) ((Math.random () * 18) + 1) * 20;
prizeY = (int) ((Math.random () * 16) + 3) * 20;
repaint ();
}
return true;
}
public void paint (Graphics g)
{
int oldX = 0;
int oldY = 0;
int holdX = 0;
int holdY = 0;
String scoreString = "Score = " + score;
if (offScreenGraphics == null)
{
offScreenImage = createImage (size ().width, size ().height);
offScreenGraphics = offScreenImage.getGraphics ();
}
for (int count = 1 ; count < numOfPieces ; count++)
{
if (snake [0] [0] == snake [count] [0] && snake [0] [1] == snake [count] [1])
{
JOptionPane.showMessageDialog (null, "You Have Died!");
numOfPieces = 1;
score = 0;
repaint ();
}
}
if (numOfPieces == 1)
{
if (xChange != 0 || yChange != 0)
{
snake [0] [0] += xChange;
snake [0] [1] += yChange;
offScreenGraphics.setColor (Color.black);
drawGrid (offScreenGraphics);
offScreenGraphics.setColor (Color.blue);
offScreenGraphics.fillRect (prizeX, prizeY, 20, 20);
offScreenGraphics.setColor (Color.green);
offScreenGraphics.fillRect (snake [0] [0], snake [0] [1], 20, 20);
g.drawImage (offScreenImage, 0, 0, this);
delay (25);
offScreenGraphics.setColor (Color.white);
offScreenGraphics.fillRect (0, 0, 400, 400);
offScreenGraphics.setColor (Color.black);
offScreenGraphics.drawString (scoreString, 20, 55);
}
}
if (numOfPieces > 1)
{
if (xChange != 0 || yChange != 0)
{
oldX = snake [0] [0];
oldY = snake [0] [1];
snake [0] [0] += xChange;
snake [0] [1] += yChange;
offScreenGraphics.setColor (Color.black);
drawGrid (offScreenGraphics);
offScreenGraphics.setColor (Color.blue);
offScreenGraphics.fillRect (prizeX, prizeY, 20, 20);
offScreenGraphics.setColor (Color.green);
offScreenGraphics.fillRect (snake [0] [0], snake [0] [1], 20, 20);
for (int count = 1 ; count < numOfPieces ; count++)
{
holdX = snake [count] [0];
holdY = snake [count] [1];
snake [count] [0] = oldX;
snake [count] [1] = oldY;
oldX = holdX;
oldY = holdY;
offScreenGraphics.setColor (Color.green);
offScreenGraphics.fillRect (snake [count] [0], snake [count] [1], 20, 20);
}
g.drawImage (offScreenImage, 0, 0, this);
delay (25);
offScreenGraphics.setColor (Color.white);
offScreenGraphics.fillRect (0, 0, 400, 400);
offScreenGraphics.setColor (Color.black);
offScreenGraphics.drawString (scoreString, 20, 55);
}
}
if (snake [0] [0] < 20 || snake [0] [0] > 360 || snake [0] [1] < 60 || snake [0] [1] > 360)
{
JOptionPane.showMessageDialog (null, "You Have Died!");
numOfPieces = 1;
snake [0] [0] = 20;
snake [0] [1] = 60;
xChange = 0;
yChange = 0;
score = 0;
repaint ();
}
if (snake [0] [0] == prizeX && snake [0] [1] == prizeY)
{
numOfPieces++;
score++;
for (int count = 0 ; count < numOfPieces ; count++)
{
if (snake [count] [0] == prizeX && snake [count] [1] == prizeY)
{
prizeX = (int) ((Math.random () * 18) + 1) * 20;
prizeY = (int) ((Math.random () * 16) + 3) * 20;
}
}
}
}
public static void main (String[] args)
{
new Snake ();
}
}
|