Advertisement
Guest User

Untitled

a guest
Mar 21st, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.31 KB | None | 0 0
  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class SecondControl extends Applet implements Runnable, KeyListener {
  6.  
  7.  int key;
  8.  boolean gravity = true;
  9.  boolean bounce = true;
  10.  double x;
  11.  double y;
  12.  double xSpeed;
  13.  double ySpeed;
  14.  
  15.  boolean keyLock = false;
  16.  boolean going = false;
  17.  
  18.  static int WIDTH = 1000;
  19.  static int HEIGHT = 600;
  20.  static int DELAY = 10;
  21.  static int SIZE = 20;
  22.  static double FRICTION = .01;
  23.  static double SPEED_CHANGE = 2.0;
  24.  static double BOUNCE_DAMPENER = .3;
  25.  static double GRAVITY = .14;
  26.  
  27.  Thread t;
  28.  
  29.  Image img;
  30.  Graphics gra;
  31.  
  32.  public void init() {
  33.   addKeyListener(this);
  34.  
  35.   img = createImage(WIDTH, HEIGHT);
  36.   gra = img.getGraphics();
  37.  
  38.   t = new Thread(this);
  39.   t.start();
  40.   going = true;
  41.  
  42.  }
  43.  public void run() {
  44.   while(true) {
  45.    keyLock = true;
  46.    x+=xSpeed;
  47.    y+=ySpeed;
  48.  
  49. //gravity accelerates downwards
  50.    if(gravity) {
  51.     ySpeed+=GRAVITY;
  52.    }
  53.  
  54. //friction slowly decelerates the ball
  55.    if(xSpeed < -1* FRICTION){
  56.     xSpeed+=FRICTION;
  57.    } else if(xSpeed > FRICTION) {
  58.     xSpeed-=FRICTION;
  59.    } else {
  60.     xSpeed = 0;
  61.    }
  62.    if(ySpeed < -1* FRICTION){
  63.     ySpeed+=FRICTION;
  64.    } else if(ySpeed > FRICTION) {
  65.     ySpeed-=FRICTION;
  66.    } else {
  67.     ySpeed = 0;
  68.    }
  69.  
  70. //wall behavior
  71.    if(x<0) {
  72.     if(bounce) {
  73.      x = 0;
  74.      xSpeed = (float)(-1+BOUNCE_DAMPENER)*xSpeed;
  75.     } else {
  76.      x = WIDTH-SIZE;
  77.      xSpeed = (float)(1-BOUNCE_DAMPENER)*xSpeed;
  78.     }
  79.    } else if(x>(WIDTH-SIZE)) {
  80.     if(bounce) {
  81.      x = WIDTH-SIZE;
  82.      xSpeed = (float)(-1+BOUNCE_DAMPENER)*xSpeed;
  83.     } else {
  84.      x = 0;
  85.      xSpeed = (float)(1-BOUNCE_DAMPENER)*xSpeed;
  86.     }
  87.    }
  88.    if(y<0) {
  89.     if(bounce) {
  90.      y = 0;
  91.      ySpeed = (float)(-1+BOUNCE_DAMPENER)*ySpeed;
  92.     } else {
  93.      y = HEIGHT-SIZE;
  94.      ySpeed = (float)(1-BOUNCE_DAMPENER)*ySpeed;
  95.     }
  96.    } else if(y>(HEIGHT-SIZE)) {
  97.     if(bounce) {
  98.      y = HEIGHT-SIZE;
  99.      ySpeed = (float)(-1+BOUNCE_DAMPENER)*ySpeed;
  100.     } else {
  101.      y = 0;
  102.      ySpeed = (float)(1-BOUNCE_DAMPENER)*ySpeed;
  103.     }
  104.    }
  105.    keyLock = false;
  106.    repaint();
  107.    try {
  108.     t.sleep(DELAY);
  109.    } catch(InterruptedException exc) { ; }
  110.   }
  111.  }
  112.  public void update(Graphics g) {
  113.   paint(g);
  114.  }
  115.  public void paint(Graphics g) {
  116.   gra.setColor(Color.white);
  117.   gra.fillRect(0,0,WIDTH, HEIGHT);
  118.  
  119.   gra.setColor(Color.blue);
  120.  
  121.   gra.fillOval((int)Math.floor(x),(int)Math.floor(y),SIZE,SIZE);
  122.   g.drawImage(img, 0, 0, this);
  123.  }
  124.  public void keyPressed(KeyEvent evt) {
  125.   key = evt.getKeyCode();
  126.   if(keyLock == false) {
  127.    switch(key){
  128.     case KeyEvent.VK_DOWN: {
  129.      ySpeed+=SPEED_CHANGE;
  130.      break;
  131.     }
  132.     case KeyEvent.VK_UP: {
  133.      ySpeed-=SPEED_CHANGE;
  134.      break;
  135.     }
  136.     case KeyEvent.VK_RIGHT: {
  137.      xSpeed+=SPEED_CHANGE;
  138.      break;
  139.     }
  140.     case KeyEvent.VK_LEFT: {
  141.      xSpeed-=SPEED_CHANGE;
  142.      break;
  143.     }
  144.     case KeyEvent.VK_G: {
  145.      gravity=!gravity;
  146.      break;
  147.     }
  148.     case KeyEvent.VK_P: {
  149.         if(going) {
  150.             t.suspend();
  151.             going = false;
  152.         } else {
  153.             t.resume();
  154.             going = true;
  155.         }
  156.         break;
  157.     }
  158.     case KeyEvent.VK_B: {
  159.      bounce=!bounce;
  160.      break;
  161.     }
  162.    }
  163.   }
  164.  }
  165.  public void keyReleased(KeyEvent evt) { ; }
  166.  public void keyTyped(KeyEvent evt) { ; }
  167.  
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement