A. Setup
package wackadot ;
import fang.* ;
public class Wackadot extends GameLoop
{
public static void main( String[ ] argv )
{
Wackadot dot = new Wackadot( ) ;
dot.runAsApplication( ) ;
}
}
B. Making Dots
package wackadot ;
import fang.* ;
import java.awt.Color ;
import java.awt.geom.* ;
public class Wackadot extends GameLoop
{
private Sprite dot ;
public void startGame( )
{
makeSprites( ) ;
addSprites( ) ;
}
private void makeSprites( )
{
dot = new OvalSprite( 1, 1 ) ;
dot.setScale( 0.1 ) ;
dot.setLocation( 0.5, 0.5 ) ;
dot.setColor( Color.RED ) ;
}
private void addSprites( )
{
canvas.addSprite( dot ) ;
}
public static void main( String[ ] argv )
{
Wackadot dot = new Wackadot( ) ;
dot.runAsApplication( ) ;
}
}
C. Using the Mouse
package wackadot ;
import fang.* ;
import java.awt.Color ;
import java.awt.geom.* ;
public class Wackadot extends GameLoop
{
private Sprite dot ;
private Sprite redDot ;
private Sprite blueDot ;
public void startGame( )
{
makeSprites( ) ;
addSprites( ) ;
}
private void makeSprites( )
{
dot = new OvalSprite( 1, 1 ) ;
dot.setScale( 0.1 ) ;
dot.setLocation( 0.5, 0.5 ) ;
dot.setColor( Color.RED ) ;
}
private void addSprites( )
{
canvas.addSprite( dot ) ;
}
public static void main( String[ ] argv )
{
Wackadot dot = new Wackadot( ) ;
dot.runAsApplication( ) ;
}
public void advanceFrame( double timePassed )
{
Point2D.Double mouse = getPlayer( ).getMouse( ).getLocation( ) ;
dot.setLocation( mouse ) ;
}
}
D. Adding more Dots
package wackadot ;
import fang.* ;
import java.awt.Color ;
import java.awt.geom.* ;
public class Wackadot extends GameLoop
{
private Sprite dot ;
private Sprite redDot ;
private Sprite blueDot ;
public void startGame( )
{
makeSprites( ) ;
addSprites( ) ;
}
private void makeSprites( )
{
dot = new OvalSprite( 1, 1 ) ;
dot.setScale( 0.1 ) ;
dot.setLocation( 0.5, 0.5 ) ;
dot.setColor( Color.RED ) ;
redDot = new OvalSprite( 1, 1 ) ;
redDot.setScale( 0.1 ) ;
redDot.setLocation( random.nextDouble( ), random.nextDouble( ) ) ;
redDot.setColor( Color.RED ) ;
blueDot = new OvalSprite( 1, 1 ) ;
blueDot.setScale( 0.1 ) ;
blueDot.setLocation( random.nextDouble( ), random.nextDouble( ) ) ;
blueDot.setColor( Color.BLUE ) ;
}
private void addSprites( )
{
canvas.addSprite( dot ) ;
canvas.addSprite( redDot ) ;
canvas.addSprite( blueDot ) ;
}
public static void main( String[ ] argv )
{
Wackadot dot = new Wackadot( ) ;
dot.runAsApplication( ) ;
}
public void advanceFrame( double timePassed )
{
Point2D.Double mouse = getPlayer( ).getMouse( ).getLocation( ) ;
dot.setLocation( mouse ) ;
}
}
E. Collision Handling
package wackadot ;
import fang.* ;
import java.awt.Color ;
import java.awt.geom.* ;
public class Wackadot extends GameLoop
{
private Sprite dot ;
private Sprite redDot ;
private Sprite blueDot ;
public void startGame( )
{
makeSprites( ) ;
addSprites( ) ;
}
private void makeSprites( )
{
dot = new OvalSprite( 1, 1 ) ;
dot.setScale( 0.1 ) ;
dot.setLocation( 0.5, 0.5 ) ;
dot.setColor( Color.RED ) ;
redDot = new OvalSprite( 1, 1 ) ;
redDot.setScale( 0.1 ) ;
redDot.setLocation( random.nextDouble( ), random.nextDouble( ) ) ;
redDot.setColor( Color.RED ) ;
blueDot = new OvalSprite( 1, 1 ) ;
blueDot.setScale( 0.1 ) ;
blueDot.setLocation( random.nextDouble( ), random.nextDouble( ) ) ;
blueDot.setColor( Color.BLUE ) ;
}
private void addSprites( )
{
canvas.addSprite( dot ) ;
canvas.addSprite( redDot ) ;
canvas.addSprite( blueDot ) ;
}
public static void main( String[ ] argv )
{
Wackadot dot = new Wackadot( ) ;
dot.runAsApplication( ) ;
}
private void repositionRandomly( Sprite sprite )
{
sprite.setLocation( random.nextDouble( ), random.nextDouble( ) ) ;
}
private void handleCollisions( )
{
if( dot.intersects( blueDot ) || dot.intersects( redDot ) )
{
long seed = (long)( 1000 * getTime( ) ) ;
random.setSeed( seed ) ;
}
if( dot.intersects( blueDot ) )
{
repositionRandomly( blueDot ) ;
}
if( dot.intersects( redDot ) )
{
repositionRandomly( redDot ) ;
}
}
public void advanceFrame( double timePassed )
{
Point2D.Double mouse = getPlayer( ).getMouse( ).getLocation( ) ;
dot.setLocation( mouse ) ;
handleCollisions( ) ;
}
}
F. Switching Dot colors on collision
package wackadot ;
import fang.* ;
import java.awt.Color ;
import java.awt.geom.* ;
public class Wackadot extends GameLoop
{
private Sprite dot ;
private Sprite redDot ;
private Sprite blueDot ;
public void startGame( )
{
makeSprites( ) ;
addSprites( ) ;
}
private void makeSprites( )
{
dot = new OvalSprite( 1, 1 ) ;
dot.setScale( 0.1 ) ;
dot.setLocation( 0.5, 0.5 ) ;
dot.setColor( Color.RED ) ;
redDot = new OvalSprite( 1, 1 ) ;
redDot.setScale( 0.1 ) ;
redDot.setLocation( random.nextDouble( ), random.nextDouble( ) ) ;
redDot.setColor( Color.RED ) ;
blueDot = new OvalSprite( 1, 1 ) ;
blueDot.setScale( 0.1 ) ;
blueDot.setLocation( random.nextDouble( ), random.nextDouble( ) ) ;
blueDot.setColor( Color.BLUE ) ;
}
private void addSprites( )
{
canvas.addSprite( dot ) ;
canvas.addSprite( redDot ) ;
canvas.addSprite( blueDot ) ;
}
public static void main( String[ ] argv )
{
Wackadot dot = new Wackadot( ) ;
dot.runAsApplication( ) ;
}
private void repositionRandomly( Sprite sprite )
{
sprite.setLocation( random.nextDouble( ), random.nextDouble( ) ) ;
}
private void handleCollisions( )
{
if( dot.intersects( blueDot ) || dot.intersects( redDot ) )
{
long seed = (long)( 1000 * getTime( ) ) ;
random.setSeed( seed ) ;
}
if( dot.intersects( blueDot ) )
{
repositionRandomly( blueDot ) ;
if( dot.getColor( ).equals( Color.BLUE ) )
{
dot.setColor( Color.RED ) ;
}
}
if( dot.intersects( redDot ) )
{
repositionRandomly( redDot ) ;
if( dot.getColor( ).equals( Color.RED ) )
{
dot.setColor( Color.BLUE ) ;
}
}
}
public void advanceFrame( double timePassed )
{
Point2D.Double mouse = getPlayer( ).getMouse( ).getLocation( ) ;
dot.setLocation( mouse ) ;
handleCollisions( ) ;
}
}
G. Keeping Score
package wackadot ;
import fang.* ;
import java.awt.Color ;
import java.awt.geom.* ;
public class Wackadot extends GameLoop
{
private Sprite dot ;
private Sprite redDot ;
private Sprite blueDot ;
private StringSprite scoreSprite ;
private int score ;
public void startGame( )
{
makeSprites( ) ;
addSprites( ) ;
score = 0 ;
}
private void makeSprites( )
{
dot = new OvalSprite( 1, 1 ) ;
dot.setScale( 0.1 ) ;
dot.setLocation( 0.5, 0.5 ) ;
dot.setColor( Color.RED ) ;
redDot = new OvalSprite( 1, 1 ) ;
redDot.setScale( 0.1 ) ;
redDot.setLocation( random.nextDouble( ), random.nextDouble( ) ) ;
redDot.setColor( Color.RED ) ;
blueDot = new OvalSprite( 1, 1 ) ;
blueDot.setScale( 0.1 ) ;
blueDot.setLocation( random.nextDouble( ), random.nextDouble( ) ) ;
blueDot.setColor( Color.BLUE ) ;
scoreSprite = new StringSprite( "Score: " + score ) ;
scoreSprite.setHeight( 0.1 ) ;
scoreSprite.rightJustify( ) ;
scoreSprite.topJustify( ) ;
scoreSprite.setLocation( 1, 0 ) ;
}
private void addSprites( )
{
canvas.addSprite( dot ) ;
canvas.addSprite( redDot ) ;
canvas.addSprite( blueDot ) ;
canvas.addSprite( scoreSprite ) ;
}
private void updateScore( )
{
scoreSprite.setText( "Score: " + score ) ;
}
public static void main( String[ ] argv )
{
Wackadot dot = new Wackadot( ) ;
dot.runAsApplication( ) ;
}
private void repositionRandomly( Sprite sprite )
{
sprite.setLocation( random.nextDouble( ), random.nextDouble( ) ) ;
}
private void handleCollisions( )
{
if( dot.intersects( blueDot ) || dot.intersects( redDot ) )
{
long seed = (long)( 1000 * getTime( ) ) ;
random.setSeed( seed ) ;
}
if( dot.intersects( blueDot ) )
{
repositionRandomly( blueDot ) ;
if( dot.getColor( ).equals( Color.BLUE ) )
{
dot.setColor( Color.RED ) ;
score++ ;
}
else
{
score-- ;
}
updateScore( ) ;
}
if( dot.intersects( redDot ) )
{
repositionRandomly( redDot ) ;
if( dot.getColor( ).equals( Color.RED ) )
{
dot.setColor( Color.BLUE ) ;
score++ ;
}
else
{
score-- ;
}
updateScore( ) ;
}
}
public void advanceFrame( double timePassed )
{
Point2D.Double mouse = getPlayer( ).getMouse( ).getLocation( ) ;
dot.setLocation( mouse ) ;
handleCollisions( ) ;
}
}