
If you have a question like, “What is the most crucial aspect of developing a game?” Then there’s plenty. However, the one thing that stands out is the logic of the game. Nevertheless, in this blog, we will try to decipher the steps that are required to write a basic game. The game will be based on ‘Trying to Figure Out the Random Number- A Guessing Game’ and we will use Golang for game development. If you are looking for a game development company, then AppFirmsReview can also help you find a company, from the top mobile app development company list, that can develop games for you.
Things We Need to Get Started
In order to write the program for the game, we need an IDE and a Go compiler. You can download both for free from the Internet. In this tutorial, I’m going to be using VS Code as an IDE to code.
So, install these applications and let’s get started.
Open Command Prompt
Now, you have to open Command Prompt in order to create a new directory. Let us name this directory ‘rangame’ and store it on the desktop. So we type:
- cd Desktop
- mkdir rangame
- cd rangame
- code .
When we type code ., our IDE opens up. In this case, we are talking about VS Code.
Programming in VS Code
- Now, it is time to code in the VS Code. We will be assessing the logic and incorporating certain parameters and conditions. When the VS Code gets launched, you will get to see the newgame directory already created. What we will do is create a source file named ‘main.go’ inside the directory.
- And we type:
package main
import
“fmt”
“time”
“math/rand”
)
You can clearly see that we have imported three different packages. Now let’s check what they do.
- fmt: The Format package handles input and output formatting. Formatting values, simple strings, inputs, and outputs are all done with it. Additionally, it can be used to write and print directly from the terminal.
- Time: The Golang standard library includes the time package. Numerous time and date-related functions are available. The package represents a certain moment in time.
- math/rand: One of the most crucial Golang packages, it provides pseudorandom number creation. For illustration, we’ll use the rand.Intn in the program, which returns a random integer n, ranging from 0 to 100 (including 0).
- Now, we move on.
- var figure int
- var tryout int= 1
Here we declare two variables. Both are of type integers. The figure variable will store whatever the player guesses or judges. The trycount has been assigned to 1, and it will store the number of times the player tries to figure out the random number.
- Following this, we will create the main function.
- func main() {
- instant := rand.NewSource(time.Now().UnixNano())
- choice := rand.New(instant)
- mysteryno := choice.Intn(10)
We have been using Golang for game development. Have you found it to be easy or complex? I bet it will be easy enough. Now, let’s comprehend what we have written above.
- The most commonly used seed is the current time, which is transformed to an int64 via the UnixNano keyword. The Newsource is in charge of supplying a fresh pseudo-random source that has been seeded with a specified value. In essence, the instant variable aids in the preservation of an instantaneous time.
- The choice variable instructs the compiler to select a random number at the precise instant that the instant variable specified. The New is in charge of producing a fresh Rand using the random values.
- The choice variable is informed by the mysteryno variable that the random number must fall between 0 and 10. Additionally, the mysteryno saves this specific random number.
Hopefully, you have understood the purpose of the variables. We now type the rest of the program and finish it.
fmt.Println (“Hey Guess the Magic Number!”)
for {
fmt.Println (“Start Playing”)
fmt.Scan(&figure)
if tryout>5 {
fmt.Println (“Bad Luck! Try Next Time”)
break
} else {
if figure>mysteryno {
fmt.Println(“The Random Number is Smaller than Your Guess”)
} else if figure<mysteryno {
fmt.Println(“The Random Number is Bigger than Your Guess”)
} else {
fmt.Println(“You Got It”)
break
}
}
tryout++
}
}
- Ok, let us understand what we have typed in the above section. The fmt.Println(“Hey Guess the Magic Number!”) marks the commencement of the game.
- Following this, I invite the player to try the game, i.e., try to figure out the number, in the first line of the for loop. The player is given five attempts in total. The player will be asked to attempt again if the number is greater than 5, at which point the game will finish.
- But, if the number of attempts is less than 5, then another condition will follow. In that case, the else condition will come into action. If the figure>mysteryno, which means that the player typed a number greater than the mystery number, he or she will be shown a message that the “Random Number is Smaller than Your Guess”.
- However, if the player types a number smaller than the mystery number (figure<mysteryno), then he or she will get a message that the “Random Number is Bigger than Your Guess”.
- Or else, only one possibility exists. That is, the player figuring out the magic number. Then, they will be displayed a message that “You Got It”
- The loop will continue with the help of tryout++. And with this, we come to the conclusion of the game.
Run the Game
In order to run the game, we go to the Terminal, and type
go run main.go
With this the game will start running.
Hey Guess the Magic Number
Start Playing
5
The Random Number is Bigger than Your Guess
7
The Random Number is Smaller than Your Guess
6
You Got It
Did you comprehend the game? As you can see, we have covered all the important aspects that were needed to build the game. Now, you can move on to bigger and challenging projects. Here, we have dealt with only one source file. As the intricacy of the game increases, there will be more files and functions to create and call.
0 Comments