Java Code
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.*;
public class GameImgBkg extends JApplet implements Runnable, KeyListener
{
int width, height; // dimensions of game
ArrayList<Rectangle> walls;
BufferedImage backgroundBuf, screenBuf; // screenBuf = draw backgrd then player and entities in layers
Graphics2D background;
MediaTracker track;
Sprite player;
Sprite fairy;
Thing exit;
Thread thread;
Random random;
Image bkdrop;
public void init( )
{
resize( 300, 300 );
loadImages( );
width = this.getWidth( );
height = this.getHeight( );
walls = new ArrayList<Rectangle>( );
buildWalls( );
player.setLocation( 50, 50 );
fairy.setLocation ( width-70, height-70 );
random = new Random( );
try {
track.waitForAll( );
} catch ( Exception e ) { };
addKeyListener(this);
}
public void start( )
{
thread = new Thread( this );
thread.start( );
setFocusable(true);
}
public BufferedImage createBufImage (int width, int height) {
return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(width, height, Transparency.BITMASK);
}
public void loadImages( )
{
track = new MediaTracker( this );
Image img = getImage( getCodeBase( ), "stand_front.gif" );
Image back = getImage( getCodeBase( ), "walk_back.gif" );
Image front = getImage( getCodeBase( ), "walk_front.gif" );
Image right = getImage( getCodeBase( ), "walk_right.gif" );
Image left = getImage( getCodeBase( ), "walk_left.gif" );
bkdrop = getImage( getCodeBase( ), "DungeonBkdrop.jpg");
track.addImage(img, 2); track.addImage(back, 6 );
track.addImage( front, 3 ); track.addImage( left, 4 );
track.addImage( right, 5 );
track.addImage( bkdrop, 6 );
player = new Sprite( img, back, front, left, right );
img = getImage( getCodeBase( ), "Fairy.gif" );
track.addImage( img, 7 );
fairy = new Sprite( img );
fairy.speed = 2;
exit = new Thing( getImage( getCodeBase( ), "door.jpg"));
}
public void buildWalls( )
{
// create Polygon or Rectangle objects and store in ArrayList walls
int wallThickness = 10;
walls.add( new Rectangle( 0,0, width, wallThickness ) ); // top
walls.add( new Rectangle( 0,0, wallThickness, height ) ); // left
walls.add( new Rectangle( width-wallThickness,0, wallThickness, height ) ); // right
walls.add( new Rectangle( 0,height-wallThickness, width, wallThickness ) ); // bottom
walls.add( new Rectangle( 0,100, 100, wallThickness ) ); // left
// bottom wall structure
walls.add( new Rectangle( 50,height-wallThickness-120, wallThickness, 70 ) ); // vert on left
walls.add( new Rectangle( 50,height-wallThickness-120, width-150, wallThickness ) ); // horiz top
walls.add( new Rectangle( 50,height-wallThickness-50, width-120, wallThickness ) ); // horiz bottom
walls.add( new Rectangle( 200,height-230, wallThickness, 130 ) ); // vert on rt
exit.setLocation( 50+wallThickness,height-wallThickness-50-60);
}
public boolean checkCollisionWalls( Rectangle entityAura )
{
for ( int i=0; i< walls.size( ); i++ )
{
if ( entityAura.intersects( walls.get( i ) ))
return true;
}
return false;
}
public boolean checkCollisionFairy( )
{
return player.getDimensions( ).intersects( fairy.getDimensions( ) );
}
public void paintWalls( )
{
backgroundBuf = createBufImage( width, height ); // backdrop and walls
background = backgroundBuf.createGraphics( );
background.drawImage( bkdrop, 0, 0, width, height, this );
/*
backgroundBuf = createBufImage( width, height ); // backdrop and walls
background = backgroundBuf.createGraphics( );
background.setColor( Color.black );
background.fillRect( 0,0, width, height );
background.setColor( Color.gray );
for ( int i=0; i<walls.size( ); i++ )
{
Rectangle r = (Rectangle )walls.get( i );
background.fillRect ( r.x, r.y, r.width, r.height );
}
*/
}
public void keyReleased( KeyEvent ke )
{
player.stopMoving( );
}
/* KeyListener methods */
public void keyPressed( KeyEvent key )
{
int code = key.getKeyCode( );
if( code == KeyEvent.VK_UP || code == KeyEvent.VK_8 )
player.moveUp( );
else if( code == KeyEvent.VK_RIGHT )
player.moveRight( );
else if( code == KeyEvent.VK_LEFT )
player.moveLeft( );
else if( code == KeyEvent.VK_DOWN || code == KeyEvent.VK_2)
player.moveDown( );
if ( checkCollisionWalls( player.getDimensions( ) ))
player.undoMove( );
}
public void keyTyped( KeyEvent ke ) { }
/* end KeyListener methods */
public void paint( Graphics g )
{
g.drawImage( screenBuf,0,0,this);
}
public void update(Graphics g)
{
paint(g);
}
public void run( )
{
paintWalls( );
screenBuf = createBufImage( width, height );
Graphics gapplet = (Graphics2D )screenBuf.getGraphics( );
while( true )
{
gapplet.drawImage( backgroundBuf, 0,0, this ); // first draw background on buffer
if ( ! exit.isVisible( ) && checkCollisionFairy( ) ) // exit not yet visible, check if collision with fairy
exit.setVisible( true );
exit.paintComponent( gapplet );
moveFairy( );
fairy.paintComponent( gapplet ); // draw the fairy
player.paintComponent( gapplet ); // then draw player on buffer
if ( exit.isVisible( ) && player.getDimensions( ).intersects( exit.getDimensions( ) ) )
{
gapplet.setFont( new Font( "Serif", Font.BOLD, 42 ) );
gapplet.setColor( Color.WHITE );
gapplet.drawString( "YOU WIN!", 60, 120 );
}
repaint( ); // draw buffer to applet
try {
Thread.sleep(10);
} catch( Exception ex ) { stop( ); }
}
}
public void moveFairy( )
{
boolean badLocation = true; // keep track of whether we found a good location or not
int numTries = 10; // give up after a while to prevent infinite loop
while( badLocation && numTries > 0 )
{
int newx = random.nextInt( fairy.speed ); // generate a new random coodinate based on speed of fairy
int newy = random.nextInt( fairy.speed );
if ( random.nextInt( 2 ) == 0 ) // change direction in x
newx = -1 * newx;
if ( random.nextInt( 2 ) == 0 )
newy = -1 * newy;
fairy.setLocation(fairy.x + newx, fairy.y + newy);
if ( checkCollisionWalls( fairy.getDimensions( ) ) )
fairy.undoMove( );
else
badLocation = false;
numTries = numTries -1;
}
}
}
Also include Sprite.java and Thing.java from other example.
Return to Code Examples
©2007 E.S.Boese