How to Create Simple Archery Game by C Programming Language

archery game by C or C++ programming language

Introduction :

Game development is an exciting and rewarding project in the programming world. Creating an archery game using the C or C++ programming language is fun and challenging project for new developers. In this article, I shall show you how you design and develop a digital archery game using graphics.h library of Turbo C++ IDE by C or C++ programming language.

In this program, I draw essential components of an archery game, such as the target, the arrow and various switches to bring the game to life. Here, you have to control the arrow to hit the target in graphics mode.

What is archery game :

An archery game is a fun and competitive activity where the player has to shoot the designated target with an arrow and score points based on their performance. There are different type of archery game such as digital and physical you can play. In the Olympic games, you can see the physical archery game as a sport event. On a computer or mobile, you can see digital archery games like target archery, field archery and video archery game.

About the program :

This program is a simple archery game, where the user can interact with mouse input. In the program, you see an arrow, a target and different buttons such as “up”, “down”, “hit” and “stop” displayed on the graphics screen. When you click (mouse click) on the “up” and “down” buttons, the arrow moves up and down respectively.

After adjust the arrow, you can hit or fire the target by clicking on “hit” button. If the arrow hits the target, you can hear a sound and see another target at new position on the graphics screen. To close the program, you have to click on “stop” button.

Explanation of the program :

At first, include graphics.h, stdlib.h, dos.h and math.h header files for graphics, standard input output, DOS interrupts and mathematical functions respectively. Inside the main function, declare various types of variables such as integer, union and void. Using initgraph() function initialize the graphics mode. Here, getmaxx() and getmaxy() functions are used for determining the screen dimensions and store in “xmax” and “ymax” variables respectively. Calculate the half of “ymax” variable and store in “y” variable.

After that, initialize and display the mouse by set regs_in.x.ax to 0 and 1 respectively. You have to set the background color of the graphics screen using setbkcolor() function. The fillellipse() function is used to draw the target of the program. You can set the color and style of the target using setcolor() and setfillstyle() functions. Here, I create an image using imagesize(), malloc() and getimage() functions.

Within the while loop, you can capture mouse events (click and position) by set regs_in.x.ax to 3. Here, you can store the position (x and y coordinates) of the mouse pointer in “x_pos” and “y_pos” variables respectively. Now, store the click state of the mouse pointer in “click” variable. In the if statements, change the value of “action” variable depending on mouse click on different buttons.The different buttons are drawn by rectangle() function.

Using settextstyle() and outtextxy(), you can label the text on the buttons. The putimage() function is used for display the target on the screen. The line() function is used for display the arrow on the screen. Based on the value of the “action” variable, you have to set the specific actions such as movement (up and down) of arrow, hit (fire) of arrow and stop the program. Using cos() and sin() functions, you have to set the updated position of different elements.

If the arrow hits the target, you can produce a sound using sound() and nosound() functions. The rand() function is used to randomly generate the target position. Here, you can control the animation speed using delay() and clear the graphics screen by cleardevice() functions. Free the memory allocated for image by free() function. The closegraph() function is used to close the graphics mode.

How run the program :

At first, you have to install the Turbo C++ IDE on your pc. Now, open the Turbo C++ IDE and create a C or C++ file with .c or .cpp extension. After that, copy the source code and paste in the file. If you want to know how to copy paste in the Turbo C++ IDE, just click here. You can also know how to use graphics.h in Turbo C++ IDE from my other post.

In this program, I have used mouse pointer input technique. You can also know how to use the mouse pointer by C or C++ programming language from my post.

Source code :

The below code is the source code of the archery game using C or C++ graphics programming language.

Output :

If you run the program on your pc, you can play the game which you just created.

output of archery game by C or C++ graphics programming language.

Conclusion :

After reading my post, you know how to create a simple archery game using the graphics.h library of Turbo C++ IDE by C or C++ graphics programming language. There are some problems with my archery game program. You can make it perfect by adding some more codes in the program. Thank you for visiting my site.

Scroll to Top