Highscore Table

Hello everyone, I’m currently working on a game project using Blender 2.48a but I keep getting stuck at a certain point…

I would like to add a complete highscore table to my game with 10 slots, I managed to save 1 highscore but don’t know how to create a table…

I hope one of you guys can help me out, oh btw no online highscore table…

That’s a problem for basic python.

The basic idea would be to have each line be a new high score. You can separate fields (Name, Score, etc.) by tabs in the file. Using python’s basic file reading and writing you can easily do this.

Here’s some info: http://pentangle.net/python/handbook/node35.html

then you would have to find out how to cheek score and move things around(no clue how).

Thanks guys for the replies… Indeed the main obstacle is to “move around” the score (IE. 1st place should become 2nd if score is higher then the 1st place etc)

This would be very easy to do with SQL. With a SQL table, you could return 10 rows ordered by score in a descending fashion:


SELECT * FROM high_scores ORDER BY score DESC LIMIT 10

I havn’t used SQL in a while, but the query would be something like that. And I’m pretty sure the is a SQL module for Python.

However, you would have to setup a SQL database on the end user’s machine…

Well… not really. Your file can just have an arbitrary list of scores, and then the high score display itself can sort those scores.

More info: http://wiki.python.org/moin/HowTo/Sorting

If you used SQLite and the sqlite3 module, it looks like this would be very simple.

Here is a link to sqlite3 information:
http://www.python.org/doc/2.5.2/lib/module-sqlite3.html

There is a link to SQLite in the documentation.

You could do a very simple bubble sort algorithm. It’s quite slow, but so long as you don’t have a million scores saved then it should be fine.

It’s very simple indeed. What you need to do is place the scores in an array. Then loop through the array starting at index 1 (where 0 is the starting index of the array). Then what you do is test to see if the current index is greater than the previous index, and if so, swap them, if not, leave them as they are.

if you made a swap, you need to set a flag, and test outside the loop whether that flag is set or not. If it is set, then repeat the process… until eventually the flag is no longer set, in which case you know that they are ordered… as no swaps occurred…

here is some pseudo code (it’s not python… but should be able to translate it easily)


scores(0) as integer
temp as integer
flag as boolean
i as integer

flag = false

do 
{
      flag = false

      for i = 1 to UpperBoundary(scores) {
         if (scores(i) > scores(i - 1)) {
             temp = scores(i)
             scores(i) = scores(i-1)
             scores(i-1) = temp
             flag = true
          }
      }
} loop until (flag = false)

if you’re not sure how to convert it to python, Pm me… and I’ll re-write it for you… but if you know python you shouldn’t have any issues here…

hope this makes sense…
Whipster

Thanks guys, if this works out nicely I surely will create a tutorial ^^

As Sambasidor says. You can use the list.sort, but I guess you’d want to use dictionaries to allow you to record score and players initials

http://code.activestate.com/recipes/52306/

or you could nest a list within a list

line = [1,“AAA”,10000]
table = [line]

One problem with Dictionaries is that each item has to have a unique key. That means that if you use Score as the key you wouldn’t be able to have 2 people tie, and if you used Name as the key you wouldn’t be able to have the same person be in there more than once.

I made a working version of this… but I left it at work. If you haven’t figured it out by Wednesday I’ll post it up.