#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#define MAP_WIDTH 80
#define MAP_HEIGHT 25
#define WALL_CHAR '#'
#define TANK_CHAR 'M'
#define BULLET_CHAR '*'
#define ENEMY_CHAR 'E'
#define SPACE_CHAR ' '
typedef struct {
int x;
int y;
int direction;
} Tank;
typedef struct {
int x;
int y;
int direction;
int isActive;
} Bullet;
typedef struct {
int x;
int y;
int direction;
int isActive;
} Enemy;
int map[MAP_WIDTH][MAP_HEIGHT];
Tank tank = {0, 0, 0};
Bullet bullet = {-1, -1, 0, 0};
Enemy enemy = {-1, -1, 0, 1};
int score = 0;
void initMap() {
int i, j;
for (i = 0; i < MAP_WIDTH; i++) {
for (j = 0; j < MAP_HEIGHT; j++) {
if (i == 0 || i == MAP_WIDTH - 1 || j == 0 || j == MAP_HEIGHT - 1) {
map[i][j] = 1;
} else {
map[i][j] = 0;
}
}
}
}
void drawMap() {
system("cls");
int i, j;
for (i = 0; i < MAP_WIDTH; i++) {
for (j = 0; j < MAP_HEIGHT; j++) {
if (map[i][j] == 1) {
printf("%c", WALL_CHAR);
} else if (i == tank.x && j == tank.y) {
printf("%c", TANK_CHAR);
} else if (i == bullet.x && j == bullet.y) {
printf("%c", BULLET_CHAR);
} else if (i == enemy.x && j == enemy.y) {
printf("%c", ENEMY_CHAR);
} else {
printf("%c", SPACE_CHAR);
}
}
printf("\n");
}
printf("Score: %d\n", score);
}
void initTank() {
tank.x = MAP_WIDTH / 2;
tank.y = MAP_HEIGHT - 2;
tank.direction = 0;
}
void initBullet() {
bullet.isActive = 0;
}
void initEnemy() {
enemy.x = rand() % (MAP_WIDTH - 2) + 1;
enemy.y = 1;
enemy.direction = rand() % 4;
enemy.isActive = 1;
}
void moveTank(int direction) {
int x = tank.x;
int y = tank.y;
switch (direction) {
case 0: // up
if (y > 1 && map[x][y - 1] == 0) {
tank.y--;
}
break;
case 1: // right
if (x < MAP_WIDTH - 2 && map[x + 1][y] == 0) {
tank.x++;
}
break;
case 2: // down
if (y < MAP_HEIGHT - 2 && map[x][y + 1] == 0) {
tank.y++;
}
break;
case 3: // left
if (x > 1 && map[x - 1][y] == 0) {
tank.x--;
}
break;
}
}
void fireBullet() {
if (bullet.isActive == 0) {
bullet.x = tank.x;
bullet.y = tank.y;
bullet.direction = tank.direction;
bullet.isActive = 1;
}
}
void moveBullet() {
if (bullet.isActive == 1) {
switch (bullet.direction) {
case 0: // up
if (bullet.y > 1) {
bullet.y--;
} else {
bullet.isActive = 0;
}
break;
case 1: // right
if (bullet.x < MAP_WIDTH - 2) {
bullet.x++;
} else {
bullet.isActive = 0;
}
break;
case 2: // down
if (bullet.y < MAP_HEIGHT - 2) {
bullet.y++;
} else {
bullet.isActive = 0;
}
break;
case 3: // left
if (bullet.x > 1) {
bullet.x--;
} else {
bullet.isActive = 0;
}
break;
}
}
}
void moveEnemy() {
if (enemy.isActive == 1) {
int x = enemy.x;
int y = enemy.y;
int direction = enemy.direction;
switch (direction) {
case 0: // up
if (y > 1 && map[x][y - 1] == 0) {
enemy.y--;
} else {
enemy.direction = rand() % 4;
}
break;
case 1: // right
if (x < MAP_WIDTH - 2 && map[x + 1][y] == 0) {
enemy.x++;
} else {
enemy.direction = rand() % 4;
}
break;
case 2: // down
if (y < MAP_HEIGHT - 2 && map[x][y + 1] == 0) {
enemy.y++;
} else {
enemy.direction = rand() % 4;
}
break;
case 3: // left
if (x > 1 && map[x - 1][y] == 0) {
enemy.x--;
} else {
enemy.direction = rand() % 4;
}
break;
}
if (bullet.isActive == 1 && bullet.x == enemy.x && bullet.y == enemy.y) {
enemy.isActive = 0;
score += 10;
}
} else {
initEnemy();
}
}
int main() {
srand(time(NULL));
initMap();
initTank();
initBullet();
initEnemy();
while (1) {
drawMap();
if (_kbhit()) {
int ch = _getch();
switch (ch) {
case 'w':
case 'W':
tank.direction = 0;
moveTank(0);
break;
case 'd':
case 'D':
tank.direction = 1;
moveTank(1);
break;
case 's':
case 'S':
tank.direction = 2;
moveTank(2);
break;
case 'a':
case 'A':
tank.direction = 3;
moveTank(3);
break;
case ' ':
fireBullet();
break;
case 'q':
case 'Q':
exit(0);
break;
}
}
moveBullet();
moveEnemy();
Sleep(50);
}
return 0;
}
用C语言写一个坦克大战
于 2023-03-28 16:28:36 首次发布