From e45733d59ae276abd44c54dbff0fd36174ba5ad0 Mon Sep 17 00:00:00 2001 From: Jason Bou-Samra <154414066+bou-samra@users.noreply.github.com> Date: Wed, 16 Oct 2024 20:09:06 +1100 Subject: [PATCH] Update hiscore.c --- src/hiscore.c | 164 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 106 insertions(+), 58 deletions(-) diff --git a/src/hiscore.c b/src/hiscore.c index 4c6dd8f..6b64842 100644 --- a/src/hiscore.c +++ b/src/hiscore.c @@ -7,62 +7,93 @@ #include "column.h" #include "backdrop.h" -SDL_Rect gameo_back = {119, 93, 82, 13}; // Game Over Background -SDL_Rect gameo_trim = {118, 92, 84, 15}; // Game Over Trim +SDL_Rect gameo_back = {119, 93, 82, 13}; // Game Over Background +SDL_Rect gameo_trim = {118, 92, 84, 15}; // Game Over Trim SDL_Rect entern_back = {115, 131, 90, 29}; // Enter Name Background SDL_Rect entern_trim = {114, 130, 92, 31}; // Enter Name Trim -SDL_Rect cursor = {128, 146, 7, 5}; // Name entry cursor -SDL_Rect cursor_last = {128, 146, 7, 5}; // last cursor position -SDL_Rect fnt = {128, 146, 7, 5}; // Name entry cursor -char pen1 = 0xff; +SDL_Rect cursor = {128, 146, 7, 5}; // Name entry cursor +SDL_Rect cursor_last = {128, 146, 7, 5}; // last cursor position +SDL_Rect fnt = {128, 146, 7, 5}; // Name entry cursor +char pen1 = 0xff; -int score = 65535; // current score -char level_r = 0x1E; // game level (remaining) -char level_c = 0x00; // game level (current) -int total = 0x00; // smashed tiles +int score; // current score +char level_r = 0x1E; // game level (remaining) +char level_c = 0x00; // game level (current) +int total = 0x00; // smashed tiles +int hscores1[8] = {1000, 780, 600, 500, 400, 300, 200, 100}; -int hscores [8]; // high score panel - scores -char names[8][7+1]; // high score panel - names -char name_temp[7+1] = "ANTARES"; +char hscores[8][7+1] = {{"......."}, {"......."}, {"......."}, {"......."}, {"......."}, {"......."}, {"......."}, {"......."}}; // high score panel - scores +char names[8][7+1] = {{"HULK "}, {"EARTH "}, {"BUSH "}, {"AXE "}, {"PERFECT"}, {"POWERS "}, {"TUGBOAT"}, {"DINO "}}; // high score panel - names +char name_temp[7+1] = " "; ///////////////// DISPLAY RESTART ///////////////// int Ren_restart(void) { SDL_SetRenderDrawColor(sr, 0xff , 0xff , 0xff, 0xff); - SDL_RenderFillRect(sr, &gameo_back); // restart background - for (int z = 0; z < 1; z++) { // 1 line of text + SDL_RenderFillRect(sr, &gameo_back); // restart background + for (int z = 0; z < 1; z++) { // 1 line of text Ren_line(gameo, gameol[z][0], gameol[z][1], z * 9, 9, 1); } SDL_RenderPresent(sr); return 0; } +/////////////// INSERT ENTRY INTO HIGH SCORE LIST /////////////////////// +int insert_score(int new) { + int hscores_new = new; + int counter; + + for (int i = 0; i < 8; i++) { + if (hscores_new > hscores1[i]) { + counter = 8 - i; // find position in list to insert + for (int j = 0; j < counter; j++) { + hscores1[7 - j] = hscores1[6 - j]; // shift right from position to make space, drop off last position + for (int k = 0; k < 8; k++) { + names[7 - j][k] = names[6 - j][k]; // do same as above for name array + } + } + hscores1[i] = hscores_new; // insert new high score into integer aray + for (int k = 0; k < 8; k++) { + names[i][k] = name_temp[k]; // insert new name into name ASCII array + } + break; + } + } + for (int j = 0; j < 8; j++){ + its(hscores1[j], str); // convert high scores to ASCII code + for (int k = 0; k < 8; k++){ + hscores[j][k] = str[k]; // store in high score ASCII array + } + } + return 0; // all done +} + /////////////////// RENDER NAME //////////////////// int Ren_name(void){ - SDL_SetRenderDrawColor(sr, 0xff , 0xff , 0xff, 0xff); // white pen - SDL_RenderFillRect(sr, &entern_back); // enter name background - SDL_RenderFillRect(sr, &gameo_back); // game over background - SDL_SetRenderDrawColor(sr, 0x00 , 0x00 , 0x00, 0xff); // black pen - SDL_RenderDrawRect(sr, &entern_trim); // enter name trim - SDL_RenderDrawRect(sr, &gameo_trim); // enter name trim - SDL_RenderDrawLine(sr, 118, 161, 207, 161); // enter name shadow + SDL_SetRenderDrawColor(sr, 0xff , 0xff , 0xff, 0xff); // white pen + SDL_RenderFillRect(sr, &entern_back); // enter name background + SDL_RenderFillRect(sr, &gameo_back); // game over background + SDL_SetRenderDrawColor(sr, 0x00 , 0x00 , 0x00, 0xff); // black pen + SDL_RenderDrawRect(sr, &entern_trim); // enter name trim + SDL_RenderDrawRect(sr, &gameo_trim); // enter name trim + SDL_RenderDrawLine(sr, 118, 161, 207, 161); // enter name shadow SDL_RenderDrawLine(sr, 118, 162, 207, 162); SDL_RenderDrawLine(sr, 206, 134, 206, 162); - SDL_RenderDrawLine(sr, 207, 134, 207, 162); // enter name shadow + SDL_RenderDrawLine(sr, 207, 134, 207, 162); // enter name shadow SDL_RenderDrawLine(sr, 122, 107, 203, 107); SDL_RenderDrawLine(sr, 122, 108, 203, 108); SDL_RenderDrawLine(sr, 202, 96, 202, 108); - SDL_RenderDrawLine(sr, 203, 96, 203, 108); // game over shadow + SDL_RenderDrawLine(sr, 203, 96, 203, 108); // game over shadow - for (int z = 0; z < 1; z++) { // 1 line of text (game over) + for (int z = 0; z < 1; z++) { // 1 line of text (game over) Ren_line(gameo, gameol[z][0], gameol[z][1], z * 9, 9, 1); } - for (int z = 0; z < 2; z++) { // 2 lines of text (enter name) + for (int z = 0; z < 2; z++) { // 2 lines of text (enter name) Ren_line(name, namel[z][0], namel[z][1], z * 10, 10, 1); } - for (int z = 0; z < 1; z++) { // 1 line of text (new high) + for (int z = 0; z < 1; z++) { // 1 line of text (new high) Ren_line(newh, newhl[z][0], newhl[z][1], z * 7, 7, 1); } // SDL_RenderPresent(sr); @@ -81,12 +112,12 @@ int Ren_cursor(void) { SDL_RenderPresent(sr); } // backdrop(colour); -// Ren_logo(); // logo -// Ren_level(); // render current level/score -// Ren_next(); // render next brick/block/piece -// Ren_high(); // render high scores +// Ren_logo(); // logo +// Ren_level(); // render current level/score +// Ren_next(); // render next brick/block/piece +// Ren_high(); // render high scores // SDL_SetRenderDrawColor (sr, 0xff, 0xff, 0xff, 255); -// SDL_RenderDrawRect(sr, &main_trim); // trim +// SDL_RenderDrawRect(sr, &main_trim); // trim // display_grid(); Ren_name(); SDL_Delay(20); @@ -95,39 +126,56 @@ int Ren_cursor(void) { ///////////////// DISPLAY GAME OVER ///////////// int Ren_gameover(void) { - - while (event.key.keysym.sym != SDLK_RETURN) { - Ren_cursor(); - SDL_PollEvent (&event); - if (event.type == SDL_KEYDOWN) { - if ((event.key.keysym.sym >= SDLK_0 && event.key.keysym.sym <= SDLK_9) || (event.key.keysym.sym >= SDLK_a && event.key.keysym.sym <= SDLK_z) || (event.key.keysym.sym == SDLK_SPACE) || (event.key.keysym.sym == SDLK_RETURN)) { - printf("event.key.keysym.sym: %02x\n", event.key.keysym.sym); - newh[(cursor.x - 128) / 8] = event.key.keysym.sym; // fill name array - SDL_RenderPresent(sr); - cursor.x = cursor.x + 8; // advance cursor - if (cursor.x == 192) { - cursor.x = 184; // 192 - 8 - } + int counter = 0; + cursor.x = 128; + for (int i = 0; i < 8; i++) { + newh[i] = 0x20; + name_temp[i] = 0x20; + } + while (event.key.keysym.sym != SDLK_RETURN) { + Ren_cursor(); + SDL_PollEvent (&event); + if (event.type == SDL_KEYDOWN) { + if ((event.key.keysym.sym >= SDLK_0 && event.key.keysym.sym <= SDLK_9) || (event.key.keysym.sym >= SDLK_a && event.key.keysym.sym <= SDLK_z) || (event.key.keysym.sym == SDLK_SPACE) || (event.key.keysym.sym == SDLK_RETURN)) { +// printf("event.key.keysym.sym: %02x\n", event.key.keysym.sym); + name_temp[counter] = event.key.keysym.sym; // ascii name array + newh[(cursor.x - 128) / 8] = event.key.keysym.sym; // fill name array +// SDL_RenderPresent(sr); + counter++; + if (counter > 7){counter = 7;} + cursor.x = cursor.x + 8; // advance cursor + if (cursor.x == 192) { + cursor.x = 184; // 192 - 8 } -else if (event.key.keysym.sym == SDLK_BACKSPACE) -{ - cursor.x = cursor.x - 8; // backspace - if (cursor.x == 120) { - cursor.x = 128; // 120 + 8 - } - newh[(cursor.x - 128) / 8] = 32; -} + } + else if (event.key.keysym.sym == SDLK_BACKSPACE) { + name_temp[counter]=0x20; // ascii name array + counter--; + if (counter < 0){counter = 0;} + cursor.x = cursor.x - 8; // backspace + if (cursor.x == 120) { + cursor.x = 128; // 120 + 8 + } + newh[(cursor.x - 128) / 8] = 32; } } - return 0; + } + return 0; } ////////////////// HANDLE END GAME ///////////////////////// int end_game(void) { - if (game_end == 1) { // restart - Ren_restart(); // Ren_restart(); - } else if (game_end == 2) { // game over - Ren_gameover(); +// score = 801; + + if (game_end == 1) { // restart + Ren_restart(); // Ren_restart(); + } else if (game_end == 2) { // game over + if (score < hscores1[7]) { + Ren_restart(); // if score less then lowest on list, no name entry + } else { + Ren_gameover(); + insert_score(score); // else display name entry dialog + } } check_key(); reset();