Friday, January 7, 2011

Obstacle V.001

// This is the source code for the Obstacle game located here.

import java.applet.*;
import java.awt.event.*;
import java.awt.*;

public class Obstacle001 extends Applet implements Runnable, KeyListener {

int width, height;
Thread t = null;
boolean threadSuspended;
int recWidth, recHeight, recXLoc, recYLoc, i;
int ovaWidth, ovaHeight, j;


public void init() {
width = getSize().width;
height = getSize().height;
recWidth = width / 3;
recHeight = height / 7;
recXLoc = (2 * width / 3) + (recWidth);
recYLoc = height / 2;
ovaWidth = 40;
ovaHeight = 40;
i = recXLoc;
j = 20;
setBackground( Color.black );
addKeyListener(this);
}


public void destroy() {

}


public void start() {
if ( t == null ) {
t = new Thread( this );
threadSuspended = false;
t.start();
}
else {
if ( threadSuspended ) {
threadSuspended = false;
synchronized( this ) {
notify();
}
}
}
}


public void stop() {
threadSuspended = true;
}


public void run() {
try {
while (true) {
--i;
if ( i == -(recWidth) ) {
i = recXLoc;
}
if ( threadSuspended ) {
synchronized( this ) {
while ( threadSuspended ) {
wait();
}
}
}
repaint();
t.sleep( 8 );
}
}
catch (InterruptedException e) { }
}

public void keyTyped(KeyEvent txt) {
char key = txt.getKeyChar();

if (key == 'w') {
j = j - 12;
repaint();
txt.consume();
} else {
j = j + 12;
repaint();
txt.consume();
}

}
public void keyPressed(KeyEvent txt) {

}
public void keyReleased(KeyEvent txt) {

}


public void paint( Graphics g ) {
g.setColor( Color.white );
g.fillRect(i, recYLoc, recWidth, recHeight);
g.setColor( Color.white);
g.fillOval(50, j, ovaWidth, ovaHeight);
}
}




============================================================================

Things I want to add / improve for V.002:

1) make the circle a triangle
2) switch from repaint(); to buffer strategy
3) move rectangle to the center of the window
4) make the oval dependent on window size
5) make it so that the oval doesn't go through the rectangle
6) make the oval drop automatically
7) add more obstacles

No comments: