C語言推箱子簡易版VC++6.0代碼

來源:互聯網
上載者:User
///////////////////////////////////////////////////////////////////////////////////////////////////遊戲中用0表示空地,1表示人物,2表示箱子,3表示目的地,6表示圍牆//本程式由VC++6.0開發,採用了easyx函數庫,請到http://www.easyx.cn/網站下載//作者:sambrown  from zzu  感謝網友:自然向日葵 /////////////////////////////////////////////////////////////////////////////////////////////////#include <stdio.h>#include <graphics.h>#include <stdlib.h>#include <conio.h>#include <iostream>int loop;int x, y;//人物座標int win(int a[][10]){for(int x1 = 0; x1 < 10; x1++)for(int y1 = 0; y1 < 10; y1++)if(a[x1][y1] == 2) return 0;//如果還有箱子返回假return 1;//如果沒有箱子返回真}void find( int a[][10] ){for(x = 0; x < 10; x++)for(y = 0; y < 10; y++)if(a[x][y] == 1 || a[x][y] == 4 ) return;}void clearman( int i, int j ){i *= 40;j *= 40;setcolor(BLACK);setfillstyle(BLACK);fillcircle(i, j, 19 );}void clearbox( int i, int j ){i *= 40;j *= 40;i -= 20;j -= 20;setfillstyle(BLACK);bar( i, j, i+39, j+39 );}void drawwall( int i, int j ){i *= 40;j *= 40;i -= 20;j -= 20;setfillstyle(RED);bar( i, j, i+40, j+40 );}void drawman( int i, int j ){i *= 40;j *= 40;setcolor(GREEN);setfillstyle(GREEN);fillcircle(i, j, 19);}void drawbox( int i, int j ){i *= 40;j *= 40;i -= 20;j -= 20;setfillstyle(BROWN);bar( i, j, i+39, j+39 );}void okbox( int i, int j ){i *= 40;j *= 40;i -= 20;j -= 20;setfillstyle(GREEN);bar( i, j, i+39, j+39 );}void drawdest( int i, int j ){i *= 40;j *= 40;setcolor(YELLOW);setfillstyle(YELLOW);fillcircle( i, j, 10 );}//////////////////////////////////////////////////////////////////////////////////print函數部分,列印出來的映像和實際的數組是相反的,////////////////////////////////////////////////////////////////////////////////void print( int b[][10] ){cleardevice();setorigin( 50, 50 );for( int i = 0; i < 10; i++ )for( int j = 0; j < 10; j++ ){if( b[i][j] == 6)//牆drawwall( i, j );if( b[i][j] == 1)drawman( i, j );//人if( b[i][j] == 2)drawbox( i, j );//箱子if( b[i][j] == 3)drawdest( i, j );//目的地//空地什麼也不列印}}void move( int b[][10], int x1, int y1 ){if( b[x+x1][y+y1] == 0 || b[x+x1][y+y1] == 3 )//如果前面是空地或目的{b[x][y] --;b[x+x1][y+y1] ++;clearman( x, y );drawman( x+x1, y+y1 );if( b[x][y] == 3 )//如果站在目的上移動,則走後要加上目的drawdest( x, y );}if((b[x + x1][y + y1] == 2 || b[x + x1][y + y1] == 5)//如果前面是空地上的箱子 “或” 目的上的箱子&& (b[x+x1+x1][y+y1+y1] == 0 || b[x+x1+x1][y+y1+y1] == 3))//“並且”  箱子前面是空地 “或”  目的{b[x][y] --;b[x+x1][y+y1]--;b[x+x1+x1][y+y1+y1] += 2;clearbox( x+x1, y+y1 );drawbox( x+x1+x1, y+y1+y1 );clearman( x, y );drawman( x+x1, y+y1 );if( b[x+x1+x1][y+y1+y1] == 5)okbox( x+x1+x1, y+y1+y1 );if( b[x][y] == 3 )//如果站在目的上移動,則走後要加上目的drawdest( x, y );}}void play( int b[][10] ){loop = 0;print(b);do{find(b);switch( getch() )//擷取鍵盤{case ' ':loop = 1;return ;//重新開始當前關 case  27:exit(0);break;//結束遊戲 case 'a':move(b, -1, 0 );break;case 'd':move(b, 1, 0 ); break;case 'w':move(b, 0, -1 ); break;case 's':move(b, 0, 1 );break;}}while(!win(b));//通過win函數判斷是否通過MessageBox(NULL, _T("恭喜你贏了"), _T("提醒"), MB_OK );}void initmap(){int b[10][10];int a[1][10][10]={//定義三維地圖數組{{6, 6, 6, 6, 6, 6, 6, 6, 6, 6},{6, 0, 0, 0, 0, 0, 0, 0, 0, 6},//此處地圖可以自訂,注意數組順序和列印出來的是相反的{6, 0, 0, 0, 2, 0, 3, 0, 0, 6},//切記數組一定要是10x10的,不能缺少元素,缺少的元素全部填補為牆{6, 0, 0, 0, 0, 0, 0, 0, 0, 6},{6, 0, 0, 0, 0, 0, 0, 0, 0, 6},{6, 0, 0, 0, 1, 0, 0, 0, 0, 6},{6, 0, 0, 0, 0, 0, 0, 0, 0, 6},{6, 0, 2, 0, 0, 0, 3, 0, 0, 6},{6, 0, 0, 0, 0, 0, 0, 0, 0, 6},{6, 6, 6, 6, 6, 6, 6, 6, 6, 6}}};for( int i = 0; i < 1; i++ ){do{loop = 0;for( int j = 0; j < 10; j++ )for( int k = 0; k < 10; k++ )b[j][k] = a[i][j][k];play(b);}while(loop);}}int main(){initgraph(640, 480);initmap();closegraph();return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.