package Snake;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Random;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.ImageIcon;
public class Board extends JPanel {
protected final int BOARDS_WIDTH = 160;
protected final int BOARDS_HEIGHT = 160;
private final int INITIAL_JOINTS = 3;
private final int JOINTS_LIMIT = 50;
private final int UNIT = 16;
private final int DELAY = 120;
private Timer timer;
private Image head, body, apple;
private int[] x = new int[JOINTS_LIMIT];
private int[] y = new int[JOINTS_LIMIT];
private int joints, applesX, applesY, score;
private boolean left, right, up, down;
private boolean inGame = true;
public Board() {
ImageIcon ii1 = new ImageIcon( getClass().getResource( "color_wheel.png" ) );
head = ii1.getImage();
ImageIcon ii2 = new ImageIcon( getClass().getResource( "contrast.png" ) );
body = ii2.getImage();
ImageIcon ii3 = new ImageIcon( getClass().getResource( "pill.png" ) );
apple = ii3.getImage();
setPreferredSize( new Dimension( BOARDS_WIDTH, BOARDS_HEIGHT ) );
setBackground( Color.DARK_GRAY );
setDoubleBuffered( true );
setFocusable( true );
addKeyListener( new Adapter() );
initGame();
timer = new Timer();
timer.scheduleAtFixedRate( new ScheduleTask(), 0, DELAY );
}
private void initGame() {
joints = INITIAL_JOINTS;
x[0] = ( BOARDS_WIDTH / 2 );
y[0] = ( BOARDS_HEIGHT / 2 );
for ( int i = 1; i < joints; ++i ) {
x[i] = x[i - 1] + UNIT;
y[i] = y[0];
}
locateApple();
}
private void locateApple() {
Random random = new Random();
applesX = random.nextInt( UNIT * ( BOARDS_WIDTH / UNIT ) ) - 1;
applesY = random.nextInt( UNIT * ( BOARDS_HEIGHT / UNIT ) ) - 1;
if( ( applesX % UNIT != 0 ) || ( applesY % UNIT != 0 ) ) {
locateApple();
}
}
private void checkApple() {
if ( ( x[0] == applesX ) && ( y[0] == applesY ) ) {
++joints;
locateApple();
}
}
private void checkCollision() {
if( x[0] < 0 || x[0] > BOARDS_WIDTH || y[0] < 0 || y[0] > BOARDS_HEIGHT ) {
inGame = false;
}
}
private void move() {
for ( int i = joints; i > 0; --i ) {
x[i] = x[i - 1];
y[i] = y[i - 1];
}
if ( left ) {
x[0] -= UNIT;
}
if ( right ) {
x[0] += UNIT;
}
if ( up ) {
y[0] -= UNIT;
}
if ( down ) {
y[0] += UNIT;
}
}
@Override
public void paintComponent( Graphics graphics ) {
super.paintComponent( graphics );
if ( inGame ) {
graphics.drawImage( apple, applesX, applesY, this );
for ( int i = 0; i < joints; ++i ) {
if ( i == 0 ) {
graphics.drawImage( head, x[i], y[i], this );
} else {
graphics.drawImage( body, x[i], y[i], this );
}
}
} else {
score = ( joints - INITIAL_JOINTS ) * 10;
graphics.setColor( Color.WHITE );
graphics.setFont( new Font( "Helvetica", Font.PLAIN, 11 ) );
graphics.drawString( "Game over! Score: " + score + " points.", 10, 20 );
}
graphics.dispose();
}
private class ScheduleTask extends TimerTask {
@Override
public void run() {
checkApple();
checkCollision();
move();
repaint();
}
}
private class Adapter extends KeyAdapter {
@Override
public void keyPressed( KeyEvent keyEvent ) {
int key = keyEvent.getKeyCode();
if ( ( key == KeyEvent.VK_LEFT ) && ( !right ) ) {
left = true;
up = false;
down = false;
}
if ( ( key == KeyEvent.VK_RIGHT ) && ( !left ) ) {
right = true;
up = false;
down = false;
}
if ( ( key == KeyEvent.VK_UP ) && ( !down ) ) {
up = true;
right = false;
left = false;
}
if ( ( key == KeyEvent.VK_DOWN ) && ( !up ) ) {
down = true;
right = false;
left = false;
}
}
}
}