0% found this document useful (0 votes)
6 views2 pages

Game Class PD

Uploaded by

xxlordcr7xx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Game Class PD

Uploaded by

xxlordcr7xx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream>

#include <cstdlib>

using namespace std;

const int WIDTH = 10;


const int HEIGHT = 10;
char grid[HEIGHT][WIDTH];
int bombTimer[HEIGHT][WIDTH];
int playerX = 0;
int playerY = 0;

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;
}

You might also like