之前我写过一个小游戏,也是飞机大战,不过这个游戏玩起来体验感不是很好,所以今天重新写了一个,(以前的链接:C++实践:小游戏--飞机大战_c++飞机游戏-CSDN博客)
我们来看看现在的代码吧!
#include
#include
#include
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME)&0x8000)?1:0)
using namespace std;
这是开始之前必不可少的代码段(包括头文件)
const short Blue4 = 16, Blue3 = 144, Blue2 = 48, Blue1 = 176, Red2 = 64, Red1 = 192, Green2 = 32, Green1 = 160, Brown2 = 96, Brown1 = 224, Purple2 = 80, Purple1 = 208, Black = 256, DarkGrey = 128, Grey = 112, White = 240;
const unsigned long long N = 10010, S = 10000;
这是游戏的几个必备数据,第一行为方块的颜色(本游戏采用像素输出),第二行为子弹和敌人的数量。
void HideCursor() {
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}//隐藏光标
void Initialization(short n) {
CONSOLE_FONT_INFOEX cfi;
cfi.cbSize = sizeof cfi;
cfi.nFont = 0;
cfi.dwFontSize.X = 0;
cfi.dwFontSize.Y = n;
cfi.FontFamily = FF_DONTCARE;
cfi.FontWeight = FW_NORMAL;
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
ShowWindow(GetForegroundWindow(), SW_MAXIMIZE);
HideCursor();
Sleep(1000);
}//初始化+最大化窗口
void Block_Print(int color) {
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | color);
cout << " ";
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | 7);
}//打印方块
void Gotoxy(int x, int y) {
COORD pos = {x, y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
}//gotoxy函数,用于行列之间的跳跃
int randNext(int left, int right) {
static unsigned int seed = 0;
seed++;
srand((unsigned)time(NULL) + seed * seed);
return rand() % (right - left + 1) + left;
}//随机数
以上为几个必备的辅助函数
struct Role_Creating {
int x = 10000, y = 10000;
} Actor, Enemy_Plane[N], Bullet[N], Bullet_R[N], Bullet_L[N];
//主角飞机、敌机、直射子弹、右前、左前
int Stage[2][65][105], Enemy_J = S, Bullet_J = S, Bullet_R_J = S, Bullet_L_J = S;
unsigned long long Runs;//运行计数
接下来是运行函数!(重点)
void Run() {
Actor.x = 50, Actor.y = 50;//主角初始坐标
while (++Runs) {
for (int i = Bullet_J + 1; i <= S; i++)Bullet[i].y -= 4;
for (int i = Enemy_J + 1; i <= S; i++)Enemy_Plane[i].y += 1;
for (int i = Bullet_R_J + 1; i <= S; i++)Bullet_R[i].y -= 1, Bullet_R[i].x += 2;
for (int i = Bullet_L_J + 1; i <= S; i++)Bullet_L[i].y -= 1, Bullet_L[i].x -= 2;
//子弹、敌机的坐标变化
if (Runs % 15 == 0)Enemy_Plane[Enemy_J].x = randNext(4, 100), Enemy_Plane[Enemy_J--].y = 2; //随机生成敌机
if (KEY_DOWN(68) && (Actor.x + 1) != 105)Actor.x += 2;
if (KEY_DOWN(83) && (Actor.y + 1) != 65)Actor.y += 2;
if (KEY_DOWN(65) && (Actor.x - 1) != 1)Actor.x -= 2;
if (KEY_DOWN(87) && (Actor.y - 1) != 1)Actor.y -= 2;
//主角移动
if (KEY_DOWN(78)) {
Bullet[Bullet_J].x = Actor.x;
Bullet[Bullet_J--].y = Actor.y - 3;
Bullet[Bullet_J].x = Actor.x - 1;
Bullet[Bullet_J--].y = Actor.y - 2;
Bullet[Bullet_J].x = Actor.x + 1;
Bullet[Bullet_J--].y = Actor.y - 2;
Bullet[Bullet_J].x = Actor.x - 2;
Bullet[Bullet_J--].y = Actor.y - 1;
Bullet[Bullet_J].x = Actor.x + 2;
Bullet[Bullet_J--].y = Actor.y - 1;
Bullet[Bullet_J].x = Actor.x;
Bullet[Bullet_J--].y = Actor.y - 4;
Bullet[Bullet_J].x = Actor.x;
Bullet[Bullet_J--].y = Actor.y - 5;
Bullet[Bullet_J].x = Actor.x - 1;
Bullet[Bullet_J--].y = Actor.y - 4;
Bullet[Bullet_J].x = Actor.x + 1;
Bullet[Bullet_J--].y = Actor.y - 4;
Bullet[Bullet_J].x = Actor.x - 2;
Bullet[Bullet_J--].y = Actor.y - 3;
Bullet[Bullet_J].x = Actor.x + 2;
Bullet[Bullet_J--].y = Actor.y - 3;
}
//前子弹
if (KEY_DOWN(77)) {
Bullet_R[Bullet_R_J].x = Actor.x + 3;
Bullet_R[Bullet_R_J--].y = Actor.y - 1;
Bullet_R[Bullet_R_J].x = Actor.x + 4;
Bullet_R[Bullet_R_J--].y = Actor.y - 3;
Bullet_R[Bullet_R_J].x = Actor.x + 5;
Bullet_R[Bullet_R_J--].y = Actor.y - 5;
Bullet_R[Bullet_R_J].x = Actor.x + 6;
Bullet_R[Bullet_R_J--].y = Actor.y - 7;
Bullet_R[Bullet_R_J].x = Actor.x + 5;
Bullet_R[Bullet_R_J--].y = Actor.y - 2;
Bullet_R[Bullet_R_J].x = Actor.x + 6;
Bullet_R[Bullet_R_J--].y = Actor.y - 4;
Bullet_R[Bullet_R_J].x = Actor.x + 7;
Bullet_R[Bullet_R_J--].y = Actor.y - 6;
}
//右前
if (KEY_DOWN(66)) {
Bullet_L[Bullet_L_J].x = Actor.x - 3;
Bullet_L[Bullet_L_J--].y = Actor.y - 1;
Bullet_L[Bullet_L_J].x = Actor.x - 4;
Bullet_L[Bullet_L_J--].y = Actor.y - 3;
Bullet_L[Bullet_L_J].x = Actor.x - 5;
Bullet_L[Bullet_L_J--].y = Actor.y - 5;
Bullet_L[Bullet_L_J].x = Actor.x - 6;
Bullet_L[Bullet_L_J--].y = Actor.y - 7;
Bullet_L[Bullet_L_J].x = Actor.x - 5;
Bullet_L[Bullet_L_J--].y = Actor.y - 2;
Bullet_L[Bullet_L_J].x = Actor.x - 6;
Bullet_L[Bullet_L_J--].y = Actor.y - 4;
Bullet_L[Bullet_L_J].x = Actor.x - 7;
Bullet_L[Bullet_L_J--].y = Actor.y - 6;
}
//左前
for (int i = 0; i < 65; i++)for (int j = 0; j < 105; j++)Stage[1][i][j] = DarkGrey;
Stage[1][Actor.y][Actor.x] = Red1;
Stage[1][Actor.y - 1][Actor.x] = White;
Stage[1][Actor.y][Actor.x - 1] = White;
Stage[1][Actor.y][Actor.x + 1] = White;
Stage[1][Actor.y - 2][Actor.x] = Blue3;
//主角绘制
for (int k = Bullet_J; k <= S; k++)for (int m = Enemy_J; m <= S; m++)
if ((Bullet[k].x == Enemy_Plane[m].x) && (Bullet[k].y == Enemy_Plane[m].y))
Bullet[k].x = 1000, Enemy_Plane[m].x = 1000;
for (int k = Bullet_R_J; k <= S; k++)for (int m = Enemy_J; m <= S; m++)
if ((Bullet_R[k].x == Enemy_Plane[m].x) && (Bullet_R[k].y == Enemy_Plane[m].y))
Bullet_R[k].x = 1000, Enemy_Plane[m].x = 1000;
for (int k = Bullet_L_J; k <= S; k++)for (int m = Enemy_J; m <= S; m++)
if ((Bullet_L[k].x == Enemy_Plane[m].x) && (Bullet_L[k].y == Enemy_Plane[m].y))
Bullet_L[k].x = 1000, Enemy_Plane[m].x = 1000;
//碰撞检测
for (int i = 0; i < 65; i++)
for (int j = 0; j < 105; j++)
for (int k = Bullet_J; k <= S; k++)
if (Bullet[k].x == j && Bullet[k].y == i)
Stage[1][Bullet[k].y][Bullet[k].x] = Green1;
for (int i = 0; i < 65; i++)
for (int j = 0; j < 105; j++)
for (int k = Bullet_R_J; k <= S; k++)
if (Bullet_R[k].x == j && Bullet_R[k].y == i)
Stage[1][Bullet_R[k].y][Bullet_R[k].x] = Green1;
for (int i = 0; i < 65; i++)
for (int j = 0; j < 105; j++)
for (int k = Bullet_L_J; k <= S; k++)
if (Bullet_L[k].x == j && Bullet_L[k].y == i)
Stage[1][Bullet_L[k].y][Bullet_L[k].x] = Green1;
//子弹绘制
for (int i = 0; i < 65; i++)for (int j = 0; j < 105; j++)for (int k = Enemy_J; k <= S; k++)if (Enemy_Plane[k].x == j && Enemy_Plane[k].y == i) {
Stage[1][Enemy_Plane[k].y][Enemy_Plane[k].x] = Blue3;
Stage[1][Enemy_Plane[k].y - 1][Enemy_Plane[k].x] = White;
Stage[1][Enemy_Plane[k].y - 2][Enemy_Plane[k].x] = Red1;
Stage[1][Enemy_Plane[k].y - 2][Enemy_Plane[k].x + 1] = Blue1;
Stage[1][Enemy_Plane[k].y - 2][Enemy_Plane[k].x - 1] = Blue1;
}
//敌机绘制
for (int i = 0; i < 65; i++)for (int j = 0; j < 105; j++)if (Stage[1][i][j] != Stage[0][i][j]) {
Gotoxy(2 * j, i);
Block_Print(Stage[1][i][j]);
}
for (int i = 0; i < 65; i++)for (int j = 0; j < 105; j++)Stage[0][i][j] = Stage[1][i][j];
Sleep(60);
}
}
main函数:
int main() {
Initialization(15);
Run();
}
全部代码:
#include
#include
#include
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME)&0x8000)?1:0)
using namespace std;
const short Blue4 = 16, Blue3 = 144, Blue2 = 48, Blue1 = 176, Red2 = 64, Red1 = 192, Green2 = 32, Green1 = 160, Brown2 = 96, Brown1 = 224, Purple2 = 80, Purple1 = 208, Black = 256, DarkGrey = 128, Grey = 112, White = 240;
const unsigned long long N = 10010, S = 10000;
void HideCursor() {
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void Initialization(short n) {
CONSOLE_FONT_INFOEX cfi;
cfi.cbSize = sizeof cfi;
cfi.nFont = 0;
cfi.dwFontSize.X = 0;
cfi.dwFontSize.Y = n;
cfi.FontFamily = FF_DONTCARE;
cfi.FontWeight = FW_NORMAL;
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
ShowWindow(GetForegroundWindow(), SW_MAXIMIZE);
HideCursor();
Sleep(1000);
}
void Block_Print(int color) {
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | color);
cout << " ";
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | 7);
}
void Gotoxy(int x, int y) {
COORD pos = {x, y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
}
int randNext(int left, int right) {
static unsigned int seed = 0;
seed++;
srand((unsigned)time(NULL) + seed * seed);
return rand() % (right - left + 1) + left;
}
struct Role_Creating {
int x = 10000, y = 10000;
} Actor, Enemy_Plane[N], Bullet[N], Bullet_R[N], Bullet_L[N];
int Stage[2][65][105], Enemy_J = S, Bullet_J = S, Bullet_R_J = S, Bullet_L_J = S;
unsigned long long Runs;
void Run() {
Actor.x = 50, Actor.y = 50;
while (++Runs) {
for (int i = Bullet_J + 1; i <= S; i++)Bullet[i].y -= 4;
for (int i = Enemy_J + 1; i <= S; i++)Enemy_Plane[i].y += 1;
for (int i = Bullet_R_J + 1; i <= S; i++)Bullet_R[i].y -= 1, Bullet_R[i].x += 2;
for (int i = Bullet_L_J + 1; i <= S; i++)Bullet_L[i].y -= 1, Bullet_L[i].x -= 2;
if (Runs % 15 == 0)Enemy_Plane[Enemy_J].x = randNext(4, 100), Enemy_Plane[Enemy_J--].y = 2;
if (KEY_DOWN(68) && (Actor.x + 1) != 105)Actor.x += 2;
if (KEY_DOWN(83) && (Actor.y + 1) != 65)Actor.y += 2;
if (KEY_DOWN(65) && (Actor.x - 1) != 1)Actor.x -= 2;
if (KEY_DOWN(87) && (Actor.y - 1) != 1)Actor.y -= 2;
if (KEY_DOWN(78)) {
Bullet[Bullet_J].x = Actor.x;
Bullet[Bullet_J--].y = Actor.y - 3;
Bullet[Bullet_J].x = Actor.x - 1;
Bullet[Bullet_J--].y = Actor.y - 2;
Bullet[Bullet_J].x = Actor.x + 1;
Bullet[Bullet_J--].y = Actor.y - 2;
Bullet[Bullet_J].x = Actor.x - 2;
Bullet[Bullet_J--].y = Actor.y - 1;
Bullet[Bullet_J].x = Actor.x + 2;
Bullet[Bullet_J--].y = Actor.y - 1;
Bullet[Bullet_J].x = Actor.x;
Bullet[Bullet_J--].y = Actor.y - 4;
Bullet[Bullet_J].x = Actor.x;
Bullet[Bullet_J--].y = Actor.y - 5;
Bullet[Bullet_J].x = Actor.x - 1;
Bullet[Bullet_J--].y = Actor.y - 4;
Bullet[Bullet_J].x = Actor.x + 1;
Bullet[Bullet_J--].y = Actor.y - 4;
Bullet[Bullet_J].x = Actor.x - 2;
Bullet[Bullet_J--].y = Actor.y - 3;
Bullet[Bullet_J].x = Actor.x + 2;
Bullet[Bullet_J--].y = Actor.y - 3;
}
if (KEY_DOWN(77)) {
Bullet_R[Bullet_R_J].x = Actor.x + 3;
Bullet_R[Bullet_R_J--].y = Actor.y - 1;
Bullet_R[Bullet_R_J].x = Actor.x + 4;
Bullet_R[Bullet_R_J--].y = Actor.y - 3;
Bullet_R[Bullet_R_J].x = Actor.x + 5;
Bullet_R[Bullet_R_J--].y = Actor.y - 5;
Bullet_R[Bullet_R_J].x = Actor.x + 6;
Bullet_R[Bullet_R_J--].y = Actor.y - 7;
Bullet_R[Bullet_R_J].x = Actor.x + 5;
Bullet_R[Bullet_R_J--].y = Actor.y - 2;
Bullet_R[Bullet_R_J].x = Actor.x + 6;
Bullet_R[Bullet_R_J--].y = Actor.y - 4;
Bullet_R[Bullet_R_J].x = Actor.x + 7;
Bullet_R[Bullet_R_J--].y = Actor.y - 6;
}
if (KEY_DOWN(66)) {
Bullet_L[Bullet_L_J].x = Actor.x - 3;
Bullet_L[Bullet_L_J--].y = Actor.y - 1;
Bullet_L[Bullet_L_J].x = Actor.x - 4;
Bullet_L[Bullet_L_J--].y = Actor.y - 3;
Bullet_L[Bullet_L_J].x = Actor.x - 5;
Bullet_L[Bullet_L_J--].y = Actor.y - 5;
Bullet_L[Bullet_L_J].x = Actor.x - 6;
Bullet_L[Bullet_L_J--].y = Actor.y - 7;
Bullet_L[Bullet_L_J].x = Actor.x - 5;
Bullet_L[Bullet_L_J--].y = Actor.y - 2;
Bullet_L[Bullet_L_J].x = Actor.x - 6;
Bullet_L[Bullet_L_J--].y = Actor.y - 4;
Bullet_L[Bullet_L_J].x = Actor.x - 7;
Bullet_L[Bullet_L_J--].y = Actor.y - 6;
}
for (int i = 0; i < 65; i++)for (int j = 0; j < 105; j++)Stage[1][i][j] = DarkGrey;
Stage[1][Actor.y][Actor.x] = Red1;
Stage[1][Actor.y - 1][Actor.x] = White;
Stage[1][Actor.y][Actor.x - 1] = White;
Stage[1][Actor.y][Actor.x + 1] = White;
Stage[1][Actor.y - 2][Actor.x] = Blue3;
for (int k = Bullet_J; k <= S; k++)for (int m = Enemy_J; m <= S; m++)
if ((Bullet[k].x == Enemy_Plane[m].x) && (Bullet[k].y == Enemy_Plane[m].y))
Bullet[k].x = 1000, Enemy_Plane[m].x = 1000;
for (int k = Bullet_R_J; k <= S; k++)for (int m = Enemy_J; m <= S; m++)
if ((Bullet_R[k].x == Enemy_Plane[m].x) && (Bullet_R[k].y == Enemy_Plane[m].y))
Bullet_R[k].x = 1000, Enemy_Plane[m].x = 1000;
for (int k = Bullet_L_J; k <= S; k++)for (int m = Enemy_J; m <= S; m++)
if ((Bullet_L[k].x == Enemy_Plane[m].x) && (Bullet_L[k].y == Enemy_Plane[m].y))
Bullet_L[k].x = 1000, Enemy_Plane[m].x = 1000;
for (int i = 0; i < 65; i++)
for (int j = 0; j < 105; j++)
for (int k = Bullet_J; k <= S; k++)
if (Bullet[k].x == j && Bullet[k].y == i)
Stage[1][Bullet[k].y][Bullet[k].x] = Green1;
for (int i = 0; i < 65; i++)
for (int j = 0; j < 105; j++)
for (int k = Bullet_R_J; k <= S; k++)
if (Bullet_R[k].x == j && Bullet_R[k].y == i)
Stage[1][Bullet_R[k].y][Bullet_R[k].x] = Green1;
for (int i = 0; i < 65; i++)
for (int j = 0; j < 105; j++)
for (int k = Bullet_L_J; k <= S; k++)
if (Bullet_L[k].x == j && Bullet_L[k].y == i)
Stage[1][Bullet_L[k].y][Bullet_L[k].x] = Green1;
for (int i = 0; i < 65; i++)for (int j = 0; j < 105; j++)for (int k = Enemy_J; k <= S; k++)if (Enemy_Plane[k].x == j && Enemy_Plane[k].y == i) {
Stage[1][Enemy_Plane[k].y][Enemy_Plane[k].x] = Blue3;
Stage[1][Enemy_Plane[k].y - 1][Enemy_Plane[k].x] = White;
Stage[1][Enemy_Plane[k].y - 2][Enemy_Plane[k].x] = Red1;
Stage[1][Enemy_Plane[k].y - 2][Enemy_Plane[k].x + 1] = Blue1;
Stage[1][Enemy_Plane[k].y - 2][Enemy_Plane[k].x - 1] = Blue1;
}
for (int i = 0; i < 65; i++)for (int j = 0; j < 105; j++)if (Stage[1][i][j] != Stage[0][i][j]) {
Gotoxy(2 * j, i);
Block_Print(Stage[1][i][j]);
}
for (int i = 0; i < 65; i++)for (int j = 0; j < 105; j++)Stage[0][i][j] = Stage[1][i][j];
Sleep(60);
}
}
int main() {
Initialization(15);
Run();
}
操纵方式:
WASD控制方向,N发射对前子弹,B发射左前,M发射右前。
提示:点击开始运行后等待程序启动后窗口会自动放大,请勿操作。
(另附:我才初一,代码写的可能有点乱,喜欢的就拿走吧,我会持续更新的,求点赞)