Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Sammy Samkough
- // Virtual Pet Object
- // Spec: This class encapsulates all of the things we wish our Virtual Pet to be able to do.
- // We want to model the behavior of an actual pet.
- import java.util.Random;
- import java.awt.*;
- import javax.swing.*;
- public class VirtualPet
- {
- // You can keep these categories and icons, or make your own:
- private int step, happiness, health, food;
- private double age;
- private ImageIcon icoHappy, icoSad, icoHungry, icoUnhealthy, icoDead;
- private boolean notDead;
- /** initialize data to zero to start with
- * instantiate ImageIcons to the graphics files that you saved
- * ie icoHappy = new ImageIcon("HappyPet.gif") etc.
- */
- public VirtualPet()
- {
- // Categories
- age = 0;
- step = 5;
- happiness = 5;
- health = 5;
- food = 5;
- notDead = true;
- // ImageIcons
- icoHappy = new ImageIcon("HappyPet.jpg");
- icoSad = new ImageIcon("SadPet.jpg");
- icoHungry = new ImageIcon("HungryPet.jpg");
- icoUnhealthy = new ImageIcon("UnhealthyPet.jpg");
- icoDead = new ImageIcon("DeadPet.jpg");
- }
- /** Post Condition: health and happiness are increased
- * food is decreased
- * These should be random amounts in a range that you create
- */
- public void walkPet()
- {
- if (notDead == true)
- {
- health += (int) (Math.random() * 5) + 1;
- happiness += (int) (Math.random() * 5) + 1;
- food -= (int) (Math.random() * 3) + 1;
- }
- }
- /** Post Condition: food is increased by a small random amount
- */
- public void feedPet()
- {
- if (notDead == true)
- {
- food += (int) (Math.random() * 3) + 1;
- }
- }
- /** Post Condition: Happiness is increased and health is increased slightly as well
- * food is decreased slightly... playing burns calories!
- */
- public void playWithPet()
- {
- if (notDead == true)
- {
- happiness += (int) (Math.random() * 5) + 1;
- health += (int) (Math.random() * 2) + 1;
- food -= (int) (Math.random() * 2) + 1;
- }
- }
- /** Post Condition: Health is increased; Happiness is decreased
- * food is decreased
- */
- public void takeToVet()
- {
- if (notDead == true)
- {
- health += (int) (Math.random() * 5) + 1;
- happiness -= (int) (Math.random() * 4) + 1;
- food -= (int) (Math.random() * 3) + 1;
- }
- }
- /** Post Condition: Health is increased and happiness slightly as well
- * food is slightly decreased
- */
- public void sleep()
- {
- if (notDead == true)
- {
- health += (int) (Math.random() * 7) + 1;
- happiness += (int) (Math.random() * 4) + 1;
- food -= (int) (Math.random() * 3) + 1;
- }
- }
- /** Everything that should happen in any given time interval
- * each category should be affected in some way
- */
- public void takeStep()
- {
- if(notDead == true)
- {
- age += .3;
- health -= age;
- happiness -= age;
- food--;
- }
- }
- /** @return the appropriate ImageIcon based on the current state of this pet
- */
- public ImageIcon getIcon()
- {
- if(food < 0 && health < 0 && happiness < 0)
- {
- notDead = false;
- return icoDead;
- }
- else if(happiness < 0 && notDead == true)
- {
- return icoSad;
- }
- else if(food < 0 && notDead == true)
- {
- return icoHungry;
- }
- else if(health < 0 && notDead == true)
- {
- return icoUnhealthy;
- }
- else
- {
- return icoHappy;
- }
- }
- /** @return a String representing the current state of this VirtualPet
- * Label each category and show the current value for each
- */
- public String toString()
- {
- String result = "";
- if(!notDead)
- {
- result += "Wow. Good Job, you killed him.";
- }
- else
- {
- result += "age: " + (int)age + " happiness " + happiness + " health " + health + " food " + food;
- }
- return result;
- }
- }
- ------------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // Virtual Pet Applet
- // Spec: An applet front-end for a VirtualPet
- // Utilizes a Timer with it's own ActionListener
- // and another ActionListener for the buttons.
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class VirtualPetApplet extends JApplet
- {
- private VirtualPet spot;
- private Timer tmr;
- private TimerListener tmrListener;
- private ButtonListener btnListener;
- private JButton btnWalk, btnFeed, btnPlay, btnVet, btnSleep;
- private JLabel lblPet, lblStats;
- public void init()
- {
- Container cp = getContentPane();
- Frame c = (Frame)this.getParent().getParent();
- cp.setLayout(new FlowLayout());
- tmrListener = new TimerListener();
- btnListener = new ButtonListener();
- spot = new VirtualPet();
- tmr = new Timer(5000, tmrListener);
- tmr.start();
- btnWalk = new JButton("Walk");
- btnFeed = new JButton("Feed");
- btnPlay = new JButton("Play");
- btnVet = new JButton("Vet");
- btnSleep = new JButton("Sleep");
- lblPet = new JLabel(spot.getIcon());
- lblStats = new JLabel("");
- btnWalk.addActionListener(btnListener);
- btnFeed.addActionListener(btnListener);
- btnPlay.addActionListener(btnListener);
- btnVet.addActionListener(btnListener);
- btnSleep.addActionListener(btnListener);
- cp.add(lblPet);
- cp.add(lblStats);
- cp.add(btnWalk);
- cp.add(btnFeed);
- cp.add(btnPlay);
- cp.add(btnVet);
- cp.add(btnSleep);
- lblStats.setText(spot.toString());
- c.setTitle("Virtual Pet");
- setSize(900, 450);
- setVisible(true);
- }
- private class TimerListener implements ActionListener
- {
- public void actionPerformed(ActionEvent event)
- {
- // This will happen at every timer interval as specified above
- // At each interval our pet (spot) should:
- // - Take a step
- // - Update the stats in lblStats
- // - Update the icon in lblPet
- spot.takeStep();
- lblStats.setText(spot.toString());
- lblPet.setIcon(spot.getIcon());
- }
- }
- private class ButtonListener implements ActionListener
- {
- public void actionPerformed(ActionEvent event)
- {
- Object source = event.getSource();
- // Build an extended-if to account for each of the other buttons in your interface
- if(source == btnWalk)
- {
- spot.walkPet();
- }
- else if(source == btnFeed)
- {
- spot.feedPet();
- }
- else if(source == btnPlay)
- {
- spot.playWithPet();
- }
- else if(source == btnVet)
- {
- spot.takeToVet();
- }
- else if(source == btnSleep)
- {
- spot.sleep();
- }
- // Update the screen for each click:
- // - Update the stats in lblStats
- // - Update the icon in lblPet
- lblStats.setText(spot.toString());
- lblPet.setIcon(spot.getIcon());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement