Begin your C# Game Development Journey Using This Challenge

Hi Fellows,

Here we will discuss a simple but important programming challenge that will help you enhance your fundamental knowledge of C# .NET Console Applications, Data Types, Declaring Variables, Constants, & mathematical operations that are critical for game development.

Declaring and using variables

Calculating with integers

Prompting for inputs and Getting inputs

Declaring and Using Constants and Variables

Trigonometry with floats

Calculations and Rounding

Here you want to have an impossible imagination. What if you are about to visit ancient Greek and meet Pythagoras, Hipparchus using a time machine. And you visit them with modern computers and games you developed using their amazing mathematical theories like Trigonometry and Pythagoras Theorem. So on that occasion, you are about to show a simple program to show them how you implemented the Pythagoras theorem using C# Programming Language.

Wait !! Pythagoras' theorem is a critical theorem used in game development mostly to calculate the distance between two points.

The Pythagorean Theorem tells us how to calculate the length of the hypotenuse of a right triangle.

Then,

Here are some applications of the theorem.

Challenge !!

In the following scenario, Imagine there are two players. Player A wants to throw a projectile at Player B as they are enemies.

The requirement for Projectile Throwing is the Direction and distance of Player B from Player A’s position.

First of all, we have to take the current positions of 2 Players as x y coordinates imagining a 2D cartesian plane.

Player A -> (point1X, point1y)

Player B-> (point2X, point2y)

So Let’s Implement it in C# Script.

then we can assign values for declared variables using the C# input function. Here we input data as a one-line string.

Player A’s XY Coordinates and Player B’s XY Coordinates Separated by spaces like below,

Also we assign q to end the while loop.

Then we read the input and assign values for points using the following function.

Then inside the previous while loop, we call the above function to assign values from the input.

Next, we are going to calculate the distance between 2 points as a float. To do the implementation of the Pythagoras Theorem, we want the delta x and delta y as 2 vertices of the triangle.

To do the next calculations, we are going to use the Math class in C#.

Math.Sqrt -> Returns the square root of a specified number as a Double.

Math.Pow -> Returns a specified number raised to the specified power as a Double.

Math.Atan2 -> Returns the angle whose tangent is the quotient of two specified numbers as a Double.

So we calculate the distance between 2 player positions as follows.

Basically what happens here is,

DeltaX = Player B’s X Coordinate Magnitude-Player A’s X Coordinate Magnitude

DeltaY = Player B’s Y Coordinate Magnitude-Player A’s Y Coordinate Magnitude

Then,

We use Math.Pow method by giving 2 numbers as the parameters like below,

Math.Pow (double x, double y);

double x = A double-precision floating-point number to be raised to a power.

double y = A double-precision floating-point number that specifies a power.

We use Math.Sqrt method by giving the number required to calculate the square root as the parameter.

Then we convert calculated double values to float using (float).

To calculate the angle we have to use Math.Atan2 method which returns the angle whose tangent is the quotient of two specified numbers.

This returns the radiant value of the angle, so we need to convert it to degrees to proceed.

The Math.PI property represents the ratio of the circumference of a circle to its diameter, approximately 3.14159: Math.PI = π ≈ 3.14159

Next, we print out the calculation answers using Console.WriteLine method in DOT NET.

Here is a test case.

Here you can have look at the source code.

So, I believe this guide would’ve provided some basic introduction to C# Programming for Game Development as a beginner.

Thank you.

--

--

Game Dev | Engineering Undergraduate Student at Sri Lanka Technological Campus.

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
Nawodya Ishan

Game Dev | Engineering Undergraduate Student at Sri Lanka Technological Campus.