Skip to content

Commit db55ef1

Browse files
Introduction to Game Development
Beginner's Guide to Game Development with sample code.
1 parent 99e345a commit db55ef1

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed

game_development.md

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
# Game Development in C++
2+
### Introduction
3+
4+
Game Development is an exciting field to explore in programming. It is a growing industry and employs a huge number of developers. This article will focus on the basics of game development for beginners to get started. The concepts are fundamental for game development in any language.
5+
6+
The following functions are fundamental for the development of any game:
7+
8+
* Setup()
9+
* Draw()
10+
* Input()
11+
* Logic()
12+
13+
**setup() -**
14+
This function initializes the game components. For eg. position, score, etc.
15+
16+
```cpp
17+
void Setup()
18+
{
19+
gameOver = false;
20+
dir = STOP;
21+
x = width / 2;
22+
y = height / 2;
23+
fruitX = rand() % width;
24+
fruitY = rand() % height;
25+
score = 0;
26+
}
27+
```
28+
**draw() -**
29+
This function creates the visual components of the game. For eg. game characters
30+
31+
```cpp
32+
void Draw()
33+
{
34+
system("cls");
35+
for (int i = 0; i < width+2; i++)
36+
cout << "-";
37+
cout << endl;
38+
39+
for (int i = 0; i < height; i++)
40+
{
41+
for (int j = 0; j < width; j++)
42+
{
43+
if (j == 0)
44+
cout << "|";
45+
if (i == y && j == x)
46+
cout << "O";
47+
else if (i == fruitY && j == fruitX)
48+
cout << "*";
49+
else
50+
{
51+
bool print = false;
52+
for (int k = 0; k < nTail; k++)
53+
{
54+
if (tailX[k] == j && tailY[k] == i)
55+
{
56+
cout << "O";
57+
print = true;
58+
}
59+
}
60+
if (!print)
61+
cout << " ";
62+
}
63+
64+
65+
if (j == width - 1)
66+
cout << "|";
67+
}
68+
cout << endl;
69+
}
70+
for (int i = 0; i < width+2; i++)
71+
cout << "-";
72+
cout << endl;
73+
cout << "Score:" << score << endl;
74+
}
75+
```
76+
77+
**input() -**
78+
This function manages inputs from the user in order to play/access the game.
79+
For eg. Control Keys
80+
81+
```cpp
82+
void Input()
83+
{
84+
if (_kbhit())
85+
{
86+
switch (_getch())
87+
{
88+
case 'a':
89+
dir = LEFT;
90+
break;
91+
case 'd':
92+
dir = RIGHT;
93+
break;
94+
case 'w':
95+
dir = UP;
96+
break;
97+
case 's':
98+
dir = DOWN;
99+
break;
100+
case 'x':
101+
gameOver = true;
102+
break;
103+
}
104+
}
105+
}
106+
```
107+
**logic() -**
108+
This function manages the flow of the game. This is the most important component of the game that has logic.
109+
110+
```cpp
111+
void Logic()
112+
{
113+
int prevX = tailX[0];
114+
int prevY = tailY[0];
115+
int prev2X, prev2Y;
116+
tailX[0] = x;
117+
tailY[0] = y;
118+
for (int i = 1; i < nTail; i++)
119+
{
120+
prev2X = tailX[i];
121+
prev2Y = tailY[i];
122+
tailX[i] = prevX;
123+
tailY[i] = prevY;
124+
prevX = prev2X;
125+
prevY = prev2Y;
126+
}
127+
switch (dir)
128+
{
129+
case LEFT:
130+
x--;
131+
break;
132+
case RIGHT:
133+
x++;
134+
break;
135+
case UP:
136+
y--;
137+
break;
138+
case DOWN:
139+
y++;
140+
break;
141+
default:
142+
break;
143+
}
144+
//if (x > width || x < 0 || y > height || y < 0)
145+
// gameOver = true;
146+
if (x >= width) x = 0; else if (x < 0) x = width - 1;
147+
if (y >= height) y = 0; else if (y < 0) y = height - 1;
148+
149+
for (int i = 0; i < nTail; i++)
150+
if (tailX[i] == x && tailY[i] == y)
151+
gameOver = true;
152+
153+
if (x == fruitX && y == fruitY)
154+
{
155+
score += 10;
156+
fruitX = rand() % width;
157+
fruitY = rand() % height;
158+
nTail++;
159+
}
160+
}
161+
```
162+
163+
---
164+
165+
So hope this helps you in getting started. Happy Coding.

0 commit comments

Comments
 (0)