import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Toolkit.*;
import java.util.*;
import java.util.Random;
public class SnakeGame extends JPanel
implements KeyListener{
char shape = 'r';
JLabel ScoreLabel;
JPanel panel1;
int RandomBox = 0;
int BoxX = 145;
int BoxY = 280;
int SnakeX = 340;
int SnakeY = 300;
int direction = 0;
int score = 0;
Random randomGenerator = new Random();
public SnakeGame() {
setFocusable(true);
addKeyListener(this);;
}
public static void main(String args[]) {
JFrame frame = new JFrame("Drawing Canvas");
SnakeGame canvas = new SnakeGame();
canvas.makeGUI();
frame.getContentPane().add(canvas);
frame.setSize(700,700);
frame.addWindowListener(new WindowAdapter(){
public void WindowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setVisible(true);
}
public void makeGUI() {
panel1 = new JPanel();
ScoreLabel = new JLabel("Score: " + score);
panel1.add(ScoreLabel);
//add panels
add(panel1);
}
public int BoxXGenerator(int BoxX,Random randomGenerator,int RandomBox) {
RandomBox = randomGenerator.nextInt(5)+1;
if(RandomBox==1) {
BoxX = 340;
}
else if(RandomBox==2) {
BoxX = 135;
}
else if(RandomBox==3) {
BoxX = 505;
}
else if(RandomBox==4) {
BoxX = 520;
}
else if(RandomBox==5) {
BoxX = 375;
}
return BoxX;
}
public int BoxYGenerator(int BoxY,Random randomGenerator,int RandomBox) {
RandomBox = randomGenerator.nextInt(5)+1;
if(RandomBox==1) {
BoxY = 95;
}
else if(RandomBox==2) {
BoxY = 400;
}
else if(RandomBox==3) {
BoxY = 425;
}
else if(RandomBox==4) {
BoxY = 170;
}
else if(RandomBox==5) {
BoxY = 560;
}
return BoxY;
}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_RIGHT) {
direction=1;
}
else if(e.getKeyCode()==KeyEvent.VK_LEFT) {
direction=2;
}
else if(e.getKeyCode()==KeyEvent.VK_UP) {
direction=3;
}
else if(e.getKeyCode()==KeyEvent.VK_DOWN) {
direction=4;
}
shape = e.getKeyChar();
repaint();
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public void delay() {
try{
Thread.sleep(100);
}
catch(Exception e){
}
}
public void paintComponent(Graphics g) {
g.setColor(Color.WHITE);
Dimension d = getSize();
g.fillRect(0,0, d.width, d.height);
// g.translate(x,y);
//each line is 520
g.setColor(Color.BLACK);
//Top Border
g.drawLine(80,80,600,80);
//Right Border
g.drawLine(600,80,600,600);
//Bottom Border
g.drawLine(600,600,80,600);
//Left Border
g.drawLine(80,80,80,600);
// y-max is 600
// y-min is 80
// x-max is 600
// x-min is 80
//Reset position if X/Y goes past border
if(SnakeX<80){
SnakeX=600;
}
if(SnakeX>600){
SnakeX=80;
}
if(SnakeY<80) {
SnakeY=600;
}
if(SnakeY>600) {
SnakeY=80;
}
//Up, down, left, right
if(direction==1) {
SnakeX=SnakeX+5;
repaint();
delay();
}
if(direction==2) {
SnakeX=SnakeX-5;
repaint();
delay();
}
if(direction==3) {
SnakeY=SnakeY-5;
repaint();
delay();
}
if(direction==4) {
SnakeY=SnakeY+5;
repaint();
delay();
}
//Collision detection
if(SnakeX==BoxX&&SnakeY==BoxY) {
BoxX = BoxXGenerator(BoxX,randomGenerator,RandomBox);
BoxY = BoxYGenerator(BoxY,randomGenerator,RandomBox);
score = score + 1;
ScoreLabel.setText("Score: "+score);
}
System.out.println(SnakeX + "," + SnakeY);
g.fillRect(BoxX,BoxY,15,15);
//Snake
g.setColor(Color.RED);
g.fillRect(SnakeX,SnakeY,15,15);
}
}