Creating a High Score screen with AI2

7,753 views
Skip to first unread message

bagi...@gmail.com

unread,
May 16, 2014, 10:00:56 PM5/16/14
to mitappinv...@googlegroups.com, abhina...@gmail.com
I have been searching for a way to get a high score screen in AI2 for 2 days. The score after the game is working fine but I just can't seem to figure out how to create a high score screen. Every other tutorial I've seen is for AI1, which I don't understand. Please tell me how to create a high score screen in AI2 as soon as possible.

bagi...@gmail.com

unread,
May 16, 2014, 11:19:58 PM5/16/14
to mitappinv...@googlegroups.com, abhina...@gmail.com
Btw I'm making a game similar to mole mash

SteveJG

unread,
May 17, 2014, 8:31:48 AM5/17/14
to
Where have you been searching?  If you use the Search for topics block at the top of this page  and type  high score   you get 

The first item is AI2.

Did this help?

Regards,
Steve

Taifun

unread,
May 17, 2014, 10:36:13 AM5/17/14
to mitappinv...@googlegroups.com
How to store high scores with TinyDB by richard
Taifun

Trying to push the limits of App Inventor! Snippets and Tutorials from Pura Vida Apps by Taifun.         

bagi...@gmail.com

unread,
May 17, 2014, 11:01:00 AM5/17/14
to mitappinv...@googlegroups.com
I've tried this but it still doesn't work. Also I'd like to say that I'm using 2 separate screens for storing high scores and game scores. Please show me an .aia file or send a picture of your blocks.

bagi...@gmail.com

unread,
May 17, 2014, 11:01:21 AM5/17/14
to mitappinv...@googlegroups.com

Taifun

unread,
May 17, 2014, 11:05:57 AM5/17/14
to mitappinv...@googlegroups.com
what have you tried?
you might want to provide a screenshot of your relevant blocks

bagi...@gmail.com

unread,
May 17, 2014, 11:31:22 AM5/17/14
to mitappinv...@googlegroups.com
I actually used a tiny db instead of labels as the score was stored in another screen. Is that wrong?

SteveJG

unread,
May 17, 2014, 11:40:28 AM5/17/14
to mitappinv...@googlegroups.com
Using TinyDB is thebest way and you did it.   You might post a png or jpg of the blocks that seem to be this issue as Taifun suggests.

Taifun

unread,
May 17, 2014, 11:41:17 AM5/17/14
to mitappinv...@googlegroups.com

Stephen Zipprich

unread,
May 17, 2014, 1:57:06 PM5/17/14
to mitappinv...@googlegroups.com, abhina...@gmail.com
I just recreated my how to keep a high score list example on my Google site here for AI2.  Check it out.

You can use control blocks for passing values between screens rather than TinyDB.  I like this method better because I'd rather create an app as I would package it for distribution rather than have to redo some of the coding later.  The disadvantage is these blocks don't work in the development environment.  Both ways have their benefits.

Taifun

unread,
May 17, 2014, 2:15:00 PM5/17/14
to mitappinv...@googlegroups.com
You can use control blocks for passing values between screens rather than TinyDB.  I like this method better because I'd rather create an app as I would package it for distribution rather than have to redo some of the coding later
why do you think, you have to redo some of the coding later, if you use TinyDB to pass values between screens? This is not necessary.
the big advantage to use TinyDB is, to be able to test during development, and this is useful especially for beginners...
as far as I can see, there are no disadvantages...
Taifun

Stephen Zipprich

unread,
May 17, 2014, 2:27:13 PM5/17/14
to mitappinv...@googlegroups.com
Just personal preference for how I do it.  There's not a disadvantage to using the TinyDB but I don't add them to every project I do in App Inventor.  Because of this, I don't want to add a TinyDB, code for development, then recreate the blocks not using the TinyDB.

bagi...@gmail.com

unread,
May 17, 2014, 11:27:07 PM5/17/14
to

Is this fine?
Blocks Picture.png

ari...@utica.edu

