comparison 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
comparison
equal deleted inserted replaced
25:342726584be2 26:a58a663dae68
1 #include <stdio.h>
2 #include "dowa/dowa.h"
3 #include "third_party/raylib/include/raylib.h"
4 #include "third_party/raylib/include/raymath.h"
5
6 typedef struct {
7 char *texts;
8 Vector2 position;
9 } PopUpText;
10
11 typedef struct {
12 Vector2 position;
13 Vector2 size;
14 Vector2 velocity;
15 } Entity;
16
17 Entity *Shoot(Entity *player)
18 {
19 Entity *box = malloc(sizeof(Entity));
20 box->velocity = (Vector2){ .x=1, .y=-10 };
21 box->position = player->position;
22 box->size = (Vector2){ .x=10, .y=2 };
23 return box;
24 }
25
26 void Entity_Update(Entity *player, Entity *object)
27 {
28 int gravity = 1;
29 player->velocity.y += gravity;
30 player->position = Vector2Add(player->position, player->velocity);
31
32 if (
33 player->position.y + player->size.y > object->position.y
34 )
35 {
36 player->position.y = object->position.y - player->size.y;
37 }
38 }
39
40 int main()
41 {
42 char mouse_position[256];
43
44 InitWindow(800, 450, "Sori the game");
45 SetTargetFPS(120);
46
47 Entity player = {
48 .size = (Vector2){ .x = 20, .y = 20 },
49 .position = (Vector2){ .x = 10, .y = 10},
50 .velocity = (Vector2){ .x = 0, .y = 0},
51 };
52
53 Entity ground = {
54 .size = (Vector2){ .x = 800, .y = 100 },
55 .position = (Vector2){ .x = 0, .y = 350},
56 .velocity = (Vector2){ .x = 0, .y = 0},
57 };
58 Entity *bullet;
59 Entity **bullets = malloc(sizeof(Entity) * 100);
60
61 PopUpText *curr_values = malloc(sizeof(PopUpText) * 100);
62 int size_bullets = 0;
63 int size_texts = 0;
64 int frame = 0;
65
66 while (!WindowShouldClose())
67 {
68 BeginDrawing();
69 ClearBackground(RAYWHITE);
70 int x = GetMouseX();
71 int y = GetMouseY();
72
73 sprintf(
74 mouse_position,
75 "Mouse position %d, %d",
76 x, y
77 );
78 if (IsMouseButtonPressed(0))
79 {
80 curr_values[size_texts].texts = "hello good sir";
81 curr_values[size_texts].position = (Vector2){
82 .x = (float)x,
83 .y = (float)y,
84 };
85 size_texts++;
86
87 Entity *bullet = Shoot(&player);
88 bullets[size_bullets] = bullet;
89 size_bullets++;
90 }
91
92 if (frame > 2)
93 {
94 Entity_Update(&player, &ground);
95 frame = 0;
96 for (uint32 i = 0; i < size_texts; i++)
97 {
98 Entity_Update(bullets[i], &ground);
99 }
100 }
101
102 int key = GetKeyPressed();
103
104 if (IsKeyDown(KEY_W))
105 {
106 player.position.y -= 1;
107 }
108
109 if (IsKeyDown(KEY_S))
110 {
111 player.position.y += 1;
112 }
113
114 if (IsKeyDown(KEY_A))
115 {
116 player.position.x -= 1;
117 }
118
119 if (IsKeyDown(KEY_D))
120 {
121 player.position.x += 1;
122 }
123
124 DrawRectangleV(player.position, player.size, RED);
125 DrawRectangleV(ground.position, ground.size, BLUE);
126
127 for (uint32 i = 0; i < size_texts; i++)
128 {
129 DrawText(curr_values[i].texts, curr_values[i].position.x, curr_values[i].position.y,
130 20, LIGHTGRAY);
131 DrawRectangleV(bullets[i]->position, bullets[i]->size, BLACK);
132 }
133 DrawText(mouse_position, 190, 200, 20, LIGHTGRAY);
134 EndDrawing();
135
136 frame++;
137 }
138 CloseWindow();
139 return 0;
140 }
141