话不多说先上代码:
本代码兼容多种编译器,C和C++语言项目多可以运行。
#include <stdio.h>
#include <windows.h>
#include <time.h>
#include <conio.h>
#define map_x 39 //定义可移动范围的长和宽
#define map_y 76
#define plane_x 6 //定义爱心的长和宽
#define plane_y 8
#define tortoise_x 8
#define tortoise_y 11
#define ball1 '+'
void setcolor(int, int);
void gotoxy(int, int);
void Begin();
void Init();
void CreateFrame();
void GetContral();
void PlaneMove();
void TortoiseMove();
void Print();
void PlaneShoot();
void TortoiseShoot();
void PlaneBlast();
char map[map_x][map_y], newmap[map_x][map_y];
char plane1[plane_x][plane_y + 1] = {
" **",
"********",
"********",
" **",
" **",
" ****"
};
char tortoise[tortoise_x][tortoise_y + 1] = {
" #",
" # ### #",
" #########",
"# # # # #",
"###########",
"# # # # #",
" #########",
"# ## #"
};
int plane_start_x = map_x - plane_x, plane_start_y = (map_y - plane_y) / 2, tortoise_start_x = 0, tortoise_start_y = (map_y - plane_y) / 2;
int plane_move_y, plane_move_x, tortoise_move_y = 1, tortoise_move_x = 1;
int game_time;
int life1 = 20, life2 = 999;
int is_blast;
/********************************************************************/
int main()
{
Begin();
Init();
while (1)
{
PlaneMove();
if (game_time % 6 == 0)TortoiseMove();
PlaneShoot();
TortoiseShoot();
Print();
Sleep(20);
}
system("pause");
return 0;
}
/********************************************************************/
void Print()
{
for (int i = 0; i < map_x; i++){
for (int j = 0; j < map_y; j++){
if (map[i][j] != newmap[i][j]){
gotoxy(i + 1, j + 2);
if (newmap[i][j] == '*' || newmap[i][j] == '|' || newmap[i][j] == '^'){
setcolor(1, 14);
}
else{
setcolor(1, 10);
}
printf("%c", newmap[i][j]);
map[i][j] = newmap[i][j];
}
}
}
gotoxy(map_x + 2, map_y + 3);
game_time++;
}
void PlaneMove()
{
GetContral();
if (plane_start_y != map_y - plane_y&&plane_move_y == 1)plane_start_y += 2;
if (plane_start_y != 0 && plane_move_y == -1)plane_start_y -= 2;
if (plane_start_x != map_x - plane_x&&plane_move_x == 1)plane_start_x += 1;
if (plane_start_x != map_x - plane_x - 15 && plane_move_x == -1)plane_start_x -= 1;
for (int i = 0; i < map_x; i++){
for (int j = 0; j < map_y; j++){
if (newmap[i][j] == '*')newmap[i][j] = ' ';
}
}
for (int i = 0; i < plane_x; i++){
for (int j = 0; j < plane_y; j++){
if (plane1[i][j] == '*'){
if (newmap[plane_start_x + i][plane_start_y + j] == '+'){
life1--;
gotoxy(map_x + 1, 7);
setcolor(1, 12);
printf("%03d", life1);
}
newmap[plane_start_x + i][plane_start_y + j] = plane1[i][j];
}
}
}
}
void TortoiseMove()
{
tortoise_start_x += tortoise_move_x;
tortoise_start_y += tortoise_move_y;
if (rand() % 91 == 0)tortoise_move_y *= -1;
if (rand() % 29 == 0)tortoise_move_x *= -1;
if (tortoise_start_x == 0)tortoise_move_x = 1;
if (tortoise_start_x == 7)tortoise_move_x = -1;
if (tortoise_start_y == 0)tortoise_move_y = 1;
if (tortoise_start_y == map_y - tortoise_y)tortoise_move_y = -1;
for (int i = 0; i < map_x; i++)
for (int j = 0; j < map_y; j++)
if (newmap[i][j] == '#')newmap[i][j] = ' ';
for (int i = 0; i < tortoise_x; i++){
for (int j = 0; j < tortoise_y; j++){
if (tortoise[i][j] == '#'){
newmap[tortoise_start_x + i][tortoise_start_y + j] = tortoise[i][j];
}
}
}
}
void PlaneShoot()
{
int oldlife = life2;
PlaneBlast();
for (int i = 0; i < map_x - plane_x; i++){
for (int j = 0; j < map_y; j++){
if (newmap[i][j] == '|'){
newmap[i][j] = ' ';
if (newmap[i - 1][j] == ' '&&i)newmap[i - 1][j] = '|';
if (newmap[i - 1][j] == '#')life2--;
}
}
}
if (oldlife > life2){
gotoxy(0, 7);
setcolor(1, 12);
printf("%03d", life2);
}
if (game_time % 2)
{
newmap[plane_start_x - 1][plane_start_y + 3] = '|';
newmap[plane_start_x - 1][plane_start_y + 4] = '|';
}
}
void TortoiseShoot()
{
if (rand() % 10 == 0){
newmap[tortoise_start_x + tortoise_x][tortoise_start_y + 5] = ball1;
newmap[tortoise_start_x + tortoise_x][tortoise_start_y + 6] = ball1;
}
if (game_time % 4 == 0){
for (int i = 0; i < map_y; i++){
if (newmap[map_x - 1][i] == ball1)newmap[map_x - 1][i] = ' ';
}
for (int i = map_x - 2; i >= 0; i--){
for (int j = 0; j < map_y; j++){
if (newmap[i][j] == ball1){
newmap[i][j] = ' ';
if (newmap[i + 1][j] != '^')newmap[i + 1][j] = ball1;
}
}
}
}
}
void PlaneBlast()
{
if (is_blast == 0)return;
static int blastmap[map_x][map_y] = { 0 }, depth;
int walkx[8] = { 0, 0, 1, -1 }, walky[8] = { 1, -1, 0, 0 }, flag = 0;
depth++;
if (is_blast == 1){
depth = 3;
newmap[plane_start_x - 1][plane_start_y + 3] = '^';
newmap[plane_start_x - 1][plane_start_y + 4] = '^';
blastmap[plane_start_x - 1][plane_start_y + 3] = depth;
blastmap[plane_start_x - 1][plane_start_y + 4] = depth;
is_blast = 2;
return;
}
for (int i = 0; i < map_x; i++){
for (int j = 0; j < map_y; j++){
if (blastmap[i][j] == depth - 3 && newmap[i][j] == '^'){
newmap[i][j] = ' ';
flag = 1;
}
if (blastmap[i][j] == depth - 1){
if (newmap[i][j] == '#')life2--;
for (int k = 0; k < 4; k++){
if (blastmap[i + walkx[k]][j + walky[k]] == 0 && i + walkx[k] >= 0 && i + walkx[k] < map_x&&j + walky[k] >= 0 && j + walky[k] < map_y){
blastmap[i + walkx[k]][j + walky[k]] = depth;
if (newmap[i + walkx[k]][j + walky[k]] == ' ' || newmap[i + walkx[k]][j + walky[k]] == '+')newmap[i + walkx[k]][j + walky[k]] = '^';
}
}
flag = 1;
}
}
}
if (!flag)
{
is_blast = 0;
memset(blastmap, 0, sizeof(blastmap));
}
}
void GetContral()
{
plane_move_x = 0;
plane_move_y = 0;
if (_kbhit())
{
int temp = _getch();
if (temp == 'f'&&is_blast == 0) is_blast = 1;
if (temp == 224)
{
temp = _getch();
switch (temp)
{
case 72://上
plane_move_x = -1;
break;
case 80://下
plane_move_x = 1;
break;
case 75://左
plane_move_y = -1;
break;
case 77://右
plane_move_y = 1;
break;
}
}
}
}
void CreateFrame()
{
for (int i = 0; i < map_y + 4; i += 2){
gotoxy(0, i);
printf("─");
gotoxy(map_x + 1, i);
printf("─");
}
for (int i = 1; i < map_x + 1; i++){
gotoxy(i, 0);
printf("│");
gotoxy(i, map_y + 2);
printf("│");
}
gotoxy(0, map_y + 2);
printf("┐");
gotoxy(0, 0);
printf("┌");
gotoxy(map_x + 1, map_y + 2);
printf("┘");
gotoxy(map_x + 1, 0);
printf("└");
}
void Init()
{
srand(time(0));
system("color 1f");
CreateFrame();
setcolor(1, 13);
gotoxy(0, map_y / 2 + 1);
printf("雷霆战机");
setcolor(1, 12);
gotoxy(0, 2);
printf("生命:%03d", life2);
gotoxy(map_x + 1, 2);
printf("生命:%03d", life1);
memset(map, ' ', sizeof(map));
memset(newmap, ' ', sizeof(newmap));
for (int i = 0; i < plane_x; i++)
{
for (int j = 0; j < plane_y; j++)
{
if (plane1[i][j] == '*')newmap[plane_start_x + i][plane_start_y + j] = plane1[i][j];
}
}
for (int i = 0; i < tortoise_x; i++)
{
for (int j = 0; j < tortoise_y; j++)
{
if (tortoise[i][j] == '#')newmap[tortoise_start_x + i][tortoise_start_y + j] = tortoise[i][j];
}
}
Print();
Sleep(20);
}
void Begin()
{
system("color F0");
gotoxy(6, 28);
printf("抵制不良游戏,拒绝盗版游戏。");
gotoxy(8, 28);
printf("注意自我保护,谨防受骗上当。");
gotoxy(10, 28);
printf("适度游戏益脑,沉迷游戏伤身。");
gotoxy(12, 28);
printf("合理安排时间,享受健康生活。");
Sleep(2000);
system("cls");
system("color e9");
gotoxy(2, 32);
printf("雷霆战机");
Sleep(500);
gotoxy(5, 28);
setcolor(14, 0);
printf("请把界面调成最大化!");
Sleep(500);
gotoxy(10, 50);
printf("制作人:圣诞老头");
Sleep(1000);
gotoxy(22, 29);
system("pause");
system("cls");
}
void gotoxy(int x, int y)
{
HANDLE hout;
COORD cor;
hout = GetStdHandle(STD_OUTPUT_HANDLE);
cor.X = y;
cor.Y = x;
SetConsoleCursorPosition(hout, cor);
}
void setcolor(int BackColor, int ForeColor)
{
HANDLE winHandle; //句柄
winHandle = GetStdHandle(STD_OUTPUT_HANDLE);
//设置文字颜色
SetConsoleTextAttribute(winHandle, ForeColor + BackColor * 0x10);
// 0-黑 1-蓝 2-绿 3-浅绿 4-红 5-紫 6-黄 7-白 8-灰 9-淡蓝
//10-淡绿 11-淡浅绿 12-淡红 13-淡紫 14-淡黄 15-亮白
}