unread,
May 18, 2014, 12:43:46 AM5/18/14
to mitappinv...@googlegroups.com
The overall concept is okay, but there are a few issues.
The flow should be:
  • Set Label1.Text to the player's score
  • If the Player's score (Label1.Text) > High Score then
    • Make the Player's score (Label1.Text) the new High Score.

Move the "set Label1.Text call TinyDB1.Get Value ..." before the "If" statement. (Also, replace the empty string red block for the valueIfTagNotThere parameter to a blue number block with the number zero in it.) This way, the Text property of this label has the current score of the player.

Now, we need to compare this score with the highest score so far (which is in your TinyDB component under the key "HighScore"). Make the following changes to the condition of the "if" statement:
  1. Change the name of the key in the TinyDB.GetValue block to "HighScore."
  2. Replace the empty string red block for the "valueIfTagNotThere" parameter to a blue block with the number zero in it. (App Inventor may do this automatically, but it is a good practice to compare 'oranges with oranges')
  3. Change the relational operator to '<'
That should do it.

bagi...@gmail.com

unread,
May 18, 2014, 1:08:29 AM5/18/14
to mitappinv...@googlegroups.com
Thanks for your help but I'm still encountering a problem:
The High Score screen doesn't save the highest score it just gives out the last score I achieved in the game.
Eg. My first game's score is 11. After restarting the app and achieving a score of 3 it says my High Score is 3.

SteveJG

unread,
May 18, 2014, 8:44:58 AM5/18/14
to mitappinv...@googlegroups.com

Several users are struggling with a logic issue involving "saving high score."  Users are attempting to code their own high score block.  You should be 
scanning the forum to see if other users are having similar problems and finding solutions using the Search block at the top of the 
AI2 forum page.


There are many ways to save a high score.

1)  game high score   replaces global high score.   Only the highest score ever achieved is posted.

   High Score: 213               global high score is the only score saved in the TinyDB.

   There are issues here.  Unless there is code to compare the game score with the HighScore, the game score 
  may end up replacing the Highest Score.  You will need a Math block to check which score is greater and logic blocks 
to tell the app which score to save.  

2) all scores achieve are posted but the scores are not sorted.  You are going to have to create a list of scores probably and then sort it.

2)  all scores are recorded along with the 'users' initials or name
    SJG  213
    TAM  200
    JM   180
    
   In this case, before a score is logged into the database, a text box appears where the user can input his/her 
   name or initials.  EVERY score is logged.  You will need tags like SJG, TAM, JM or Steve rather than a single highscore tag.  To show 
the scores, you will need to save the scores of each user from the database into a list, for example, then print out the results in a 
label. possibly using the \n  characters to force line changes in the label.

You can record only the highest score for each tag unless you include more information in the tag, for instance the time.

You as a developer need to provide the logic for the type of high score display you want.

I am not solving your issues, here.   I am only attempting to explain why the results you are coding are different than you expect.

bagi...@gmail.com

unread,
May 18, 2014, 8:57:20 AM5/18/14
to mitappinv...@googlegroups.com
Thank you but I'm failing to put the pieces together. Could you send me a picture of your blocks, if it's possible?

bagi...@gmail.com

unread,
May 18, 2014, 9:02:17 AM5/18/14
to mitappinv...@googlegroups.com
Is there anyome who has actually made a high score screen with AI2?

Taifun

unread,
May 18, 2014, 9:04:34 AM5/18/14
to
why not just using Stephens example?
Taifun

bagi...@gmail.com

unread,
May 18, 2014, 9:07:44 AM5/18/14
to mitappinv...@googlegroups.com
I tried it out but it didn't work for me.

Taifun

unread,
May 18, 2014, 9:44:12 AM5/18/14
to mitappinv...@googlegroups.com
you might want to provide a screenshot of your relevant blocks

bagi...@gmail.com

unread,
May 18, 2014, 11:39:42 AM5/18/14
to mitappinv...@googlegroups.com
I actually do not have acess to my laptop right now but if its possible could you show me a .aia file.

