Game Dev Journey #2
My Journey of Fabricating Game using C++, Raylib, and Flecs
Hello! Back again with me and my story about game development. From the previous story, I realize that I still deliver my story as a “tutorial” instead of what I promised, that my story will act as “storytelling.” So here in this part 2 and final part, I won’t give you guys a complete code on how the game works. Instead, you guys can use this story as a “little reference” for your game and add your creativity there.
[1] Moving The Ball ⚽
The easiest way to move the ball is to add the current position of the ball with some value. Here, we use “velocity” as the value we will add to the ball’s position.
struct Velocity {
public:
float x{};
float y{};
};
// Put the code before the InitWindow()
auto ball = world.entity("ball");
ball.set<Position>({WIDTH/2, HEIGHT/2});
ball.set<SizeCircle>({30});
ball.set<Velocity>({0.2,0.2});
// Put the code inside the looping WindowShouldClose()
ball.set<Position>({
position_ball->x + velocity_ball->x,
position_ball->y + velocity_ball->y,
});
[2] Ball and Wall Collision 🧱
For the collision, we can check the ball position plus radius with the screen width and height.
const auto* position_ball = ball.get<Position>();
const auto* radius_ball = ball.get<SizeCircle>();
const auto* velocity_ball = ball.get<Velocity>();
// Ball collides with the upper or lower walls
int rand_x_direction = GetRandomValue(-1, 1)
if (position_ball->y - radius_ball->radius <= 0 || position_ball->y + radius_ball->radius >= GetScreenHeight()) {
ball.set<Velocity>({velocity_ball->x * rand_x_direction, velocity_ball->y* (-1)});
ball.set<Position>({position_ball->x, position_ball->y + velocity_ball->y});
}
// Ball collides witht the right or left walls
if (position_ball->x + radius_ball->radius >= (float)GetScreenWidth() ||
position_ball->x - radius_ball->radius <= 0) {
ball.set<Velocity>({velocity_ball->x * (-1), velocity_ball->y * (-1)});
}
[3] Creativity ✨
And for the final step is to add your creativity to make this prototype into a complete game. You can use your imagination and gather references on the internet and YouTube. Feel free to comment if you need the following “tutorial” about this game. In the meantime, enjoy your creative time!
Thanks all 🚀 See you on another story 👋