//
//brandon witt
//paddle game
//dec 6 2010
//
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class paddlegame extends Applet implements Runnable {
Graphics g;
//font declaration
Font font1 = new Font("Helvetica", Font.BOLD, 90);
Font font2 = new Font("Helvetica", Font.PLAIN, 30);
//speed/trajectory
int x_shift = 1;
int y_shift = 1;
int x1_shift = 2;
int y1_shift = 2;
int x1 = 500;
int y1 = 500;
//scorer
double counter = 0;
//etc.
boolean bouncing;
boolean testOutput = true;
public void init() {
r = new Rectangle(30,50,20,20);
b = new Rectangle (40, 60, 20, 20);
Thread t = new Thread(this);
t.start();// behaviour of thread controlled by run method
bouncing = true;
}
public void paint(Graphics g) {
//square
g.setColor(Color.BLUE);
g.fillRect(x1, y1, 80, 80);
//score display
g.setColor(Color.BLACK);
g.setFont(font2);
g.drawString("Score:" + counter, 500, 25 );
//bouncing balls
if (bouncing) g.setColor(Color.RED);
g.fillOval(r.x,r.y,r.width,r.height);
if (bouncing) g.setColor(Color.BLUE);
g.fillOval(b.x,b.y,b.width,b.height);
if (testOutput)
System.out.println ("x = " + r.x + " y = " + r.y + " bouncing = " + bouncing);
if (r.x > x1 && r.x < x1 + 80 && r.y > y1 && r.y < y1 + 80 ){
//explosion scene
g.setColor(Color.BLACK);
g.fillOval(r.x - 100,r.y - 100,100, 100);
g.setColor(Color.YELLOW);
g.fillOval(r.x - 20,r.y - 40,100, 100);
g.setColor(Color.RED);
g.fillOval(r.x - 30,r.y - 15,100, 100);
g.setColor(Color.ORANGE);
g.fillOval(r.x - 100,r.y - 55,100, 100);
g.setColor(Color.BLUE);
g.fillOval(r.x - 5,r.y - 30,100, 100);
g.setColor(Color.PINK);
g.fillOval(r.x - 10,r.y - 40,100, 100);
g.setColor(Color.GREEN);
g.fillOval(r.x - 50,r.y - 50,100, 100);
g.setColor(Color.RED);
g.fillOval(r.x - 150,r.y - 150,250, 300);
g.setColor(Color.orange);
g.fillOval(r.x - 50,r.y - 50,40, 100);
g.setColor(Color.orange);
g.fillOval(r.x - 100,r.y - 80,100, 200);
g.setColor(Color.yellow);
g.fillOval(r.x - 50,r.y - 50,50, 100);
g.setColor(Color.red);
g.fillOval(r.x - 100,r.y - 40,40, 10);
g.setColor(Color.ORANGE);
g.fillOval(r.x - 10,r.y - 30,100, 100);
g.setColor(Color.ORANGE);
g.fillOval(r.x - 60,r.y - 80,20, 10);
g.setColor(Color.BLUE);
g.setFont(font1);
g.drawString("YOU LOSE!!!", 300, 300);
g.setColor(Color.BLACK);
g.setFont(font2);
g.drawString("Your Final Score:" + counter, 300, 400 );
}
if (b.x > x1 && b.x < x1 + 80 && b.y > y1 && b.y < y1 + 80){
//explosion scene 2
g.setColor(Color.BLACK);
g.fillOval(b.x - 100,b.y - 100,100, 100);
g.setColor(Color.YELLOW);
g.fillOval(b.x - 20,b.y - 40,100, 100);
g.setColor(Color.RED);
g.fillOval(b.x - 30,b.y - 15,100, 100);
g.setColor(Color.ORANGE);
g.fillOval(b.x - 100,b.y - 55,100, 100);
g.setColor(Color.BLUE);
g.fillOval(b.x - 5,b.y - 30,100, 100);
g.setColor(Color.PINK);
g.fillOval(b.x - 10,b.y - 40,100, 100);
g.setColor(Color.GREEN);
g.fillOval(b.x - 50,b.y - 50,100, 100);
g.setColor(Color.RED);
g.fillOval(b.x - 150,b.y - 150,250, 300);
g.setColor(Color.orange);
g.fillOval(b.x - 50,b.y - 50,40, 100);
g.setColor(Color.orange);
g.fillOval(b.x - 100,b.y - 80,100, 200);
g.setColor(Color.yellow);
g.fillOval(b.x - 50,b.y - 50,50, 100);
g.setColor(Color.red);
g.fillOval(b.x - 100,b.y - 40,40, 10);
g.setColor(Color.ORANGE);
g.fillOval(b.x - 10,b.y - 30,100, 100);
g.setColor(Color.ORANGE);
g.fillOval(b.x - 60,b.y - 80,20, 10);
g.setColor(Color.BLUE);
g.setFont(font1);
//final score text
g.drawString("YOU LOSE!!!", 300, 300);
g.setColor(Color.BLACK);
g.setFont(font2);
g.drawString("Your Final Score:" + counter, 300, 400 );
}
}
public void update(Graphics g) {
Image offScreenImage = createImage(getSize().width, getSize().height);
paint(offScreenImage.getGraphics());
g.drawImage(offScreenImage,0,0, null);
}
public void run() {
while (true) {
// Thread performs endless loop
r.x += x_shift;
r.y += y_shift;
if (r.x>= getSize().width || r.x < 0) {
x_shift *= -1;
}
if (r.y>= getSize().height || r.y < 40) {
y_shift *= -1;
}
b.x += x1_shift;
b.y += y1_shift;
if (b.x>= getSize().width || b.x < 0) {
x1_shift *= -1;
}
if (b.y>= getSize().height || b.y < 40) { // Why 40?
y1_shift *= -1;
}
try {
//speed of balls (long)
Thread.currentThread().sleep((long) 0.9 );
}
catch (Exception ke) {
}
//loop break if ball 1 or 2 hits square
if (r.x > x1 && r.x < x1 + 80 && r.y > y1 && r.y < y1 + 80 ){
break;
}
if (b.x > x1 && b.x < x1 + 80 && b.y > y1 && b.y < y1 + 80 ){
break;
}
//repaints the balls at their current location
repaint();
//increases the score
counter += 0.01;
}
}
private Rectangle r;
private Rectangle b;
public boolean mouseDown(Event e) {
return true;
}
public boolean mouseDrag(Event e ) {
return true;
}
public KeyEvent events(KeyEvent ke){
return ke;
}
public boolean mouseUp(Event e) {
return true;
}
}