News:

Welcome to RetroCoders Community

Main Menu

Tamagutchi cat program 2 versions

Started by ron77_db, Apr 03, 2022, 09:37 AM

Previous topic - Next topic

ron77_db

hello here is a relic of my attempt to make a Tamagotchi can program in freebasic...

version 1 with freebasic OOP:

'#INCLUDE ONCE "fbsound_dynamic.bi"
#INCLUDE "vbcompat.bi"

CONST AS SHORT scr_w = 800, scr_h = 600 ' screen constants
CONST file AS STRING = "cat_log.txt"

ENUM age
   kitten
   young
   adult
   old
END ENUM

TYPE cat
   PRIVATE:
   AS STRING cat_name
   PUBLIC:
   AS DOUBLE d1, d2
   AS STRING text1, text2, text3, text4, text5, text6, cat_age
   AS boolean isOut, isneglect
   AS INTEGER hunger, pet_count, nap_count
   DECLARE PROPERTY cat_name1() AS STRING
   DECLARE PROPERTY cat_name1(BYVAL cat_name AS STRING)
   DECLARE SUB time_diff()
   DECLARE SUB settings(i AS age)
   DECLARE SUB pet_cat(key AS STRING)
   DECLARE SUB CP(row AS INTEGER, s AS STRING)
   DECLARE SUB display_screen(index AS INTEGER)
   'DECLARE SUB make_sound(f AS STRING, hWave AS INTEGER,t AS INTEGER)
   DECLARE SUB animation()
   DECLARE SUB cat_hunger(key AS STRING)
   DECLARE SUB nap_time(inx AS INTEGER)
   DECLARE SUB cat_napping()
   DECLARE SUB write_file(filename AS STRING)
   DECLARE SUB read_file(filename AS STRING)
   DECLARE CONSTRUCTOR()
   DECLARE DESTRUCTOR()
   
END TYPE

CONSTRUCTOR cat
this.isOut = FALSE
THIS.text1 = ""
this.text2 = ""
this.text3 = ""
this.text4 = ""
this.text5 = ""
END CONSTRUCTOR

DESTRUCTOR cat

END DESTRUCTOR

PROPERTY cat.cat_name1() AS STRING
RETURN this.cat_name
END PROPERTY

PROPERTY cat.cat_name1(BYVAL cat_name AS STRING)
this.cat_name = cat_name
END PROPERTY

SUB cat.write_file(filename AS STRING)
   this.d1 = NOW()
   
   DIM f AS LONG = FREEFILE()
   OPEN filename FOR OUTPUT AS #f
   WRITE #f, cat_name1
   WRITE #f, this.hunger
   WRITE #f, THIS.pet_count
   WRITE #f, this.nap_count
   WRITE #f, this.isOut
   WRITE #f, this.cat_age
   write #f, this.d1
   CLOSE #f
END SUB

SUB cat.read_file(filename AS STRING)
   DIM h AS LONG = FREEFILE()
   DIM fline AS STRING
   OPEN filename FOR INPUT AS #h
   INPUT #h, fline
   cat_name1 = fline
   INPUT #h, this.hunger
   INPUT #h, THIS.pet_count
   INPUT #h, this.nap_count
   INPUT #h, this.isOut
   INPUT #h, this.cat_age
   INPUT #h, this.d1
   CLOSE #h
   
END SUB

sub cat.time_diff()
   this.d2 = NOW()
   IF DATEDIFF("d", this.d1, this.d2) > 7 THEN
      this.isneglect = TRUE
   ENDIF
END sub


SUB cat.nap_time(inx AS INTEGER)
   SELECT CASE inx
   CASE 0:
   CLS
   CP 4, "------------"
   CP 5, "          / "
   CP 6, "         /  "
   CP 7, "        /   "
   CP 8, "       /    "
   CP 9, "      /     "
   CP 10, "     /      "
   CP 11, "    /       "
   CP 12, "   /        "
   CP 13, "  /         "
   CP 13, " /          "
   CP 14, "------------"
   CP 35, text5
   CP 15, this.cat_age & " " & cat_name1
