Follow bouncing ball Game coding

Posted by Unknown on 05:12
public class Bounce extends Applet implements Runnable {
private Thread ticker;
private boolean running = false;
public void run () {
while (running) {
repaint();
public synchronized void start () ['
i f (ticker == null I ~ ! ticker.isAlive()) i
running ° true;
ticker = new Thread(this);

ticker.setPriority(Thread.MIN_PRIORITY + 1);
ticker.start();
try I

ticker.sleep(1000 / 15);
catch (InterruptedException e) I }
public synchronized void stop () [
running = false;
I
try
ticker.sleep(1000 / 15);
1 catch (InterruptedException e) { I
1
public synchronized void start () f
if (ticker == null i ~ ! ticker.isAlive()) I
running = true;
ticker = new Thread(this);
ticker.setPriority(Thread.MIN_PRIORITY + 1);
ticker.start();
public synchronized void stop t)
running = false;
class Ball {
public float x, y, dx, dy;
private Color color;
private int size;
Ball (float x, float y, float dx, float dy, int size,
Color color) i
this.x = x;
this.y = y;
this.dy = dx;
this.dy = dy;
this.color = color;
this.size = size;
public void draw (Graphics g) f
g.setColor(color);
g.fillOval((int) x, (int) y, size, size);
public void move (Rectangle bounds) I
// Add velocity values dx/dy to position to
get ,
// ball s new position
x +_ dx;
y +° dy;
// Check for collision with left edge
if (x < bounds.x && dx < 0)
dx = -dx;
x -= 2 * (x - bounds.x);
// Check for collision with right edge
else if ((x + size) ) (bounds.x + bounds.width) &&
d x > 0' )
dx = -dx;
x -= 2 * ((x + size) - (bounds.x + bounds.width));
// Check for collision with top edge
if (y < bounds.y && dy < 0) I
dy = -dy;
y -° 2 * (y - bounds.y);
// Check for collision with bottom edge
else i f (
.
( y + size) > ( bounds.y + bounds.height) &&
dy = -dy;
y -= 2 * ((y + size) - (bounds.y + bounds.height));
private Rectangle bounds;
private Ball ball;
private int width, height;
public void init() {
width = width = size().width;
height = size().height;
bounds = new Rectangle(width, height);
// Initialize Ball position and velocity
ball = new Ball(width / 3f, height / 4f, 1.5f,
2.3f, 12, Color.blue);

Kindly Bookmark and Share it:

Comment Form is loading comments...
 
© 2010 Tricks Site Template by Tricks Site