DilloDude wrote:I made a working high-score page system for my Super-Human game I'm working on (see my sig). It stores the top ten highscores and along with the player's initials, and the character they used. The results are sorted when a new one isadded, and they are displayed in a single text actor. (Limitations have to be made for length, because a text string can contain only 255 characters.) If you like, I'll post a simplified and commented version of the code for you soon.
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
}
strcpy(text, "_");
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
}
}
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.
}
Users browsing this forum: No registered users and 1 guest