Author Topic: Math Flash Card Trainer for Children  (Read 4101 times)

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Math Flash Card Trainer for Children
« on: July 28, 2017, 01:29:30 pm »
Code: QB64: [Select]
  1. TYPE ResultsType
  2.     num1 AS LONG
  3.     op AS LONG
  4.     num2 AS LONG
  5.     num3 AS LONG
  6.     answer AS LONG
  7.     correct AS LONG
  8.  
  9.  
  10. _TITLE "Flash Card Math Trainer"
  11. DEFLNG A-Z
  12. _DELAY 0.5
  13. WS = _NEWIMAGE(1280, 720, 32) 'WorkScreen
  14.  
  15. DIM SHARED WF 'Workfont
  16. WF = _LOADFONT("C:\Windows\Fonts\Cour.ttf", 150)
  17.  
  18.  
  19. DIM SHARED RST 'Result Font
  20. RST = _LOADFONT("C:\Windows\Fonts\Cour.ttf", 50)
  21.  
  22. REDIM SHARED Results(0) AS ResultsType
  23. DIM SHARED GameTime, Difficulty
  24.  
  25. CONST Red = _RGB32(255, 0, 0)
  26. CONST White = _RGB32(255, 255, 255)
  27. CONST Black = _RGB32(0, 0, 0)
  28. CONST Blue = _RGB32(0, 0, 255)
  29. CONST Green = _RGB32(0, 255, 0)
  30. CONST Yellow = _RGB32(255, 255, 0)
  31.  
  32. CONST Plus = 1, Minus = 2, Times = 3, Divide = 4
  33.  
  34.  
  35.  
  36.  
  37.  
  38. ON TIMER(t1, 1) CountDown
  39.  
  40.  
  41.  
  42. MainChoiceScreen mode, Difficulty, length
  43.  
  44. SELECT CASE length 'This is the number of seconds we're going to test ourselves
  45.     CASE 1: GameTime = 15
  46.     CASE 2: GameTime = 30
  47.     CASE 3: GameTime = 60
  48.     CASE 4: GameTime = 120
  49.     CASE 5: GameTime = 300
  50.  
  51. SELECT CASE Difficulty 'This determines how hard the game will be
  52.     CASE 1: LowLimit = 0: HighLimit = 10
  53.     CASE 2: LowLimit = 0: HighLimit = 12
  54.     CASE 3: LowLimit = 0: HighLimit = 99
  55.     CASE 4: LowLimit = 10: HighLimit = 89
  56.  
  57.  
  58. TIMER(t1) ON
  59.  
  60.  
  61.  
  62.     _LIMIT 10
  63.     num1 = RND * HighLimit + LowLimit
  64.  
  65.     SELECT CASE mode 'Only give the user the numbers they wanted to play with
  66.         CASE 1: op = 1
  67.         CASE 2: op = 2
  68.         CASE 3: op = RND + 1
  69.         CASE 4: op = 3
  70.         CASE 5: op = 4
  71.         CASE 6: op = RND + 3
  72.         CASE 7: op = RND: IF op = 0 THEN op = 1 ELSE op = 3
  73.         CASE 8: op = INT(RND * 4) + 1
  74.     END SELECT
  75.  
  76.     num2 = RND * HighLimit + LowLimit
  77.  
  78.     IF op = 4 THEN 'it's divide, let's get the numbers in the proper order
  79.         DO
  80.             num1 = RND * HighLimit + LowLimit
  81.             num2 = RND * HighLimit + LowLimit
  82.             num1 = num1 * num2
  83.         LOOP UNTIL num1 < 100
  84.     END IF
  85.     IF op = 2 AND num1 < num2 THEN SWAP num1, num2
  86.     DrawCard num1, op, num2
  87.     DrawUserClickAreas
  88.     GetUserAnswer answer$
  89.     CheckAnswer num1, op, num2, answer$
  90.     DisplayTime
  91.     _DISPLAY
  92.  
  93.  
  94. SUB MainChoiceScreen (mode, difficulty, length)
  95. _FONT RST
  96. PRINT "Welcome to Math Flash!"
  97. LOCATE 5, 1: PRINT "Your personal math trainer!"
  98. PRINT "What would you like to learn to work with today?"
  99. PRINT "1) Addition"
  100. PRINT "2) Subtraction"
  101. PRINT "3) Addition and Subtraction"
  102. PRINT "4) Multiplication"
  103. PRINT "5) Division"
  104. PRINT "6) Multiplication and Division"
  105. PRINT "7) Addition and Multiplication"
  106. PRINT "8) ALL of them"
  107. PRINT "9) QUIT THE TRAINER"
  108.  
  109.     _LIMIT 10
  110.     a = VAL(INKEY$)
  111. LOOP UNTIL a > 0 AND a < 10
  112. IF a = 9 THEN SYSTEM
  113.  
  114. PRINT "How difficult do you wish for this game to be?"
  115. PRINT "1) Easy"
  116. PRINT "2) Normal"
  117. PRINT "3) Hard"
  118. PRINT "4) Grueling"
  119.     _LIMIT 10
  120.     b = VAL(INKEY$)
  121. LOOP UNTIL b > 0 AND b < 5
  122.  
  123. PRINT "How long a game do you want?"
  124. PRINT "1) Very Short"
  125. PRINT "2) Short"
  126. PRINT "3) Average"
  127. PRINT "4) Long"
  128. PRINT "5) Bring It On, Long!"
  129.  
  130.     c = VAL(INKEY$)
  131. LOOP UNTIL c > 0 AND c < 6
  132.  
  133. _FONT RST
  134. DO: LOOP UNTIL INKEY$ = "" 'Clear the keyboard buffer
  135. PRINT "<LEFT CLICK> TO BEGIN MATH FLASH CARDS!"
  136.     _LIMIT 10
  137.     x = mouseclick
  138. LOOP UNTIL x = 1
  139.  
  140. mode = a: difficulty = b: length = c
  141.  
  142.  
  143.  
  144. SUB DisplayTime
  145. _FONT RST
  146. COLOR White, Black
  147. LOCATE 1, 1000: PRINT GameTime; "LEFT  "
  148.  
  149. SUB CountDown
  150. GameTime = GameTime - 1
  151. IF GameTime <= 0 THEN ShowResults 'Clock has ran out
  152.  
  153.  
  154. SUB ShowResults
  155. Score = 0
  156. CLS , Black
  157. COLOR White, Black
  158. PRINT "YOUR ANSWER"; TAB(40); "CORRECT ANSWER"
  159. FOR i = 1 TO UBOUND(results)
  160.     text$ = Num2Str(Results(i).num1)
  161.     SELECT CASE Results(i).op
  162.         CASE 1: text$ = text$ + " + "
  163.         CASE 2: text$ = text$ + " - "
  164.         CASE 3: text$ = text$ + " * "
  165.         CASE 4: text$ = text$ + " / "
  166.     END SELECT
  167.  
  168.     IF Results(i).correct = -1 THEN
  169.         COLOR Green, Black
  170.         Correct = Correct + 1
  171.         YourText$ = text$ + Num2Str(Results(i).num2) + " = " + Num2Str(Results(i).answer)
  172.     ELSE
  173.         COLOR Red, Black
  174.         Wrong = Wrong + 1
  175.         IF Results(i).correct = 1 THEN
  176.             YourText$ = text$ + Num2Str(Results(i).num2) + " = " + "SKIPPED"
  177.         ELSE
  178.             YourText$ = text$ + Num2Str(Results(i).num2) + " = " + Num2Str(Results(i).answer)
  179.         END IF
  180.     END IF
  181.     CorrectText$ = text$ + Num2Str(Results(i).num2) + " = " + Num2Str(Results(i).num3)
  182.     PRINT YourText$; TAB(40);
  183.     COLOR White, Black
  184.     PRINT CorrectText$
  185. PRINT "You got "; Correct; " correct answers, and "; Wrong; " wrong."
  186. PRINT "If this was a test, you would have scored "; INT(100 * (Correct / (i - 1))); "%"
  187. ScoreMultiplier = 25 * Difficulty
  188. Score = ScoreMultiplier * Correct + ScoreMultiplier * -Wrong
  189. PRINT "YOUR GAME SCORE IS: "; Score
  190. SELECT CASE Difficulty
  191.     CASE 1: file$ = "Easy.txt": diff$ = "EASY"
  192.     CASE 2: file$ = "Average.txt": diff$ = "NORMAL"
  193.     CASE 3: file$ = "Hard.txt": diff$ = "HARD"
  194.     CASE 4: file$ = "Grueling.txt": diff$ = "GRUELING"
  195.     'life should be good as we have an existing file.
  196.     'if not, then let's make a blank highscore file for the proper difficulty.
  197.     OPEN file$ FOR OUTPUT AS #1
  198.     FOR i = 1 TO 25
  199.         PRINT #1, "None"
  200.         PRINT #1, 0
  201.         PRINT #1, "None"
  202.     NEXT
  203.     CLOSE #1
  204. OPEN file$ FOR INPUT AS #1
  205. DIM person(25) AS STRING, score(25), diff$(25)
  206. HS = 0 'no high score until we validate it
  207. FOR i = 1 TO 25
  208.     LINE INPUT #1, person(i)
  209.     INPUT #1, score(i)
  210.     LINE INPUT #1, diff$(i)
  211.     IF Score > score(i) AND NOT HS THEN
  212.         PRINT "CONGRATULATIONS!!  YOU GOT A HIGH SCORE!"
  213.         PRINT "What name would you like to be known by? ";
  214.         INPUT person$
  215.         IF i < 25 THEN person(i + 1) = person(i): score(i + 1) = score(i): diff$(i + 1) = diff$(i)
  216.         person(i) = person$: score(i) = Score: diff$(i) = diff$
  217.         i = i + 1: HS = -1
  218.     END IF
  219.  
  220. PRINT "Press <ANY KEY> to see the "; diff$; " HIGH SCORES"
  221. PRINT diff$; " HIGH SCORERS"
  222. FOR i = 1 TO 25
  223.     PRINT person(i); TAB(20); score(i); TAB(40); diff$(i)
  224.  
  225. OPEN file$ FOR OUTPUT AS #1
  226. FOR i = 1 TO 25
  227.     PRINT #1, person(i)
  228.     PRINT #1, score(i)
  229.     PRINT #1, diff$(i)
  230.  
  231.  
  232. PRINT "Do the test again? (Y/N)"
  233.     _LIMIT 10
  234.     a$ = UCASE$(INKEY$)
  235. LOOP UNTIL a$ = "Y" OR a$ = "N"
  236. IF a$ = "Y" THEN
  237.     SHELL _DONTWAIT "Untitled.exe"
  238.  
  239.  
  240. SUB CheckAnswer (num1, op, num2, answer$)
  241. ans = VAL(answer$)
  242.     CASE Plus: num3 = num1 + num2
  243.     CASE Minus: num3 = num1 - num2
  244.     CASE Times: num3 = num1 * num2
  245.     CASE Divide: num3 = num1 / num2
  246. QuestionsAsked = UBOUND(results) + 1
  247. REDIM _PRESERVE Results(QuestionsAsked) AS ResultsType
  248. Results(QuestionsAsked).num1 = num1
  249. Results(QuestionsAsked).op = op
  250. Results(QuestionsAsked).num2 = num2
  251. Results(QuestionsAsked).num3 = num3
  252. Results(QuestionsAsked).answer = ans
  253.  
  254. _FONT RST
  255. COLOR White, Black
  256. LOCATE 1, 1: PRINT "Last Answer:"
  257. IF ans = num3 AND answer$ <> "" THEN
  258.     Correct = Correct + 1
  259.     GameTime = GameTime + 1 'A bonus to time for each correct answer
  260.     COLOR Green, Black
  261.     LOCATE 1, 400: PRINT "CORRECT!"
  262.     Results(QuestionsAsked).correct = -1
  263.     Wrong = Wrong + 1
  264.     GameTime = GameTime - 2 'A penalty to time for each wrong answer
  265.     COLOR Red, Black
  266.     LOCATE 1, 400: PRINT "WRONG!  "
  267.     Results(QuestionsAsked).correct = 0
  268.     IF answer$ = "" THEN Results(QuestionsAsked).correct = 1
  269. COLOR White, Black
  270. DisplayTime
  271.  
  272.  
  273. SUB DisplayUserAnswer (answer$)
  274. COLOR Black, White
  275. LINE (700, 100)-(1100, 300), White, BF
  276. top = 125
  277. left = (400 - GetPrintWidth(answer$, WF)) / 2 + 700
  278. _PRINTSTRING (left, top), answer$
  279.  
  280.  
  281. SUB GetUserAnswer (answer$)
  282. done = 0: answer$ = ""
  283.     _LIMIT 30
  284.     Button = mouseclick
  285.     IF Button = 1 THEN 'We have a mouse click somewhere
  286.         x = _MOUSEX: y = _MOUSEY
  287.         IF y >= 400 AND y <= 600 THEN 'they clicked down where the numbers and OK button are
  288.             num = 0
  289.             IF x \ 100 = (x + 10) \ 100 THEN num = x \ 100 'they didn't click on the 10 pixel blank space between numbers
  290.             IF num > 0 AND num < 12 THEN 'It's a click on either a number or the OK button on the top row
  291.                 IF num = 10 THEN num = 0
  292.                 IF num = 11 THEN
  293.                     done = -1
  294.                 ELSE
  295.                     answer$ = answer$ + LTRIM$(RTRIM$(STR$(num)))
  296.                 END IF
  297.             END IF
  298.         END IF
  299.         IF y >= 610 AND y <= 710 AND x >= 100 AND x <= 1200 THEN done = -1 'Click on the bottom ENTER area
  300.     END IF
  301.     DisplayUserAnswer answer$
  302.     DisplayTime
  303.     _DISPLAY
  304. LOOP UNTIL done
  305.  
  306.  
  307.  
  308. SUB DrawUserClickAreas
  309. FOR i = 1 TO 10
  310.     left = 100 * i
  311.     LINE (left, 400)-(left + 90, 600), Yellow, BF
  312.     COLOR Black, 0
  313.     _PRINTSTRING (left + 10, 425), Num2Str$(i MOD 10)
  314. LINE (100, 610)-(1200, 710), Green, BF
  315. LINE (1100, 400)-(1200, 710), Green, BF
  316. _PRINTSTRING (425, 595), "ENTER"
  317.  
  318.  
  319.  
  320. SUB DrawCard (num1, operator, num2) 'Pass it our 3 numbers like 3, plus, 3 would be 3 + 3 = ?
  321. COLOR Red, 0
  322. LINE (100, 100)-(650, 300), White, BF
  323. text$ = Num2Str(num1)
  324. SELECT CASE operator
  325.     CASE Plus: text$ = text$ + "+"
  326.     CASE Minus: text$ = text$ + "-"
  327.     CASE Times: text$ = text$ + "*"
  328.     CASE Divide: text$ = text$ + "/"
  329. text$ = text$ + Num2Str(num2) + "="
  330. top = 125
  331. left = (550 - GetPrintWidth(text$, WF)) / 2 + 100
  332. _PRINTSTRING (left, top), text$
  333.  
  334. FUNCTION Num2Str$ (num)
  335. Num2Str$ = LTRIM$(RTRIM$(STR$(num)))
  336.  
  337. FUNCTION GetPrintWidth (text AS STRING, fonthandle)
  338. w = _WIDTH: h = _HEIGHT
  339. d = _DEST: s = _SOURCE
  340. t = _NEWIMAGE(w, h, 32)
  341. _FONT fonthandle
  342. COLOR _RGB32(255, 0, 0), _RGB32(255, 255, 255)
  343. PRINT text;
  344. m = _MEMIMAGE(t)
  345. p = 0 'pointer
  346.     _MEMGET m, m.OFFSET + p, c 'Get our color
  347.     p = p + 4 'increase 4 as we're working with 4-byte long values for RGBA color
  348.     GetPrintWidth = GetPrintWidth + 1
  349. LOOP UNTIL NOT c 'until we don't have no color no more
  350.  
  351. FUNCTION mouseclick%
  352. DO WHILE _MOUSEINPUT 'check mouse status
  353.     scroll% = scroll% + _MOUSEWHEEL ' if scrollwheel changes, watch the change here
  354.     IF _MOUSEBUTTON(1) THEN 'left mouse pushed down
  355.         speedup = 1
  356.     ELSEIF _MOUSEBUTTON(2) THEN 'right mouse pushed down
  357.         speedup = 2
  358.     ELSEIF _MOUSEBUTTON(3) THEN 'middle mouse pushed down
  359.         speedup = 3
  360.     END IF
  361.     IF speedup THEN 'buton was pushed down
  362.         mouseclickxxx1% = _MOUSEX: mouseclickyyy1% = _MOUSEY 'see where button was pushed down at
  363.         DO WHILE _MOUSEBUTTON(speedup) 'while button is down
  364.             i% = _MOUSEINPUT
  365.         LOOP 'finishes when button is let up
  366.         IF mouseclickxxx1% >= _MOUSEX - 2 AND mouseclickxxx1% <= _MOUSEX + 2 AND mouseclickyyy1% >= _MOUSEY - 2 AND mouseclickyyy1% <= _MOUSEY + 2 THEN 'if mouse hasn't moved (ie  clicked down, dragged somewhere, then released)
  367.             mouseclick% = speedup
  368.         ELSE
  369.             mouseclick% = 0
  370.         END IF
  371.     END IF
  372. IF scroll% < 0 THEN mouseclick% = 4
  373. IF scroll% > 0 THEN mouseclick% = 5
  374.  

A simple little game which kids can use to help them learn basic math.  Does addition, subtraction, multiplication, and division. 

Just copy and paste into the QB64 IDE, compile and you're good to go!  :)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Todd_Bevins

  • Newbie
  • Posts: 7
    • View Profile
Re: Math Flash Card Trainer for Children
« Reply #1 on: July 16, 2018, 10:59:28 pm »
Hi I just found this and think it looks great.  Thanks for posting it and I am anxious to dissect this to understand how you did everything.  :)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Math Flash Card Trainer for Children
« Reply #2 on: July 17, 2018, 12:08:04 am »
Hey I remember working on this!

Quote
_TITLE "Flash Card Math Trainer by Steve McNeil, 2017-10-28 fix by bplus" ' >>> yeah for the +
' >>> all >>>>>>> and <<<<<< comments are from bplus
' >>> 2017-10-28 This fix does not involve SHELL or RUN, just an outer loop
' >>> that sets up a new test, then the inner loop conducts the test.
' >>> As it should be done! ;-))  ;D
' >>> PS a credit to Steve that I can follow his code enough to fix it.

' >>> Now who wants to fix all the text at extreme left side of the screen in setups for test?