本帖最后由 cad20020601 于 2014-3-25 18:15 编辑
- #include <stdio.h>
- /* 为了支持颜色 */
- #include <windows.h>
- // 地图区域
- const int MAP_X_LENGTH = 24;
- const int MAP_Y_LENGTH = 18;
- /* 声明函数 */
- void game();
- int deriction_down();
- int deriction_up();
- int deriction_left();
- int deriction_right();
- void role_up();
- void role_display();
- void gameinfo();
- int NPC(int npc);
- // 屏幕缓冲区
- int display[18][24] = {
- {3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
- {3,1,2,0,0,0,0,0,2,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0},
- {3,0,2,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
- {3,0,2,2,2,0,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
- {3,0,0,0,2,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
- {3,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
- {3,0,2,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
- {3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
- {3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
- {3,0,0,0,0,0,0,0,0,0,0,0,0,0,4,0,0,0,0,0,0,0,0,0},
- {3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
- {3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3},
- {3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3},
- {3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3},
- {3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3},
- {3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3},
- {3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3},
- {3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3}
- };
- // 描述位置结构体
- struct Position{
- int x;// x
- int y;// y
- };
- // 角色
- struct Role{
- struct Position oldP;// 旧位置
- struct Position newP;// 新位置
- void (* display)();
- };
- struct Role role = {
- {1, 1},
- {1, 1}
- };
- int main(){
- role.display = &role_display;
-
- while(1){
- game();
- }
- return 0;
- }
- void gameinfo(){
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY |
- FOREGROUND_RED | FOREGROUND_GREEN );
-
- printf("=======================\n");
- printf("=====欢迎来玩游戏======\n");
- printf("=======================\n");
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |
- FOREGROUND_RED | FOREGROUND_BLUE);
- }
- void game(){
- role.display();
- gameinfo();
- // 遍历map中的数据
- int i,j;
- for( i=0; i< MAP_Y_LENGTH ; i++){
- for( j=0; j< MAP_X_LENGTH ; j++){
- int temp = display<I>[j];
-
- if(temp == 1){
- printf("¤");
- }else if(temp == 2){
- printf("∏");
- }else if(temp == 3){
- printf("■");
- }else if(temp == 4){
- printf("N ");
- }else if(temp == 5){// 猪
- printf("Z ");
- }else{
- printf(" ");
- }
- }
- printf("\n");
- }
-
- char a = getch();
-
- // 记录旧位置
- role.oldP.y = role.newP.y;
- role.oldP.x = role.newP.x;
- switch(a){
- case 's': // 向下
- if(NPC(deriction_down()) == 0){
- role.newP.y = role.newP.y + 1;// 向下走
- }
- break;
- case 'd': // 向右
- if(NPC(deriction_right()) == 0){
- role.newP.x = role.newP.x + 1;
- }
- break;
- case 'a': // 向左
- if(NPC(deriction_left()) == 0){
- role.newP.x = role.newP.x - 1;
- }
- break;
- case 'w': // 向上
- if(NPC(deriction_up()) == 0){
- role.newP.y = role.newP.y - 1;
- }
- break;
- default: ;
- }
- system("cls");
- }
- char npc_info[4][80] = {
- "我:你就是传说中的NPC?\n",
- "NPC:你这是看不起NPC嘛!!!\n",
- "我:不不不,我是来接任务的!\n",
- "NPC:我有一头猪不见了,你帮我找回来吧!\n",
- };
- int NPC(int npc){
- printf("%d",npc);
- if(npc == 4){//遇到NPC
- system("cls");
- gameinfo();
- int i = 0,j=0;
- for(j=0;j < 4; j++){
- for(i=0; i < sizeof(npc_info[j]); i++){
- printf("%c",npc_info[j]<I>);
- _sleep(10);
- }
- printf("\n");
- }
- printf("是否接收任务?\n");
- printf("1. 是 2. 否\n");
- char key = getchar();
- if(key == '1'){
- display[10][9] = 5;
- return 0;
- } else{
- return 4;
- }
-
- return 4;
- }else if(npc == 5){//找到了猪
- system("cls");
- gameinfo();
- printf("恭喜你,任务完成!\n");
- system("pause");
- return 0;
- }
- return npc;
- }
- /* 向下 */
- int deriction_down(){
- return display[role.newP.y + 1][role.newP.x];
- }
- /* 向上 */
- int deriction_up(){
- return display[role.newP.y - 1][role.newP.x];
- }
- /* 向左 */
- int deriction_left(){
- return display[role.newP.y][role.newP.x - 1];
- }
- /* 向右 */
- int deriction_right(){
- return display[role.newP.y][role.newP.x + 1];
- }
- /*
- * 角色向上走
- */
- void role_display(){
- display[role.oldP.y][role.oldP.x] = 0;
- display[role.newP.y][role.newP.x] = 1;
- }
- </I></I>
复制代码
哼哼,我翻出来了。 |