IPT - A Virtual Approach IPT A Virtual Approach by Peter Whitehouse
Quick Links:
Information and Intelligent Systems Social and Ethical Implications Human Computer Interaction Software and Systems Engineering eXercise Files Course Outline and Assessment A-Z of Geeky Acronyms Terrace Work Program 2004 Sillybus FAQ = Frequently Asked Questions Help

A Virtual Pet


A Simple application based on timer events

wonkoPET running

This application uses a number of timers to control 'life processes', gauges to display them and some simple graphics to create the cutesy face 'pet' that reacts to how it is feeling.

wonkoPET Design

The Components:

BEFORE beginning this project, create a working directory, and then save the following images into that directory: wakeyes.wmf, sleepeye.wmf, happym.wmf, sickm.wmf and sadm.wmf (if your brawser has screwy file associations, a zip file of all the graphics used is also available).

What follows is a list of components needed for this application, along with some properties that need to be set for those components. Be systematic and TIDY placing your components.

  • Change FORM1's Name to frmPetMain, and it's Caption to wonkoPET
  • From the Win32 Component menu, add a PROGRESSBAR, Name it prbRestLevel.
  • From the SYSTEM Component menu, add a TIMER, name it timRest
  • From the STANDARD Component menu, add a BUTTON, name it butRest, change it's Caption to rest
  • repeat the previous step for food, waste, play and sick
  • From the ADDITIONAL Component menu, add a SHAPE, change it's Name to shPetFace, change it's Shape to stCircle and change it's Brush-Color to clWhite, then stretch it to size.
  • From the ADDITIONAL Component menu, add an IMAGE, change it's Name to imgEyes, change it's Picture property by loading the file wakeyes.wmf, and setting the Stretch property to true, then resize the eyes to fit the head nicely.
  • Repeat the above step for an IMAGE called imgMouth
  • From the STANDARD Component menu, add a BUTTON, Name it butRIP, set it's Caption to match the name.

Other Objects

Once you have got the 'look and feel' of this system, you will want to add more timer based events to further effect the life statistics in a less predicatable manner.

The Event Handlers

There are a number of simple event handlers that are necessary for the successful control of this 'lifeform'.

Much of the Initialization for this project occurs in the onCreate Event for the form - the timers are set in motion and the pet's life statistics begin to change.

Component Event Event Handler Code
frmPetMain onCreate
  {'seed' random number generator}
  {different colour pet and form each run}
  frmPetMain.color := RGB(random(256),random(256),random(256));
  shPetFace.brush.color := RGB(random(256),random(256),random(256));
  {each new pet gets a different set of initial values for life statistics}
  prbRestLevel.position := random(100);
  prbFoodLevel.position := random(100);
  prbWasteLevel.position := random(100);
  prbPlayLevel.position := random(100);
  prbSickLevel.position := random(100);
  {each pet has unique rates of change for life statistics}
  timRest.interval := random(10000)+10;
  timRood.interval := random(10000)+10;
  timWaste.interval := random(10000)+10;
  timPlay.interval := random(10000)+10;
  timSick.interval := random(10000)+10;
butRIP onClick
butRest onClick
  {change eyes to sleeping}
  {add some rest}
  prbRestLevel.position := restLevel.position + 5;
  {more rest means less play}
  prbPlayLevel.position := playLevel.position - 1
timRest onTimer
  {as time ticks by, the pet gets tired = less rested}
  restLevel.position := restLevel.position -1;
  {if the pet gets too tired, it gets sicker quicker}
  if prbRestLevel.position< 50
    then prbSickLevel.position := prbSickLevel.position + 2
butFood onClick
  {you must be awake if you are going to eat}
  {add some food}
  prbFoodLevel.position := prbFoodLevel.position + 5;
  {more food = more waste}
  prbWasteLevel.position := prbWasteLevel.position+8
timFood onTimer
  {remove some food reserves}
  prbFoodLevel.position := prbFoodLevel.position -1;
  {if food reserves fall half way, the pet becomes sad}
  if prbFoodLevel.position< 50
     then imgMouth.picture.loadfromfile('sadm.wmf');
  {if food runs out, the pet dies}
  if prbFoodLevel.position = 0
     then close
timWaste onTimer
  {add some waste}
  prbWasteLevel.position := prbWasteLevel.position +1;
  {lots of waste makes a pet feel sick}
  if prbWasteLevel.position> 25
     then ingMouth.picture.loadfromfile('sickm.wmf');
  {excess waste kills the pet}
  if prbWasteLevel.position = 100
     then close
timSick onTimer
  {add a little sickness}
  prbSickLevel.position := prbSickLevel.position +1;
  {if only a little sick, the pet is happy}
  if prbSickLevel.position< 20
     then imgMouth.picture.loadfromfile('happym.wmf')
     {otherwise pet looks sicker}
     else if prbSickLevel.position< 50
             then imgMouth.picture.loadfromfile('sickm.wmf')
             else imgMouth.picture.loadfromfile('sadm.wmf');
  {too much sickness kills the pet}
  if prbSickLevel.position = 100
     then close

Each of the BUTTONS and TIMERS need some code, from the above, you should see the pattern - some life statistics go up whilst others go down.

Final Touches

COMPILE the program to check for errors of 'SYNTAX', fix any that show, then RUN it to check it works correctly. Test it by over-feeding it, allowing to to die of sicness or waste build up.


©Copyright t 1992..2017+. Edition 25.150117
Creative Commons License
This work is licensed under a
Creative Commons Attribution-NonCommercial-ShareAlike 2.1 Australia License