What a well-articulated set of lessons! I ran with the challenges a bit second-guessing what we’d do next and often overdoing it.
So here I am in the comments to the final lesson looking at the projects others posted and it inspires me to “complete” this project. I am first left to my observations.
- That’s not an ax, it’s a rectangle.
- Is this even a game yet or just a toy?
- There are a lot of different colors…
- anto made a great front-end and game loop
- zfbx used lots of math and syntax I hadn’t seen yet.
These then led to a slew of questions:
- How come pasting online code into a new project doesn’t build right?
- One time I ended up with .json errors
- One time it got hung up because the version of double quotes was wrong
- Often it seems to complain about other files in the project being wrong
- How can I use new features when I don’t know they’re there?
- I tried RED, BLUE, BLACK, GREY, and VIOLET
- CYAN and TURQUOISE were a no-go.
Finally, what can I do to make my project awesome and adopt the things I like?
- Add a front end and game loop for easy replay.
- Add more movement and a scoring mechanic.
- Make them both balls so I can simplify collision and call it dodgeball.
Right now, this is where I’m at…
#include "raylib.h"
int main()
{
// window dimensions
int WindowWidth = 800;
int WindowHeight = 450;
InitWindow(WindowWidth, WindowHeight, "Dodge Ball");
// circle coordinates
int CircleCenterX = 200;
int CircleCenterY = 200;
int CircleRadius = 25;
// circle edges
int LeftCircleEdge = CircleCenterX - CircleRadius;
int RightCircleEdge = CircleCenterX + CircleRadius;
int UpperCircleEdge = CircleCenterY - CircleRadius;
int BottomCircleEdge = CircleCenterY + CircleRadius;
// axe coordinates
int AxeX = 400;
int AxeY = 0;
int AxeLength = 50;
// axe edges
int LAxeX = AxeX;
int RAxeX = AxeX+AxeLength;
int UAxeY = AxeY;
int BAxeY = AxeY+AxeLength;
float DirectionV = 5;
float DirectionH = 5;
bool CollisionWithAxe =
(BAxeY >= UpperCircleEdge) &&
(UAxeY <= BottomCircleEdge) &&
(LAxeX <= RightCircleEdge) &&
(RAxeX >= LeftCircleEdge);
SetTargetFPS(60);
while(!WindowShouldClose())
{
BeginDrawing();
ClearBackground(BLACK);
if(CollisionWithAxe)
{
DrawText("Game Over!", 400, 200, 20, RED);
DrawText("Score:", WindowWidth/2, WindowHeight-200, 20, VIOLET);
DrawText("000", 2*WindowWidth/3, WindowHeight-200, 20, VIOLET);
}
else
{
//draw score
DrawText("Score:", WindowWidth-150, WindowHeight-50, 20, VIOLET);
DrawText("000", WindowWidth-50, WindowHeight-50, 20, VIOLET);
// update the edges
LeftCircleEdge = CircleCenterX - CircleRadius;
RightCircleEdge = CircleCenterX + CircleRadius;
UpperCircleEdge = CircleCenterY - CircleRadius;
BottomCircleEdge = CircleCenterY + CircleRadius;
LAxeX = AxeX;
RAxeX = AxeX + AxeLength;
UAxeY = AxeY;
BAxeY = AxeY + AxeLength;
// update collision with axe
CollisionWithAxe =
(BAxeY >= UpperCircleEdge) &&
(UAxeY <= BottomCircleEdge) &&
(LAxeX <= RightCircleEdge) &&
(RAxeX >= LeftCircleEdge);
DrawCircle(CircleCenterX, CircleCenterY, CircleRadius, GRAY);
DrawRectangle(AxeX, AxeY, AxeLength, AxeLength, RED);
// Move the axe
AxeY += DirectionV;
if (AxeY > (WindowHeight-AxeLength) || AxeY < 0)
{
DirectionV = -DirectionV;
}
AxeX += DirectionH;
if (AxeX > (WindowWidth-AxeLength) || AxeX < 0)
{
DirectionH = -DirectionH;
}
// Left Right Control input
if (IsKeyDown(KEY_D) && (CircleCenterX+CircleRadius) < WindowWidth)
{
CircleCenterX += 10;
}
if (IsKeyDown(KEY_A) && (CircleCenterX-CircleRadius) > 0)
{
CircleCenterX -= 10;
}
//Up and Down Movement
if (IsKeyDown(KEY_W) && (CircleCenterY-CircleRadius) > 0)
{
CircleCenterY = CircleCenterY - 10;
}
if (IsKeyDown(KEY_S) && (CircleCenterY+CircleRadius) < WindowHeight)
{
CircleCenterY = CircleCenterY + 10;
}
// Game logic ends
}
EndDrawing();
}
}