From 8b03d2ebbfc4d99d5d1d38a0ee2f3af4188ef624 Mon Sep 17 00:00:00 2001 From: Eleott-hi Date: Sat, 11 Dec 2021 11:19:04 +0700 Subject: [PATCH 01/11] Cycled texture loading, NUMBER OF PIESES!!! --- main.c | 100 ++++++++++++--------------------------------------------- 1 file changed, 21 insertions(+), 79 deletions(-) diff --git a/main.c b/main.c index dde0f00..e20ffb0 100644 --- a/main.c +++ b/main.c @@ -6,10 +6,12 @@ #include "raylib.h" #define SPRITE_EDGE_SIZE 64 +#define PIECE_NUMBER_IN_WIDTH 5 +#define PIECE_NUMBER_IN_HEIGHT 3 #define CANVAS_WIDTH 800 #define CANVAS_HEIGHT 400 #define SNAKE_SIZE 40 -#define SNAKE_SPEED 0.3f +#define SNAKE_SPEED 0.1f #define FEILD_WIDTH CANVAS_WIDTH / SNAKE_SIZE #define FEILD_HEIGHT CANVAS_HEIGHT / SNAKE_SIZE @@ -54,18 +56,19 @@ typedef enum SnakeParts HEAD_UP, HEAD_RIGHT, TURN_DOWN_TO_RIGHT, + TALE_UP, BODY_VERTICAL, HEAD_LEFT, HEAD_DOWN, + APPLE, + TALE_RIGHT, TURN_DOWN_TO_LEFT, TALE_DOWN, TALE_LEFT, - APPLE, - TALE_RIGHT, - TALE_UP + NUMBER_OF_PIECES, } SnakeParts; -Texture2D textureSnakeParts[TALE_UP + 1]; // Array of textures of snake parts +Texture2D textureSnakeParts[NUMBER_OF_PIECES]; // Array of textures of snake parts Sound eatApple; Sound wallCollision; @@ -247,84 +250,23 @@ int main(void) void UploadSnakeParts(void) { // Upload snake parts from image to texture array - Image snakeParts[TALE_UP + 1]; - Image snakePartsImage = LoadImage("resources/snake-parts.png"); - - for (int textureIndex = TURN_UP_TO_RIGHT; textureIndex < TALE_UP + 1; textureIndex++) + Image snakeSprites = LoadImage("resources/snake-parts.png"); + for (int x = 0; x < PIECE_NUMBER_IN_HEIGHT; x++) { - int x = 0; - int y = 0; - switch (textureIndex) + for (int y = 0; y < PIECE_NUMBER_IN_WIDTH; y++) { - case TURN_UP_TO_RIGHT: - x = 0; - y = 0; - break; - case BODY_HORIZONTAL: - x = 1; - y = 0; - break; - case TURN_UP_TO_LEFT: - x = 2; - y = 0; - break; - case HEAD_UP: - x = 3; - y = 0; - break; - case HEAD_RIGHT: - x = 4; - y = 0; - break; - case TURN_DOWN_TO_RIGHT: - x = 0; - y = 1; - break; - case BODY_VERTICAL: - x = 2; - y = 1; - break; - case HEAD_LEFT: - x = 3; - y = 1; - break; - case HEAD_DOWN: - x = 4; - y = 1; - break; - case TURN_DOWN_TO_LEFT: - x = 2; - y = 2; - break; - case TALE_DOWN: - x = 3; - y = 2; - break; - case TALE_LEFT: - x = 4; - y = 2; - break; - case APPLE: - x = 0; - y = 2; - break; - case TALE_RIGHT: - x = 1; - y = 2; - break; - case TALE_UP: - x = 1; - y = 1; - break; - default: - break; + int Index = x * 5 + y; + Rectangle crop = {SPRITE_EDGE_SIZE * y, + SPRITE_EDGE_SIZE * x, + SPRITE_EDGE_SIZE, + SPRITE_EDGE_SIZE}; + Image partImage = ImageFromImage(snakeSprites, crop); + ImageResize(&partImage, SNAKE_SIZE, SNAKE_SIZE); + textureSnakeParts[Index] = LoadTextureFromImage(partImage); + UnloadImage(partImage); } - - Rectangle crop = {x * SPRITE_EDGE_SIZE, y * SPRITE_EDGE_SIZE, SPRITE_EDGE_SIZE, SPRITE_EDGE_SIZE}; - Image partImage = ImageFromImage(snakePartsImage, crop); - ImageResize(&partImage, SNAKE_SIZE, SNAKE_SIZE); - textureSnakeParts[textureIndex] = LoadTextureFromImage(partImage); } + UnloadImage(snakeSprites); } void SetupSnake(void) From fd443ed883a6e8716fb2101119bfd2db23a32508 Mon Sep 17 00:00:00 2001 From: Eleott-hi Date: Sat, 11 Dec 2021 14:06:33 +0700 Subject: [PATCH 02/11] added hi-score rate for 1 player --- main.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/main.c b/main.c index e20ffb0..1145321 100644 --- a/main.c +++ b/main.c @@ -27,7 +27,7 @@ int key; bool collision = 0; bool pause = 0; bool death = 0; -int scoreCount = 0; +int score = 0; typedef struct Snake { @@ -48,6 +48,13 @@ typedef struct Snake bool hasEaten; // Return true if food has eaten } Snake; +typedef enum StoragePosition +{ + STORAGE_POSITION_SCORE, + STORAGE_POSITION_HISCORE, + +} StoragePosition; + typedef enum SnakeParts { TURN_UP_TO_RIGHT, @@ -177,7 +184,8 @@ int main(void) //----------------------------------------------------------------------------------- srand(time(NULL)); int framesCounter = 0; - + int hiScore = LoadStorageValue(STORAGE_POSITION_HISCORE); + UploadSnakeParts(); //----------------------------------------------------------------------------------- @@ -194,6 +202,7 @@ int main(void) death = 0; collision = 0; key = 0; + score = 0; SetupSnake(); } // Pause @@ -204,6 +213,10 @@ int main(void) if (!pause && !death) { Update(); + if (score > hiScore){ + SaveStorageValue(STORAGE_POSITION_HISCORE, score); + hiScore = score; + } } else framesCounter++; @@ -222,8 +235,8 @@ int main(void) if (death) { - DrawText(TextFormat("SCORE: %i", scoreCount), 280, 50, 40, MAROON); - DrawText(" YOU ARE DEAD !!! =(\n Press R to restart the game", CANVAS_WIDTH / 2 - 220, CANVAS_HEIGHT / 2 - 60, 30, BLACK); + DrawText(TextFormat("SCORE: %i\nHI-SCORE: %i", score, hiScore), 280, 50, 40, MAROON); + DrawText(" YOU ARE DEAD !!! =(\n Press R to restart the game", CANVAS_WIDTH / 2 - 220, CANVAS_HEIGHT / 2 - 20, 30, BLACK); } EndTextureMode(); @@ -444,7 +457,7 @@ void CheckFoodIsEaten(void) if (foodX == snake.headX && foodY == snake.headY) { snake.length++; - scoreCount += 10; + score += 10; snake.hasEaten = 1; PlaySound(eatApple); SpawnFood(); From a699106ee9796ad25e0dbc0a07f9150c0ede1aba Mon Sep 17 00:00:00 2001 From: Eleott-hi Date: Sun, 12 Dec 2021 00:02:18 +0700 Subject: [PATCH 03/11] 5 = PIECE_NUMBER_IN_WIDTH --- main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.c b/main.c index 1145321..7173790 100644 --- a/main.c +++ b/main.c @@ -268,7 +268,7 @@ void UploadSnakeParts(void) { for (int y = 0; y < PIECE_NUMBER_IN_WIDTH; y++) { - int Index = x * 5 + y; + int Index = x * PIECE_NUMBER_IN_WIDTH + y; Rectangle crop = {SPRITE_EDGE_SIZE * y, SPRITE_EDGE_SIZE * x, SPRITE_EDGE_SIZE, From f5ac9f5b3b07af5a81c84a89717a00e7f71ed5c2 Mon Sep 17 00:00:00 2001 From: Eleott-hi Date: Sun, 12 Dec 2021 00:13:45 +0700 Subject: [PATCH 04/11] speed = 0.2 --- main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.c b/main.c index 7173790..ecc7a11 100644 --- a/main.c +++ b/main.c @@ -11,7 +11,7 @@ #define CANVAS_WIDTH 800 #define CANVAS_HEIGHT 400 #define SNAKE_SIZE 40 -#define SNAKE_SPEED 0.1f +#define SNAKE_SPEED 0.2f #define FEILD_WIDTH CANVAS_WIDTH / SNAKE_SIZE #define FEILD_HEIGHT CANVAS_HEIGHT / SNAKE_SIZE From 50179ac55fb2e467779dd5559e44ef1bec363a1c Mon Sep 17 00:00:00 2001 From: Trofimov Alexey Date: Sun, 12 Dec 2021 15:19:55 +0300 Subject: [PATCH 05/11] dynamic size of game parts --- .gitignore | 3 ++- main.c | 21 ++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index adb36c8..cfd6760 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -*.exe \ No newline at end of file +*.exe +storage.data \ No newline at end of file diff --git a/main.c b/main.c index ecc7a11..0554a5d 100644 --- a/main.c +++ b/main.c @@ -6,8 +6,6 @@ #include "raylib.h" #define SPRITE_EDGE_SIZE 64 -#define PIECE_NUMBER_IN_WIDTH 5 -#define PIECE_NUMBER_IN_HEIGHT 3 #define CANVAS_WIDTH 800 #define CANVAS_HEIGHT 400 #define SNAKE_SIZE 40 @@ -72,10 +70,9 @@ typedef enum SnakeParts TURN_DOWN_TO_LEFT, TALE_DOWN, TALE_LEFT, - NUMBER_OF_PIECES, } SnakeParts; -Texture2D textureSnakeParts[NUMBER_OF_PIECES]; // Array of textures of snake parts +Texture2D textureSnakeParts[TALE_LEFT + 1]; // Array of textures of snake parts Sound eatApple; Sound wallCollision; @@ -185,7 +182,7 @@ int main(void) srand(time(NULL)); int framesCounter = 0; int hiScore = LoadStorageValue(STORAGE_POSITION_HISCORE); - + UploadSnakeParts(); //----------------------------------------------------------------------------------- @@ -213,7 +210,8 @@ int main(void) if (!pause && !death) { Update(); - if (score > hiScore){ + if (score > hiScore) + { SaveStorageValue(STORAGE_POSITION_HISCORE, score); hiScore = score; } @@ -264,13 +262,14 @@ void UploadSnakeParts(void) { // Upload snake parts from image to texture array Image snakeSprites = LoadImage("resources/snake-parts.png"); - for (int x = 0; x < PIECE_NUMBER_IN_HEIGHT; x++) + + for (int y = 0; y < snakeSprites.height / SPRITE_EDGE_SIZE; y++) { - for (int y = 0; y < PIECE_NUMBER_IN_WIDTH; y++) + for (int x = 0; x < snakeSprites.width / SPRITE_EDGE_SIZE; x++) { - int Index = x * PIECE_NUMBER_IN_WIDTH + y; - Rectangle crop = {SPRITE_EDGE_SIZE * y, - SPRITE_EDGE_SIZE * x, + int Index = y * snakeSprites.width / SPRITE_EDGE_SIZE + x; + Rectangle crop = {SPRITE_EDGE_SIZE * x, + SPRITE_EDGE_SIZE * y, SPRITE_EDGE_SIZE, SPRITE_EDGE_SIZE}; Image partImage = ImageFromImage(snakeSprites, crop); From a451de26a966e71abc34404ecf653236da2ba9d1 Mon Sep 17 00:00:00 2001 From: Eleott-hi Date: Tue, 14 Dec 2021 18:17:58 +0700 Subject: [PATCH 06/11] Enter name, save json, score table --- Makefile | 2 +- main.c | 303 +++++++++++++++++++++++++++++++++++++++++++++++----- rating.json | 1 + 3 files changed, 281 insertions(+), 25 deletions(-) create mode 100644 rating.json diff --git a/Makefile b/Makefile index a47ab28..55ebf66 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ win: ./snake.exe rm ./snake.exe linux: - $(CC) ./main.c -o snake -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 + $(CC) ./main.c -o snake -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 -ljson-c ./snake rm ./snake mac: diff --git a/main.c b/main.c index 1145321..108a9d7 100644 --- a/main.c +++ b/main.c @@ -3,7 +3,10 @@ #include #include #include -#include "raylib.h" +#include +#include + +#define MAX_INPUT_CHARS 10 #define SPRITE_EDGE_SIZE 64 #define PIECE_NUMBER_IN_WIDTH 5 @@ -11,7 +14,7 @@ #define CANVAS_WIDTH 800 #define CANVAS_HEIGHT 400 #define SNAKE_SIZE 40 -#define SNAKE_SPEED 0.1f +#define SNAKE_SPEED 0.3f #define FEILD_WIDTH CANVAS_WIDTH / SNAKE_SIZE #define FEILD_HEIGHT CANVAS_HEIGHT / SNAKE_SIZE @@ -28,6 +31,8 @@ bool collision = 0; bool pause = 0; bool death = 0; int score = 0; +int prevScore = 0; +bool scoreSave = 1; typedef struct Snake { @@ -48,12 +53,27 @@ typedef struct Snake bool hasEaten; // Return true if food has eaten } Snake; -typedef enum StoragePosition +typedef struct Player { - STORAGE_POSITION_SCORE, - STORAGE_POSITION_HISCORE, + char name[MAX_INPUT_CHARS + 1]; +} Player; -} StoragePosition; +typedef enum StoragePositionChampion +{ + STORAGE_POSITION_NAME_CHAR_0, + STORAGE_POSITION_NAME_CHAR_1, + STORAGE_POSITION_NAME_CHAR_2, + STORAGE_POSITION_NAME_CHAR_3, + STORAGE_POSITION_NAME_CHAR_4, + STORAGE_POSITION_NAME_CHAR_5, + STORAGE_POSITION_NAME_CHAR_6, + STORAGE_POSITION_NAME_CHAR_7, + STORAGE_POSITION_NAME_CHAR_8, + STORAGE_POSITION_NAME_CHAR_9, + STORAGE_POSITION_NAME_CHAR_10, + STORAGE_POSITION_NAME_CHAR_11, + STORAGE_POSITION_HISCORE, +} StoragePositionChampion; typedef enum SnakeParts { @@ -135,6 +155,12 @@ void CheckCollision(void); void UploadSnakeParts(void); +void Restart(char *); + +char EnterName(char *, int *, Player *, json_object *, int *, json_object *, char *); + +void DrawStart(char[], int); + //==================================================================================== void Draw(void) @@ -160,6 +186,8 @@ Rectangle GetCanvasTarget() return rec; } +//==================================================================================== + int main(void) { // Resizable window @@ -183,39 +211,123 @@ int main(void) // Declarations //----------------------------------------------------------------------------------- srand(time(NULL)); + + char nameIsExist = 0; + char table = 0; + char start = 1; + + int letterCount = 0; int framesCounter = 0; int hiScore = LoadStorageValue(STORAGE_POSITION_HISCORE); + + // Initiolize json structure + const char *rating = "rating.json"; + json_object *root = json_object_from_file(rating); + if (!root) + return 1; + json_object *players = json_object_object_get(root, "players"); + json_object *player; + int index = 0; + int objectsCount = 0; + + Player newPlayer = { + .name = "\0", + }; + + //====================================================================================== + + + + char champName[MAX_INPUT_CHARS + 1] = "\0"; + + for (int i = 0; i < MAX_INPUT_CHARS + 1; i++) + { + champName[i] = (char)LoadStorageValue(i); + } + + //====================================================================================== + UploadSnakeParts(); //----------------------------------------------------------------------------------- SetupSnake(); SetTargetFPS(60); + // Main loop //==================================================================================== while (!WindowShouldClose()) { - // Restart - if (IsKeyPressed(KEY_R)) + + // Enter nickname + if (start) { - death = 0; - collision = 0; - key = 0; - score = 0; - SetupSnake(); + char new = EnterName(&nameIsExist, &letterCount, &newPlayer, players, &index, player, &start); + + char newName = 1; + char existingName = 2; + + if (new == newName) + { + + player = json_object_new_object(); + json_object_array_add(players, player); + json_object_object_add(player, "nickname", json_object_new_string(newPlayer.name)); + } + else if (new == existingName) + { + player = json_object_array_get_idx(players, index); + } + + framesCounter++; } + + // Restart + Restart(&table); // Pause - if (IsKeyPressed(KEY_SPACE) && !death) + if (IsKeyPressed(KEY_SPACE) && !death && !start) pause = !pause; - // Pause and death - if (!pause && !death) + // Table + if (death && IsKeyPressed(KEY_T)) + { + table = 1; + objectsCount = json_object_array_length(players); + } + + // Update + if (!pause && !death && !start) { Update(); - if (score > hiScore){ - SaveStorageValue(STORAGE_POSITION_HISCORE, score); + + if (score > hiScore) + { hiScore = score; + + for (int i = 0; i < MAX_INPUT_CHARS + 1; i++) + { + SaveStorageValue(i, newPlayer.name[i]); + champName[i] = newPlayer.name[i]; + } + + SaveStorageValue(STORAGE_POSITION_HISCORE, hiScore); + } + + if (prevScore <= score) + { + if (nameIsExist) + { + prevScore = score; + json_object *playerHiScore = json_object_object_get(player, "Hi-score"); + if (score > json_object_get_int(playerHiScore)) + json_object_set_int(playerHiScore, prevScore); + } + else + { + prevScore = score; + json_object_object_add(player, "Hi-score", json_object_new_int(prevScore)); + } } } else @@ -226,17 +338,63 @@ int main(void) // ------------------------------------------------------------------------------- BeginDrawing(); BeginTextureMode(canvas); - ClearBackground(GREEN); - Draw(); + + if (!start && !table) + { + ClearBackground(GREEN); + Draw(); + } + + //====================================================================================== + + if (start) + { + DrawStart(newPlayer.name, framesCounter); + if (nameIsExist) + { + DrawText("This name is already taken\n", CANVAS_WIDTH / 2.0f - 200, 160, 30, GREEN); + DrawText("Do you want to continue to play for this player?\n", CANVAS_WIDTH / 2 - 370, 190, 30, GREEN); + DrawText("(y/n?)", CANVAS_WIDTH / 2 - 30, 220, 30, GREEN); + } + } + //====================================================================================== // On pause, we draw a blinking message if (pause && ((framesCounter / 30) % 2)) DrawText("PAUSED", CANVAS_WIDTH / 2 - 60, CANVAS_HEIGHT / 2 - 20, 30, GRAY); - if (death) + if (death && !table) { - DrawText(TextFormat("SCORE: %i\nHI-SCORE: %i", score, hiScore), 280, 50, 40, MAROON); - DrawText(" YOU ARE DEAD !!! =(\n Press R to restart the game", CANVAS_WIDTH / 2 - 220, CANVAS_HEIGHT / 2 - 20, 30, BLACK); + DrawText(TextFormat("SCORE: %s - %i\nHI-SCORE: %s - %i", newPlayer.name, score, champName, hiScore), 230, 50, 40, MAROON); + DrawText("YOU ARE DEAD !!! =(", CANVAS_WIDTH / 2 - 150, CANVAS_HEIGHT / 2 - 20, 30, BLACK); + DrawText("Press R to restart the game", CANVAS_WIDTH / 2 - 220, CANVAS_HEIGHT / 2 + 10, 30, BLACK); + DrawText("Press T to see player table", CANVAS_WIDTH / 2 - 215, CANVAS_HEIGHT / 2 + 40, 30, BLACK); + } + + if (table) + { + + ClearBackground(BLACK); + + DrawText("Players' scores", 240, 20, 40, MAROON); + + for (int i = 0; i < objectsCount; i++) + { + int boxSize = 40; + int font = 30; + int shift = (boxSize + 5) * i; + Rectangle textBox_1 = {20, 100 + shift, CANVAS_WIDTH / 2 - 30, boxSize}; + Rectangle textBox_2 = {CANVAS_WIDTH / 2 + 10, 100 + shift, CANVAS_WIDTH / 2 - 30, boxSize}; + DrawRectangleRec(textBox_1, LIGHTGRAY); + DrawRectangleRec(textBox_2, LIGHTGRAY); + + json_object *currentPlayer = json_object_array_get_idx(players, i); + json_object *currentNickname = json_object_object_get(currentPlayer, "nickname"); + json_object *currentHiscore = json_object_object_get(currentPlayer, "Hi-score"); + + DrawText(json_object_get_string(currentNickname), (int)textBox_1.x + 5, (int)textBox_1.y + 8, font, MAROON); + DrawText(json_object_get_string(currentHiscore), (int)textBox_2.x + 5, (int)textBox_2.y + 8, font, MAROON); + } } EndTextureMode(); @@ -252,6 +410,9 @@ int main(void) // Close audio device CloseAudioDevice(); + json_object_to_file(rating, root); + json_object_put(root); + CloseWindow(); return 0; @@ -336,6 +497,20 @@ void DrawFeild(void) } } +void DrawStart(char newPlayername[], int framesCounter) +{ + ClearBackground(BLACK); + + DrawText("Enter your name", 240, 50, 40, MAROON); + Rectangle textBox = {CANVAS_WIDTH / 2.0f - 100, 100, 240, 50}; + DrawRectangleRec(textBox, LIGHTGRAY); + + DrawText(newPlayername, (int)textBox.x + 5, (int)textBox.y + 8, 40, MAROON); + + if (((framesCounter / 20) % 2) == 0) + DrawText("_", (int)textBox.x + 8 + MeasureText(newPlayername, 40), (int)textBox.y + 12, 40, MAROON); +} + void DrawSnakeBody(int x, int y) { int prevDirX = gameFeild[x][y].prevDirectionX; @@ -575,4 +750,84 @@ void CheckCollision(void) death = 1; PlaySound(wallCollision); } -} \ No newline at end of file +} + +void Restart(char *table) +{ + if (IsKeyPressed(KEY_R)) + { + death = 0; + *table = 0; + collision = 0; + key = 0; + score = 0; + scoreSave = 1; + SetupSnake(); + } +} + +char EnterName(char *nameIsExist, int *letterCount, Player *newPlayer, json_object *players, int *index, json_object *player, char *start) +{ + + int keyStart = GetCharPressed(); + + while (keyStart > 0 && !(*nameIsExist) && keyStart != KEY_BACKSPACE) + { + if ((keyStart >= 32) && (keyStart <= 125) && (*letterCount < MAX_INPUT_CHARS) && (keyStart != KEY_SPACE)) + { + newPlayer->name[*letterCount] = (char)keyStart; + newPlayer->name[*letterCount + 1] = '\0'; + (*letterCount) = (*letterCount) + 1; + } + keyStart = GetCharPressed(); + } + + if (IsKeyPressed(KEY_ENTER) && (*letterCount) > 0) + { + char check = 0; + + for (size_t i = 0, playersIndex = json_object_array_length(players); i < playersIndex; i++) + { + json_object *currentPlayer = json_object_array_get_idx(players, i); + json_object *currentNickname = json_object_object_get(currentPlayer, "nickname"); + + char *curNick = strdup(json_object_get_string(currentNickname)); + + if (!strcmp(newPlayer->name, curNick)) + { + check = 1; + *nameIsExist = 1; + free(curNick); + *index = i; + break; + } + + free(curNick); + } + + if (!check) + { + *start = 0; + return 1; + } + } + + if (*nameIsExist && IsKeyPressed(KEY_Y)) + { + *start = 0; + return 2; + } + else if (*nameIsExist && IsKeyPressed(KEY_N)) + { + *nameIsExist = 0; + } + + if (IsKeyPressed(KEY_BACKSPACE)) + { + (*letterCount) = (*letterCount) - 1; + if (*letterCount < 0) + *letterCount = 0; + newPlayer->name[*letterCount] = '\0'; + } + return 0; +} diff --git a/rating.json b/rating.json new file mode 100644 index 0000000..67c5980 --- /dev/null +++ b/rating.json @@ -0,0 +1 @@ +{"players":[{"nickname":"Eleott","Hi-score":270},{"nickname":"John_Snow","Hi-score":810},{"nickname":"HaHiHo","Hi-score":180}]} \ No newline at end of file From 2d45313c25b373085a62d9347fa53dcc5c1e9a89 Mon Sep 17 00:00:00 2001 From: Eleott-hi Date: Tue, 14 Dec 2021 18:36:53 +0700 Subject: [PATCH 07/11] json --- rating.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rating.json b/rating.json index 67c5980..3473b28 100644 --- a/rating.json +++ b/rating.json @@ -1 +1 @@ -{"players":[{"nickname":"Eleott","Hi-score":270},{"nickname":"John_Snow","Hi-score":810},{"nickname":"HaHiHo","Hi-score":180}]} \ No newline at end of file +{"players":[{"nickname":"Eleott","Hi-score":270},{"nickname":"John_Snow","Hi-score":810},{"nickname":"HaHiHo","Hi-score":180},{"nickname":"Vasya","Hi-score":30},{"nickname":"fdshkfh","Hi-score":40}]} \ No newline at end of file From 90314d9129b61b9b91f03f5a92e46e6c2e29eb2a Mon Sep 17 00:00:00 2001 From: Eleott-hi Date: Tue, 14 Dec 2021 20:13:35 +0700 Subject: [PATCH 08/11] Save storage in the end of program --- main.c | 59 ++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/main.c b/main.c index f390302..bf7b182 100644 --- a/main.c +++ b/main.c @@ -212,6 +212,7 @@ int main(void) char nameIsExist = 0; char table = 0; char start = 1; + char newChamp = 0; int letterCount = 0; int framesCounter = 0; @@ -224,17 +225,15 @@ int main(void) return 1; json_object *players = json_object_object_get(root, "players"); json_object *player; - + int index = 0; int objectsCount = 0; - + Player newPlayer = { .name = "\0", }; - - //====================================================================================== - + //====================================================================================== char champName[MAX_INPUT_CHARS + 1] = "\0"; @@ -301,29 +300,32 @@ int main(void) if (score > hiScore) { hiScore = score; - - for (int i = 0; i < MAX_INPUT_CHARS + 1; i++) + newChamp = 1; + char checkNewChamp = 1; + if (checkNewChamp) { - SaveStorageValue(i, newPlayer.name[i]); - champName[i] = newPlayer.name[i]; - } - - SaveStorageValue(STORAGE_POSITION_HISCORE, hiScore); - } + for (int i = 0; i < MAX_INPUT_CHARS + 1; i++) + { + champName[i] = newPlayer.name[i]; + } - if (prevScore <= score) - { - if (nameIsExist) - { - prevScore = score; - json_object *playerHiScore = json_object_object_get(player, "Hi-score"); - if (score > json_object_get_int(playerHiScore)) - json_object_set_int(playerHiScore, prevScore); + checkNewChamp = 0; } - else + + if (prevScore <= score) { - prevScore = score; - json_object_object_add(player, "Hi-score", json_object_new_int(prevScore)); + if (nameIsExist) + { + prevScore = score; + json_object *playerHiScore = json_object_object_get(player, "Hi-score"); + if (score > json_object_get_int(playerHiScore)) + json_object_set_int(playerHiScore, prevScore); + } + else + { + prevScore = score; + json_object_object_add(player, "Hi-score", json_object_new_int(prevScore)); + } } } } @@ -407,6 +409,15 @@ int main(void) // Close audio device CloseAudioDevice(); + if (newChamp) + { + for (int i = 0; i < MAX_INPUT_CHARS + 1; i++) + { + SaveStorageValue(i, newPlayer.name[i]); + } + SaveStorageValue(STORAGE_POSITION_HISCORE, hiScore); + } + json_object_to_file(rating, root); json_object_put(root); From 565e15bdf8a0b7e9858a5ecf832cbc042a48052c Mon Sep 17 00:00:00 2001 From: Eleott-hi Date: Wed, 15 Dec 2021 12:03:03 +0700 Subject: [PATCH 09/11] Table switcher, fix score saving in json --- main.c | 82 ++++++++++++++++++++++++++++++++--------------------- rating.json | 2 +- 2 files changed, 51 insertions(+), 33 deletions(-) diff --git a/main.c b/main.c index bf7b182..cf6f9b9 100644 --- a/main.c +++ b/main.c @@ -11,8 +11,8 @@ #define SPRITE_EDGE_SIZE 64 #define CANVAS_WIDTH 800 #define CANVAS_HEIGHT 400 -#define SNAKE_SIZE 40 -#define SNAKE_SPEED 0.2f +#define SNAKE_SIZE 25 +#define SNAKE_SPEED 0.1f #define FEILD_WIDTH CANVAS_WIDTH / SNAKE_SIZE #define FEILD_HEIGHT CANVAS_HEIGHT / SNAKE_SIZE @@ -214,8 +214,9 @@ int main(void) char start = 1; char newChamp = 0; - int letterCount = 0; + int letterCounter = 0; int framesCounter = 0; + int tableCounter = 6; int hiScore = LoadStorageValue(STORAGE_POSITION_HISCORE); // Initiolize json structure @@ -227,7 +228,7 @@ int main(void) json_object *player; int index = 0; - int objectsCount = 0; + int objectsCounter = 0; Player newPlayer = { .name = "\0", @@ -259,7 +260,7 @@ int main(void) // Enter nickname if (start) { - char new = EnterName(&nameIsExist, &letterCount, &newPlayer, players, &index, player, &start); + char new = EnterName(&nameIsExist, &letterCounter, &newPlayer, players, &index, player, &start); char newName = 1; char existingName = 2; @@ -286,10 +287,27 @@ int main(void) pause = !pause; // Table + if (death && IsKeyPressed(KEY_T)) { table = 1; - objectsCount = json_object_array_length(players); + objectsCounter = json_object_array_length(players); + } + if (table) + { + if (IsKeyPressed(KEY_RIGHT)) + { + if (tableCounter < objectsCounter) + tableCounter += 6; + } + else if (IsKeyPressed(KEY_LEFT)) + { + tableCounter -= 6; + if (tableCounter < 6) + { + tableCounter = 6; + } + } } // Update @@ -311,21 +329,20 @@ int main(void) checkNewChamp = 0; } - - if (prevScore <= score) + } + if (prevScore <= score) + { + if (nameIsExist) { - if (nameIsExist) - { - prevScore = score; - json_object *playerHiScore = json_object_object_get(player, "Hi-score"); - if (score > json_object_get_int(playerHiScore)) - json_object_set_int(playerHiScore, prevScore); - } - else - { - prevScore = score; - json_object_object_add(player, "Hi-score", json_object_new_int(prevScore)); - } + prevScore = score; + json_object *playerHiScore = json_object_object_get(player, "Hi-score"); + if (score > json_object_get_int(playerHiScore)) + json_object_set_int(playerHiScore, prevScore); + } + else + { + prevScore = score; + json_object_object_add(player, "Hi-score", json_object_new_int(prevScore)); } } } @@ -376,12 +393,13 @@ int main(void) ClearBackground(BLACK); DrawText("Players' scores", 240, 20, 40, MAROON); + DrawText("Use LEFT and RIGHT arrows to switch the table", 150, 65, 20, MAROON); - for (int i = 0; i < objectsCount; i++) + for (int i = tableCounter - 6, line = 0; i < tableCounter && i < objectsCounter; i++, line++) { int boxSize = 40; int font = 30; - int shift = (boxSize + 5) * i; + int shift = (boxSize + 5) * line; Rectangle textBox_1 = {20, 100 + shift, CANVAS_WIDTH / 2 - 30, boxSize}; Rectangle textBox_2 = {CANVAS_WIDTH / 2 + 10, 100 + shift, CANVAS_WIDTH / 2 - 30, boxSize}; DrawRectangleRec(textBox_1, LIGHTGRAY); @@ -775,23 +793,23 @@ void Restart(char *table) } } -char EnterName(char *nameIsExist, int *letterCount, Player *newPlayer, json_object *players, int *index, json_object *player, char *start) +char EnterName(char *nameIsExist, int *letterCounter, Player *newPlayer, json_object *players, int *index, json_object *player, char *start) { int keyStart = GetCharPressed(); while (keyStart > 0 && !(*nameIsExist) && keyStart != KEY_BACKSPACE) { - if ((keyStart >= 32) && (keyStart <= 125) && (*letterCount < MAX_INPUT_CHARS) && (keyStart != KEY_SPACE)) + if ((keyStart >= 32) && (keyStart <= 125) && (*letterCounter < MAX_INPUT_CHARS) && (keyStart != KEY_SPACE)) { - newPlayer->name[*letterCount] = (char)keyStart; - newPlayer->name[*letterCount + 1] = '\0'; - (*letterCount) = (*letterCount) + 1; + newPlayer->name[*letterCounter] = (char)keyStart; + newPlayer->name[*letterCounter + 1] = '\0'; + (*letterCounter) = (*letterCounter) + 1; } keyStart = GetCharPressed(); } - if (IsKeyPressed(KEY_ENTER) && (*letterCount) > 0) + if (IsKeyPressed(KEY_ENTER) && (*letterCounter) > 0) { char check = 0; @@ -833,10 +851,10 @@ char EnterName(char *nameIsExist, int *letterCount, Player *newPlayer, json_obje if (IsKeyPressed(KEY_BACKSPACE)) { - (*letterCount) = (*letterCount) - 1; - if (*letterCount < 0) - *letterCount = 0; - newPlayer->name[*letterCount] = '\0'; + (*letterCounter) = (*letterCounter) - 1; + if (*letterCounter < 0) + *letterCounter = 0; + newPlayer->name[*letterCounter] = '\0'; } return 0; } diff --git a/rating.json b/rating.json index 3473b28..ce74301 100644 --- a/rating.json +++ b/rating.json @@ -1 +1 @@ -{"players":[{"nickname":"Eleott","Hi-score":270},{"nickname":"John_Snow","Hi-score":810},{"nickname":"HaHiHo","Hi-score":180},{"nickname":"Vasya","Hi-score":30},{"nickname":"fdshkfh","Hi-score":40}]} \ No newline at end of file +{"players":[{"nickname":"Eleott","Hi-score":270},{"nickname":"John_Snow","Hi-score":810},{"nickname":"HaHiHo","Hi-score":180},{"nickname":"Vasya","Hi-score":30},{"nickname":"Nero","Hi-score":250},{"nickname":"Hero","Hi-score":140},{"nickname":"Lucy","Hi-score":140},{"nickname":"Jaja","Hi-score":50},{"nickname":"Monkey","Hi-score":170},{"nickname":"Jerar","Hi-score":110},{"nickname":"Kile","Hi-score":280},{"nickname":"katy","Hi-score":110},{"nickname":"Kitty","Hi-score":50},{"nickname":"Bart","Hi-score":90},{"nickname":"Mark_Twen","Hi-score":90},{"nickname":"Barbara","Hi-score":180},{"nickname":"Rob_Shnayd","Hi-score":100},{"nickname":"Common","Hi-score":70}]} \ No newline at end of file From a43a6dff387f4afaa4a564689e9ef700426bc73c Mon Sep 17 00:00:00 2001 From: Eleott-hi Date: Wed, 15 Dec 2021 12:28:13 +0700 Subject: [PATCH 10/11] Unload SnakePartsTexture --- main.c | 5 +++++ rating.json | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index cf6f9b9..ac3cd53 100644 --- a/main.c +++ b/main.c @@ -436,6 +436,11 @@ int main(void) SaveStorageValue(STORAGE_POSITION_HISCORE, hiScore); } + for (int i = 0; i < TALE_LEFT + 1; i++) + { + UnloadTexture (textureSnakeParts[i]); + } + json_object_to_file(rating, root); json_object_put(root); diff --git a/rating.json b/rating.json index ce74301..22cce1c 100644 --- a/rating.json +++ b/rating.json @@ -1 +1 @@ -{"players":[{"nickname":"Eleott","Hi-score":270},{"nickname":"John_Snow","Hi-score":810},{"nickname":"HaHiHo","Hi-score":180},{"nickname":"Vasya","Hi-score":30},{"nickname":"Nero","Hi-score":250},{"nickname":"Hero","Hi-score":140},{"nickname":"Lucy","Hi-score":140},{"nickname":"Jaja","Hi-score":50},{"nickname":"Monkey","Hi-score":170},{"nickname":"Jerar","Hi-score":110},{"nickname":"Kile","Hi-score":280},{"nickname":"katy","Hi-score":110},{"nickname":"Kitty","Hi-score":50},{"nickname":"Bart","Hi-score":90},{"nickname":"Mark_Twen","Hi-score":90},{"nickname":"Barbara","Hi-score":180},{"nickname":"Rob_Shnayd","Hi-score":100},{"nickname":"Common","Hi-score":70}]} \ No newline at end of file +{"players":[{"nickname":"Eleott","Hi-score":270},{"nickname":"John_Snow","Hi-score":810},{"nickname":"HaHiHo","Hi-score":180},{"nickname":"Vasya","Hi-score":30},{"nickname":"Nero","Hi-score":250},{"nickname":"Hero","Hi-score":140},{"nickname":"Lucy","Hi-score":140},{"nickname":"Jaja","Hi-score":50},{"nickname":"Monkey","Hi-score":170},{"nickname":"Jerar","Hi-score":110},{"nickname":"Kile","Hi-score":280},{"nickname":"katy","Hi-score":110},{"nickname":"Kitty","Hi-score":50},{"nickname":"Bart","Hi-score":90},{"nickname":"Mark_Twen","Hi-score":90},{"nickname":"Barbara","Hi-score":180},{"nickname":"Rob_Shnayd","Hi-score":100},{"nickname":"Common","Hi-score":70},{"nickname":"Harry","Hi-score":390}]} \ No newline at end of file From c03b99801e3c97a77e540c7f0a5c519a5c402b74 Mon Sep 17 00:00:00 2001 From: Eleott-hi Date: Thu, 16 Dec 2021 22:09:34 +0700 Subject: [PATCH 11/11] Sort algoritm in table --- main.c | 131 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 117 insertions(+), 14 deletions(-) diff --git a/main.c b/main.c index ac3cd53..5e95b4b 100644 --- a/main.c +++ b/main.c @@ -152,12 +152,16 @@ void CheckCollision(void); void UploadSnakeParts(void); -void Restart(char *); +void Restart(char *, int *); char EnterName(char *, int *, Player *, json_object *, int *, json_object *, char *); void DrawStart(char[], int); +void Merge(int *, int *, int *, int, int, char * [MAX_INPUT_CHARS + 1], char * [MAX_INPUT_CHARS + 1], char * [MAX_INPUT_CHARS + 1]); + +void SortMerge(int *, int, char * [MAX_INPUT_CHARS + 1]); + //==================================================================================== void Draw(void) @@ -213,6 +217,8 @@ int main(void) char table = 0; char start = 1; char newChamp = 0; + char *tableMemoryBlockName[MAX_INPUT_CHARS + 1] = {0}; + int *tableMemoryBlockScore = NULL; int letterCounter = 0; int framesCounter = 0; @@ -228,7 +234,7 @@ int main(void) json_object *player; int index = 0; - int objectsCounter = 0; + int jsonArrayLength = 0; Player newPlayer = { .name = "\0", @@ -281,7 +287,7 @@ int main(void) } // Restart - Restart(&table); + Restart(&table, tableMemoryBlockScore); // Pause if (IsKeyPressed(KEY_SPACE) && !death && !start) pause = !pause; @@ -291,13 +297,31 @@ int main(void) if (death && IsKeyPressed(KEY_T)) { table = 1; - objectsCounter = json_object_array_length(players); + + if (tableMemoryBlockScore == NULL) + { + jsonArrayLength = json_object_array_length(players); + + tableMemoryBlockScore = malloc(jsonArrayLength * sizeof(int)); + + for (int i = 0; i < jsonArrayLength; i++) + { + json_object *currentPlayer = json_object_array_get_idx(players, i); + json_object *currentHiscore = json_object_object_get(currentPlayer, "Hi-score"); + json_object *currentNickname = json_object_object_get(currentPlayer, "nickname"); + + tableMemoryBlockScore[i] = json_object_get_int(currentHiscore); + tableMemoryBlockName[i] = strdup(json_object_get_string(currentNickname)); + } + + SortMerge(tableMemoryBlockScore, jsonArrayLength, tableMemoryBlockName); + } } if (table) { if (IsKeyPressed(KEY_RIGHT)) { - if (tableCounter < objectsCounter) + if (tableCounter < jsonArrayLength) tableCounter += 6; } else if (IsKeyPressed(KEY_LEFT)) @@ -395,22 +419,19 @@ int main(void) DrawText("Players' scores", 240, 20, 40, MAROON); DrawText("Use LEFT and RIGHT arrows to switch the table", 150, 65, 20, MAROON); - for (int i = tableCounter - 6, line = 0; i < tableCounter && i < objectsCounter; i++, line++) + for (int i = tableCounter - 6, line = 0; i < tableCounter && i < jsonArrayLength; i++, line++) { int boxSize = 40; int font = 30; int shift = (boxSize + 5) * line; + Rectangle textBox_1 = {20, 100 + shift, CANVAS_WIDTH / 2 - 30, boxSize}; Rectangle textBox_2 = {CANVAS_WIDTH / 2 + 10, 100 + shift, CANVAS_WIDTH / 2 - 30, boxSize}; DrawRectangleRec(textBox_1, LIGHTGRAY); DrawRectangleRec(textBox_2, LIGHTGRAY); - json_object *currentPlayer = json_object_array_get_idx(players, i); - json_object *currentNickname = json_object_object_get(currentPlayer, "nickname"); - json_object *currentHiscore = json_object_object_get(currentPlayer, "Hi-score"); - - DrawText(json_object_get_string(currentNickname), (int)textBox_1.x + 5, (int)textBox_1.y + 8, font, MAROON); - DrawText(json_object_get_string(currentHiscore), (int)textBox_2.x + 5, (int)textBox_2.y + 8, font, MAROON); + DrawText(tableMemoryBlockName[i], (int)textBox_1.x + 5, (int)textBox_1.y + 8, font, MAROON); + DrawText(TextFormat("%i", tableMemoryBlockScore[i]), (int)textBox_2.x + 5, (int)textBox_2.y + 8, font, MAROON); } } @@ -438,7 +459,7 @@ int main(void) for (int i = 0; i < TALE_LEFT + 1; i++) { - UnloadTexture (textureSnakeParts[i]); + UnloadTexture(textureSnakeParts[i]); } json_object_to_file(rating, root); @@ -784,10 +805,11 @@ void CheckCollision(void) } } -void Restart(char *table) +void Restart(char *table, int *tableMemoryBlockScore) { if (IsKeyPressed(KEY_R)) { + free(tableMemoryBlockScore); death = 0; *table = 0; collision = 0; @@ -863,3 +885,84 @@ char EnterName(char *nameIsExist, int *letterCounter, Player *newPlayer, json_ob } return 0; } + +void Merge(int *targetArr, int *arr1, int *arr2, int sizeArr1, int sizeArr2, char *targetName[MAX_INPUT_CHARS + 1], char *arrName1[MAX_INPUT_CHARS + 1], char *arrName2[MAX_INPUT_CHARS + 1]) +{ + int arr1Index = 0; + int arr2Index = 0; + int targetArrIndex = 0; + + while (arr1Index < sizeArr1 && arr2Index < sizeArr2) + { + + if (arr1[arr1Index] >= arr2[arr2Index]) + { + targetArr[targetArrIndex] = arr1[arr1Index]; + targetName[targetArrIndex] = strdup(arrName1[arr1Index]); + arr1Index++; + } + else + { + targetArr[targetArrIndex] = arr2[arr2Index]; + + targetName[targetArrIndex] = strdup(arrName2[arr2Index]); + arr2Index++; + } + + targetArrIndex++; + } + + while (arr1Index < sizeArr1) + { + + targetArr[targetArrIndex] = arr1[arr1Index]; + targetName[targetArrIndex] = strdup(arrName1[arr1Index]); + arr1Index++; + targetArrIndex++; + } + while (arr2Index < sizeArr2) + { + + targetArr[targetArrIndex] = arr2[arr2Index]; + targetName[targetArrIndex] = strdup(arrName2[arr2Index]); + arr2Index++; + targetArrIndex++; + } +} + +void SortMerge(int *arr, int sizeArr, char *arrName[MAX_INPUT_CHARS + 1]) +{ + if (sizeArr < 2) + return; + + int sizeLeftArray = sizeArr / 2; + int sizeRightArray = sizeArr - sizeLeftArray; + + int *leftArray = malloc(sizeof(arr[0]) * sizeLeftArray); + int *rightArray = malloc(sizeof(arr[0]) * sizeRightArray); + + char *leftArrayName[MAX_INPUT_CHARS + 1] = {0}; + char *rightArrayName[MAX_INPUT_CHARS + 1] = {0}; + + for (int i = 0; i < sizeArr; i++) + { + if (i < sizeLeftArray) + { + leftArray[i] = arr[i]; + leftArrayName[i] = strdup(arrName[i]); + } + else + { + rightArray[i - sizeLeftArray] = arr[i]; + rightArrayName[i - sizeLeftArray] = strdup(arrName[i]); + } + } + + SortMerge(leftArray, sizeLeftArray, leftArrayName); + SortMerge(rightArray, sizeRightArray, rightArrayName); + + Merge(arr, leftArray, rightArray, sizeLeftArray, sizeRightArray, arrName, leftArrayName, rightArrayName); + + free(leftArray); + free(rightArray); +} \ No newline at end of file