// TreasureWorld: a computer game. // Collect the treasure before running out of strength // Note: the code assumes an applet window 400 pixels wide, and 300 high // sbj May 1998; updated to Java 1.1 May 1999 import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class TreasureWorld extends Applet implements ActionListener, MouseListener, MouseMotionListener, ItemListener { private final int windowWidth = 400; private final int windowHeight = 300; private final int clickSep = 5; // How close the cursor has to be to player icon // for a click to start dragging private final int displayFrequency = 10; // Once in every this many repaints (on average) private final int displayTime = 200; // show treasure for this many milliseconds private final int initialStrength = 200; private TreasureCollection theTreasure; private int collected; // How many items collected so far private Label collectedIndicator; private Panel controls, info; // To hold controls buttons & information widgets private Button restart; // To reset the game to the start private Choice icon; // For choosing the player icon private int strength; // Reduced by 1 per mouse event private Label strengthIndicator; private int currentX, currentY; // Current player position private boolean trackingNow; // True when mouse is dragging the player public void init() { int noOfTreasures = (int)(Math.random()*11+5); // Random from 5 - 15 theTreasure = new TreasureCollection(noOfTreasures); collected = 0; currentX = 200; currentY = 200; trackingNow = false; strength = initialStrength; setLayout(new BorderLayout()); // Alter the applet's layout manager info = new Panel(); // info panel holds title, remaining 'strength' and collection 'score' Label title = new Label("Treasure World."); title.setFont(new Font("TimesRoman", Font.BOLD, 14)); info.add(title); strengthIndicator = new Label("Remaining strength: "); info.add(strengthIndicator); collectedIndicator = new Label("Collected so far: "); info.add(collectedIndicator); add("North", info); // info panel is across top of window controls = new Panel(); // controls panel for restart button and icon choice restart = new Button("Restart game"); controls.add(restart); restart.addActionListener(this); icon = new Choice(); icon.addItem("Cross"); icon.addItem("Fido"); controls.add(icon); icon.addItemListener(this); add("South", controls); // controls panel is across bottom of window addMouseListener(this); addMouseMotionListener(this); } public void paint(Graphics g) { strengthIndicator.setText("Remaining strength: "+strength); collectedIndicator.setText("Collected so far: "+collected); if (collected == theTreasure.maxItems) // All treasure collected? g.drawString("Well done! You have won!", 100, 150); else if (strength <= 0) // No strength left? g.drawString("Bad luck! You are dead!", 100, 150); else { // Still playing... if ((int) (Math.random()*displayFrequency) == 0) { // One in so many repaints, theTreasure.display(g); // display the treasure... drawIcon(g); try { Thread.sleep(displayTime); // for while... } catch (InterruptedException e) { } // (Ignore it!) g.clearRect(0, 0, windowWidth, windowHeight); // then hide the treasure again } drawIcon(g); // Draw the player } } private void drawIcon(Graphics g) { if (icon.getSelectedItem().equals("Cross")) drawCross(g); if (icon.getSelectedItem().equals("Fido")) drawFido(g); } private void drawCross(Graphics g) { // A large cross centred at currentX, currentY g.setColor(Color.black); g.drawLine(currentX-10, currentY-10, currentX+10, currentY+10); g.drawLine(currentX-10, currentY+10, currentX+10, currentY-10); } private void drawFido(Graphics g) { // Fido the dog roughly centred at currentX, currentY g.setColor(Color.black); g.fillOval(currentX-7, currentY-3, 15, 7); // Body g.drawLine(currentX-5, currentY, currentX-5, currentY+7); // Back leg g.drawLine(currentX+5, currentY, currentX+5, currentY+7); // Front leg g.fillOval(currentX+4, currentY-7, 8, 6); // Head g.drawLine(currentX+6, currentY-6, currentX+6, currentY-9); // Ear g.drawLine(currentX-10, currentY-4, currentX-6, currentY); // Tail } public void actionPerformed(ActionEvent e) { if (e.getSource() == restart) { // Reset game to initial state int noOfTreasures = (int)(Math.random()*11+5); // Random from 5 - 15 theTreasure = new TreasureCollection(noOfTreasures); collected = 0; currentX = 200; currentY = 200; strength = initialStrength; trackingNow = false; repaint(); } } public void itemStateChanged(ItemEvent e) { // User has selected an icon in the choice list if (e.getSource() == icon) repaint(); } public void mousePressed(MouseEvent e) { // The mouseDown event only starts player icon dragging if // quite close to the current player position int x = e.getX(); int y = e.getY(); int distance = (x-currentX)*(x-currentX)+(y-currentY)*(y-currentY); // Pythagoras if (distance<=clickSep*clickSep) { // No more than clickSep pixels from current player position? strength--; // Use up some strength currentX = x; // Record current position currentY = y; collected += theTreasure.check(x, y); // Collect any treasure here trackingNow = true; // Mouse down, so now tracking repaint(); } } public void mouseReleased(MouseEvent e) { // Stop tracking, if we have been int x = e.getX(); int y = e.getY(); if (trackingNow) { strength--; // Use up some strength currentX = x; currentY = y; collected += theTreasure.check(x, y); // Collect any treasure here trackingNow = false; // Mouse released, so stop tracking repaint(); } } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseDragged(MouseEvent e) { // Only drag player icon if we are actually tracking the mouse int x = e.getX(); int y = e.getY(); if (trackingNow) { strength--; // Use up some strength currentX = x; currentY = y; collected += theTreasure.check(x, y); // Collect any treasure here repaint(); } } public void mouseMoved(MouseEvent e) { } } // End of TreasureWorld