import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class TreasureApplet extends Applet implements KeyListener {
private Island island;
public void init() {
island = new Island(10);
addKeyListener(this);
}
public void keyPressed(KeyEvent e) {
if (island.currentLocation() == Island.WATER) {
return;
}
if (island.currentLocation() == Island.TREASURE) {
return;
}
if (island.currentLocation() == Island.PIRATE) {
return;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
island.moveWest();
}
repaint();
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
island.moveEast();
}
repaint();
if (e.getKeyCode() == KeyEvent.VK_UP) {
island.moveNorth();
}
repaint();
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
island.moveSouth();
}
repaint();
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public void paint(Graphics g) {
int x = 0;
while(x < 10) {
int y = 0;
while(y < 10) {
if (island.terrainAt(x,y) == Island.WATER) {
//draw the water
g.setColor(new Color(0,0,210));
g.fillRect(x*40, y*40, 40, 40);
g.drawString("Splash! You got to the water.", 50, 430);
}
else if (island.terrainAt(x,y) == Island.SAND) {
//draw the sand
g.setColor(new Color(160,82,45));
g.fillRect(x*40, y*40, 40, 40);
g.drawString("Ouch. Sand got into your sandals.", 50, 430);
}
else if (island.terrainAt(x,y) == Island.TREE) {
//draw the treasure
g.setColor(new Color(50,205,50));
g.fillRect(x*40, y*40, 40, 40);
g.drawString("You are standing by a tree.", 50, 430);
}
else if (island.terrainAt(x,y) == Island.TREASURE) {
//draw the treasure
g.setColor(new Color(225,215,0));
g.fillRect(x*40, y*40, 40, 40);
g.drawString("Congratulations. You found the treasure", 50, 430);
}
else {
//draw the pirate
g.setColor(new Color(0,0,0));
g.fillRect(x*40, y*40, 40, 40);
g.drawString("It's the pirate. Run!", 50, 430);
}
//draw a square so the user can distinguish locations clearly
g.setColor(Color.BLACK);
g.drawRect(x*40, y*40, 40, 40);
y++;
}
x++;
}
}
}