用C语言写一个坦克大战

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

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

个人练习生xx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值