Taifun

unread,
May 18, 2014, 11:45:58 AM5/18/14
to mitappinv...@googlegroups.com
no, I do not have an aia file for you
probably you might want to read this whathaveyoutried.com
Taifun

Abraham Getzler

unread,
May 18, 2014, 8:41:09 PM5/18/14
to mitappinv...@googlegroups.com, abhina...@gmail.com

Here's another high score sample to try ...


Stephen Zipprich

unread,
May 18, 2014, 10:38:30 PM5/18/14
to mitappinv...@googlegroups.com
I tried it out but it didn't work for me.


What exactly about it didn't work for you?  Were you able to modify it for your project?

bagi...@gmail.com

unread,
May 18, 2014, 11:13:14 PM5/18/14
to mitappinv...@googlegroups.com

My problem is that the high score doesn't save. Eg. Game one's score is eleven. Therefore, eleven is saved as the high score. Game two's score is three but the high score is three instead of eleven. Please tell me if its a problem with my blocks or is it something else?

bagi...@gmail.com

unread,
May 18, 2014, 11:41:32 PM5/18/14
to mitappinv...@googlegroups.com
I figured out half of the problem. I just misunderstood arevirlegna. Thank you arevirlegna.
Now I would like to add initials to high score and I would also like to add second and third place too.
So far I've figured how to get the highest score without initials.
Here are the blocks:

Stephen Zipprich

unread,
May 19, 2014, 12:34:12 AM5/19/14
to mitappinv...@googlegroups.com
You'd likely be best benefited by using a list then.  See my example again.  You'll probably need a second list for the initials.

bagi...@gmail.com

unread,
May 19, 2014, 6:43:17 AM5/19/14
to mitappinv...@googlegroups.com
I'll try but could you explain to me what I can do?

bagi...@gmail.com

unread,
May 19, 2014, 10:19:45 AM5/19/14
to mitappinv...@googlegroups.com
Guys I just tested my app again and I realised it doesn't work. I guess I was wrong. Please help I have 2 days left to submit my project.

SteveJG

unread,
May 19, 2014, 10:42:48 AM5/19/14
to mitappinv...@googlegroups.com
This is a very simple demo showing one way to save ALL scores in a game using the new File tool.  The demo saves to the info internally within the app.
Read the File documentation to see how to save to your SD card (see 3 below).  The screen changing routines do NOT free memory as written..write appropriate 
code..this is not a demo about how to change screens.

I tested the example both on the emulator and a tablet.   If you find bugs, please post here.



1)  In this demo, the 'high score' is input using a text box; in a game, you would not use a text box but the variable containing the score.  You will also 
  change the logic requiring text in both textbox1 and textbox2.
2)  You need a File control in each screen you use.  You only need one screen if you post the results in a Notifier instead of in a label.
3)  This code stores the high score file internaly into the app.  You can also store to your SD card using the control character \ preceeding the file name.
4)  Because AI2 apps can not delete Resources, you can not use the DeleteFile directive storing within the app.  Instead the example saves an empty text 
file to MyHighScores.  When the file is overwritten, the contents are completely changed.
5)  There is little or no error control in this example; you need to write your own.
6)  The routine posts ALL scores you capture.  They are not sorted.  Routines can be written to sort them.  You will need to parse the label that contains 
the scores or parse the text file saved.   I imagine you could save this info as a csv file too.
7)  Is this better than using a TinyDB?   Perhaps.









I hope this is useful for someone.    The File control documentation is here:  http://ai2.appinventor.mit.edu/reference/components/storage.html#File

Regards,
Steve


arevirlegna

unread,
May 19, 2014, 11:16:28 AM5/19/14
to mitappinv...@googlegroups.com
This should work. It is the right logic. I would suggest adding a second label just to show the value of "HighScore" before the "if" statement.
Are you seeing the current score in Label1 when the HighScoreScreen appears?

bagi...@gmail.com

