Axe Game

  • The C++ Standard Libray vs External Libraries
  • Multiple Function Inputs

Library that are added externally you use double quotes “ instead of <> fpr native libraries.

Header files have a .h extension. They contain information about the library.

raylib is not a standard library so we have to add it to our includes.

we are going to use InitWindow to create a window(height, width, name).

When you run it appears very quickly and disappears.

  • Start debugging-use debugging tools

You can establish a breakpoint by placing one on the left side of the code on the desired line. The code will pause on the line where the breakpoint is. When you run the program it will show the value, but if the value has not been initialized you will have to step into or F11 to move to the next line to trigger the code. Now we see our root beer has been assigned.

When we step again we see our double value and step again we can see our bool value. We have not stepped past width yet so it is unilitialized.

float root_beer = 1.99; Initialized

double cheese_burger{5.99}; Initialized

bool shouldHaveLunch{}; Initialized = 0

Now we are going to change our code to be initialized in this way. Now if we use a bool and give it a value of 0 it will be false;

Comparison Operators

  • Return a bool
  • Create some bool variable-store the results of the comparisons.

== equal to compares the value of the left to the value of the right

Loops

Repeat sections of code

Loop condition is a bool that has the loop happens while the condition is true. If the loop turns false the loop stops running.

While Loop checks to see if the code should repeat

Our loop starts at 100, but it subtracts by 1 every time it goes through it till it reaches 0. Once it reaches 0 it exits out.

Now if you create a loop that will always be true you will create an infinite loop. If we launch the debug our window will launch, but to close it we will have to stop debugging.

This is only a temporary solution.

Clear the background

  • Clear the window background — raylib colors
  • Double Buffering — must clear the background to avoid flickering

We will want to add the Being/EndDrawing () functions. They tell us to begin working within the canvas area.

ClearBackground(WHITE); clears the background and sets it a set color. If this is not done flickering will occur.

Function Return Values

  • Use raylib’s WindowShouldClose() Function
  • Close our Window Properly

Our window only closes when we hit the X icon or the ESC key.

Drawing a Circle

  • Learn about coordinates
  • Use variables for the circles coordinates

DrawCircle(centerX, centerY, radius, color)

We place drawcircle after clear background, because we need our loop to still run to draw our circle.

Moving the circle

  • Check a boolean expression
  • Run code under certain conditions
  • Detect key presses
  • use key presses to move the circle

(IsKeyDown(KEY_A)) check if the key is down and this example the A key.

We are going to add 10 to our circle’s X positions

Now when we run our program our circle zooms off screen. This is because 25 is being added every FPS. We are now going to set the target FPS. Let’s set it 60 FPS and run the program. When we do this the circle moves in a controlled state.

Now we want to move to the circle to the left using another key.

Logical AND

We add the && condition to make sure the center can only go to those set limits.

Drawing the Axe

  • Change the squares vertical position
  • Use the += and -= operators

DrawRectangle(posX, posY, width, height, color);

We also are going to move our rectangle at a rate of 10 in the Y direction. using a += instead of having to go through and assign posy = posy +10; We are going to clean up where we can after that.

Logical OR

We are going to check if the Y pos if <0 go in a negative direction and if positive go till > 450 then reverse.

Collision Detection

  • Created variables for the collision boxes

The Else Statement

Executes code if the if statement fails.

It will always be with an if before it and the if is false.

We are now going to see if our collision happens a message will appear. This is called the DrawText(text, posX, posY, fontSize, color);

else run all of our game code.

Finishing the game

  • Finishing the game

We created a bool of all of our edges that could collide with the axe. We then made sure they were checked every frame and set our collision was true.

NOTE: The latency of running a remote desktop creates an issue where the circle does not collide initially.

There we go we now have a colliding circle and rectangle!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store