C语言飞机大战源码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include
#include
#include
#include
#include
using namespace std;
/*=============== all the structures ===============*/
typedef struct Frame
{
COORD position[2];
int flag;
}Frame;
/*=============== all the functions ===============*/
void SetPos(COORD a)// set cursor
{
HANDLE out=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(out, a);
}
void SetPos(int i, int j)// set cursor
{
COORD pos={i, j};
SetPos(pos);
}
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); }
//把第y行,[x1, x2) 之间的坐标填充为ch
void drawRow(int y, int x1, int x2, char ch)
{
SetPos(x1,y);
for(int i = 0; i <= (x2-x1); i++)
cout< } 精选文库 //在a, b 纵坐标相同的前提下,把坐标[a, b] 之间填充为ch void drawRow(COORD a, COORD b, char ch) { if(a.Y == b.Y) drawRow(a.Y, a.X, b.X, ch); else { SetPos(0, 25); cout<<"error code 01:无法填充行,因为两个坐标的纵坐标(x)不相等"; system("pause"); } } //把第x列,[y1, y2] 之间的坐标填充为ch void drawCol(int x, int y1, int y2, char ch) { int y=y1; while(y!=y2+1) { SetPos(x, y); cout< y++; } } //在a, b 横坐标相同的前提下,把坐标[a, b] 之间填充为ch void drawCol(COORD a, COORD b, char ch) { if(a.X == b.X) drawCol(a.X, a.Y, b.Y, ch); else { SetPos(0, 25); cout<<"error code 02:无法填充列,因为两个坐标的横坐标(y)不相等"; system("pause"); } } //左上角坐标、右下角坐标、用row填充行、用col填充列 void drawFrame(COORD a, COORD b, char row, char col) { drawRow(a.Y, a.X+1, b.X-1, row); drawRow(b.Y, a.X+1, b.X-1, row); drawCol(a.X, a.Y+1, b.Y-1, col); 精选文库 drawCol(b.X, a.Y+1, b.Y-1, col); } void drawFrame(int x1, int y1, int x2, int y2, char row, char col) { COORD a={x1, y1}; COORD b={x2, y2}; drawFrame(a, b, row, col); } void drawFrame(Frame frame, char row, char col) { COORD a = frame.position[0]; COORD b = frame.position[1]; drawFrame(a, b, row, col); } void drawPlaying() { drawFrame(0, 0, 48, 24, '=', '|');// draw map frame; drawFrame(49, 0, 79, 4, '-', '|');// draw output frame drawFrame(49, 4, 79, 9, '-', '|');// draw score frame drawFrame(49, 9, 79, 20, '-', '|');// draw operate frame drawFrame(49, 20, 79, 24, '-', '|');// draw other message frame SetPos(52, 6); cout<<"得分:"; SetPos(52, 7); cout<<"称号:"; SetPos(52,10); cout<<"操作方式:"; SetPos(52,12); cout<<" a,s,d,w 控制战机移动。"; SetPos(52,14); cout<<" p 暂停游戏。"; SetPos(52,16); cout<<" e 退出游戏。"; } //在[a, b)之间产生一个随机整数 int random(int a, int b) { int c=(rand() % (a-b))+ a; return c; }