CS150: Interactive Programming with Java |
Lab 10
|
You're going to start creating a game like Zelda. Although we won't have time to finish it, this will give you some ideas how to get started on game programming.
We have a dungeon that is a big image with maps of all the rooms that our player could go through:
We'll only show a small portion of the dungeon at any given time: a 480 by 500 pixel section of the map where the player currently is.
For example, the first room will be displayed as follows:
For the player, we have several images to use for the player. These images are animated,
so when we want the player to be walking, it will actually look like the player is walking.
The one that is standing is what we'll display when the player isn't moving.
Instructions:
public Sprite( Image i, Image u, Image d, Image l, Image r )
{
image = i; stand = i;
up = u; down = d; left = l; right = r;
x = 0; y=0;
setOpaque(false); // ensures transparency
}
implements Runnableafter your class header extends JApplet, e.g.:
public class GameVersion1 extends JApplet implements Runnable
MediaTracker track = new MediaTracker( this );
Now add each of the images we want to wait for:
track.addImage( bkgrnd, 1 ); track.addImage( stand, 2);
track.addImage( back, 3 ); track.addImage( front, 4 );
track.addImage( left, 5 ); track.addImage( right, 6 );
Note: the numbers are unique IDs necessary for reference specific images in the MediaTracker.
Now we'll tell the MediaTracker to wait for all the images to load. We need to put this code within a try...catch block in case there is a problem with one of the images.
try { track.waitForAll( );
} catch(Exception e ) { }
player = new Sprite( stand, back, front, left, right );
player.x = 130;
player.y = 200;
getRootPane().getGraphics().drawImage( bkgrnd, 0,0, this );
this.setFocusable(true); // Allow this panel to get focus.
thread = new Thread(this);
thread.start( );
Graphics g = getGraphics( ); // obtains a Graphics object of our applet
while( true ) // loop forever while we're playing
{
g.drawImage( bkgrnd,0,0, this); // draw the background
g.drawImage( player.image, player.x, player.y, this ); // draw the player at coordinates x,y
try { // wait for a bit between drawings
Thread.sleep(10);
} catch( Exception ex ) { stop( ); }
}
import java.awt.image.*;
Graphics g = getGraphics( );with the following code:
Graphics gapplet = (Graphics2D )getGraphics( );
BufferedImage bimg = new BufferedImage( this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_RGB );
Graphics2D g = bimg.createGraphics();
fullimg = new BufferedImage(bkgrnd.getWidth(null),bkgrnd.getHeight(null), BufferedImage.TYPE_INT_RGB);
fullg = fullimg.createGraphics();
fullg.drawImage(bkgrnd, 0,0,this);
Now, inside the while loop, change the code to have the following:
while( true )
{
BufferedImage bkg = fullimg.getSubimage(0,0, this.getWidth()-1, this.getHeight()-1);
g.drawImage( bkg, 0,0, this ); // draw the background on to the BufferedImage
g.drawImage( player.image, player.x, player.y, this ); // draw the player
gapplet.drawImage( bimg, 0,0, this ); // draw the buffered image to the applet
try { Thread.sleep(10);
} catch( Exception ex ) { stop( ); }
}
public void update(Graphics g) { }
public void keyPressed(KeyEvent ke)
{
}
public void keyTyped(KeyEvent ke)
{
}
public void keyReleased(KeyEvent ke)
{
}
addKeyListener(this);
int code = key.getKeyCode( );We'll assume the user will use the arrow keys to move around, so let's check for each.
KeyEvent.VK_LEFT = left arrow key KeyEvent.VK_RIGHT = right arrow key KeyEvent.VK_UP = up arrow key KeyEvent.VK_DOWN = down arrow keyFor example, to get you started,
if( code == KeyEvent.VK_UP ) player.moveUp( );Inside the if-else structure, call methods in the player class (which we'll write next!) : moveUp, moveDown, moveLeft and moveRight.
Now inside the GameVersion1 class, in the keyReleased method, call the stopMoving method on the player.