Here's what I have: First I have three variables in which to save the state of the highscores:
- recscore as an integer array of size 10, which is saved in HSCORE. This variable stores the top ten highscores.
- recnam is a string array of size ten, also saved in HSCORE. This is where the player's initials are stored.
- recchar is another string array saved in HSCORE. This is an array of size 10 where the character used to obtain the score is stored.
I am also using an integer called score; this is where the current score is stored.
The name entry screen (image below) is made of three actors:
- NameMenu is the actor that is the background image; it includes a text-box where the text actor goes and instructions for entering your name.
- ScoreCount2 is a number that shows the score you achieved.
- NAMTXT is a text actor where you enter your initials. I have simplified it so that you only use letters and spaces. For best alligning results, you will need to use a fixed-width font (such as courier, or you can make your own as an image font or use mine (link below)).
The high-score display screen (third image) consists of two actors:
- HscoreMenu is the background actor, it also has a keydown event to return to the title screen.
- HscoreTXT is the text actor that displays the high-scores. It has the same sort of font as mentioned above. Because the text string is limited to 255 characters, I have limited the spaces between the different fields to one, and the character name shouldn't be longer than twelve, the score is limited to six charcters (It isn't that likely that you'll score more than 999999 points).
And now for the script that drives it.
My view actor has an event that checks when the game is over. It goes through some actions that stop the game, and then it checks if the score is big enough to be on the high-score list:
- Code: Select all
if (score >recscore[9])//the ten spots in recchar are numbered 0-9, so check if score is greater than the lowest one
{
//script to move to the name entry scrren and enable events on NAMTXT
ScoreCount2.textNumber = score;//set the display on the name entry menu to the score
}
else
{
//if score does not go on the list, return to the appropriate menu to play again and set score to 0
}
NameMenu only has a create actor event where it sets its x and y position and disables collision state.
NAMTXT is where the real action takes place. On the createactor event, disable collision state, and all events. Then initialize the text:
- Code: Select all
strcpy(text, "_");
This just means that when no name has been entered yet, an nderscore is displayed.
Next comes the keydown event. Make it any key, with repeat disabled. Then use the following script (edit it if needed, I'll try to put plenty of comments in it)
- Code: Select all
int k = getLastKey();//create an integer which stores the key pressed.
unsigned char len = strlen(text);//create a variable to store the length of the string entered.
if (strncmp(text, "_", 1) == 0)//if no text has been entered, remove the underscore
{
char dummy[4];//create a temporary string
strncpy(dummy, text, len - 1);//fill this string with all of the text, except for the last character
len --;//decrease the value of the string length
strcpy(text, dummy);//copy the temporary value back into the text
}
if (k >= KEY_a && k <= KEY_z)//if the key is a letter key, add the appropriate charcter
{
if (len < 3)//don't put a new character if the string is already full
{
char add[2];//create a temporary string to hold the value to add on
add[0] = k - 32;//set the value of the first character of this string to the appropriate value based on the pressed key
strcat(text, add);// add this temporary string to the text
}
}
else if (k == KEY_BACKSPACE)//if backspaced is pressed, remove the last character (same as above, except you must check the length first)
{
if (len > 0)
{
char dummy[4];
strncpy(dummy, text, len - 1);
strcpy(text, dummy);
}
}
else if (k == KEY_SPACE)//add a space if that key is pressed
{
strcat(text, " ");
}
else if (k == KEY_RETURN && len > 0 && strncmp(text, " ", len))//if the key pressed is enter, and there is text in the string, and the string is not all spaces, submit the highscore
{
setKeys();//this is a function I wrote to remap the keys to the player's configuration. If you aren't having custom controls, you don't need this
view.x += 600;//move the view to the high-score screen
EventEnable("HscoreMenu", EVENTALL);//enable events on the high-score screen
EventDisable("Event Actor", EVENTALL);//disable events on the text
//update hscore
{
int i, slot;//initialize some variables
//find the space where the new high-score number should go
slot = 9;
while (slot > 0 && score > recscore[slot - 1])
{
slot --;
}
//move all lower scores down on slot
for (i = 9; i >slot; i --)
{
recscore[i] = recscore[i - 1];//move the score
strcpy(recchar[i], recchar[i - 1]);//move the character
strcpy(recnam[i], recnam[i - 1]);//move the player's nam
}
recscore[slot] = score; score = 0;//set the score appropriate spot
strcpy(recchar[slot], character);//put the character in the appropriate spot
strcpy(recnam[slot], text);//put the initials in the appropriate spot
saveVars("conf.shd", "HSCORE");//save the new high-score list
SendActivationEvent("HscoreTXT");//tell the high-score screen to update the score
}
}
HscoreMenu has a create actor event where it sets its initial position, disables collision state, and disables events. It also has a keydown event to return you to the main menu. This should also disable events on the event actor.
Finally, you need to display the high-score list. HscoreTXT has a create actor event:
- Code: Select all
char i;//make a counter variable
char add[26];//make a string for one line
loadVars("conf.shd", "HSCORE");//load the high-scores
strcpy(text, "");//start with nothing in the text
for (i = 0; i < 10; i ++)//loop through for each line
{
sprintf(add, "%-4.4s%-13.13s%06i\n\n", recnam[i], recchar[i], recscore[i]);//fill up the line
//%-4.4s: the - means left allign. The 4 means to put four characters even if there are more. The .4 means don't put more than four characters in. The s says that the variable is a string.
//%-13.13s: same as above, except thirteen characters (twelve for the character name, plus one space).
//%06i: the 06 means that six characters will be inserted, any characters not part of the number will be zeroes. The i says that it is an integer.
//\n\n: insert two newline characters, to move down two lines.
strcat(text, add);//add the line onto the text.
}
HscoreTXT also has an activation event (from NAMTXT) that does the same as the above script except without loading the vars.
I think that wraps it all up. As with all codes and solutions that someone gives you,
try to understand what makes it work. That way you will be able to learn from it, and use bits of it for other stuff (so you can make your own
games rather than someone else telling you everything to do). If you have problems with getting it to work, adapting it to your situation, or understanding parts of it, just post and I (or someone else) should be able to help you.
PS: It looks complicated (and a lot of things do when you just see the completed lot) but it is really quite simple.
Here is the link to the font I made:
http://www.imageviper.com/displayimage/ ... _black.bmp