This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Snake

By: a guest on Dec 28th, 2012  |  syntax: None  |  size: 4.87 KB  |  views: 44  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.applet.*;
  2. import java.awt.event.*;
  3. import java.awt.*;
  4. import javax.swing.*;
  5.  
  6. //we need the applet methods and the MouseMotionListener interface
  7. //(used for the human controlled paddle)
  8.  
  9. public class snakeMain extends Applet implements KeyListener,
  10. ActionListener
  11. {
  12.     // a font used to display the score
  13.     Font newFont = new Font("sansserif", Font.BOLD, 20);
  14.     //The image is going to be double buffered to avoid flicker
  15.     Graphics bufferGraphics;
  16.     //the Image will contain everything drawn on bufferGraphics
  17.     Image offscreen;
  18.     //variables used to set the width and height of the applet
  19.     final int WIDTH = 500, HEIGHT = 500, BORDER = 100, GRIDSIZE = 10, INITIALLENGTH = 3, INITIALSNAKEX = 5, INITIALSNAKEY = 5;
  20.    
  21.     private boolean gameOver = false;
  22.     private int snakeLength = INITIALLENGTH;
  23.     private int snakeX = INITIALSNAKEX, snakeY = INITIALSNAKEY;
  24.     private int snakeUpDown = -1, snakeLeftRight = 0;
  25.     private int frameCounter = 0;
  26.     //declare the array
  27.     int[][] grid = new int[GRIDSIZE][GRIDSIZE];
  28.    
  29.     //I am going to use a timer to do a certain list of tasks
  30.     //every 15 milliseconds (67 FPS)
  31.     Timer time = new Timer(15, this);  
  32.    
  33.     public void init()
  34.     {
  35.         //set the applet to be 500*300
  36.         setSize(WIDTH, HEIGHT);
  37.        
  38.         gameOver = false;
  39.         grid[snakeY][snakeX] = snakeLength;
  40.        
  41.         //mouseMotionListener allows the player to control their paddle
  42.         addKeyListener(this);
  43.         setBackground(Color.black);
  44.                
  45.         //create offscreen image to draw on
  46.         offscreen = createImage(WIDTH, HEIGHT);
  47.         bufferGraphics = offscreen.getGraphics();
  48.        
  49.         //this is the game loop
  50.         time.start();
  51.                      
  52.     }
  53.    
  54.     //every 15 milliseconds the timer triggers the actionPerformed method
  55.     public void actionPerformed(ActionEvent arg0)
  56.     {
  57.         if(gameOver == true)
  58.         {
  59.             //after the game needs to end we stop the timer,
  60.             time.stop();
  61.             repaint();
  62.         }
  63.         else
  64.         {
  65.             frameCounter = frameCounter + 1;
  66.        
  67.             if (frameCounter == 10)
  68.             {
  69.            
  70.                 snakeX = snakeX + snakeLeftRight;
  71.                 snakeY = snakeY + snakeUpDown;
  72.            
  73.                 if (snakeX < 0)
  74.                 {
  75.                     snakeX = GRIDSIZE;
  76.                 }
  77.                
  78.                 if (snakeX > GRIDSIZE)
  79.                 {
  80.                     snakeX = 0;
  81.                 }
  82.                
  83.                 if (snakeY < 0)
  84.                 {
  85.                     snakeY = GRIDSIZE;
  86.                 }
  87.                
  88.                 if (snakeY > GRIDSIZE)
  89.                 {
  90.                     snakeY = 0;
  91.                 }
  92.            
  93.                 for (int i = 0; i <= GRIDSIZE; i++)
  94.                 {
  95.                     for (int j = 0; j <= GRIDSIZE; j++)
  96.                     {
  97.                         if (grid[i][j] > 0)
  98.                         {
  99.                             grid[i][j]--;
  100.                         }
  101.                     }
  102.                 }
  103.            
  104.                 grid[snakeY][snakeX] = snakeLength;
  105.            
  106.                 frameCounter = 0;
  107.                        
  108.             }
  109.             else
  110.             {
  111.            
  112.             }
  113.             //repaints the applet
  114.             repaint();
  115.         }
  116.     }
  117.    
  118.     public void checkCollision()
  119.     {
  120.     }
  121.    
  122.          
  123.     public void paint(Graphics g)
  124.     {
  125.         // first clear off the image
  126.         bufferGraphics.clearRect(0,0,WIDTH,HEIGHT);
  127.        
  128.         //Now draw the snake in white
  129.         bufferGraphics.setColor(Color.white);
  130.        
  131.         //placeholder to test graphics
  132.         bufferGraphics.fillRect(10,10,100,100);
  133.         bufferGraphics.drawString(" " + frameCounter, 150, 15);
  134.        
  135.         for (int i = 0; i <= GRIDSIZE; i++)
  136.           {
  137.               for (int j = 0; j <= GRIDSIZE; j++)
  138.               {
  139.                    if(grid[i][j] > 0)
  140.                    {
  141.                        bufferGraphics.fillRect(((i*10) + BORDER),((j*10) + BORDER),10,10);
  142.                    }
  143.               }
  144.           }
  145.        
  146.         //finally draw the offscreen image to the applet
  147.         g.drawImage(offscreen,0,0,this);
  148.            
  149.         //this line makes sure all the monitors are up to date before proceeding
  150.         Toolkit.getDefaultToolkit().sync();
  151.     }
  152.    
  153.     public void update(Graphics g)
  154.     {
  155.         paint(g);
  156.     }
  157.        
  158.     public void keyPressed(KeyEvent e)
  159.     {
  160.            
  161.     }
  162.        
  163.     public void keyTyped(KeyEvent e)
  164.     {
  165.         //this is placeholder
  166.     }
  167.        
  168.     public void keyReleased(KeyEvent e)
  169.     {
  170.         //this is placeholder
  171.     }
  172.        
  173.        
  174. }
clone this paste RAW Paste Data