package snake;
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.Timer;
import javax.swing.JFrame;
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<Point> snakeParts = new ArrayList<Point>();
public int ticks = 0, direction = DOWN, score, BodyLength = 10;
public static final int UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3, SCALE = 10;
public Point head, food;
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(605, 629);
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;
score = 0;
BodyLength = 2;
ticks = 0;
direction = DOWN;
head = new Point(15, 15);
random = new Random();
snakeParts.clear();
food = new Point(random.nextInt(59), random.nextInt(59));
timer.start();
}
@Override
public void actionPerformed(ActionEvent arg0) {
renderPanel.repaint();
ticks++;
if (ticks % 5 == 0 && head != null && !over && !paused){
snakeParts.add(new Point(head.x, head.y));
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 < 59 && noTailAt(head.x + 1, head.y))
head = new Point(head.x + 1, head.y);
else
over = true;
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 < 59 && noTailAt(head.x, head.y + 1))
head = new Point(head.x, head.y + 1);
else
over = true;
if (snakeParts.size() > BodyLength)
snakeParts.remove(0);
if (food != null) {
if (head.equals(food)){
score += 2;
BodyLength++;
food.setLocation(random.nextInt(59),random.nextInt(59));
}
}
}
}
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_LEFT && direction != RIGHT)
direction = LEFT;
if (i == KeyEvent.VK_RIGHT && direction != LEFT)
direction = RIGHT;
if (i == KeyEvent.VK_UP && direction != DOWN)
direction = UP;
if (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) {
}
}