unread,
May 19, 2014, 11:25:01 AM5/19/14
to mitappinv...@googlegroups.com
At first it was working fine but once I tested the app a few hours later it wasn't working. Now it just shows 0 as the high score.

arevirlegna

unread,
May 19, 2014, 11:27:40 AM5/19/14
to mitappinv...@googlegroups.com
Regarding @SteveJG 's solution: The inner "if" statement in the "When Button2.Click" event handler, is not needed as the condition is always True. Keep the blocks in the "then" part of the "if" statement, and get rid of the inner "if" block.

bagi...@gmail.com

unread,
May 19, 2014, 11:29:45 AM5/19/14
to mitappinv...@googlegroups.com
I'll try out this method and notify you If I'm having problems.

arevirlegna

unread,
May 19, 2014, 11:32:04 AM5/19/14
to mitappinv...@googlegroups.com
Is it possible you made some changes somewhere else in the app that affected the execution of this code? 

SteveJG

unread,
May 19, 2014, 11:33:16 AM5/19/14
to mitappinv...@googlegroups.com
@arevirlegna       Yes, the if is not needed but the blocks in the If event handler are needed as you stated. Thanks for your observations.



-- Steve

bagi...@gmail.com

unread,
May 19, 2014, 11:19:17 PM5/19/14
to
The High Score Bar works but it's not in order. Please help as soon as possible.

SteveJG

unread,
May 19, 2014, 11:14:33 PM5/19/14
to mitappinv...@googlegroups.com
Here is one way to sort a list:  http://www.appinventorblocks.com/appinventor-tutorials-tips/appinventor-list-sorting-tutorial   This example uses  a bubble sort of a list.

Unfortunately, AI2 does not have any built in sorting routines.

If you used Stephen's example, I think everything is sorted as it is produced.

Regards,
Steve

bagi...@gmail.com

unread,
May 20, 2014, 12:01:44 AM5/20/14
to mitappinv...@googlegroups.com
Could you show us the components you added in designer and send them. Also does this have name support?

bagi...@gmail.com

unread,
May 20, 2014, 7:04:30 AM5/20/14
to mitappinv...@googlegroups.com
Guy's I'm really struggling now. Can somebody please help me out? I have 2 days left. I also have examinations for the next 2 days, so this might be my last chance to finish it.

bagi...@gmail.com

unread,
May 20, 2014, 7:07:46 AM5/20/14
to mitappinv...@googlegroups.com
Is it possible to sort text in files?

SteveJG

unread,
May 20, 2014, 7:58:55 AM5/20/14
to mitappinv...@googlegroups.com
It is possible to sort text files.  I would convert them to a csv file; convert the csv to a list then use the bubble sort to order them.  How easy it is to sort depends on if the file is all numbers or all text (easy) to files that are characters and text (much more difficult).

What do your text files look like:
235
534
256

or 

mit  235
bag  534
sos  256   ?


-- Steve

Taifun

unread,
May 20, 2014, 9:20:08 AM5/20/14
to mitappinv...@googlegroups.com
Guy's I'm really struggling now. Can somebody please help me out? I have 2 days left. I also have examinations for the next 2 days, so this might be my last chance to finish it.
my suggestion is, start doing your work now... you always can adjust an example to your needs...
the homework should be done by the student and not by someone else
Taifun

bagi...@gmail.com

unread,
May 20, 2014, 9:55:40 PM5/20/14
to mitappinv...@googlegroups.com
It looks like the 2nd one with the initials. Thanks for the help.

SteveJG

unread,
May 20, 2014, 10:27:17 PM5/20/14
to mitappinv...@googlegroups.com
I have no way to sort the second list at the moment.  The first list (numbers only) is possible with  the bubble sort I mentioned earlier.
If you need a list like the second sorted, I think the example given by Stephen produces that.  With the method I presented you can have all the scores with initials  or sorted numerical scores only (if you add the bubble sort code ).

-- Steve

bagi...@gmail.com

