#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <windows.h>
#pragma comment(lib,"Winmm.lib")
#define high 25 //画布高
#define wide 60 //画布宽
#define bian -1 //边界
#define kong 0 //空白
#define fly 1 //飞机
#define zidan 2 //子弹
#define diji 3 //敌机
#define boom 4 //摧毁标记
#define flyci1 5
#define flyci2 6
int map[high + 2][wide +2]; //游戏场景的高和宽
int fly_x, fly_y; //飞机位置
int dijinum; //敌机数量
int jiange; //同个计次来模拟时间间隔
int dijimove; //敌机移动的时间间隔
int dijinew; //敌机刷新的时间间隔
int score; //分数
int gameover; //判断游戏是否结束
void gamestar(); //游戏数值初始化
void show(); //游戏界面输出
void move(); //因输入导致的游戏状态更新
void gamenew(); //与输入无关的游戏状态更新
void HideCursor(); //隐藏光标
void gotoxy(int x, int y); //回调光标
int main() {
SetConsoleTitle("飞机大战");
gamestar(); //初始化
while (gameover) { //游戏循环
move();
gamenew();
show();
}
gotoxy(wide/2-4,high/2+2);
printf("Game Over! \n\n\n\n\n\n\n\n\n\n\n\n\n");
MessageBox(NULL, "玩不了一点,游戏结束","飞机大战", MB_ICONINFORMATION | MB_OK);
}
void gamestar() { //游戏数值初始化
gameover = 1;
score = 0; //初始化分数
for (int i = 0; i < high + 2; i++) { //初始化画布
for (int j = 0; j < wide + 2; j++) {
if (i == 0 || i == high + 1 ||
j == 0 || j == wide + 1) {
map[i][j] = bian;
} else map[i][j] = kong;
}
fly_x = high / 2; //初始化飞机竖直坐标
fly_y = wide / 2; //初始化飞机水平坐标
map[fly_x][fly_y] = fly; //初始化飞机位置
map[fly_x][fly_y-1] = flyci1;
map[fly_x][fly_y+1] = flyci2;
dijinum = 20; //敌机数量
srand(time(NULL));
jiange = 5; //初始化时间间隔计数
dijimove = 5; //初始化敌机移动时间间隔
dijinew = 5 ; //初始化敌机刷新时间间隔
}
}
void show() { //游戏界面输出
HideCursor(); //隐藏光标
gotoxy(1, 1); //回调光标、刷新画面
for (int i = 0; i < high + 2; i++) {
for (int j = 0; j < wide + 2; j++) {
if (map[i][j] == fly) { //当前位置为飞机位置
printf("\033[35m┃\033[0m");
}
if (map[i][j] == flyci1) { //当前位置为飞机左翼位置
printf("\033[35m◢\033[0m");
}
if (map[i][j] == flyci2) { //当前位置为飞机右翼位置
printf("\033[35m◣\033[0m");
}
else if (map[i][j] == zidan) { //当前位置为子弹位置
printf("\033[33m|\033[0m");
} else if (map[i][j] == diji) { //当前位置为敌机位置
printf("\033[31m◆\033[0m");
} else if (map[i][j] == bian) { //当前位置为边界
printf("\033[36m█\033[0m");
} else if (map[i][j] == kong) { //当前位置无物,且在边界内
printf(" ");
} else if (map[i][j] == boom) { //飞机爆炸
printf("\033[31mX\033[0m");
}
}
printf("\n");
}
printf("\n得分:%d", score);
}
void move() { //移动
char key_W = GetKeyState('W'), //监测 W 键是否按下
key_S = GetKeyState('S'), //监测 S 键是否按下
key_A = GetKeyState('A'), //监测 A 键是否按下
key_D = GetKeyState('D'), //监测 D 键是否按下
key_attack = GetKeyState(' '); //监测 空格 键是否按下
if (_kbhit()) {
if (key_W < 0) { //当按下 W 键,上移
if (fly_x > 2) {
map[fly_x][fly_y] = kong;
map[fly_x][fly_y-1] = kong;
map[fly_x][fly_y+1] = kong;
if (map[fly_x - 1][fly_y] == diji||map[fly_x - 1][fly_y-1] == diji||map[fly_x - 1][fly_y+1] == diji) { //下个位置是敌机,撞毁
map[fly_x - 1][fly_y] = boom;
gameover = 0;
} else {
map[--fly_x][fly_y] = fly;
map[fly_x][fly_y-1] = flyci1;
map[fly_x][fly_y+1] = flyci2;
}
}
}
if (key_S < 0) { //当按下 S 键,下移
if (fly_x < high) {
map[fly_x][fly_y] = kong;
map[fly_x][fly_y-1] = kong;
map[fly_x][fly_y+1] = kong;
if (map[fly_x + 1][fly_y] == diji||map[fly_x + 1][fly_y-1] == diji||map[fly_x + 1][fly_y+1] == diji) { //下个位置是敌机,撞毁
map[fly_x + 1][fly_y] = boom;
gameover = 0;
} else {
map[++fly_x][fly_y] = fly;
map[fly_x][fly_y-1] = flyci1;
map[fly_x][fly_y+1] = flyci2;
}
}
}
if (key_A < 0) { //当按下 A 键,左移
if (fly_y > 2) {
map[fly_x][fly_y] = kong;
map[fly_x][fly_y-1] = kong;
map[fly_x][fly_y+1] = kong;
if (map[fly_x][fly_y - 1] == diji||map[fly_x ][fly_y-2] == diji) { //下个位置是敌机,撞毁
map[fly_x][fly_y - 1] = boom;
gameover = 0;
} else {
map[fly_x][--fly_y] = fly;
map[fly_x][fly_y-1] = flyci1;
map[fly_x][fly_y+1] = flyci2;
}
}
}
if (key_D < 0) { //当按下 D 键,右移
if (fly_y < wide-1) {
map[fly_x][fly_y] = kong;
map[fly_x][fly_y-1] = kong;
map[fly_x][fly_y+1] = kong;
if (map[fly_x][fly_y + 1] == diji||map[fly_x][fly_y+2] == diji) { //下个位置是敌机,撞毁
map[fly_x][fly_y + 1] = boom;
gameover = 0;
} else {
map[fly_x][++fly_y] = fly;
map[fly_x][fly_y-1] = flyci1;
map[fly_x][fly_y+1] = flyci2;
}
}
}
if (key_attack < 0) { //当按下空格键,发射子弹
if (fly_x != 1)map[fly_x - 1][fly_y] = zidan;
if (fly_x != 1)map[fly_x - 1][fly_y-1] = zidan;
if (fly_x != 1)map[fly_x - 1][fly_y+1] = zidan;
}
}
}
void gamenew() { //与输入无关的游戏状态更新
int temp[high + 2][wide + 2]; //用来判断原位置的临时数组
for (int i = 1; i <= high; i++) {
for (int j = 1; j <= wide; j++) {
temp[i][j] = map[i][j];
}
}
for (int i = 1; i <= high; i++) { //遍历临时数组,修改画布
for (int j = 1; j <= wide; j++) {
if (temp[i][j] == diji && jiange % dijimove == 0) { //当前位置为敌机
map[i][j] = kong;
if (temp[i + 1][j] == zidan) { //下面为子弹,敌机被击中
map[i + 1][j] = kong;
score++;
} else if (i < high) {
map[i + 1][j] = diji;
}
if( (i + 1 == fly_x && j == fly_y)||(i + 1 == fly_x && j == fly_y-1)||(i + 1 == fly_x && j == fly_y+1) ) {
//玩家飞机被撞毁
map[i + 1][j] = boom;
gameover = 0;
}
}
if (temp[i][j] == zidan) { //当前位置为子弹
map[i][j] = kong;
if (temp[i - 1][j] == diji) { //下个位置是敌机,敌机被击毁
map[i - 1][j] = kong;
score++;
// PlaySound(TEXT("boom.wav"),NULL,SND_FILENAME | SND_ASYNC);//敌机爆炸音效
} else if (i > 1) {
map[i - 1][j] = zidan;
}
}
}
}
if (jiange % dijinew == 0) //刚好到时间间隔
for (int i = 0; i < dijinum; i++) { //新增敌机群
map[rand() % 2 + 1][rand() % wide + 1] = diji;
}
if (jiange <= 100) { //时间间隔计次
jiange++;
} else { //时间间隔计次清零
jiange = 0;
}
}
void gotoxy(int x, int y) { //回调光标
COORD pos;
pos.X = x - 1;
pos.Y = y - 1;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void HideCursor() { //隐藏光标函数
CONSOLE_CURSOR_INFO cursor;
cursor.bVisible = FALSE;
cursor.dwSize = sizeof(cursor);
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorInfo(handle, &cursor);
}
c语言飞机大战基本框架,200行左右
最新推荐文章于 2023-12-29 21:50:05 发布