打飞机游戏C语言重构函数封装(进阶版)

Novia ·
更新时间:2024-09-21
· 685 次阅读

代码重构

这样可以提供一个简化的游戏框架,实现代码复用

#include #include #include int x, y; int high, width; void startup() { high = 20; width = 30; x = high / 2; y = width / 2; } void show() { system("cls"); int i, j; for(i = 0; i < high; i++) //显示飞机 { for(j = 0; j < width; j++) { if((i == x) && (j == y)) printf("*"); else printf(" "); } printf("\n"); } } void updateWithoutInput() { } void updateWithInput() { char input; if(kbhit()) { input = getch(); if(input == 'a') y--; if(input == 'd') y++; if(input == 'w') x--; if(input == 's') x++; } } int main() { startup(); while(1) { show(); updateWithoutInput(); updateWithInput(); } return 0; } 让子弹飞

低配版飞机:只有一个机头*

新式子弹低配飞机

#include #include #include int x, y; int bullet_x, bullet_y; int high, width; void startup() { high = 20; width = 30; x = high / 2; y = width / 2; bullet_x = 0; bullet_y = y; //子弹药与飞机头同列 } void show() { system("cls"); int i, j; for(i = 0; i < high; i++) { for(j = 0; j -1) bullet_x--; } void updateWithInput() { char input; if(kbhit()) { input = getch(); if(input == 'a') y--; if(input == 'd') y++; if(input == 'w') x--; if(input == 's') x++; if(input == ' ') { bullet_x = x - 1; //让子弹飞 bullet_y = y; } } } int main() { startup(); while(1) { show(); updateWithoutInput(); updateWithInput(); } return 0; }

高配版飞机:

新式子弹高配飞机

#include #include #include int x, y; int bullet_x, bullet_y; int high, width; void startup() { high = 20; width = 30; x = high / 2; y = width / 2; bullet_x = 0; bullet_y = y; //子弹药与飞机头同列 } void show1() { int i, j; for(i = 0; i < x; i++) printf("\n"); for(j = 0; j < y; j++) printf(" "); printf("*\n"); for(j = 0; j < y - 2; j++) printf(" "); printf(" * *\n"); for(j = 0; j < y - 2; j++) printf(" "); printf("* * *\n"); for(j = 0; j < y - 6; j++) printf(" "); printf("* * * * * * *\n"); for(j = 0; j < y - 8; j++) printf(" "); printf("* * * * * * * * *\n"); for(j = 0; j < y- 1; j++) printf(" "); printf("* * \n"); for(j = 0; j < y - 9; j++) printf(" "); } void show() { int i, j; system("cls"); for(i = 0; i < x; i++) { for(j = 0; j < width; j++) { if((i == bullet_x) && (j == bullet_y)) printf("|"); else printf(" "); } printf("\n"); } for(j = 0; j < y; j++) printf(" "); printf("*\n"); for(j = 0; j < y - 2; j++) printf(" "); printf(" * *\n"); for(j = 0; j < y - 2; j++) printf(" "); printf("* * *\n"); for(j = 0; j < y - 6; j++) printf(" "); printf("* * * * * * *\n"); for(j = 0; j < y - 8; j++) printf(" "); printf("* * * * * * * * *\n"); for(j = 0; j < y- 1; j++) printf(" "); printf("* * \n"); for(j = 0; j -1) bullet_x--; } void updateWithInput() { char input; if(kbhit()) { input = getch(); if(input == 'a') y--; if(input == 'd') y++; if(input == 'w') x--; if(input == 's') x++; if(input == ' ') { bullet_x = x - 1; //让子弹飞 bullet_y = y; } } } int main() { startup(); show1(); while(1) { show(); updateWithoutInput(); updateWithInput(); } return 0; } 静止敌机

这里先把敌机粗略的设置为@

#include #include #include int x, y; int bullet_x, bullet_y; int enemy_x, enemy_y; //敌机位置 int high, width; void startup() { high = 20; width = 30; x = high / 2; y = width / 2; bullet_x = -1; bullet_y = y; enemy_x = 0; enemy_y = y; } void show() { system("cls"); int i, j; for(i = 0; i < high; i++) { for(j = 0; j -1) bullet_x--; } void updateWithInput() { char input; if(kbhit()) { input = getch(); if(input == 'a') y--; if(input == 'd') y++; if(input == 'w') x--; if(input == 's') x++; if(input == ' ') { bullet_x = x - 1; bullet_y = y; } } } int main() { startup(); while(1) { show(); updateWithoutInput(); updateWithInput(); } return 0; }

部分运行结果如下:
原创文章 115获赞 19访问量 8059 关注 私信 展开阅读全文
作者:源代码•宸



函数 重构 函数封装 C语言 封装

需要 登录 后方可回复, 如果你还没有账号请 注册新账号