import javax.swing.*;
import java.util.ArrayList;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
public class Snake extends JPanel implements ActionListener, KeyListener
{
Timer t = new Timer(75, this);
double x =0, y = 0, velx = 0, vely = 0, x1 = 0, y1 = 0;
private static final int FRAME = 480;
private boolean dup,ddown,dright,dleft, go;
private Food food;
private SnakeBody snakeH;
private ArrayList<SnakeBody> snake = new ArrayList<SnakeBody>();
int count = 0;
int distance = 15;
public Snake()
{
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
go = true;
x = 240;
y = 240;
x1 = 15 * (int)(Math.random()* 32);
y1 = 15 * (int)(Math.random()* 32);
food = new Food(x1,y1,15,Color.BLUE);
snake.add(new SnakeBody(x,y,15, Color.RED));
SnakeBody temp = new SnakeBody(0,0,0,Color.red);
snakeH=new SnakeBody(0,0,0,Color.RED);
t.start();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
food.draw(g);
for(SnakeBody s: snake)
{
s.draw(g);
}
for(int i = 0; i < 480; i+=15)
{
g.setColor(Color.BLACK);
g.drawLine(i,FRAME,i,0);
g.drawLine(FRAME,i,0,i);
}
g.drawLine(FRAME,0,FRAME,FRAME);
g.drawLine(0,FRAME,FRAME,FRAME);
g.setColor(Color.BLACK);
g.setFont(new Font("Monospaced",Font.BOLD,24));
g.drawString("Count:" + count, FRAME - 300, 25);
}
public void actionPerformed(ActionEvent e)
{
x += velx;
y += vely;
if(x<0)
{
x = 465;
}
if(x>465)
{
x = 0;
}
if(y<0)
{
y = 465;
}
if(y>465)
{
y = 0;
}
snakeH.setX(x);
snakeH.setY(y);
SnakeBody temp=new SnakeBody(0,0,0,Color.red);
SnakeBody temp1=new SnakeBody(0,0,0,Color.red);
go=true;
collide(snakeH, food);
for(SnakeBody s: snake)
{
if(snake.indexOf(s)==0)
{
snakeH=new SnakeBody(0,0,0,Color.red);
snakeH=s;
}
}
if(go)
{
int i=0;
for(SnakeBody s: snake)
{
if(snake.indexOf(s)>=1)
{
temp1=s;
s.follow(snake.get(i));
temp=temp1;
}
else
{
snakeH = s;
temp=s;
}
i=snake.indexOf(s);
}
}
go=true;
repaint();
}
private void collide(SnakeBody s, Food food)
{
int dx=0,dy=0;
if(dup)
{
dx=0;
dy=-15;
}
else if(ddown)
{
dx=0;
dy=15;
}
else if(dleft)
{
dx = -15;
dy = 0;
}
else if(dright)
{
dx = 15;
dy = 0;
}
double d = distance(s.getX()+dx,s.getY()+dy,food.getX(),food.getY());
if(d < s.getHeight()/2 + food.getHeight()/2)
{
snake.add(0,new SnakeBody(food.getX(),food.getY(),15,Color.red));
int x3 = 15 * (int)(Math.random()* 32);
int y3 = 15 * (int)(Math.random()* 32);
food.jump(x3,y3);
go=false;
count += 250;
}
}
private double distance(double x1, double y1, double x2, double y2)
{
return Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2));
}
public void up()
{
vely = -15;
velx = 0;
dup=true;
ddown=false;
dleft = false;
dright = false;
}
public void down()
{
vely = 15;
velx = 0;
dup=false;
ddown=true;
dleft = false;
dright = false;
}
public void left()
{
velx = -15;
vely = 0;
dup=false;
ddown=false;
dleft = true;
dright = false;
}
public void right()
{
velx = 15;
vely = 0;
dup=false;
ddown=false;
dleft = false;
dright = true;
}
public void keyPressed(KeyEvent e)
{
int code = e.getKeyCode();
if(code == KeyEvent.VK_LEFT)
{
if(velx == 15)
{
up();
}
else if(velx == -15)
{
down();
}
else if(vely == 15)
{
right();
}
else if(vely == -15)
{
left();
}
else
{
left();
}
}
if(code == KeyEvent.VK_RIGHT)
{
if(velx == 15)
{
down();
}
else if(velx == -15)
{
up();
}
else if(vely == 15)
{
left();
}
else if(vely == -15)
{
right();
}
else
{
right();
}
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
}