Advertisement
pseudocreator

BouncingBall

Aug 30th, 2015
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.18 KB | None | 0 0
  1. package cg2d.v7animation;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5.  
  6. @SuppressWarnings("serial")
  7. public class FuckedUpBall extends AnimationPanel {
  8.  
  9.     private static final float SPEED = 100.0f;
  10.     private static final int SIZE = 50;
  11.    
  12.     //pass middle
  13.     private boolean state1 = false;
  14.    
  15.     //end of cycle
  16.     private boolean state2 = false;
  17.    
  18.     private float y;
  19.    
  20.     //ball first goes down
  21.     private int dirY = 1;
  22.    
  23.    
  24.     public FuckedUpBall(){}
  25.    
  26.     @Override
  27.     protected boolean step(double elapsedTime) {
  28.         float delta = (float) (elapsedTime * SPEED);
  29.         y += dirY * delta;
  30.        
  31.        
  32.         if(((y + SIZE) >= getHeight()) && !state2 && (dirY == 1)){
  33.             state2 = true;
  34.             dirY = -1;     
  35.         }else if((y >= getHeight()) && state2 && (dirY == 1)){
  36.             y = 1 - SIZE;      
  37.             dirY = 1;
  38.             state1 = false;
  39.             state2 = false;
  40.         }
  41.        
  42.        
  43.         if((y <= getHeight() / 2) && !state1 && (dirY == -1)){
  44.             dirY = 1;
  45.             state1 = true;
  46.         }
  47.        
  48.         return true;
  49.     }
  50.  
  51.     @Override
  52.     protected void draw(Graphics g) {
  53.         g.setColor(Color.red);
  54.         g.fillOval(getWidth()/2 - SIZE/2, (int) y, SIZE, SIZE);
  55.     }
  56.  
  57.     public static void main(String[] args) {
  58.         new FuckedUpBall().run(500, 300, "Bounce", true);
  59.     }
  60.    
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement