Game Class PD
Game Class PD
#include <cstdlib>
void initialize() {
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
grid[y][x] = ' ';
bombTimer[y][x] = 0;
}
}
grid[playerY][playerX] = 'P';
}
void draw() {
system("clear");
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
cout << grid[y][x] << " ";
}
cout << endl;
}
}
void input() {
cout << "Use WASD to move, 'b' to place bomb: ";
char command;
cin >> command;
int nextX = playerX;
int nextY = playerY;
switch (command) {
case 'w': nextY--; break;
case 's': nextY++; break;
case 'a': nextX--; break;
case 'd': nextX++; break;
case 'b':
bombTimer[playerY][playerX] = 3;
grid[playerY][playerX] = 'B';
break;
}
if (nextX >= 0 && nextX < WIDTH && nextY >= 0 && nextY < HEIGHT) {
if (grid[nextY][nextX] == ' ') {
grid[playerY][playerX] = ' ';
playerX = nextX;
playerY = nextY;
grid[playerY][playerX] = 'P';
}
}
}
void update() {
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
if (bombTimer[y][x] > 0) {
bombTimer[y][x]--;
if (bombTimer[y][x] == 0) {
grid[y][x] = ' ';
for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) {
int ny = y + dy;
int nx = x + dx;
if (nx >= 0 && nx < WIDTH && ny >= 0 && ny < HEIGHT) {
grid[ny][nx] = '*';
}
}
}
}
}
}
}
}
int main() {
initialize();
while (true) {
draw();
input();
update();
}
return 0;
}