Mercurial
diff sori/main.c @ 26:a58a663dae68
[Sori] Making a simple game.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Thu, 09 Oct 2025 06:41:49 -0700 |
| parents | |
| children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sori/main.c Thu Oct 09 06:41:49 2025 -0700 @@ -0,0 +1,141 @@ +#include <stdio.h> +#include "dowa/dowa.h" +#include "third_party/raylib/include/raylib.h" +#include "third_party/raylib/include/raymath.h" + +typedef struct { + char *texts; + Vector2 position; +} PopUpText; + +typedef struct { + Vector2 position; + Vector2 size; + Vector2 velocity; +} Entity; + +Entity *Shoot(Entity *player) +{ + Entity *box = malloc(sizeof(Entity)); + box->velocity = (Vector2){ .x=1, .y=-10 }; + box->position = player->position; + box->size = (Vector2){ .x=10, .y=2 }; + return box; +} + +void Entity_Update(Entity *player, Entity *object) +{ + int gravity = 1; + player->velocity.y += gravity; + player->position = Vector2Add(player->position, player->velocity); + + if ( + player->position.y + player->size.y > object->position.y + ) + { + player->position.y = object->position.y - player->size.y; + } +} + +int main() +{ + char mouse_position[256]; + + InitWindow(800, 450, "Sori the game"); + SetTargetFPS(120); + + Entity player = { + .size = (Vector2){ .x = 20, .y = 20 }, + .position = (Vector2){ .x = 10, .y = 10}, + .velocity = (Vector2){ .x = 0, .y = 0}, + }; + + Entity ground = { + .size = (Vector2){ .x = 800, .y = 100 }, + .position = (Vector2){ .x = 0, .y = 350}, + .velocity = (Vector2){ .x = 0, .y = 0}, + }; + Entity *bullet; + Entity **bullets = malloc(sizeof(Entity) * 100); + + PopUpText *curr_values = malloc(sizeof(PopUpText) * 100); + int size_bullets = 0; + int size_texts = 0; + int frame = 0; + + while (!WindowShouldClose()) + { + BeginDrawing(); + ClearBackground(RAYWHITE); + int x = GetMouseX(); + int y = GetMouseY(); + + sprintf( + mouse_position, + "Mouse position %d, %d", + x, y + ); + if (IsMouseButtonPressed(0)) + { + curr_values[size_texts].texts = "hello good sir"; + curr_values[size_texts].position = (Vector2){ + .x = (float)x, + .y = (float)y, + }; + size_texts++; + + Entity *bullet = Shoot(&player); + bullets[size_bullets] = bullet; + size_bullets++; + } + + if (frame > 2) + { + Entity_Update(&player, &ground); + frame = 0; + for (uint32 i = 0; i < size_texts; i++) + { + Entity_Update(bullets[i], &ground); + } + } + + int key = GetKeyPressed(); + + if (IsKeyDown(KEY_W)) + { + player.position.y -= 1; + } + + if (IsKeyDown(KEY_S)) + { + player.position.y += 1; + } + + if (IsKeyDown(KEY_A)) + { + player.position.x -= 1; + } + + if (IsKeyDown(KEY_D)) + { + player.position.x += 1; + } + + DrawRectangleV(player.position, player.size, RED); + DrawRectangleV(ground.position, ground.size, BLUE); + + for (uint32 i = 0; i < size_texts; i++) + { + DrawText(curr_values[i].texts, curr_values[i].position.x, curr_values[i].position.y, + 20, LIGHTGRAY); + DrawRectangleV(bullets[i]->position, bullets[i]->size, BLACK); + } + DrawText(mouse_position, 190, 200, 20, LIGHTGRAY); + EndDrawing(); + + frame++; + } + CloseWindow(); + return 0; +} +