cat class

Game development specific discussions.
Post Reply
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

cat class

Post by BasicCoder2 »

This was my attempt at using OOP to appease MrSwiss

Redirected from,
viewtopic.php?f=15&p=279855#p279855
MrSwiss wrote:
@BasicCoder2,
please do us all a favour and, don't mess up beginners brains.
Here was my beginning effort at a cat class.

Code: Select all

type Cat
    as integer action
    
    'body states
    as integer hunger = 100
    as integer thirst = 100
    as integer tired  = 100
    as integer lonely = 100
    as integer state

    as string ACTIONS(0 to 5)

    Declare Sub Draw()
    Declare Sub Update()
    
    declare constructor()
    declare constructor(as string, as integer, as string, as string, as integer)
    declare destructor() 
    
end type

Sub Cat.Draw()
    print this.hunger
    print this.thirst
    print this.tired
    print this.lonely
    print this.ACTIONS(this.state)
end sub

constructor cat ()
    ACTIONS(0) = "sitting"
    ACTIONS(1) = "sleeping"
    ACTIONS(2) = "playing"
    ACTIONS(3) = "eating"
    ACTIONS(4) = "purring"
    ACTIONS(5) = "drinking"
end constructor

destructor cat
end destructor


dim shared fiori as Cat = cat()
dim shared fluffy as Cat = cat()

sub Cat.update()
    this.hunger = this.hunger - 1
    this.thirst = this.thirst - 1
    this.tired  = this.tired  - 1
    this.lonely = this.lonely - 1
end sub

sub update()
    fiori.Update()
    fluffy.Update()
end sub

sub display()
    screenlock
    cls
    fiori.Draw()
    print "==========="
    fluffy.Draw()
    screenunlock
end sub

dim as double st
st = TIMER

do
    if TIMER-st > 0.5 then
        st = TIMER
        update()
        display()
        fiori.state = int(rnd(1)*6)
        fluffy.state = int(rnd(1)*6)
    end if
    sleep 2
loop until multikey(&H01)
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: cat class

Post by MrSwiss »

@BasicCoder2,

first: it isn't OOP, as long as "inheritance" is NOT part of the deal.
second: you are stll using "shared" variables (which is the "most critique part" of all).
Try to do it, without that "shared" nonsense and, you're on the way to better code.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: cat class

Post by BasicCoder2 »

But when I remove the shared they are no longer seen within the update and display routines.
So do I display everything in the main loop as shown below?
With inheritance I assume you mean deriving the cat class from the animal class or some such thing?

Code: Select all

type Cat
    as integer action
    
    'body states
    as integer hunger = 100
    as integer thirst = 100
    as integer tired  = 100
    as integer lonely = 100
    as integer state

    as string ACTIONS(0 to 5)

    Declare Sub Draw()
    Declare Sub Update()
    
    declare constructor()
    declare constructor(as string, as integer, as string, as string, as integer)
    declare destructor() 
    
end type

Sub Cat.Draw()
    print this.hunger
    print this.thirst
    print this.tired
    print this.lonely
    print this.ACTIONS(this.state)
end sub

constructor cat ()
    ACTIONS(0) = "sitting"
    ACTIONS(1) = "sleeping"
    ACTIONS(2) = "playing"
    ACTIONS(3) = "eating"
    ACTIONS(4) = "purring"
    ACTIONS(5) = "drinking"
end constructor

destructor cat
end destructor

sub Cat.update()
    this.hunger = this.hunger - 1
    this.thirst = this.thirst - 1
    this.tired  = this.tired  - 1
    this.lonely = this.lonely - 1
end sub

dim fiori as Cat = Cat()
dim fluffy as Cat = Cat()

dim as double st
st = TIMER

do
    if TIMER-st > 0.5 then
        st = TIMER
        screenlock
        cls
        fiori.Update()
        fluffy.Update()
        fiori.Draw()
        print "==========="
        fluffy.Draw()
        fiori.state = int(rnd(1)*6)
        fluffy.state = int(rnd(1)*6)
        screenunlock
    end if
    sleep 2
loop until multikey(&H01)
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: cat class

Post by MrSwiss »

BasicCoder2 wrote:But when I remove the shared they are no longer seen within the update and display routines.
So do I display everything in the main loop as shown below?
That is definitely one way of doing it.
If you need to pass variables to procedures, then you have to "pass" them as parameters (aka: arguments).
They must of course be redefined accordingly ...
BasicCoder2 wrote:With inheritance I assume you mean deriving the cat class from the animal class or some such thing?
Yes, that's "inheritance".
paul doe
Moderator
Posts: 1733
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: cat class

Post by paul doe »

BasicCoder2 wrote:But when I remove the shared they are no longer seen within the update and display routines.
...
Precisely the point.
...
So do I display everything in the main loop as shown below?
...

Code: Select all

#include once "fbgfx.bi"

enum CatState
  Sitting
  Sleeping
  Playing
  Eating
  Purring
  Drinking
end enum

type Cat
    as integer action
   
    'body states
    as integer hunger = 100
    as integer thirst = 100
    as integer tired  = 100
    as integer lonely = 100
    as integer state

    as string ACTIONS(0 to 5)
    as string name
    
    declare constructor()
    declare constructor( as string, as CatState )
    declare destructor()
end type

constructor cat ()
  constructor( "Unnamed", Sitting )
end constructor

constructor Cat( aName as string, aState as CatState )
  name = aName
  state = aState
  
  ACTIONS(0) = "sitting"
  ACTIONS(1) = "sleeping"
  ACTIONS(2) = "playing"
  ACTIONS(3) = "eating"
  ACTIONS(4) = "purring"
  ACTIONS(5) = "drinking"
end constructor

destructor cat
end destructor

sub update( cats() as Cat )
  for i as integer = 0 to ubound( cats )
    with cats( i )
      .hunger -= 1
      .thirst -= 1
      .tired -= 1
      .lonely -= 1
      .state = int( rnd() * ( ubound( .ACTIONS ) + 1 ) )
    end with
  next
end sub

sub render( cats() as Cat )
  screenLock()
    cls()
    
    for i as integer = 0 to ubound( cats )
      with cats( i )
        ? .name
        ? .hunger
        ? .thirst
        ? .tired
        ? .lonely
        ? .ACTIONS( .state )
      end with
      
      ? iif( i < ubound( cats ), "===========", "" )
    next
  screenUnlock()
end sub

screenRes( 800, 400, 32 )

dim as Cat cats( ... ) = { _
  Cat( "Fiori", Sitting ), _
  Cat( "Fluffy", Sleeping ) }

dim as double st
st = TIMER

do
    if TIMER-st > 0.5 then
      update( cats() )
      render( cats() )
    end if
    sleep 2
loop until multikey( Fb.SC_ESCAPE )
Learn to pass parameters to functions as required. Also, try not to hardcode limits nor using 'magic numbers'.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: cat class

Post by BasicCoder2 »

@Paul Doe

Thanks for the input Paul, I will see how I can use it in my own code.

I like BASIC for reading much like a natural language so I avoid short cuts like ? for PRINT or using C-like code although I do realise the speed enhancement such c-like code can sometimes offer.

It will be a challenge to figure out how to finish this OO virtual cat project as I add more features to it.

Have added another cat which I will probably call Ginge :)
Perhaps a zoo full of virtual animals to be looked after with all their different needs and behaviors.

Image
Image
Post Reply