CASE 1:
CLS
CP 4, "%%%%%%%%%%%%"
CP 5, "          % "
CP 6, "         %  "
CP 7, "        %   "
CP 8, "       %    "
CP 9, "      %     "
CP 10, "     %      "
CP 11, "    %       "
CP 12, "   %        "
CP 13, "  %         "
CP 13, " %          "
CP 14, "%%%%%%%%%%%%"
CP 35, text5
CP 15, this.cat_age & " " & cat_name1
CASE 2:
CLS
CP 4, "zzzzzzzzzzzz"
CP 5, "          z "
CP 6, "         z  "
CP 7, "        z   "
CP 8, "       z    "
CP 9, "      z     "
CP 10, "     z      "
CP 11, "    z       "
CP 12, "   z        "
CP 13, "  z         "
CP 13, " z          "
CP 14, "zzzzzzzzzzzz"
CP 35, text5
CP 15, this.cat_age & " " & cat_name1
END SELECT
END SUB

SUB cat.settings(i AS age)
   SELECT CASE i
   CASE 0:
   this.hunger = INT(RND*101)
   this.pet_count = INT(RND*201)
   this.nap_count = INT(RND*151)
   this.cat_age = "kitten"
CASE 1:
this.hunger = INT(RND*201)
this.pet_count = INT(RND*301)
this.nap_count = INT(RND*250)
this.cat_age = "young cat"
CASE 2:
this.hunger = INT(RND*351)
this.pet_count = INT(RND*451)
this.nap_count = INT(RND*401)
this.cat_age = "adult cat"
CASE 3:
this.hunger = INT(RND*301)
this.pet_count = INT(RND*401)
this.nap_count = INT(RND*161)
this.cat_age = "old cat"   
END SELECT
END SUB




SUB cat.pet_cat(key AS STRING)
   
   THIS.pet_count -= 1
   
   IF THIS.pet_count <= 50 THEN
      this.text4 = "the cat need some petting press key 'p' to pet cat!"
     
   ELSE
      this.text4 = ""
     
      ENDIF
      IF key = CHR(112) THEN
         THIS.pet_count += 1000
         this.text4 = ""
         ENDIF
         
      END SUB
     
      SUB cat.CP(row AS INTEGER, s AS STRING)
         LOCATE row, (100 - LEN(s)) / 2 : PRINT s
      END SUB
     
      SUB cat.display_screen(index AS INTEGER)
         
         SELECT CASE index
         CASE 0:
         CLS
         CP 15, this.cat_age & " " &cat_name1
         CP 4, "^                        ^"
         CP 5, "( ) %%%%%%%%%%%%%%%%%%%  ( )"
         CP 6, "<| (( <|> ))  ||   ((  <|>  )) |>"
         CP 7, "_                              _"
         CP 8, "_                            _"
         CP 9, "_    >(::| * |::)<     _"
         CP 10, "        -------          "
         CP 11, "(  -----  )"
         CP 12, "\\---//"
         CP 13, "%%%"
         CP 34, text1
         CP 35, text2
         CP 32, text4
         CP 33, text3
         CP 31, text6
      CASE 1:
      CLS
      CP 15, this.cat_age & " " &cat_name1
      CP 4, "^                        ^"
      CP 5, "( ) %%%%%%%%%%%%%%%%%%%  ( )"
      CP 6, "<| (( <I> ))  ||   ((  <I>  )) |>"
      CP 7, "_                              _"
      CP 8, "_                            _"
      CP 9, "_    >(::| * |::)<     _"
      CP 10, "                          "
      CP 11, "(  OOOO  )"
      CP 12, "\\   //"
      CP 13, "%%%"
      CP 34, text1
      CP 35, text2
      CP 32, text4
      CP 33, text3
      CP 31, text6
   CASE 2:
   CLS
   CP 15, this.cat_age &" "& cat_name1
   CP 4, "^                        ^"
   CP 5, "( ) %%%%%%%%%%%%%%%%%%%  ( )"
   CP 6, "<| (( <dOb> ))  ||   ((  <dOb>  )) |>"
   CP 7, "_                              _"
   CP 8, "_                            _"
   CP 9, "_    >(::| & |::)<     _"
   CP 10, "                          "
   CP 11, "(  ~~~~~  )"
   CP 12, "\\ ~~~ //"
   CP 13, "%%%"
   CP 34, text1
   CP 35, text2
   CP 32, text4
   CP 33, text3
   CP 31, text6