unread,
May 21, 2014, 7:50:25 AM5/21/14
to mitappinv...@googlegroups.com, abhina...@gmail.com
Does anyone know what my problem is?

SteveJG

unread,
May 21, 2014, 8:03:48 AM5/21/14
to
@bagi..    What is the problem you are talking about?    Not being able to sort?   using a bubble sort or what?     Post some code?   

If you notice, none of the solutions anyone has provided can provide  a high score list sorted    when characters and numerals are part of the list  ...   mit   2340    format.    Stephen's example
comes closest.    I can show you how to sort   high scores    2340,   3421    but without the initials.  Sorry, best I can do  and I do believe no one on the forum can provide code to sort mit 2340 data at the moment with AI2.   I would love to have someone prove me wrong and provide you with an aia demonstrating it.  though I think that might be possible but involved.

bagi...@gmail.com

unread,
May 21, 2014, 9:24:10 AM5/21/14
to mitappinv...@googlegroups.com
I've tried quite a few methods but none work. I would like to use a method with tinydb instead of lists as I understand tinydb better. Please give me a method I can use.

Taifun

unread,
May 21, 2014, 9:43:17 AM5/21/14
to mitappinv...@googlegroups.com
you might want to provide a screenshot of your relevant blocks
Taifun

Trying to push the limits of App Inventor! Snippets and Tutorials from Pura Vida Apps by Taifun.         

SteveJG

unread,
May 21, 2014, 9:58:39 AM5/21/14
to mitappinv...@googlegroups.com
if you have to a high score list in the format      mit 123
                                                                    abc 321

You are going to have to use a list somewhere in my opinion.     I suggest you may try this:  https://sites.google.com/site/stevozip/home/text/highscore    and then 
save the results to a TinyDb with code you supply.

Alternatively, here is a different way entirely of doing the high score and it might still be possible.  Taifun shows this method where you  http://puravidaapps.com/score.php 

Echoing Taifun... to help you, the people here need to see what your present code is.   A png image is the best way to do this.

Yes, I know you are running out of time and I also know you are trying very hard. 

You are going to either stay with what you got or do a lot of work..... Please do not ignore studying for your other exams.

-- Steve

Abraham Getzler

unread,
May 21, 2014, 2:06:50 PM5/21/14
to mitappinv...@googlegroups.com, abhina...@gmail.com
This example keeps both initials and scores, and keeps the list sorted in descending order by score.
ABG

max_score.aia
high_scores_list.jpg

SteveJG

unread,
May 21, 2014, 2:48:39 PM5/21/14
to mitappinv...@googlegroups.com, abhina...@gmail.com
Great work ABG!!!

@abhina....     this is what this code does: 

..add the following blocks and an additional label to get this:


Hope this helps.  Good luck.


Regards,

Steve


bgee...@gmail.com

unread,
May 22, 2014, 7:15:10 AM5/22/14
to mitappinv...@googlegroups.com, abhina...@gmail.com
The .aia file isn't working. Can you provide a link to the file or re-upload it? Thanks for your help.

SteveJG

unread,
May 22, 2014, 7:56:22 AM5/22/14
to mitappinv...@googlegroups.com
I just downloaded the  max_score.aia  and it works perfectly.  Did you use the View or Download link?   Use the Download link, copy to your desktop then use the AI2 browser to upload tha aia to your Projects.

What exactly is not working?  Tell us more and you will get additional help.

Regards,
Steve

Matheus Hobold Sovernigo

unread,
Jun 8, 2014, 8:30:39 PM6/8/14
to mitappinv...@googlegroups.com
Guys, any chance of making this code or any other with same function to work with TinyWebDB?

Taifun

unread,
Jun 8, 2014, 8:53:06 PM6/8/14
to mitappinv...@googlegroups.com
yes, you can do this with any database
first do the tutorials http://appinventor.mit.edu/explore/ai2/tutorials.html to learn the basics of App Inventor, then try something and if you get stuck, post a screenshot of your relevant blocks

