Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Entry #6 UP2212859

During my time off over Christmas I made a small coordinate based system that allows two players to spawn into a map and it will calculate the distance between both players.  It took a very long time for me to figure out how to get everything working, but I now feel so much more confident with my coding ability and I now no longer have to look up how to do basic things like I did before!

Under my main function I started by creating my player 1 function.  On line 12 and 13 I initialise two floats "x & y" as zero.  It doesn't really matter what they're initialised at, since I'll be allowing user input to override their primary initialisation.

Line 14 just tells the "player" what they should be doing, and then line 15 allows the player to input any  number into the console, as long as it's between 0 and 100.  Of course, if I left it at that, the player could enter any number and the console would accept it, which is why I then used a bunch of while loops to lock the player into choosing a specific range of numbers.  I set four different conditions for the variables initialisation.  The console will repeatedly ask for a number between 0 and 100 if the entered number is outside the range.

Once the player has stopped testing the limits of the system, they will then enter their valid set of coordinates and the console will print out Player 1's spawn location.  This then repeats for player 2 under the function "Player2" - it is exactly the same code, the only thing that has changed is the names of the variables.

Knowing how to restrict players movements is useful for most games.  As all games have map boundaries, forcing players to play within the developers set coordinates.

The console application below shows that the code is functional and won't allow players to spawn unless they enter valid coordinates.  


As I had two players in the world, I decided to add a calculation that works out the distance between each player using Pythagoras' theorem.  For this I had to include the "<Math.h>" C++ library to my code.

The basic math for Pythagoras' theorem states: "A^2 + B^2 = C^2".  I created a new function under line 71 and then made six float variables and started plugging in the necessary sums to make the calculation work.  I doubt this is the most efficient way of coding calculations but it worked for me!  Line 73 - 78 is where I plugged in all the sums and then worked out the square root of "A2B2" which then works out the distance between the given coordinates - with a high level of accuracy as I used a float.

I also added two if statements to prevent players spawning too closely to one another.  If both players spawn between 2 or less metres apart then they'll have to repeat the process again by re-running the application.  

However, if the players have spawned more than two metres apart, they'll get this screen instead!

This was a fun little project I did over Christmas and it really helped me become more independent with my coding.  As I can now start writing code without having to look everything up all the time.  I'm pretty comfortable with while loops and if statements.  Using the math.h library was also a fun new thing for me to try.  Using math in my code went far more smoothly than I imagined and was super engaging to learn!

I'm sure there are ways of simplifying my current code and I aim to be able to shorten my code in the future if it's possible.  This little project helped me learn vital components for coding and I had a lot of fun and frustration doing it! 

Support this post

Did you like this post? Tell us