END SELECT
END SUB

SUB cat.cat_napping()
   
   DIM x AS INTEGER = 0
   DIM xx AS INTEGER = 50
   
   DO
      nap_time(x)
      this.text5 = "NAP TIME!"
      x += 1
      xx -= 1
      SLEEP 250
      IF x >= 3 THEN x = 0
   LOOP UNTIL xx <= 0
   
END SUB
SUB cat.cat_hunger(key AS STRING)
   
   IF this.hunger < 25 THEN
      this.text1 = "cat is hungry! press key 'f' to feed it"
   ELSE
      this.text1 = ""

   ENDIF
   IF this.hunger <= -450 THEN
     isOut = TRUE
   
   ENDIF
   IF key = CHR(102) THEN
      this.hunger += 1000
   ENDIF
END SUB
         
         
         
'SUB cat.make_sound(f AS STRING, hWave AS INTEGER, t AS INTEGER)
   '
   'fbs_Load_WAVFile(f , @hWave)
   'fbs_Play_Wave(hWave , t)
   '
'END SUB
         
'SUB CAT.ANIMATION(f AS STRING, t AS INTEGER)
SUB CAT.ANIMATION()
         VAR key = "" , hwave = 0
         DO
      WITH THIS
            IF isOut = TRUE OR NOT CBool(FILEEXISTS(file)) OR isNeglect = TRUE THEN
               DIM AS STRING catname, age1
               INPUT "enter cat name or enter 'quit' to exit: ", catname
               IF catname = "quit" THEN
                  write_file(file)
                  END
               ENDIF
               cat_name1 = catname
               INPUT "select age of cat - 0 for kitten, 1 for young, 2 for adult, 3 for old: ", age1
               settings(Cast(Age, Val(age1)))
           
            ELSE
               read_file(file)
            ENDIF
               isOut = FALSE
               isNeglect = FALSE
            DIM i AS INTEGER = 0
            'make_sound(f,hWave, t)
                       
            DIM x AS INTEGER = 0
            DIM xx AS INTEGER = 50
            DO
                  key = INKEY
               IF nap_count > 50 THEN
                  'IF key = CHR(115) THEN
                     'make_sound(f,hWave, t)
                  'ENDIF
                 
                  IF i >= 3 THEN i = 0
                     DISPLAY_SCREEN(i)
                     SLEEP 500
                     i += 1
                     cat_hunger(key)
                     pet_cat(key)
                     time_diff()
                  IF isOut = TRUE OR isneglect = TRUE THEN EXIT DO
                     text2 = " cat hunger is: " & hunger & " cat napping counter is: " & nap_count & _
                     " cat pet time is: " & pet_count
                     nap_count -= 1
                     hunger -= 1
                  IF key = CHR(27) THEN
                     write_file(file)
                     END
                  ENDIF
               ELSEIF nap_count <= 50 THEN
                  cat_napping()
                  nap_count = INT(RND*151)
               ENDIF 
                                 
            LOOP
               'fbs_Destroy_Wave(@hWave)
         IF isOut = TRUE OR isNeglect = TRUE THEN
            CLS
               
            text2 = "IT'S BEEN " & DATEDIFF("d", this.d1, this.d2) & " MINUTES SINCE YOU LAST OPEN THE PROGRAM"
            text1 = "CAT DIED FROM NEGLECT!!!"
            text4 = "PRESS ANY KEY TO START OVER WITH A NEW CAT!"
            CP 31, text1
            CP 34, text4
            CP 30, text2
            SLEEP
            cls
            write_file(file)
         ENDIF
      END WITH
   LOOP UNTIL INKEY() = CHR(27)
                           
END SUB


'IF fbs_Init()=false THEN
'PRINT "error: FBS_INIT() " & FBS_Get_PlugError()
'BEEP : SLEEP : END 1
'END IF

SCREENRES scr_w, scr_h
WIDTH scr_w \ 8, scr_h \ 16
DIM SHARED fiori AS cat
'fiori.animation("cat_sound.wav", 1)
fiori.animation()

I was asked if I could make the same program of Tamagotchi cat but without OOP so here is a second version of the Tamagotchi program without the OOP in freebasic so you can compare and see for yourselves

version 2 without freebasic OOP:

'#INCLUDE ONCE "fbsound_dynamic.bi"
#INCLUDE "vbcompat.bi"

CONST AS SHORT scr_w = 800, scr_h = 600 ' screen constants
CONST file AS STRING = "cat_log.txt"

ENUM age
	kitten
	young
	adult
	old
END ENUM

TYPE cat
	AS STRING cat_name
	AS DOUBLE d1, d2
	AS STRING text1, text2, text3, text4, text5, text6, cat_age
	AS boolean isOut, isneglect
	AS INTEGER hunger, pet_count, nap_count
end type   

'DECLARE PROPERTY cat_name1() AS STRING
'DECLARE PROPERTY cat_name1(BYVAL cat_name AS STRING)
'DECLARE SUB time_diff()
'DECLARE SUB settings(i AS age)
'DECLARE SUB pet_cat(key AS STRING)
'DECLARE SUB CP(row AS INTEGER, s AS STRING)
'DECLARE SUB display_screen(index AS INTEGER)
''DECLARE SUB make_sound(f AS STRING, hWave AS INTEGER,t AS INTEGER)
'DECLARE SUB animation()
'DECLARE SUB cat_hunger(key AS STRING)
'DECLARE SUB nap_time(inx AS INTEGER)
'DECLARE SUB cat_napping()
'DECLARE SUB write_file(filename AS STRING)
'DECLARE SUB read_file(filename AS STRING)
'DECLARE CONSTRUCTOR()
'DECLARE DESTRUCTOR()

'dim fiori as CAT
SUB CP(row AS INTEGER, s AS STRING)
	LOCATE row, (100 - LEN(s)) / 2 : PRINT s
END SUB


function cat_name2(c as cat) AS STRING
	RETURN c.cat_name
END function

sub cat_name1(BYVAL cat_name AS STRING, c as cat)
	c.cat_name = cat_name
END sub

SUB write_file(filename AS STRING, c as cat)
	c.d1 = NOW()
	
	DIM f AS LONG = FREEFILE()
	OPEN filename FOR OUTPUT AS #f
	WRITE #f, cat_name2(c)
	WRITE #f, c.hunger
	WRITE #f, c.pet_count
	WRITE #f, c.nap_count
	WRITE #f, c.isOut
	WRITE #f, c.cat_age
	write #f, c.d1
	CLOSE #f
END SUB

SUB read_file(filename AS STRING, c as cat)
	DIM h AS LONG = FREEFILE()
	DIM fline AS STRING
	OPEN filename FOR INPUT AS #h
	INPUT #h, fline
	cat_name1(fline, c)
	INPUT #h, c.hunger
	INPUT #h, c.pet_count
	INPUT #h, c.nap_count
	INPUT #h, c.isOut
	INPUT #h, c.cat_age
	INPUT #h, c.d1
	CLOSE #h
	
END SUB

sub time_diff(c as cat)
	c.d2 = NOW()
	IF DATEDIFF("d", c.d1, c.d2) > 7 THEN
		c.isneglect = TRUE
	ENDIF
END sub


SUB nap_time(inx AS INTEGER, c as cat)
	SELECT CASE inx
		CASE 0:
			CLS
			CP 4, "------------"
			CP 5, "          / "
			CP 6, "         /  "
			CP 7, "        /   "
			CP 8, "       /    "
			CP 9, "      /     "
			CP 10, "     /      "
			CP 11, "    /       "
			CP 12, "   /        "
			CP 13, "  /         "
			CP 13, " /          "
			CP 14, "------------"
			CP 35, c.text5
			CP 15, c.cat_age & " " & cat_name2(c)
		CASE 1:
			CLS
			CP 4, "%%%%%%%%%%%%"
			CP 5, "          % "
			CP 6, "         %  "
			CP 7, "        %   "
			CP 8, "       %    "
			CP 9, "      %     "
			CP 10, "     %      "
			CP 11, "    %       "
			CP 12, "   %        "
			CP 13, "  %         "
			CP 13, " %          "
			CP 14, "%%%%%%%%%%%%"
			CP 35, c.text5
			CP 15, c.cat_age & " " & cat_name2(c)
		CASE 2:
			CLS
			CP 4, "zzzzzzzzzzzz"
			CP 5, "          z "
			CP 6, "         z  "
			CP 7, "        z   "
			CP 8, "       z    "
			CP 9, "      z     "
			CP 10, "     z      "
			CP 11, "    z       "
			CP 12, "   z        "
			CP 13, "  z         "
			CP 13, " z          "
			CP 14, "zzzzzzzzzzzz"
			CP 35, c.text5
			CP 15, c.cat_age & " " & cat_name2(c)
	END SELECT
END SUB

SUB settings(i AS age, c as cat)
	SELECT CASE i
		CASE 0:
			c.hunger = INT(RND*101)
			c.pet_count = INT(RND*201)
			c.nap_count = INT(RND*151)
			c.cat_age = "kitten"
		CASE 1:
			c.hunger = INT(RND*201)
			c.pet_count = INT(RND*301)
			c.nap_count = INT(RND*250)
			c.cat_age = "young cat"
		CASE 2:
			c.hunger = INT(RND*351)
			c.pet_count = INT(RND*451)
			c.nap_count = INT(RND*401)
			c.cat_age = "adult cat"
		CASE 3:
			c.hunger = INT(RND*301)
			c.pet_count = INT(RND*401)
			c.nap_count = INT(RND*161)
			c.cat_age = "old cat"   
	END SELECT
END SUB




SUB pet_cat(key AS STRING, c as cat)
	
	c.pet_count -= 1
	
	IF c.pet_count <= 50 THEN
		c.text4 = "the cat need some petting press key 'p' to pet cat!"
		
	ELSE
		c.text4 = ""
		
	ENDIF
	IF key = CHR(112) THEN
		c.pet_count += 1000
		c.text4 = ""
	ENDIF
	
END SUB



SUB display_screen(index AS INTEGER, c as cat)
	
	SELECT CASE index
		CASE 0:
			CLS
			CP 15, c.cat_age & " " &cat_name2(c)
			CP 4, "^                        ^"
			CP 5, "( ) %%%%%%%%%%%%%%%%%%%  ( )"
			CP 6, "<| (( <|> ))  ||   ((  <|>  )) |>"
			CP 7, "_                              _"
			CP 8, "_                            _"
			CP 9, "_    >(::| * |::)<     _"
			CP 10, "        -------          "
			CP 11, "(  -----  )"
			CP 12, "\\---//"
			CP 13, "%%%"
			CP 34, c.text1
			CP 35, c.text2
			CP 32, c.text4
			CP 33, c.text3
			CP 31, c.text6
		CASE 1:
			CLS
			CP 15, c.cat_age & " " &cat_name2(c)
			CP 4, "^                        ^"
			CP 5, "( ) %%%%%%%%%%%%%%%%%%%  ( )"
			CP 6, "<| (( <I> ))  ||   ((  <I>  )) |>"
			CP 7, "_                              _"
			CP 8, "_                            _"
			CP 9, "_    >(::| * |::)<     _"
			CP 10, "                          "
			CP 11, "(  OOOO  )"
			CP 12, "\\   //"
			CP 13, "%%%"
			CP 34, c.text1
			CP 35, c.text2
			CP 32, c.text4
			CP 33, c.text3
			CP 31, c.text6
		CASE 2:
			CLS
			CP 15, c.cat_age &" "& cat_name2(c)
			CP 4, "^                        ^"
			CP 5, "( ) %%%%%%%%%%%%%%%%%%%  ( )"
			CP 6, "<| (( <dOb> ))  ||   ((  <dOb>  )) |>"
			CP 7, "_                              _"
			CP 8, "_                            _"
			CP 9, "_    >(::| & |::)<     _"
			CP 10, "                          "
			CP 11, "(  ~~~~~  )"
			CP 12, "\\ ~~~ //"
			CP 13, "%%%"
			CP 34, c.text1
			CP 35, c.text2
			CP 32, c.text4
			CP 33, c.text3
			CP 31, c.text6
	END SELECT
END SUB

SUB cat_napping(c as CAT)
	
	
	DIM x AS INTEGER = 0
	DIM xx AS INTEGER = 50
	
	DO
		nap_time(x, c)
		c.text5 = "NAP TIME!"
		x += 1
		xx -= 1
		SLEEP 250
		IF x >= 3 THEN x = 0
	LOOP UNTIL xx <= 0
	
END SUB
SUB cat_hunger(key AS STRING, c as cat)
	
	IF c.hunger < 25 THEN
		c.text1 = "cat is hungry! press key 'f' to feed it"
	ELSE
		c.text1 = ""
		
	ENDIF
	IF c.hunger <= -450 THEN
		c.isOut = TRUE
		
	ENDIF
	IF key = CHR(102) THEN
		c.hunger += 1000
	ENDIF
END SUB



'SUB cat.make_sound(f AS STRING, hWave AS INTEGER, t AS INTEGER)
'
'fbs_Load_WAVFile(f , @hWave)
'fbs_Play_Wave(hWave , t)
'
'END SUB

'SUB CAT.ANIMATION(f AS STRING, t AS INTEGER)
SUB ANIMATION(c as cat)
	VAR key = "" , hwave = 0
	DO
		
			IF c.isOut = TRUE OR NOT CBool(FILEEXISTS(file)) OR c.isNeglect = TRUE THEN
				DIM AS STRING catname, age1
				INPUT "enter cat name or enter 'quit' to exit: ", catname
				IF catname = "quit" THEN
					write_file(file, c)
					END
				ENDIF
				cat_name1(catname, c)
				INPUT "select age of cat - 0 for kitten, 1 for young, 2 for adult, 3 for old: ", age1
				settings(Cast(Age, Val(age1)), c)
				
			ELSE
				read_file(file, c)
			ENDIF
			c.isOut = FALSE
			c.isNeglect = FALSE
			DIM i AS INTEGER = 0
			'make_sound(f,hWave, t)
			
			DIM x AS INTEGER = 0
			DIM xx AS INTEGER = 50
			DO
				key = INKEY
				IF c.nap_count > 50 THEN
					'IF key = CHR(115) THEN
					'make_sound(f,hWave, t)
					'ENDIF
					
					IF i >= 3 THEN i = 0
					DISPLAY_SCREEN(i, c)
					SLEEP 500
					i += 1
					cat_hunger(key, c)
					pet_cat(key, c)
					time_diff(c)
					IF c.isOut = TRUE OR c.isneglect = TRUE THEN EXIT DO
					c.text2 = " cat hunger is: " & c.hunger & " cat napping counter is: " & c.nap_count & _
					" cat pet time is: " & c.pet_count
					c.nap_count -= 1
					c.hunger -= 1
					IF key = CHR(27) THEN
						write_file(file,c)
						END
					ENDIF
				ELSEIF c.nap_count <= 50 THEN
					cat_napping(c)
					c.nap_count = INT(RND*151)
				ENDIF 
				
			LOOP
			'fbs_Destroy_Wave(@hWave)
			IF c.isOut = TRUE OR c.isNeglect = TRUE THEN
				CLS
				
				c.text2 = "IT'S BEEN " & DATEDIFF("d", c.d1, c.d2) & " MINUTES SINCE YOU LAST OPEN THE PROGRAM"
				c.text1 = "CAT DIED FROM NEGLECT!!!"
				c.text4 = "PRESS ANY KEY TO START OVER WITH A NEW CAT!"
				CP 31, c.text1
				CP 34, c.text4
				CP 30, c.text2
				SLEEP
				cls
				write_file(file,c)
			ENDIF
		
	LOOP UNTIL INKEY() = CHR(27)
	
END SUB


'IF fbs_Init()=false THEN
'PRINT "error: FBS_INIT() " & FBS_Get_PlugError()
'BEEP : SLEEP : END 1
'END IF

SCREENRES scr_w, scr_h
WIDTH scr_w \ 8, scr_h \ 16
DIM SHARED fiori AS cat
with fiori
	.isOut = FALSE
	.text1 = ""
	.text2 = ""
	.text3 = ""
	.text4 = ""
	.text5 = ""
end with 

'fiori.animation("cat_sound.wav", 1)
animation(fiori)