Kladdy

D:

Apr 22nd, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.84 KB | None | 0 0
  1. import java.applet.Applet;
  2. import java.awt.Graphics;
  3. import java.awt.Graphics2D;
  4. import java.awt.Rectangle;
  5. import java.awt.event.KeyEvent;
  6. import java.awt.event.KeyListener;
  7. import java.util.ArrayList;
  8.  
  9.  
  10. public class Snake extends Applet implements KeyListener {
  11.  
  12.     long timeS = System.currentTimeMillis();
  13.     private Rectangle rect;
  14.     private ArrayList<Integer> keysDown;
  15.     private boolean isUp;
  16.     private boolean isDown;
  17.     private boolean isLeft;
  18.     private boolean isRight;
  19.  
  20.    
  21.     public void init(){    
  22.        
  23.         this.addKeyListener(this);
  24.         keysDown = new ArrayList<Integer>();
  25.         rect = new Rectangle(15, 15, 15, 15);
  26.    
  27.        
  28.         }
  29.    
  30.     public void paint(Graphics g)
  31.     {
  32.         setSize(500, 500);
  33.         Graphics2D g2 = (Graphics2D)g;
  34.        
  35.         g2.fill(rect);
  36.        
  37.     }
  38.  
  39.    
  40.    
  41.     public void timer(){
  42.         long timeS = System.currentTimeMillis();
  43.         while(true)
  44.         {
  45.             if(System.currentTimeMillis()>=timeS + 1000 * 1); //Change this value to however many seconds interval you want.
  46.             {
  47.                 move();
  48.                 timeS = System.currentTimeMillis();
  49.             }
  50.         }
  51.     }
  52.    
  53.    
  54.    
  55.    
  56.    
  57.     public void move() {
  58.         if (this.isUp==true){
  59.             rect.setLocation(rect.x, rect.y + 10);
  60.         }
  61.         if (this.isDown==true){
  62.             rect.setLocation(rect.x, rect.y - 10);
  63.         }
  64.         if (this.isLeft==true){
  65.             rect.setLocation(rect.x - 10, rect.y);
  66.         }
  67.         if (this.isRight==true){
  68.             rect.setLocation(rect.x -10, rect.y);
  69.         }
  70.        
  71.     }
  72.  
  73.     @Override
  74.     public void keyTyped(KeyEvent e) {
  75.         if (!keysDown.contains(e.getKeyCode()))
  76.             keysDown.add(new Integer(e.getKeyCode()));
  77.        
  78.         moveRect();
  79.        
  80.        
  81.        
  82.         /*
  83.         if (e.getKeyCode() == KeyEvent.VK_UP && this.isDown != true)
  84.         {
  85.             this.isUp = true;
  86.             this.isDown = false;
  87.             this.isLeft = false;
  88.             this.isRight = false;
  89.         }
  90.         if (e.getKeyCode() == KeyEvent.VK_DOWN && this.isUp != true)
  91.         {
  92.             this.isUp = false;
  93.             this.isDown = true;
  94.             this.isLeft = false;
  95.             this.isRight = false;
  96.         }
  97.         if (e.getKeyCode() == KeyEvent.VK_LEFT && this.isRight != true)
  98.         {
  99.             this.isUp = false;
  100.             this.isDown = false;
  101.             this.isLeft = true;
  102.             this.isRight = false;
  103.         }
  104.         if (e.getKeyCode() == KeyEvent.VK_RIGHT && this.isLeft != true)
  105.         {
  106.             this.isUp = false;
  107.             this.isDown = false;
  108.             this.isLeft = false;
  109.             this.isRight = true;
  110.         }*/
  111.        
  112.     }
  113.  
  114.     @Override
  115.     public void keyPressed(KeyEvent e) {
  116.         // TODO Auto-generated method stub
  117.        
  118.     }
  119.  
  120.     @Override
  121.     public void keyReleased(KeyEvent e) {
  122.         keysDown.remove(new Integer(e.getKeyCode()));
  123.        
  124.        
  125.     }
  126.    
  127.     public void moveRect()
  128.     {
  129.         int x = rect.x;
  130.         int y = rect.y;
  131.        
  132.         if (keysDown.contains(KeyEvent.VK_UP))
  133.             y -= 15;
  134.        
  135.         if (keysDown.contains(KeyEvent.VK_DOWN))
  136.             y += 15;
  137.        
  138.         if (keysDown.contains(KeyEvent.VK_LEFT))
  139.             x -= 15;
  140.        
  141.         if (keysDown.contains(KeyEvent.VK_RIGHT))
  142.             x += 15;
  143.        
  144.         rect.setLocation(x,y);
  145.        
  146.         repaint();
  147.    
  148.        
  149.     }
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment