小游戏"猫捉老鼠"

来源:互联网 发布:淘宝可靠长板店 编辑:程序博客网 时间:2024/04/30 02:40
#include<stdio.h>#include<windows.h>#include<conio.h>#include<stdlib.h>#include<time.h>void gotoxy(int x, int y);int main(){charb[25][40]={{"┏━━━━━━━━━━━━━━━━━━┓"},{"┃                                      "},{"┃ ■■■ ■■■■■■■■■■ ■■■ ┃"},{"┃ ■                              ■ ┃"},{"┃ ■ ■■■■■■ ■ ■■■■■■ ■ ┃"},{"┃    ■                        ■    ┃"},{"┃ ■ ■ ■■ ■■■■■■ ■■ ■ ■ ┃"},{"┃ ■    ■                  ■    ■ ┃"},{"┃ ■ ■    ■■ ■■■ ■■    ■ ■ ┃"},{"┃ ■ ■ ■ ■            ■ ■ ■ ■ ┃"},{"┃ ■ ■ ■    ■■■■■ ■ ■ ■ ■ ┃"},{"┃ ■ ■ ■ ■ ■      ■ ■ ■ ■ ■ ┃"},{"┃ ■ ■ ■ ■ ■ ■■ ■ ■ ■ ■ ■ ┃"},{"┃ ■ ■ ■ ■ ■      ■ ■ ■ ■ ■ ┃"},{"┃ ■ ■ ■ ■ ■■  ■■    ■ ■ ■ ┃"},{"┃ ■ ■ ■ ■            ■ ■ ■ ■ ┃"},{"┃ ■ ■    ■■ ■■■ ■■    ■ ■ ┃"},{"┃ ■    ■                  ■    ■ ┃"},{"┃ ■ ■ ■■ ■■■■■■ ■■ ■ ■ ┃"},{"┃    ■                        ■    ┃"},{"┃ ■ ■■■■■■ ■ ■■■■■■ ■ ┃"},{"┃ ■                              ■ ┃"},{"┃ ■■■ ■■■■■■■■■■ ■■■ ┃"},{"                                      ┃"},{"┗━━━━━━━━━━━━━━━━━━┛"},};int sx,sy,mx,my;int flag=0;int i, j, k, d, n=0, f=0;int t;char c;for(i=0; i<25; i++){   for(j=0; j<40; j++)   printf("%c", b[i][j]);   printf("\n");}mx=0, my=23;sx=20, sy=11;gotoxy(mx,my); printf("M");gotoxy(sx,sy); printf("@");while(1){k=_kbhit();    if(k){   c=_getch();   gotoxy(mx, my);   printf(" ");   if(c=='i'&&b[my-1][mx]==' ')   my--;       if(c=='k'&&b[my+1][mx]==' ')   my++;   if(c=='j'&&b[my][mx-1]==' ')   mx--;   if(c=='l'&&b[my][mx+1]==' ')   mx++;   gotoxy(mx, my);   printf("M");}//0right 1 down 2left 3 upgotoxy(sx,sy);printf(" ");switch(f)//f中存放移动方向,初始可为0{case 0:if(n!=2 && b[sy][sx+1]==' '){sx++;n=0;}if(rand()%2)f=3;elsef=1;break;case 1:if(n!=3 && b[sy-1][sx]==' '){sy--;n=1;}if(rand()%2)f=0;elsef=2;break;case 2:if(n!=0 && b[sy][sx-1]==' '){sx--;n=2;}if(rand()%2)f=3;elsef=1;break;case 3:if(n!=1 && b[sy+1][sx]==' '){sy++;n=3;}if(rand()%2)f=0;elsef=2;break;}gotoxy(sx,sy);printf("@");Sleep(20);if(mx==sx&&my==sy){    gotoxy(mx,my);printf("M");break;}if(sx==40&&sy==1){flag=1;   break;}}if(flag) gotoxy(40,1), printf("you lost");elsegotoxy(40, 1), printf("you win");}void gotoxy(int x, int y) //定位到第y行的第x列   {       int xx=0x0b;       HANDLE hOutput;       COORD loc;       loc.X=x;       loc.Y=y;       hOutput = GetStdHandle(STD_OUTPUT_HANDLE);       SetConsoleCursorPosition(hOutput, loc);       return;  } 


 

0 0
原创粉丝点击