Advertisement
Guest User

Breakout ball class

a guest
Jun 27th, 2012
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. package com.breakout.objects;
  2.  
  3. import com.breakout.Breakout;
  4. import com.gej.object.GObject;
  5. import com.gej.util.GUtil;
  6.  
  7. public class Ball extends GObject {
  8.  
  9.     public Ball(float x, float y){
  10.         super(Breakout.BALL, x, y);
  11.     }
  12.    
  13.     public void update(long elapsedTime){
  14.         if (getVelocityX()==0 && getVelocityY()==0){
  15.             //if (GKeyBoard.isPressed(KeyEvent.VK_SPACE)){
  16.                 setVelocityY(-0.02f);
  17.                 int random = GUtil.random(2);
  18.                 switch (random){
  19.                     case 0:
  20.                         setVelocityX(-0.02f);
  21.                         break;
  22.                     case 1:
  23.                         setVelocityX(0.02f);
  24.                         break;
  25.                 }
  26.            // }
  27.         }
  28.     }
  29.    
  30.     public void bounce(GObject other){
  31.         if (getVelocityX()>0){
  32.             setX(other.getX()-getWidth());
  33.         } else if (getVelocityX()<0){
  34.             setX(other.getX()+other.getWidth());
  35.         }
  36.         if (getVelocityY()>0){
  37.             setY(other.getY()-getHeight());
  38.         } else if (getVelocityY()<0){
  39.             setY(other.getY()+other.getHeight());
  40.         }
  41.         bounce();
  42.     }
  43.  
  44.     public void bounce(){
  45.         boolean left = false;
  46.         boolean right = false;
  47.         boolean up = false;
  48.         boolean down = false;
  49.         if (dx < 0) {
  50.             left = true;
  51.         } else if (dx > 0) {
  52.             right = true;
  53.         }
  54.         if (dy < 0) {
  55.             up = true;
  56.         } else if (dy > 0) {
  57.             down = true;
  58.         }
  59.         if (left && up) {
  60.             dx = -dx;
  61.         }
  62.         if (left && down) {
  63.             dy = -dy;
  64.         }
  65.         if (right && up) {
  66.             dx = -dx;
  67.         }
  68.         if (right && down) {
  69.             dy = -dy;
  70.         }
  71.     }
  72.    
  73.     public void collision(GObject other){
  74.         if (other instanceof Bat || other instanceof Block){
  75.             bounce(other);
  76.         } else if (other instanceof Stone){
  77.             other.destroy();
  78.             bounce(other);
  79.         }
  80.     }
  81.    
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement