还是按照童老师在网易云授课写的代码,emmmm。
执行环境是VS2017.
现在功能:1,计分;2,按ESC暂停:3,绘制边框;4,根据分数控制速度,分数越高速度越快;5,飞机是复杂图形
未完成功能:1,提示用户运行飞机时候不要越界;2,在框子外右边写游戏规则;3,增加其他功能
以下为代码:
//消灭敌机加强版
#include<stdio.h>
#include<stdlib.h>
#include <windows.h> //清屏函数加上隐藏光标
#include<conio.h> //输入字符不显示
void Starup();
void show();;
void updateWithoutinput();
void updateWithinput();
void gotoxy(int x, int y);
void HideCursor();
int main()
{
Starup(); //数据初始化
while (1) //游戏循环执行
{
show(); //显示画面
updateWithoutinput(); //与用户输入无关的更新
updateWithinput(); //与用户输入有关的更新
}
system("pause");
return 0;
}
void gotoxy(int x, int y) //光标移动到(x,y)位置
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void HideCursor() //隐藏光标
{
CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
//函数外全局变量定义
int poison_x; //飞机位置
int poison_y;
int enemy_x; //敌机位置
int enemy_y;
int wideth; //游戏界面
int high;
int bullet_x; //子弹位置
int bullet_y;
int score; //计算分数
const int num_speed = 5;
void Starup() //数据初始化
{
wideth = 50; //游戏界面位置初始化
high = 20;
poison_x = wideth / 2; //飞机位置初始化在游戏界面中间
poison_y = high / 2;
enemy_x = wideth / 2; //敌机位置初始化在第一排最中间
enemy_y = 1;
bullet_x = poison_x; //初始化子弹位置,设置-1为了起初不显示子弹
bullet_y = -1;
score = 0;
HideCursor(); // 隐藏光标
}
void show() //显示画面
{
gotoxy(0, 0); // 光标移动到原点位置,以下重画清屏
int i, j;
for (i = 0; i < high; i++)
{
for (j = 0; j < wideth; j++)
{
if (i == 0 || j == 0 || i == (high - 1) || j == (wideth - 1)) //输出边框
printf("#");
else if ((i == poison_y) && (j == poison_x)) //输出飞机
printf("*");
else if ((i == poison_y + 1) && (j >= poison_x - 1)&(j <= poison_x + 1))
printf("*");
else if ((i == bullet_y) && (j == bullet_x)) //输出子弹
printf("|");
else if ((i == enemy_y) && (j == enemy_x)) //输出敌机
printf("+");
else
printf(" ");
}
printf("\n");
}
}
void updateWithoutinput() //与用户输入无关的更新
{
static int speed=0; //降低靶子速度
speed++;
bullet_y--; //发射子弹
if (score <= 20) //根据得分分段加速
{
if (speed < 4 * num_speed)
speed++;
if (speed == 4 * num_speed)
{
if (enemy_y < high)
enemy_y++; //靶子下降
else
{
enemy_y = 1;
enemy_x = rand() % wideth + 1; //靶子下降到底端的时候取随机数1-wideth重新生成
}
speed = 0;
}
}
else if (score <= 40)
{
if (speed < 2 * num_speed)
speed++;
if (speed == 2 * num_speed)
{
if (enemy_y < high)
enemy_y++; //靶子下降
else
{
enemy_y = 1;
enemy_x = rand() % wideth + 1; //靶子下降到底端的时候取随机数1-wideth重新生成
}
speed = 0;
}
}
else
{
if (speed < num_speed)
speed++;
if (speed == num_speed)
{
if (enemy_y < high)
enemy_y++; //靶子下降
else
{
enemy_y = 1;
enemy_x = rand() % wideth + 1; //靶子下降到底端的时候取随机数1-wideth重新生成
}
speed = 0;
}
}
if ((bullet_x == enemy_x) && (bullet_y == enemy_y))
{
score++;
enemy_y = -1;
enemy_x = rand() % wideth + 1;
}
printf("score:%d\n", score); //打印分数
}
void updateWithinput() //与用户输入有关的更新
{
char input;
if (_kbhit()) // 判断是否有输入
{
input = _getch(); // 根据用户的不同输入来移动,不必输入回车
if (input == 'a') //通过输入字符来控制飞机运行
poison_x--;
if (input == 'd')
poison_x++;
if (input == 'w')
poison_y--;
if (input == 's')
poison_y++;
if (input == ' ') //发射子弹
{
bullet_y = poison_y - 1;
bullet_x = poison_x;
}
if (input ==27)
system("pause");
}
}