#include<dos.h>
#include<conio.h>
#include<stdlib.h>
#include<iostream.h>
class tank {
int x;
int y;
public:
tank() {
x = 5;
y = 5;
}
tank(int a, int b) {
x = a;
y = b;
}
char ch, ch1;
void model();
void move();
void bomb();
};
void tank::model() {
clrscr();
gotoxy(x, y);
cout << '²';
gotoxy(++x, y);
cout << '²';
x--;
gotoxy(--x, y);
cout << '²';
x++;
gotoxy(x, ++y);
cout << '²';
y--;
gotoxy(x, --y);
cout << '²';
y++;
if (ch1 == 'w' || ch1 == 'W') {
gotoxy(++x, ++y);
cout << '²';
x--;
y--;
gotoxy(--x, ++y);
cout << '²';
x++;
y--;
} else if (ch1 == 's' || ch1 == 'S') {
gotoxy(++x, --y);
cout << '²';
x--;
y++;
gotoxy(--x, --y);
cout << '²';
x++;
y++;
} else if (ch1 == 'd' || ch1 == 'D') {
gotoxy(--x, --y);
cout << '²';
x++;
y++;
gotoxy(--x, ++y);
cout << '²';
x++;
y--;
} else if (ch1 == 'a' || ch1 == 'A') {
gotoxy(++x, --y);
cout << '²';
x--;
y++;
gotoxy(++x, ++y);
cout << '²';
x--;
y--;
}
}
void tank::move() {
// ch=getche();
if (ch == 'w' || ch == 'W')
y--;
else if (ch == 's' || ch == 'S')
y++;
else if (ch == 'd' || ch == 'D')
x++;
else if (ch == 'a' || ch == 'A')
x--;
else if (ch == '0')
bomb();
else {
cout << "Wrong key";
exit(1);
}
ch1 = ch;
}
void tank::bomb() {
int x1, y1;
if (ch1 == 'a' || ch1 == 'A') {
x1 = x - 1;
y1 = y;
do {
gotoxy(--x1, y1);
cout << '-';
delay(10);
// cout<<" ";
} while (x1 > 1);
} else if (ch1 == 'd' || ch1 == 'D') {
x1 = x + 1;
y1 = y;
do {
gotoxy(++x1, y1);
cout << '-';
delay(10);
// cout<<'-';
} while (x1 < 80);
} else if (ch1 == 'w' || ch1 == 'W') {
x1 = x;
y1 = y - 1;
do {
gotoxy(x1, --y1);
cout << '|';
delay(10);
// cout<<" ";
} while (y1 > 1);
} else if (ch1 == 's' || ch1 == 'S') {
x1 = x;
y1 = y + 1;
do {
gotoxy(x1, ++y1);
cout << '|';
delay(10);
// cout<<" ";
} while (y1 < 25);
}
}
int main() {
tank t, t1(10, 10);
do {
t.ch = getche();
t.model();
t.move();
/* if(t.ch=='0')
t.bomb();
else
break; */
} while (1);
//return(0);
}
This is a classic tank game which is written in cpp programming language. You can play it by compiling it with cpp compiler. Its very simple tank game with ability to fire bomb features. Hope you will enjoy this game.
You will also like our Article: C Program to remove comments from source code
Its very simple game. There is only movement of object. Add some more features. like second player and fire bomb.
Sure Akash