December 03, 2020, 08:36:29 PM

Author Topic: [bb] Highscore functions by wedoe [ 1+ years ago ]  (Read 594 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Highscore functions by wedoe [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : Highscore functions
Author : wedoe
Posted : 1+ years ago

Description : Implement this in your game to save,
load and sort your highscores.
20 players in the example but any
number can be used by increasing the array.


Code :
Code: BlitzBasic
  1. ; Highscore load/save/sort by Wedoe
  2.  
  3. Dim names$(21)
  4. Dim scores(21)
  5.  
  6. ; Your game goes here !
  7.  
  8. End
  9. ;-------------------------------- Read highscore from file
  10. Function readhs()
  11. file=ReadFile("highscore.sco")
  12. For a=1 To 20
  13.         names$(a)=ReadLine(file)
  14.         scores(a)=ReadLine(file)
  15. Next
  16. CloseFile (file)
  17. End Function
  18. ;-------------------------------- Write highscore to file
  19. Function writehs()
  20. file=WriteFile("highscore.sco")
  21. For a=1 To 20
  22.         WriteLine (file,names$(a))
  23.         WriteLine (file,scores(a))
  24. Next
  25. CloseFile (file)
  26. End Function
  27. ;------------------------------- Simple bubblesort
  28. Function sorths()                                                                                                              
  29. .sorths
  30. swap=0
  31. For a=1 To 20
  32.         If scores(a+1) > scores(a) Then
  33.                 tmp1=scores(a)
  34.                 tmp2$=names$(a)
  35.                 scores(a)=scores(a+1)
  36.                 names$(a)=names$(a+1)
  37.                 scores(a+1)=tmp1
  38.                 names$(a+1)=tmp2$
  39.                 swap=1
  40.                 EndIf  
  41. Next
  42. If swap=1 Then Goto sorths
  43. End Function
  44. ;-------------------------------
  45.  
  46. ; When you have some data for the scorelist
  47. ; always put last player in position 21
  48. ; and then sort the list.
  49. ; If he is worthy to get on the list he will
  50. ; and if not he will remain at number 21
  51. ; which you of course never prints out


Comments :


Steen(Posted 1+ years ago)

 Pretty simple and does the trick. I'm using this myself, nice work!


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal