Ooops
November 28, 2020, 02:37:27 AM

Author Topic: [bb] High Score Entry and Saving by Grey Alien [ 1+ years ago ]  (Read 618 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : High Score Entry and Saving
Author : Grey Alien
Posted : 1+ years ago

Description : This is the high score code from Easter Bonus.  It supports the creating of a high score table, loading, saving, entering, sorting etc.  Some of the functions are based on Tracer's source from the asteroid Shower example.

P.S. sorry the code window is so wide, it's the original comments.


Code :
Code: BlitzBasic
  1. Const MAX_HIGH_SCORES = 10
  2. Const HIGH_SCORE_NAME_LENGTH = 12
  3. Const HIGH_SCORE_CHAR_WIDTH = 25 ;width of a letter in the font (50 point CourierNew)
  4.  
  5. ;high scores
  6. Dim HighScoreName$(MAX_HIGH_SCORES) ;Holds the hiscore strings
  7. Dim HighScore%(MAX_HIGH_SCORES) ;Holds the hiscore integers
  8. Dim HighScoreLevel%(MAX_HIGH_SCORES) ;Holds the hiscore levels
  9. Dim HighName$(HIGH_SCORE_NAME_LENGTH) ;the name currently being entered
  10. Global HighNameChar% = 0 ;pointer to the letter currently being entered
  11. Global EnteringHighScore% = 0 ;app should set to 1 when entering high score
  12. Global HighScoreDone% = 0 ;app should maintain this at 0, it gets set to 1 when they've finished typing and pressed enter
  13.  
  14. ;Shadow Text
  15. Global fntCourierNew% = ccLoadFont("Courier New", 30, True, False, False)
  16. Global ShadowTextDepth% = 1
  17.  
  18. ; -----------------------------------------------------------------------------
  19. ; Clear High Score
  20. ; -----------------------------------------------------------------------------
  21. Function HighScoreClear()      
  22.         For i = 1 To HIGH_SCORE_NAME_LENGTH
  23.                 HighName(i) = ""
  24.         Next
  25. End Function
  26.  
  27. Function HighScoreResetVars()
  28.         HighScoreDone = 0
  29.         EnteringHighScore = 0
  30. End Function
  31.  
  32. ; -----------------------------------------------------------------------------
  33. ; Enter High Score
  34. ; -----------------------------------------------------------------------------
  35. Function HighScoreEnter(StartY, Special)
  36.         ;call this once per frame
  37.        
  38. ;       Local charwidth = 12 ;width of a letter in the font ;24 pitch courierNew
  39.         Local charwidth = HIGH_SCORE_CHAR_WIDTH
  40.         Local x = (GAME_WIDTH - (charwidth*HIGH_SCORE_NAME_LENGTH)) / 2
  41.         SetFont fntCourierNew  
  42.         Local lt = 0
  43.         Local pl = 0
  44.        
  45.         ;output the current high name
  46.         For lt = 1 To HIGH_SCORE_NAME_LENGTH ; Draw entered letters or dots if not entered.
  47.                 If HighName$(lt) <> "" Then
  48.                         If lt = HighNameChar
  49.                                 ccShadowText(x + (lt-1)*charwidth, StartY, HighName$(lt), 0)
  50.                         Else
  51.                                 ccShadowText(x + (lt-1)*charwidth, StartY, HighName$(lt), 0)
  52.                         EndIf
  53.                 Else
  54.                         If lt = HighNameChar
  55.                                 ccShadowText(x + (lt-1)*charwidth, StartY, "_", 0)
  56.                         Else
  57.                                 ccShadowText(x + (lt-1)*charwidth, StartY, ".", 0)
  58.                         EndIf
  59.                 EndIf
  60.         Next
  61.        
  62.         ;detect cursor and backspace keys
  63. ;       If KeyHit(RIGHT_ARROW)                                                                  ; Right key.
  64. ;               HighNameChar = HighNameChar + 1                                         ; Move one letter to right.
  65. ;               If HighNameChar > HIGH_SCORE_NAME_LENGTH Then HighNameChar = 1          ; Wrap to first letter if past letter 12.
  66. ;       EndIf
  67. ;       If KeyHit(LEFT_ARROW)                                                                   ; Left key.
  68. ;               HighNameChar = HighNameChar - 1                                         ; Move one letter to left.
  69. ;               If HighNameChar < 1 Then HighNameChar = HIGH_SCORE_NAME_LENGTH          ; Wrap to last letter if past letter 1.
  70. ;       EndIf
  71.         ; 97 - 122 (letters), (48 - 57) numbers, 32 = space
  72.         ltr = GetKey()                                                                                  ; Get all the keys that can be entered.
  73.  
  74.         If ltr = 8 Then ;8 = backspace.  Use get key so key repeat works
  75.                 HighNameChar = HighNameChar - 1                                         ; Delete letter.
  76.                 If HighNameChar < 1 Then HighNameChar = 1                       ; Keep on first if needed.
  77.                 HighName$(HighNameChar) = ""                                            ; Clear letter from array.
  78.         EndIf
  79.  
  80.         If (ltr > 96 And ltr < 123) Or (ltr >= 48 And ltr <= 57) Or (ltr = 32) Or (ltr > 64 And ltr < 91) Then
  81.                 If HighNameChar <= HIGH_SCORE_NAME_LENGTH
  82.                         HighName$(HighNameChar) = Upper$(Chr$(ltr))     ; Put letter in array.
  83.                         HighNameChar = HighNameChar + 1                                 ; Move to next letter position.
  84.                 EndIf
  85.         EndIf
  86.                                
  87.         If KeyHit(RETURN_KEY)                                                                   ; Return key.
  88.                 Local name$ = ""
  89.                 For pl = 1 To HIGH_SCORE_NAME_LENGTH                            ; Put name in hiscore list.
  90.                         If HighName$(pl) = ""
  91.                                 name$ = name$ + " "
  92.                         Else
  93.                                 name$ = name$ + HighName$(pl)
  94.                         EndIf
  95.                 Next
  96.                 HighScoresSort(name$, Score)                                                    ; Sort hiscore table.
  97.                 HighScoresSave(Special)                                                                 ; Save hiscore table.
  98.                 HighScoreDone = 1
  99.                 EnteringHighScore = 0
  100.                 Return 1
  101.         Else
  102.                 Return 0
  103.         EndIf
  104. End Function
  105.  
  106. ; -------------------------------------------------------------------------
  107. ; Save High Scores
  108. ; -------------------------------------------------------------------------
  109.  ; These high score functions were modified from Tracer's from the Asteroid Shower example
  110. Function HighScoresSave(Special%)
  111.         For q = 0 To MAX_HIGH_SCORES-1
  112.                 HighScoreName$(q) = ccPadRight(HighScoreName$(q), HIGH_SCORE_NAME_LENGTH); Add spaces to fill up empty slots for all entries.
  113.         Next
  114.        
  115.         hi = WriteFile("hiscore.dat")                                                           ; Open the file for writing.
  116.         For q = 0 To MAX_HIGH_SCORES-1                                                          ; Go through all entries.
  117.                 For p = 1 To Len(HighScoreName$(q))                                             ; Write each letter (rather than a string so we can change the ASCII code to avoid score hacking)
  118.                         a$ = Mid$(HighScoreName$(q),p,1)                                        ; Get letter.
  119.                         as = Asc(a$)                                                                            ; Make it ascii format.
  120.                         WriteByte hi,as - 19                                                            ; Decrease by 19 to make different character.
  121.                 Next
  122.                 WriteInt hi,HighScore(q)
  123.                 WriteInt hi,HighScoreLevel(q)
  124.         Next
  125.         ;write special value shifted by big number
  126.         WriteInt hi,Special + 917648
  127.         CloseFile hi                                                                                            ; Close the file.
  128. End Function
  129.  
  130. ; -------------------------------------------------------------------------
  131. ; Load High Scores
  132. ; -------------------------------------------------------------------------
  133. Function HighScoresLoad%(Special)
  134.         hi = OpenFile("hiscore.dat")                                                            ; Open the file.
  135.         If hi = 0                                                                                                       ; Does the file exist?
  136.                 HighScoreName$(0) = "JAKE"                                                      ; If not then we fill the hiscore table with these.
  137.                 HighScore(0) = 50000
  138.                 HighScoreLevel(0) = 10
  139.                 HighScoreName$(1) = "HELEN"
  140.                 HighScore(1) = 45000
  141.                 HighScoreLevel(1) = 9
  142.                 HighScoreName$(2) = "CONAN"
  143.                 HighScore(2) = 40000
  144.                 HighScoreLevel(2) = 8
  145.                 HighScoreName$(3) = "CALLUM"
  146.                 HighScore(3) = 35000
  147.                 HighScoreLevel(3) = 7
  148.                 HighScoreName$(4) = "JASON"
  149.                 HighScore(4) = 30000
  150.                 HighScoreLevel(4) = 6
  151.                 HighScoreName$(5) = "DAMIEN"
  152.                 HighScore(5) = 25000
  153.                 HighScoreLevel(5) = 5
  154.                 HighScoreName$(6) = "PENNY"
  155.                 HighScore(6) = 20000
  156.                 HighScoreLevel(6) = 4
  157.                 HighScoreName$(7) = "TIM"
  158.                 HighScore(7) = 15000
  159.                 HighScoreLevel(7) = 3
  160.                 HighScoreName$(8) = "CHARLES"
  161.                 HighScore(8) = 10000
  162.                 HighScoreLevel(8) = 2
  163.                 HighScoreName$(9) = "ANDREW"  
  164.                 HighScore(9) = 5000
  165.                 HighScoreLevel(9) = 1
  166.                 HighScoresSave(Special)
  167.         Else                                                                                                            ; File does exist.
  168.                 For q = 0 To MAX_HIGH_SCORES-1                                                  ; Read all 10 hiscore entries.
  169.                         For p = 1 To HIGH_SCORE_NAME_LENGTH                                     ; Read all bytes of the name.
  170.                                 a = ReadByte(hi)                                                                ; Read a single byte.
  171.                                 a = a + 19                                                                              ; Increase by 19 to get good character.
  172.                                 ac$ = ac$ + Chr$(a)                                                             ; Put character in temp string.
  173.                         Next
  174.                         HighScoreName$(q) = ac$                                                         ; Put loaded name in hiscore array.
  175.                         ac$ = ""                                                                                        ; Clear temp string.
  176.                         HighScore%(q) = ReadInt(hi)                                                     ; Put loaded score in hiscore array.
  177.                         HighScoreLevel%(q) = ReadInt(hi)                                                        ; Put loaded level in hiscore array.
  178.                 Next
  179.                 ;read special value shifted by big number
  180.                 Special = ReadInt(hi) - 917648         
  181.                 CloseFile hi
  182.         EndIf
  183.         Return Special
  184. End Function
  185.  
  186. ; -------------------------------------------------------------------------
  187. ; Sort High Scores
  188. ; -------------------------------------------------------------------------
  189. ; This function will physically sort the
  190. ; hiscore table to put highest on top and
  191. ; lowest at the bottom. It uses a simple
  192. ; Bubble sort.
  193. Function HighScoresSort(name$, Score%)
  194.         HighScoreName$(MAX_HIGH_SCORES) = name$                                                                 ; Not saved part of hiscore array gets the entered name.
  195.         HighScore(MAX_HIGH_SCORES) = score                                                                      ; Not saved part of hiscore array gets the gotten score.
  196.         HighScoreLevel(MAX_HIGH_SCORES) = Level                                                                 ; Not saved part of hiscore array gets the gotten score.
  197.        
  198.         ; A bubble sort!
  199.         ; It checks to see if the score below is higher than the one above.
  200.         ; If so it will swap them.. after going through the entire
  201.         ; array it will have sorted it.. slow but effective.
  202.         For bub1 = 0 To MAX_HIGH_SCORES
  203.                 counter = 0
  204.                 For bub2 = 0 To MAX_HIGH_SCORES-1-bub1
  205.                         a = HighScore(counter)
  206.                         b = HighScore(counter + 1)
  207.                         If b > a
  208.                                 n1$ = HighScoreName$(counter)
  209.                                 n2$ = HighScoreName$(counter + 1)
  210.                                 l1% = HighScoreLevel(counter)
  211.                                 l2% = HighScoreLevel(counter + 1)
  212.                                 HighScore(counter) = b
  213.                                 HighScore(counter + 1) = a
  214.                                 HighScoreName$(counter) = n2$
  215.                                 HighScoreName$(counter + 1) = n1$
  216.                                 HighScoreLevel(counter) = l2
  217.                                 HighScoreLevel(counter + 1) = l1                               
  218.                         EndIf
  219.                         counter = counter + 1
  220.                 Next
  221.         Next
  222. End Function
  223.  
  224. ; -----------------------------------------------------------------------------
  225. ; ccShadowText
  226. ; -----------------------------------------------------------------------------
  227. Function ccShadowText(x%, y%, TheText$, Centre)
  228.         ;make a black shadow in the same font behind the text so it shows up on top of images
  229.         ;first store the current color
  230.         red = ColorRed()
  231.         green = ColorGreen()
  232.         blue = ColorBlue()
  233.        
  234.         Color 0,0,0
  235.         Text x + ShadowTextDepth, y + ShadowTextDepth, TheText, Centre
  236.         Color red, green, blue
  237.         Text x, y, TheText, Centre     
  238. End Function
  239.  
  240. ; -----------------------------------------------------------------------------
  241. ; pad a string with spaces on the right (best for left aligning strings)
  242. ; -----------------------------------------------------------------------------
  243. Function ccPadRight$(TheString$, TheSize)
  244.         sl = TheSize - Len(TheString$) ; Get length of string
  245.         If sl > 0 Then ; Needs padding?
  246.                 For p = 1 To sl                                                                        
  247.                         z$ = z$ + " " ; make a blank string
  248.                 Next
  249.                 TheString$ = TheString$ + z$; add the blank string to the main string
  250.         EndIf
  251.         Return TheString$
  252. End Function
  253.  
  254. ; -----------------------------------------------------------------------------
  255. ; Load a font and error if not found
  256. ; -----------------------------------------------------------------------------
  257. Function ccLoadFont (TheFont$, Size, Bold, Italic, Underline)
  258.         pointer = LoadFont(TheFont$, Size, Bold, Italic, Underline)
  259.         If Not pointer Then
  260.         RuntimeError ("Error loading font "+TheFont$)
  261.                 End
  262.         Else
  263.         Return Pointer 
  264.         EndIf
  265. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal