Author Topic: Number Game  (Read 4292 times)

0 Members and 1 Guest are viewing this topic.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Number Game
« on: January 09, 2019, 12:12:44 pm »
Super Simple, Super simplistic number guessing game. Anybody recognize where it came from?
Code: QB64: [Select]
  1. 'Number Guessing Game
  2. 'Cobalt 2019/1/2
  3.  
  4. '-------------setup------------------------------
  5. TYPE Game_Data
  6.  Wins AS _BYTE
  7.  Losses AS _BYTE
  8.  Face AS _BYTE
  9.  Trys AS _BYTE
  10.  Happiness AS _BYTE
  11.  
  12. SCREEN _NEWIMAGE(640, 480, 32)
  13. Numbers& = _NEWIMAGE(640, 480, 32)
  14. _PRINTSTRING (0, 0), "0 1 2 3 4 5 6 7 8 9", Numbers&
  15. DIM SHARED Pic(16, 16) AS DOUBLE '_UNSIGNED LONG
  16. DIM SHARED G AS Game_Data
  17. CONST TRUE = -1, FALSE = NOT TRUE
  18. CONST HAPPY = 1, ANGRY = 2, Neutral = 0
  19. Ani& = _FREETIMER
  20. ON TIMER(Ani&, .33) Animate_Face
  21. _DELAY .05
  22. _TITLE "WHOs GREATER!"
  23. '------------------------------------------------
  24. 'Face Data FF,FR,FL,FH,FAR,FAL,W1,W2
  25. DATA 10,2016,2064,5160,4488,4488,4104,4104,4104,2064,2016,9,2016,2064,4360,4104,7688,4104,4104,2064,2016
  26. DATA 9,2016,2064,4232,4104,4216,4104,4104,2064,2016,11,2016,2640,4104,5160,6120,6120,6120,5064,4104,2064,2016
  27. DATA 12,992,1040,2096,4192,5824,4224,4264,4344,4104,4104,2064,2016,12,1984,2080,3088,1544,872,264,5384,7944,4104,4104,2064,2016
  28. DATA 10,2016,2064,4680,4104,4488,4104,4104,4104,2064,2016,9,2016,2064,5160,4104,4488,4104,4104,2064,2016
  29. '-----------Load Face Data-----------------------
  30. FOR J%% = 1 TO 8
  31.  READ L%%
  32.  FOR i%% = 1 TO L%%
  33.   shift%% = (16 - L%%) \ 2 'center image in 16 lines
  34.   READ Pic(J%%, i%% + shift%%)
  35.   Pic(J%%, i%% + shift%%) = _SHL(Pic(J%%, i%% + shift%%), 8) 'center image in 32 rows
  36. '------------------------------------------------
  37. Pick_Numbs NumbA%%, NumbB%%
  38. '--------------side A Number---------------------
  39. _PUTIMAGE (32, 64)-STEP(32, 64), Numbers&, _DISPLAY, (16 * NumbA%%, 0)-STEP(8, 16)
  40. '------------------------------------------------
  41. '--------------Side B Number---------------------
  42. '_PUTIMAGE (288, 64)-STEP(32, 64), Numbers&, _DISPLAY, (16 * NumbB%%, 0)-STEP(8, 16)
  43. '------------------------------------------------
  44.  
  45. '------------------Main Game Loop----------------
  46. _PRINTSTRING (75, 330), "Using Left\Right Arrow keys Pick which number will be Greater"
  47. Dot_Display 4, 447, 350, 2, _RGB32(16, 212, 96)
  48. Dot_Display 5, 0, 350, 2, _RGB32(224, 16, 32)
  49. TIMER(Ani&) ON
  50. G.Happiness = 0
  51.  
  52.  _DELAY .25
  53.  IF face_frame%% = 7 THEN face_frame%% = 8 ELSE face_frame%% = 7
  54.   CASE 27
  55.    ExitFlag%% = TRUE
  56.   CASE 19200 'left
  57.    '------------------CHOOSE LEFT-----------------
  58.    G.Trys = G.Trys + 1
  59.    Dot_Display 2, 80, 64, 5, _RGB32(128, 128, 128)
  60.    TIMER(Ani&) OFF
  61.    _DELAY .33
  62.    TIMER(Ani&) ON
  63.    _PUTIMAGE (588, 64)-STEP(32, 64), Numbers&, _DISPLAY, (16 * NumbB%%, 0)-STEP(8, 16)
  64.    IF NumbA%% > NumbB%% THEN
  65.     G.Face = HAPPY
  66.     G.Wins = G.Wins + 1
  67.     G.Happiness = G.Happiness + 1
  68.     _DELAY 2.5
  69.    ELSE
  70.     G.Face = ANGRY
  71.     G.Losses = G.Losses + 1
  72.     G.Happiness = G.Happiness - 1
  73.     _DELAY 2.5
  74.    END IF
  75.    LINE (0, 0)-(639, 320), _RGB32(0, 0, 0), BF
  76.    Pick_Numbs NumbA%%, NumbB%%
  77.    _PUTIMAGE (32, 64)-STEP(32, 64), Numbers&, _DISPLAY, (16 * NumbA%%, 0)-STEP(8, 16)
  78.    G.Face = Neutral
  79.    '---------------------------------------------
  80.   CASE 19712 'right
  81.    '----------------CHOOSE RIGHT------------------
  82.    G.Trys = G.Trys + 1
  83.    Dot_Display 3, 80, 64, 5, _RGB32(128, 128, 128)
  84.    TIMER(Ani&) OFF
  85.    _DELAY .33
  86.    TIMER(Ani&) ON
  87.    _PUTIMAGE (588, 64)-STEP(32, 64), Numbers&, _DISPLAY, (16 * NumbB%%, 0)-STEP(8, 16)
  88.    IF NumbB%% > NumbA%% THEN
  89.     G.Face = HAPPY
  90.     G.Wins = G.Wins + 1
  91.     G.Happiness = G.Happiness + 1
  92.     _DELAY 2.5
  93.    ELSE
  94.     G.Face = ANGRY
  95.     G.Losses = G.Losses + 1
  96.     G.Happiness = G.Happiness - 1
  97.     _DELAY 2.5
  98.    END IF
  99.    LINE (0, 0)-(639, 320), _RGB32(0, 0, 0), BF
  100.    Pick_Numbs NumbA%%, NumbB%%
  101.    _PUTIMAGE (32, 64)-STEP(32, 64), Numbers&, _DISPLAY, (16 * NumbA%%, 0)-STEP(8, 16)
  102.    G.Face = Neutral
  103.    '---------------------------------------------
  104.  
  105.  Display_Happiness
  106.  '-------------------after 5 go's check how player did----------
  107.  IF G.Trys = 5 THEN G.Trys = 0
  108.  '--------------------------------------------------------------
  109. LOOP UNTIL ExitFlag%%
  110. '------------------------------------------------
  111.  
  112. '-------------Dot Matrix display-----------------
  113. '32x16- 16 LONG integers
  114. SUB Dot_Display (image%%, X%, Y%, Scale%%, Colr~&)
  115.  FOR J%% = 1 TO 16
  116.   Yloc% = Y% + (3 * Scale%%) * J%%
  117.   FOR I%% = 31 TO 0 STEP -1 'Read Bits
  118.    Xloc% = X% + (3 * Scale%%) * (31 - I%%)
  119.    IF Pic(image%%, J%%) AND 2 ^ I%% THEN 'Dot is Lit
  120.     LINE (Xloc%, Yloc%)-STEP(2 * Scale%%, 2 * Scale%%), Colr~&, BF
  121.    ELSE 'Dot is Off
  122.     LINE (Xloc%, Yloc%)-STEP(2 * Scale%%, 2 * Scale%%), _RGB32(16, 12, 16), BF
  123.    END IF
  124.   NEXT I%%
  125.  NEXT J%%
  126. '------------------------------------------------
  127. '-----------------32Bit PRNG---------------------
  128. FUNCTION RND32~& ()
  129.  High~& = INT(RND * (2 ^ 16)) 'upper 16 bits
  130.  Low~% = INT(RND * (2 ^ 16)) 'lower 16 bits
  131.  RND32~& = _SHL(High~&, 16) + Low~%
  132. '------------------------------------------------
  133.  
  134. '------------Create Face data values-------------
  135. FUNCTION MakeData~& (SHFL%%, Lins%%)
  136.  FOR i%% = 0 TO Lins%%
  137.   READ t%%
  138.   IF t%% THEN result~& = result~& + 2 ^ i%%
  139.  NEXT i%%
  140.  result~& = _SHL(result~&, SHFL%%)
  141.  MakeData~& = result~&
  142. '------------------------------------------------
  143. '------------Change Face animations--------------
  144. SUB Animate_Face
  145.  STATIC face_frame%%
  146.  SELECT CASE G.Face
  147.   CASE Neutral
  148.    IF face_frame%% = 7 THEN face_frame%% = 8 ELSE face_frame%% = 7
  149.   CASE HAPPY
  150.    IF face_frame%% = 1 THEN face_frame%% = 4 ELSE face_frame%% = 1
  151.   CASE ANGRY
  152.    IF face_frame%% = 5 THEN face_frame%% = 6 ELSE face_frame%% = 5
  153.  Dot_Display face_frame%%, 80, 64, 5, _RGB32(128, 128, 128)
  154. '------------------------------------------------
  155. '------------------pick new numbers--------------
  156. SUB Pick_Numbs (NumbA%%, NumbB%%)
  157.  NumbA%% = INT(RND * 9) + 1
  158.  DO 'make sure 2 different numbers chosen
  159.   NumbB%% = INT(RND * 9) + 1
  160.  LOOP UNTIL NumbB%% <> NumbA%%
  161. '------------------------------------------------
  162. '---------------Happiness Scale------------------
  163. SUB Display_Happiness
  164.  IF G.Happiness > 0 THEN vals%% = 1 ELSE vals%% = -1
  165.  LINE (192, 350)-(447, 450), _RGB32(0, 0, 0), BF
  166.  FOR i%% = 0 TO 0 + G.Happiness STEP vals%%
  167.   LINE (320 + i%%, 350)-(320 + i%%, 450), _RGB32(127 - i%%, 127 + i%%, 0)
  168.  NEXT i%%
  169. '------------------------------------------------
  170.  

there are a few aspects that I haven't buttoned up yet, meter flips when reaching 128\-128 for example.
but been wanting to get my first output of 2019 up for a few days now.
Granted after becoming radioactive I only have a half-life!

FellippeHeitor

  • Guest
Re: Number Game
« Reply #1 on: January 09, 2019, 12:17:15 pm »
Tamagotchi! That was adorable, Cobalt!

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Number Game
« Reply #2 on: January 09, 2019, 12:52:27 pm »
Maybe I need a new IDE, my IDE know not  _SHL command. Is this a new command?

FellippeHeitor

  • Guest
Re: Number Game
« Reply #3 on: January 09, 2019, 12:56:17 pm »
It is. Try the dev build. Here's what's new so far:

https://www.qb64.org/forum/index.php?topic=304.msg2554#msg2554

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Number Game
« Reply #4 on: January 09, 2019, 12:57:45 pm »
Aha! Thank you Fellippe.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Number Game
« Reply #5 on: January 09, 2019, 01:01:22 pm »
Yes, cute!

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Number Game
« Reply #6 on: January 09, 2019, 01:11:48 pm »
So again, nice work, Cobalt!