Building a math-based game: my journey so far
Tags: gamedev, beginners, math, programming
Published: 2022-06-07 05:09:05

I got introduced to an exciting startup "Terminus School". It is being built by Harshad Saykhedkar (https://www.linkedin.com/in/harshadss, one of my father's friends), and I met him in February of this year. Before I tell you about my journey, I'll give you a brief idea of what Terminus School is trying to do.

Terminus School (https://beta.terminus.school) is an ed-tech startup. At Terminus, we create courses catering to physics, chemistry and math topics typically taught in an Indian high school. The current courses that are out are trigonometry, coordinate geometry and logarithms. Terminus focuses on game-based learning and practical learning. All courses have a lot of games, which will help build an intuitive and, thus, a better understanding of the concept. All quizzes and exercises also use empirical data or have a connection to something that happens in real life.
The courses focus more on First Principle learning. I personally find the process of making these courses and the courses themselves very enriching! 

My father connected me to Harshad. I initially began with game ideation and testing. I found this startup idea to be promising. I decided to continue working, ideating and testing more games. Eventually, I started developing a game to explain quadratics.

My game's goal is to show a quadratic graph's structure. The game would go like this:

There would be tanks that generate and fire bullets at the player's base. These tanks would follow the graphs of different graphs. As time progresses, these tanks get faster. The player has to fire bullets at the tanks and destroy them before the tanks destroy the player's base.

I started working with the Defold game engine. I completed the tutorials the Defold team had provided and got accustomed to the functions.

I learned how to make the player shoot bullets and make different game elements disappear when a bullet hits it. I was quick to implement this feature. I started with the tanks following a linear motion. 

Then came one of the most complex parts: simulating a quadratic movement. I went to Desmos and plotted the equation: x^2 + 4x. I plotted 9 points and decided to make the tanks follow a linear path from point to point.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/maurfmkzcj8bw6iuh0l0.png)

To simulate diagonal movement, I made the tanks go a particular distance right (positive value on the x-axis) and a particular distance down/up (positive/negative values on the y-axis) in the same frame. Defold renders graphics at 60 frames per second.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vcbo44fmvu6s10xgztah.png)

Great! Now there was a way to move the tank from point to point. To track where the tank is, I set up two variables called logic_x_bool and logic_y_bool. These would increase in value as the tank progressed. They would start off as 0, and after going to a point, they would get incremented by 1. To actually set the x and y movements, I set up two more variables: x_logic and y_logic. All of this would be controlled by a series of if-else conditions.

The quadratic curve was complete! The tank could be shot down as well!

Now, there was a problem. The movement code had a lot of hard-coded values. Harshad sat with me and explained to me the proper coding guidelines, how I can use loops, functions, recursion etc., to optimize this and remove these magic numbers. I am also adding more documentation to the code to be more readable and understandable. These are some precious lessons in my coding journey.

The code runs like this now!

https://youtu.be/YDDBCfhIzQg

https://youtu.be/V0PmUOJ4_v0

I also learned to use Github for version control and how game engines work! More about that in a future blog :)

I am thankful to Harshad and Terminus school for this opportunity.

This is my progress so far! If you want to see my coding journey, follow this project at: https://github.com/sifar21353/DesktopGame!

Socials: https://twitter.com/SifarJirgale
https://www.reddit.com/user/JuliusCheesy
https://www.linkedin.com/in/sifarjirgale/

Email (for inquiries): sifar@jirgale.com

Thanks for reading!