Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.awt.*;
- import javax.swing.*;
- public class Snake extends JPanel implements KeyListener,ActionListener{
- int[][] grid;
- int direction;
- int snakeSize;
- int history;
- int head_x;
- int head_y;
- int food_x;
- int food_y;
- boolean gameover;
- Timer t;
- Points p;
- public Snake(int width, int height) {
- grid=new int[width][height];
- setSize(width*25,height*25);
- p=new Points(this);
- add(p);
- gameover=true;
- }
- public void start() {
- gameover=false;
- p=new Points(this);
- head_x=0;
- head_y=0;
- snakeSize=1;
- direction=1;
- history=1;
- for(int i=0;i<grid.length;i++)
- for(int j=0;j<grid[i].length;j++)
- grid[i][j]=0;
- grid[0][0]=1;
- food_x=grid.length/2;
- food_y=grid[0].length/2;
- t=new Timer(250,this);
- t.start();
- }
- public boolean snakeMove() {
- switch(direction) {
- case 0:
- if(head_y-1<0||grid[head_x][head_y-1]>history-snakeSize)
- return false;
- else {
- head_y--;
- history++;
- if(head_x==food_x&&head_y==food_y)
- eat();
- grid[head_x][head_y]=history;
- return true;
- }
- case 1:
- if(head_x+1>=grid.length||grid[head_x+1][head_y]>history-snakeSize)
- return false;
- else {
- head_x++;
- history++;
- if(head_x==food_x&&head_y==food_y)
- eat();
- grid[head_x][head_y]=history;
- return true;
- }
- case 2:
- if(head_y+1>=grid[head_x].length||grid[head_x][head_y+1]>history-snakeSize)
- return false;
- else {
- head_y++;
- history++;
- if(head_x==food_x&&head_y==food_y)
- eat();
- grid[head_x][head_y]=history;
- return true;
- }
- case 3:
- if(head_x-1<0||grid[head_x-1][head_y]>history-snakeSize)
- return false;
- else {
- head_x--;
- history++;
- if(head_x==food_x&&head_y==food_y)
- eat();
- grid[head_x][head_y]=history;
- return true;
- }
- default:
- return false;
- }
- }
- public void eat() {
- snakeSize++;
- int rand_x = (int)(Math.random()*grid.length);
- int rand_y = (int)(Math.random()*grid[0].length);
- while(grid[rand_x][rand_y]>history-snakeSize) {
- rand_x = (int)(Math.random()*grid.length);
- rand_y = (int)(Math.random()*grid[0].length);
- }
- food_x=rand_x;
- food_y=rand_y;
- if(t.getDelay()>=70)
- t.setDelay(t.getDelay()-5);
- }
- public void tick() {
- if(!snakeMove()) {
- t.stop();
- gameover=true;
- }
- repaint();
- }
- public void pauseToggle() {
- if(t.isRunning())
- t.stop();
- else if(!gameover)
- t.start();
- }
- public static void main(String[] args) {
- //System.out.println("Go.");
- JFrame frame = new JFrame("Snake");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- Snake s = new Snake(25,25);
- frame.addKeyListener(s);
- frame.setLayout(null);
- frame.setContentPane(s);
- frame.setUndecorated(true);
- frame.setSize(s.getSize().width,s.getSize().height);
- frame.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width/2-frame.getWidth()/2,Toolkit.getDefaultToolkit().getScreenSize().height/2-frame.getHeight()/2);
- JLabel info = new JLabel("<html>SPACE to pause<br>ESC to reset</html>");
- info.setFont(new Font("Arial",Font.PLAIN,10));
- info.setForeground(Color.gray);
- info.setBounds(frame.getWidth()-85,0,100,30);
- s.add(info);
- s.setLayout(null);
- frame.setVisible(true);
- s.start();
- }
- public void paintComponent(Graphics g) {
- for(int x=0;x<grid[0].length;x++)
- for(int y=0;y<grid[x].length;y++) {
- if(x==food_x&&y==food_y)
- g.setColor(Color.red);
- else if(grid[x][y]>history-snakeSize) {
- if(x==head_x&&y==head_y)
- g.setColor(Color.green);
- else
- g.setColor(Color.blue);
- }
- else
- g.setColor(Color.black);
- g.fillRect(x*25,y*25,25,25);
- }
- p.paintComponent(g);
- //g.setColor(Color.yellow);
- //g.fillRect(100,100,100,100);
- }
- //public void paintSquare(Graphics g, int x, int y) {
- // g.fillRect(x,y,10,10);//x+10,y+10);
- //}
- @Override
- public void keyPressed(KeyEvent k) {
- /*
- if(Character.toLowerCase(k.getKeyChar())=='w')
- direction=0;
- if(Character.toLowerCase(k.getKeyChar())=='d')
- direction=1;
- if(Character.toLowerCase(k.getKeyChar())=='s')
- direction=2;
- if(Character.toLowerCase(k.getKeyChar())=='a')
- direction=3;
- */
- if(k.getKeyCode()==KeyEvent.VK_LEFT||k.getKeyCode()==KeyEvent.VK_A)
- if(dirChange(3))direction=3;
- if(k.getKeyCode()==KeyEvent.VK_DOWN||k.getKeyCode()==KeyEvent.VK_S)
- if(dirChange(2))direction=2;
- if(k.getKeyCode()==KeyEvent.VK_RIGHT||k.getKeyCode()==KeyEvent.VK_D)
- if(dirChange(1))direction=1;
- if(k.getKeyCode()==KeyEvent.VK_UP||k.getKeyCode()==KeyEvent.VK_W)
- if(dirChange(0))direction=0;
- if(k.getKeyCode()==KeyEvent.VK_SPACE)
- pauseToggle();
- if(k.getKeyCode()==KeyEvent.VK_ESCAPE) {
- t.stop();
- start();
- }
- }
- public void keyReleased(KeyEvent k) {
- // TODO Auto-generated method stub
- }
- public void keyTyped(KeyEvent k) {
- /*
- //System.out.println("Key typed: "+k);
- if(Character.toLowerCase(k.getKeyChar())=='w') {
- //System.out.print("UP ");
- if(dirChange(0))direction=0;
- //System.out.println(direction);
- }
- if(Character.toLowerCase(k.getKeyChar())=='d'){
- //System.out.print("RIGHT ");
- if(dirChange(1))direction=1;
- //System.out.println(direction);
- }
- if(Character.toLowerCase(k.getKeyChar())=='s'){
- //System.out.print("DOWN ");
- if(dirChange(2))direction=2;
- //System.out.println(direction);
- }
- if(Character.toLowerCase(k.getKeyChar())=='a'){
- //System.out.print("LEFT ");
- if(dirChange(3))direction=3;
- //System.out.println(direction);
- }
- */
- }
- public boolean dirChange(int newdir) {
- switch(newdir) {
- case 0:
- if(head_y-1<0) //allow failure into wall
- return true;
- if(grid[head_x][head_y-1]==history-1)
- return false;
- return true;
- case 1:
- if(head_x+1==grid.length) //allow failure into wall
- return true;
- if(grid[head_x+1][head_y]==history-1)
- return false;
- return true;
- case 2:
- if(head_y+1==grid[0].length) //allow failure into wall
- return true;
- if(grid[head_x][head_y+1]==history-1)
- return false;
- return true;
- case 3:
- if(head_x-1<0) //allow failure into wall
- return true;
- if(grid[head_x-1][head_y]==history-1)
- return false;
- return true;
- }
- return false;
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- tick();
- }
- }
- class Points extends JLabel{
- Snake s;
- Points(Snake snake) {
- s=snake;
- setText("0");
- }
- public void paintComponent(Graphics g) {
- setText(String.valueOf(s.snakeSize*10-10));
- g.setColor(Color.white);
- g.drawString(getText(),5,15);
- }
- }
Add Comment
Please, Sign In to add comment