home *** CD-ROM | disk | FTP | other *** search
/ RISCWORLD 7 / RISCWORLD_VOL7.iso / Software / Issue2 / SDL.ARC / !gcc / examples / m / Fishie next >
Encoding:
Text File  |  2001-01-02  |  2.6 KB  |  146 lines

  1. /*
  2.  * You may freely copy, distribute and reuse the code in this example.
  3.  * NeXT disclaims any warranty of any kind, expressed or implied, as to its
  4.  * fitness for any particular use.
  5.  */
  6.  
  7.  
  8.  
  9. /* always import your particular class .h file!! */
  10. #import "Fishie.h"
  11. #import <stdio.h>
  12. #import <stdlib.h>
  13.  
  14. #define STARTSIZE 64
  15.  
  16.  
  17. @implementation Fishie
  18.  
  19.     /* general methods */
  20. - init
  21. {
  22.         printf("\t >() \t making a Fishie\n");
  23.     [super init];    /* ALWAYS init your inherited instance vars first!! */
  24.  
  25.         printf("\t >() \t setting the instance variables for the Fishie\n");
  26.     [self setSize:STARTSIZE]; /* initial fishie size */
  27.     [self setHLoc:0];
  28.     [self setVLoc:0];
  29.     [self setIsHappy:YES];
  30.  
  31.         printf("\t >() \t look Ma, i made a Fishie!\n\n");
  32.     return self;
  33. }
  34.  
  35.  
  36.  
  37.     /* state-returning methods */
  38. - (int) size
  39. {
  40.         printf("\t >() \t you asked for it...here's size: %d\n", size);
  41.     return size;
  42. }
  43.  
  44.  
  45. - (int) hLoc
  46. {
  47.         printf("\t >() \t you asked for it...here's hLoc: %d\n", hLoc);
  48.     return hLoc;
  49. }
  50.  
  51.  
  52. - (int) vLoc
  53. {
  54.         printf("\t >() \t you asked for it...here's vLoc: %d\n", vLoc);
  55.     return vLoc;
  56. }
  57.  
  58.  
  59. - (BOOL) isHappy
  60. {
  61.         printf("\t >() \t you asked for it...here's isHappy: %d\n", isHappy);
  62.     return isHappy;
  63. }
  64.  
  65.  
  66.  
  67.  
  68.     /* state-changing methods */
  69. - setSize:(int) val
  70. {
  71.         printf("\t >() \t I'm changing size\n");
  72.     size = val;
  73.     return self;
  74. }
  75.  
  76.  
  77. - setHLoc:(int) val
  78. {
  79.         printf("\t >() \t I'm changing hLoc\n");
  80.     hLoc = val;
  81.     return self;
  82. }
  83.  
  84.  
  85. - setVLoc:(int) val
  86. {
  87.         printf("\t >() \t I'm changing vLoc\n");
  88.     vLoc = val;
  89.     return self;
  90. }
  91.  
  92.  
  93. - setIsHappy:(BOOL) val
  94. {
  95.         printf("\t >() \t I'm changing isHappy\n");
  96.     isHappy = val;
  97.     return self;
  98. }
  99.  
  100.  
  101.  
  102.     /* special-purpose methods */
  103.     /* swimming moves the fish left/right by the Fishie's size or keeps it
  104.         stationary; ditto for vertical motion; up and right motion forces
  105.         happiness; left and down motion forces unhappiness */
  106. - swim
  107. {
  108.     int hInc, vInc;
  109.  
  110.         printf("\t >() \t making a Fishie swim\n");
  111.  
  112.     hInc = rand()%3 - 1;    /* horizontal increment is -1,0,or 1 */
  113.     vInc = rand()%3 - 1;    /* vertical increment is -1,0,or 1 */
  114.  
  115.     if (hInc > 0 && vInc > 0)    /* double positive motion makes happiness */
  116.         [self setIsHappy:YES];
  117.     else if (hInc < 0 && vInc < 0)        /* opposite brings sadness */
  118.         [self setIsHappy:NO];
  119.  
  120.     [self incHBy:[self size]*hInc];    /* new pos is same or
  121.                        left/right shift of size units */
  122.     [self incVBy:[self size]*vInc];
  123.  
  124.         printf("\t >() \t FINished making a Fishie swim (pun intended)\n\n");
  125.     return self;
  126. }
  127.  
  128.  
  129. - incHBy:(int) val
  130. {
  131.         printf("\t >() \t I'm incrementing hLoc\n");
  132.     [self setHLoc:[self hLoc]+val];
  133.     return self;
  134. }
  135.  
  136.  
  137. - incVBy:(int) val
  138. {
  139.         printf("\t >() \t I'm incrementing vLoc\n");
  140.     [self setVLoc:[self vLoc]+val];
  141.     return self;
  142. }
  143.  
  144.  
  145. @end
  146.