Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.25 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3.  
  4. import javax.swing.*;
  5.  
  6. public class Snake extends JApplet implements Runnable{
  7.  
  8.     int x1 = 1, y1 = 0, x2 = 1, y2 = 0, x3 = 1, y3 = 0, x4 = 1, y4 = 0, x5 = 1, y5 = 0;
  9.     boolean goRight = true, goDown = false, goLeft = false, goUp = false;
  10.    
  11.     public void init() {
  12.         // TODO Auto-generated method stub
  13.         super.init();
  14.         x2 = x1 + 20;
  15.         x3 = x2 + 20;
  16.         x4 = x3 + 20;
  17.         x5 = x4 + 20;
  18.         Thread nitka = new Thread(this);
  19.         nitka.start();
  20.     }
  21.    
  22.     public void paint(Graphics g) {
  23.         super.paintComponents(g);
  24.  
  25.         //Go right
  26.         if(y5 <= 0) {
  27.             y5 = 0;
  28.             x5 += 5;
  29.         }
  30.         if(y4 <= 0) {
  31.             y4 = 0;
  32.             x4 += 5;
  33.         }
  34.         if(y3 <= 0) {
  35.             y3 = 0;
  36.             x3 += 5;
  37.         }
  38.         if(y2 <= 0) {
  39.             y2 = 0;
  40.             x2 += 5;
  41.         }
  42.         if(y1 <= 0) {
  43.             y1 = 0;
  44.             x1 += 5;
  45.         }
  46.        
  47.         //Go down
  48.         if(x5 >= getWidth() - 20) {
  49.             x5 = getWidth() - 20;
  50.             y5 += 5;
  51.         }
  52.         if(x4 >= getWidth() - 20) {
  53.             x4 = getWidth() - 20;
  54.             y4 += 5;
  55.         }
  56.         if(x3 >= getWidth() - 20) {
  57.             x3 = getWidth() - 20;
  58.             y3 += 5;
  59.         }
  60.         if(x2 >= getWidth() - 20) {
  61.             x2 = getWidth() - 20;
  62.             y2 += 5;
  63.         }
  64.         if(x1 >= getWidth() - 20) {
  65.             x1 = getWidth() - 20;
  66.             y1 += 5;
  67.         }
  68.        
  69.         //Go left
  70.         if(y5 >= getHeight() - 20) {
  71.             y5 = getWidth() - 20;
  72.             x5 -= 5;
  73.         }
  74.         if(y4 >= getWidth() - 20) {
  75.             y4 = getWidth() - 20;
  76.             x4 -= 5;
  77.         }
  78.         if(y3 >= getWidth() - 20) {
  79.             y3 = getWidth() - 20;
  80.             x3 -= 5;
  81.         }
  82.         if(y2 >= getWidth() - 20) {
  83.             y2 = getWidth() - 20;
  84.             x2 -= 5;
  85.         }
  86.         if(y1 >= getWidth() - 20) {
  87.             y1 = getWidth() - 20;
  88.             x1 -= 5;
  89.         }
  90.        
  91.         //Go up
  92.         if(x5 <= 0) {
  93.             x5 = 0;
  94.             y5 -= 5;
  95.         }
  96.         if(x4 <= 0) {
  97.             x4 = 0;
  98.             y4 -= 5;
  99.         }
  100.         if(x3 <= 0) {
  101.             x3 = 0;
  102.             y3 -= 5;
  103.         }
  104.         if(x2 <= 0) {
  105.             x2 = 0;
  106.             y2 -= 5;
  107.         }
  108.         if(x1 <= 0) {
  109.             x1 = 0;
  110.             y1 -= 5;
  111.         }
  112.        
  113.         g.setColor(Color.blue);
  114.         g.fillRect(x1, y1, 20, 20);
  115.         g.fillRect(x2, y2, 20, 20);
  116.         g.fillRect(x3, y3, 20, 20);
  117.         g.fillRect(x4, y4, 20, 20);
  118.         g.fillRect(x5, y5, 20, 20);
  119.     }
  120.    
  121.     public void run() {
  122.         // TODO Auto-generated method stub
  123.         while(true){           
  124.             repaint();
  125.             try {
  126.                 Thread.sleep(50);
  127.             } catch (InterruptedException e) {
  128.                 // TODO Auto-generated catch block
  129.                 e.printStackTrace();
  130.             }
  131.         }
  132.     }
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement