The goal of this game is wander throughout the house and find the owner of the house.
If you are done early, in the main hall have a "start new game" button. That button chooses a random number. Use the random number to choose a room to put the hidden house owner in.
1. Draw a floorplan. Make at least 8 rooms. For example (this one is a little crazy and big):
2. Make a list of the rooms. Number them, these will be your screen numbers. Make the first screen be your main hall. For example:
3. Make a list of where each room heads:
4. Code the screens (my example does not match the planning above):
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
import java.io.*;
//Requires a hall.gif and billard.gif in folder "pics"
public class FindMe extends Applet implements ActionListener
{
Panel p_card; //to hold all of the cards
Panel card1, card2; //the two screens
CardLayout cdLayout = new CardLayout ();
public void init ()
{
p_card = new Panel ();
p_card.setLayout (cdLayout);
//make the 2 screens
hall (); //1
billard (); //2
resize (300, 300);
setLayout (new BorderLayout ());
add ("Center", p_card);
}
public void hall ()
{ //Pre: p_card is a cdLayout, card1 is declared
//Post: initializes opening screen's widgets.
card1 = new Panel ();
card1.setBackground (Color.white);
JLabel hall = new JLabel (createImageIcon ("pics\\hall.gif"));
JLabel title = new JLabel (" Find Me! ");
title.setFont (new Font ("Arial", Font.BOLD, 30));
title.setForeground (Color.red);
JLabel title2 = new JLabel ("You are in the main hall.");
title2.setFont (new Font ("Arial", Font.BOLD, 18));
title2.setForeground (Color.blue);
JLabel title3 = new JLabel ("Look throughout my house to find me.");
JButton billard = new JButton ("Billard Room");
billard.setActionCommand ("2");
billard.addActionListener (this);
JButton dining = new JButton ("Dining Room");
dining.setActionCommand ("3");
dining.addActionListener (this);
JButton conserve = new JButton ("Conservatory");
conserve.setActionCommand ("4");
conserve.addActionListener (this);
JButton ball = new JButton ("Ballroom");
ball.setActionCommand ("5");
ball.addActionListener (this);
Panel p = new Panel (new GridLayout (4, 1));
p.add (billard);
p.add (dining);
p.add (conserve);
p.add (ball);
card1.add (title);
card1.add (hall);
card1.add (title2);
card1.add (title3);
card1.add (p);
p_card.add ("1", card1);
}
public void billard ()
{ //Pre: p_card is a cdLayout, card2 is declared
//Post: initializes rules screen's widgets.
card2 = new Panel ();
card2.setBackground (Color.white);
JLabel title = new JLabel ("Billard Room");
title.setFont (new Font ("Arial", Font.BOLD, 18));
title.setForeground (Color.red);
JLabel words = new JLabel ("I'm not here! Keep Looking.");
JLabel pic = new JLabel (createImageIcon ("pics\\billard.gif"));
//to move to the next screen
JButton next2 = new JButton ("Back to Hall", createImageIcon ("pics\\hall.gif"));
next2.setActionCommand ("1");
next2.addActionListener (this);
card2.add (pic);
card2.add (title);
card2.add (words);
card2.add (next2);
p_card.add ("2", card2);
}
/** Listens to the radio buttons and buttons.
@param e.getActionCommand holds Again, Reset, rock, scissors, paper
*/
public void actionPerformed (ActionEvent e)
{
//moves between the screens
if (e.getActionCommand ().equals ("1"))
cdLayout.show (p_card, "1");
else if (e.getActionCommand ().equals ("2"))
cdLayout.show (p_card, "2");
}
/**
Makes an image icon to go on a button
@param path is a valid path to a jpg or gif image
@return an ImageIcon, or null if the path was invalid.
Directly from: http://java.sun.com/docs/books/tutorial/uiswing/components/example-1dot4/index.html#RadioButtonDemo
*/
protected static ImageIcon createImageIcon (String path)
{
java.net.URL imgURL = FindMe.class.getResource (path);
if (imgURL != null)
{
return new ImageIcon (imgURL);
}
else
{
System.err.println ("Couldn't find file: " + path);
return null;
}
}
}
The two pictures: (billard.gif and hall.gif)
...