Java Code

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class MiniGame extends JApplet implements Runnable, KeyListener
{
	AnimateSprite user;
	AnimateSprite monster, troll;
	Coins ten, twenty;
	Thread thread;
	Random random;
	public void init( )
	{
		random = new Random( );
		user = new AnimateSprite( );
		user.setImage( getImage( getCodeBase( ), "player.gif" ));
		monster = new AnimateSprite( );
		monster.setImage( getImage( getCodeBase( ), "monster.gif" ) );
		monster.setLocation( 250,250 );
		troll = new AnimateSprite( );
		troll.setImage( getImage( getCodeBase( ), "troll.gif" ));
		troll.setLocation( 350,350);
		setupCoins( );
		setFocusable(true);
		addKeyListener( this );
		thread = new Thread( this );
		thread.start( );
	}
	public void setupCoins( )
	{
		ten = new Coins( 10 );
		twenty = new Coins( 20 );
		ten.setLocation( 400,350 );
		twenty.setLocation( 450, 50 );
		ten.setImage( getImage( getCodeBase( ), "coins.gif" ));
		twenty.setImage( getImage( getCodeBase( ), "coins.gif" ));
	}
	public void keyPressed( KeyEvent ke )
	{
		int key = ke.getKeyCode( );
		if ( key == KeyEvent.VK_UP )
			user.moveUp( );
		else if ( key == KeyEvent.VK_DOWN )
			user.moveDown( );
		else if ( key == KeyEvent.VK_LEFT )
			user.moveLeft( );
		else if ( key == KeyEvent.VK_RIGHT )
			user.moveRight( );
	}
	public void keyReleased( KeyEvent ke ) { }
	public void keyTyped( KeyEvent ke ) { }
	public void update( Graphics g ) { paint( g ); }
	public void paint( Graphics g )
	{
		g.clearRect( 0,0, this.getWidth(), this.getHeight() );
		ten.paintComponent( g );
		twenty.paintComponent( g );
		monster.setLocation( random.nextInt(10)-5+monster.x, 
										random.nextInt(10-5+monster.y) );
		monster.paintComponent( g );
		user.paintComponent( g );
		if ( user.intersects( monster ) )
		{
			boolean alive = user.hit( monster.weaponDamage );
			if ( ! alive )
			{
				g.setFont( new Font( "Serif", Font.BOLD, 26 ) );
				g.drawString( "YOU LOSE!", 20,100 );
				thread.interrupt( );	// stop thread, game is over
			}				
		}
	}
	public void run( )
	{
		try
		{
			while( true )
			{
				repaint( );
				Thread.sleep(10);
			}
		} catch( Exception e ) { }
	}
}

import java.awt.*; import javax.swing.*; public class Sprite { Image image; int x, y; boolean isVisible; public Sprite( ) { isVisible = false; image = null; } public Sprite( Image i ) { isVisible = true; image = i; x = 10; y=10; } public void setImage( Image img ) { image = img; isVisible = true; } public void setLocation( int _x, int _y ) { x = _x; y = _y; } public Rectangle getDimensions( ) { return new Rectangle( x, y, image.getWidth(null), image.getHeight(null) ); } public boolean intersects( Sprite s ) { return getDimensions( ).intersects( s.getDimensions( ) ); } public void setVisible( boolean vis ) { isVisible = vis; } public void paintComponent( Graphics g ) { if ( isVisible ) g.drawImage( image, x, y, null ); } }
import java.awt.*; import javax.swing.*; public class Coins extends Sprite { int amount; public Coins( int amt ) { amount = amt; } public int getAmount( ) { return amount; } public void setAmount ( int amt ) { amount = amt; } }
import java.awt.*; import javax.swing.*; public class AnimateSprite extends Sprite { int speed=5; int directionX = 1, directionY = 1; int healthPoints = 100; int weaponDamage = 10; final boolean DEAD = false; final boolean ALIVE = true; public void moveUp( ) { y -= speed; } public void moveDown( ) { y += speed; } public void moveLeft( ) { x -= speed; } public void moveRight( ) { x += speed; } public int getHealthPoints( ) { return healthPoints; } public void setHealthPoints( int hp ) { healthPoints = hp; } public boolean hit( int amt ) { healthPoints -= amt; if ( healthPoints < 0 ) return DEAD; else return ALIVE; } }

©2007- by E.S.Boese. All Rights Reserved