Introduction
"Linux is not for gaming!" - an outdated phrase: now there are many wonderful games specifically for this wonderful system. But still, sometimes, you want something special that would be right for you ... And I decided to create this special.
Basis
I will not show and tell the whole code (this is not very interesting) - only the main points.
1.Character
All the parameters of the character are listed here (health, armor, experience, etc.), drawing and direction of movement (which is not currently available) are of interest.
int x = 5, y = 5;
hp = 100,
maxhp = 100,
dm = 20,
armor = 0,
xp = 0,
level = 0,
diff = 10, // ΡΠ»ΠΎΠΆΠ½ΠΎΡΡΡ
pos = 0; // Π½Π°ΠΏΡΠ°Π²Π»Π΅Π½ΠΈΠ΅
bool reg = 0,
Mdm = 0, // Π±ΠΎΠ½ΡΡΡ
ght = 0;
string color; // ΡΠ²Π΅Ρ Π±ΡΠ΄Π΅Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ Π² ΠΊΠ°ΡΠ΅ΡΡΠ²Π΅ ΠΈΠ½Π΄ΠΈΠΊΠ°ΡΠΎΡΠ° ΡΠΎΡΡΠ°ΡΠ½ΠΈΡ Π³Π΅ΡΠΎΡ
void hero() // Π·Π΄Π΅ΡΡ ΠΏΡΠΎΠΈΡΡ
ΠΎΠ΄ΠΈΡ ΠΏΠ΅ΡΠ΅ΠΌΠ΅ΡΠ΅Π½ΠΈΠ΅ Π³Π΅ΡΠΎΡ Π½Π° ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°ΡΡ (x ; y)
{
cout << "e[u " << "e[0;0H"; // Π²ΠΎΡΡΡΠ°Π½ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΏΠΎΠ·ΠΈΡΠΈΠΈ ΠΊΡΡΡΠΎΡΠ°, Π·Π°ΡΠΈΡΠ°Π½ΠΈΠ΅ ΠΏΡΠΎΠ±Π΅Π»ΠΎΠΌ
for (int i = 0; i <= x; i++)
cout << RIGHT; // ΠΌΠ°ΠΊΡΠΎΡ "e[1C"
for (int i = 0; i <= y; i++)
cout << DOWN; // ΠΌΠ°ΠΊΡΠΎΡ "e[1B"
cout << "e[s" << color << "β¬"; // ΡΠΎΡ
ΡΠ°Π½Π΅Π½ΠΈΠ΅ ΠΏΠΎΠ·ΠΈΡΠΈΠΈ ΠΊΡΡΡΠΎΡΠ°
}
2. Management
How to move the character is clear (xβ++, yβ++). But the keyboard processing is more entertaining:
char key;
char getkey()
{
system("stty raw");
key = getchar();
system("stty cooked");
return key;
}
It remains only to set the "control characters". Can be done with switch, but I hate it.
switch(...) case .. : ... ; break
better like this
#define KEY if (key ==
#define I ){
#define J ;}else
void keys()
{
getkey();
KEY 'a' I x-- ; pos = 1 J
KEY......
}
Beauty! Looping functions and running around the screen! But somehow harshly ... And the cursor flickers, and the letters ... Let's fix it!
//ΠΠΎ ΡΠΈΠΊΠ»Π°
cout << "e[?25l"; //ΠΎΡΠΊΠ»ΡΡΠ°Π΅ΠΌ ΠΎΡΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅ ΠΊΡΡΡΠΎΡΠ°
system("stty -echo"); //ΠΎΡΠΊΠ»ΡΡΠ°Π΅ΠΌ ΡΡ
ΠΎ-Π²Π²ΠΎΠ΄
system("xset r rate 120 10"); // ΠΈΠ·ΠΌΠ΅Π½ΡΠ΅ΠΌ Π·Π°Π΄Π΅ΡΠΆΠΊΡ Π½Π° Π±ΠΎΠ»Π΅Π΅ ΠΏΠ»Π°Π²Π½ΡΡ
//ΠΠΎΡΠ»Π΅ ΡΠΈΠΊΠ»Π°
//-------Return_normal_system_settings--------
cout << "e[00m";
system("reset");
system("xset r rate 200 20");
Uhhh! One percent done!
3. Surrounding world
Here we make arrays for x, y pieces of the world and the pieces themselves (char o[N])
, the same for monsters and bonuses.
Create a function world(int objx[N] .... objy[N] ... obj[N], ... objcolor[N])
by analogy with hero()
, but with parameters and an additional loop for outputting an array ... for fun, we draw only in the field of view (vis) (if (ox[k] < vis && oy[k]....))
Now we fill the screen with particles of the world using a simple for and procedurally gouge out rooms and passages, at the same time we enter enemies and objects, for complete randomness, do not forget about srand(time(NULL));
//------------------GENERATION---------------
void rooms()
{
for (int i = 0; i <= 50; i++)
{
px[i] = rand() % 115 + 2;
py[i] = rand() % 34 + 2;
pl[i] = rand() % 5 + 5;
ph[i] = rand() % 5 + 5;
if (px[i] + pl[i] > 117) px[i] = 50 - pl[i] / 2; else
if (px[i] < 2) px[i] = 50 - pl[i] / 2; else
if (py[i] < 1) py[i] = 15 - ph[i] / 2; else
if (py[i] + ph[i] > 37) py[i] = 15 - ph[i] / 2;
for (int j = 0; j <= i; j++)
{
while (px[i] > px[j] && px[i] < px[j] + pl[j])
(px[i]+pl[i]/2 >= 55) ? px[i]++ : px[i]-- ;
while (py[i] > py[j] && py[i] < py[j] + ph[j])
(py[i]+ph[i]/2 >= 18) ? py[i]++ : py[i]-- ;
while (px[i]+pl[i] > px[j] && px[i]+pl[i] < px[j] + pl[j])
(px[i]+pl[i]/2 >= 55) ? px[i]++ : px[i]-- ;
while (py[i]+ph[i] > py[j] && py[i]+ph[i] < py[j] + ph[j])
(py[i]+ph[i]/2 >= 18) ? py[i]++ : py[i]-- ;
}
for (int j = 0; j <= i; j++)
{
while (px[j] + pl[j] >= 116) px[j]-- ;
while (px[j] < 2) px[j]++ ;
while (py[j] < 1) py[j]++ ;
while (py[j] + ph[j] >= 37) py[j]-- ;
}
tx[i] = px[i]+10; ty[i] = py[i]-3;
if (i <= diff)
{
ex[i] = px[i];
ey[i] = py[i];
while (ex[i] < 10){ ex[i]++ ; epos[i] = 3 ;}
while (ey[i] < 10){ ey[i]++ ; epos[i] = 1 ;}
e[i] = evar[pl[i]];
ecolor[i] = "e[00me[31m";
edm[i] = edmvar[pl[i]];
ehp[i] = ehpvar[pl[i]];
exp[i] = expvar[pl[i]];
}
rect(px[i], py[i], pl[i], ph[i]);
}
}
void corrs()
{
int pc, px, py;
for (int i = 0; i <= 4; i++)
{
if (i < 2){
px = 3;
py = rand() % 33 + 3;
pc = 110;
line(px, py, pc, true);
line(px, py+1, pc, true);
} else {
px = rand() % 100 + 3;
py = 3;
pc = 33;
line(px, py, pc, false);
line(px+1, py, pc, false);
}
}
}
4.Interaction
Now we need to somehow not go through walls and monsters, get bonuses from items.
Our favorite for and #define
#define TOUCH if (x == ox[i] && y == oy[i] && pos ==
#define HIT x == ex[i] && y == ey[i] && pos ==
for (int i = 0; i <= n; i++)
{
if (i <= diff)
{
if (Mdm) ehp[i]-=2 ; // Π΅ΡΠ»ΠΈ Π±ΠΎΠ½ΡΡ "ΠΌΠ°ΡΡΠΎΠ²ΡΠΉ ΡΡΠΎΠ½" Π²ΠΊΠ»ΡΡΠ΅Π½
epos[i] = 0;
if (ex[i] < x+5 && ex[i] > x-5 &&
ey[i] < y+5 && ey[i] > y-5 )
{
edel(i); // ΡΡΠ½ΠΊΡΠΈΡ ΠΏΠ΅ΡΠ΅ΠΏΠΈΡΡΠ²Π°ΡΡΠ°Ρ ΠΏΡΠ΅Π΄ΡΠ΄ΡΡΠ΅Π΅ ΠΏΠΎΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅ ΠΏΡΠΎΡΠΈΠ²Π½ΠΈΠΊΠ°
if (ex[i] < x I ex[i]++ ; epos[i] = 1 J
if (ex[i] > x I ex[i]-- ; epos[i] = 2 J
if (ey[i] < y I ey[i]++ ; epos[i] = 3 J
if (ey[i] > y I ey[i]-- ; epos[i] = 4 ;}
}
for (int j = 0; j <= n; j++) // ΡΡΠΎΠ»ΠΊΠ½ΠΎΠ²Π΅Π½ΠΈΠ΅ ΠΌΠΎΠ±Π° ΡΠΎ ΡΡΠ΅Π½ΠΊΠ°ΠΌΠΈ
while (ex[i] == ox[j] && ey[i] == oy[j] || ex[i] == ex[j] && ey[i] == ey[j] && j != i)
{
if (epos[i] == 1) ex[i]-- ; else
if (epos[i] == 2) ex[i]++ ; else
if (epos[i] == 3) ey[i]-- ; else
if (epos[i] == 4) ey[i]++ ;
}
if (x == ex[i] && y == ey[i]) // "Π±ΠΈΡΠ²Π°"
{
if (ehp[i] > 1)
{
ehp[i] -= dm;
(edm[i] < armor) ?
hp -= 0 :
hp -= edm[i]-armor;
} else {
ex[i] = ey[i] = -1;
xp += exp[i];
ehp[i] = 12;
}
}
if (!ght) // Π΅ΡΠ»ΠΈ Π½Π΅ ΠΏΡΠΈΠ·ΡΠ°ΠΊ ΠΏΡΠΎΠ²Π΅ΡΡΡΡ ΡΡΠΎΠ»ΠΊΠ½ΠΎΠ²Π΅Π½ΠΈΠ΅ ΠΈΠ³ΡΠΎΠΊΠ° Ρ Π²ΡΠ°Π³Π°ΠΌΠΈ
{
if (HIT 1) y++ ;else
if (HIT 2) x-- ;else
if (HIT 3) y-- ;else
if (HIT 4) x++ ;
}
}
if (!ght) // ΡΠΎ ΠΆΠ΅, Π½ΠΎ ΡΠΎ ΡΡΠ΅Π½Π°ΠΌΠΈ
{
TOUCH 1 I y++ J
TOUCH 2 I x-- J
TOUCH 3 I y-- J
TOUCH 4 ) x++ ;
}
}
5.Menu
The menu is simply displayed on the screen, numbering the items, using getkey () we process the player's choice. We write the status bar of the character, implement the pumping menu, write the background, and we get what I called βSubsoilβ (βNedraβ).
Conclusion
Such is something. You can play it
$ sudo chmod +x Subsoil-1.0/Subsoil
$ Subsoil-1.0/Subsoil
, or, finally inspired, write yourself an adventure to your liking. I warn you in advance: my game is not easy!
Links
Source: habr.com