Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMichael Pastran
4,727 Pointshow to keep high scores in pygame
hey, so i created a simple game where objects fall from the top of the screen and we dodge them. i made a main menu where i want to keep the current high score. ive read around and from what i understand i need to write a function that is going to write a file that keeps the score as a string after each "crash" in the game. then we sort the file by largest value, and when we call the main menu function it loads up the first value which after the sort should be the highest score. also ive read i can use the pickle module??
any help would be awsome. thanks
2 Answers
Michael Pastran
4,727 Pointsyeah that kind of helped. i get the general idea of how it needs to be worked out. but im failing at writing the logic in my head to the script. btw do you know if theres a way to tag Kenneth onto this. he would prob know how to do this . lol
Darren Joy
19,573 PointsDon't know how to add a user to a comment stream... everywhere else it would be something like using the @ symbol... @Darren Joy... or something like that
That answered my question.. typing that pulled up a drop down that showed my own name... I imagine you can tag it from there?
Anyhoooo. I reread some notes and using the list you should be able to slice the rest of the list into itself
like:
myPlaceholder = myList[5:] if you found you needed to 'replace' the 5th item... What I read seems to suggest you can just add it back to the list and it will replace everything else.. something like myList = myList + myPlaceholder. Check the slicing notes on how to add two list together to make sure I'm not leading you astray..
Darren Joy
19,573 PointsIn terms of the logic: you'de have a place to hold your present score from the game rules itself, you'd need a score list to store all scores and an if statement to compare them all. I might do it with a counter if presentScore > scoreList[x] then scoreList[x] = presentScore else increment the counter and compare again
but remember to rename save or store all the other ones incremented by one something like
myPlaceholder = scoreList[x:] do this before you replace the scoreList[x] with the presentScore then add the top x items of the list to whatever you saved in myPlaceholder something like scoreList = scoreList[:x] + myPlaceholder but check syntax and python rules for adding lists cause it's been a month or so since I checked out python and I've been heavily into javascript stuff here on the site... But basically you want to take the top X, add the new score between it and the lower x+1 where it would sit in the heirarchy...
As an aside, it might be easier to store the top 5 or top 10 score and replace, incrememnt but if it's not in the top then discard it...
Darren Joy
19,573 PointsDarren Joy
19,573 PointsI did something not quite similar but sorta maybe over at Code Combat for lining up soldiers by order of 'strength' attribute. This was in their version of 'javascript', so I can only talk to this in pseudo code generalities. But it might help.
Basically I used an array. The array consisted of the strength of each object from highest to lowest. The function included comparing the 'new items' strength to the array items from item 0 on down the list. If it found my 'new' item was greater than the array item, it added it to the array at that position and then moved each item 'down one'.
To move the items I created a variable to hold the array item 'to be moved' then converted that array item to this new item. I then looped through the rest of the array from that point 'down' to swap items 'down one'. There's probably a more elegant way to do this in python. I seem to recall (as you suggest) from the courses that you can indeed add an item to a list and do all sorts of fun stuff with it like sort etc....
Hope this helps. Probably gives too much insight into the robotism of my braincells