Matheus Hobold Sovernigo

unread,
Jun 9, 2014, 6:56:35 AM6/9/14
to mitappinv...@googlegroups.com
Unfortunately there are no tutorials on TinyWebDB component. I've already used it in some apps, but I never managed to do a high score table that worked flawlessly to show a list of both initials and scores up to a limit and to keep the list sorted in descending order by score. In this approach, when I try to add a score, I get the following error message: "The operation select list item cannot accept the arguments 1 1". Seems like these 1 1 are the list index and something I can't remember now. If I change the blocks back to TinyDB they work.


Taifun

unread,
Jun 9, 2014, 8:55:35 AM6/9/14
to mitappinv...@googlegroups.com




if is empty get valueFromWebDB
then ---

Taifun

Trying to push the limits of App Inventor! Snippets and Tutorials from Pura Vida Apps by Taifun.         

http://appinventor.mit.edu/explore/ai2/support/blocks/text.html#isempty

is empty

Returns whether or not the string contains any characters (including spaces). When the string length is 0, returns false otherwise it returns true.

Matheus Hobold Sovernigo

unread,
Jun 9, 2014, 9:12:28 AM6/9/14
to mitappinv...@googlegroups.com
Now I get the following error message: "The operation select list item cannot accept the arguments 0 1". It only changed from 1 to 0.

Taifun

unread,
Jun 9, 2014, 9:48:32 AM6/9/14
to mitappinv...@googlegroups.com
delete the TinyDB using the TinyDB.ClearAll block and restart the companion app, then try again
also use the Do it functionality to debug your code: right click for example onto a global high_scores block and select "Do it" to receive the result in a comment window
the high_scores variable should be ((0 Beat my score!)) at the beginning...
Taifun

Matheus Hobold Sovernigo

unread,
Jun 9, 2014, 8:02:27 PM6/9/14
to mitappinv...@googlegroups.com
I deleted using the block. Restarted, rebuilt and tested the "Do it" function of a global high_scores block. I got the "(0 Beat my score!)" message a little different than you said, as you can see in the picture. And when I tried to add_a_high_score, I got the same message as before "The operation select list item cannot accept the arguments 0 1". If I make a "Do it" on the TinyWebDB.StoreValue, it works to save the "(0 Beat my score!)" on the appspot page, so the problem is really on the procedure.


Stephen Zipprich

unread,
Jun 9, 2014, 10:41:57 PM6/9/14
to mitappinv...@googlegroups.com

Taifun

unread,
Jun 10, 2014, 9:32:12 AM6/10/14
to mitappinv...@googlegroups.com
as already said the high_scores variable should be ((0 Beat my score!)) at the beginning...
you get currently only (0 Beat my score!)
which means, a make a list block is missing (see screenshot)



remember: you are working with a list of lists, the correct initialization would be a list having one item, which is a sublist
Taifun 

Matheus Hobold Sovernigo

unread,
Jun 10, 2014, 3:01:04 PM6/10/14
to mitappinv...@googlegroups.com
I don't get it. The "make a list block" is already placed there. Putting another block of the same kind there doesn't help.

Stephen Zipprich

unread,
Jun 10, 2014, 3:12:38 PM6/10/14
to
It does help.  It creates a sublist to avoid errors.  You have a nested select list item block right?  That is because you're working with sublists in your parent list.  Otherwise you wouldn't do this.

Abraham Getzler

unread,
Jun 10, 2014, 3:22:00 PM6/10/14
to mitappinv...@googlegroups.com
There are two places in your latest screenshot needing double nesting.
You are missing the extra layer in the initialization step directly above the trash can.
The initialization on the left side has the required double layer.

ABG

Matheus Hobold Sovernigo

unread,
Jun 10, 2014, 9:02:53 PM6/10/14
to mitappinv...@googlegroups.com
Wow, I can't believe it's working now! Thank you a lot, you're awesome. I would never realize the problem was with the sublists.
Reply all
Reply to author
Forward
0 new messages