c語言代碼游戲
DEV C++寫的
#include<windows.h>
#include<io.h>
#include<direct.h>
#include<string.h>
struct_finddata_tFile[100];
charcStr[100][260];
intfilenum=0,picnum=0;
HDChDC=NULL,mDC=NULL;
HBITMAPhBitmap=NULL;
intFirst_Run=0;
intxpos,ypos;
voidCALLBACKmovewindow(HWNDhWnd,UINTnMsg,UINTnTimerid,DWORDdwTime)
{
POINTpoi;
LPRECTinfo;
GetCursorPos(&poi);
MoveWindow(hWnd,(poi.x-xpos),(poi.y-ypos),371,342,FALSE);
return;
}
voidfindbmp()
{
longhandle;
handle=_findfirst("E:\DancingMiku\Picture\*.bmp",&File[0]);
if(handle==-1L)
{
MessageBox(NULL,"Thereisnofilewithbmpinthe「E:/DancingMiku」!!","Warn!",MB_OKCANCEL);
exit(0);
}
else
{
picnum++;
while(_findnext(handle,&File[picnum])==0)
picnum++;
}
inti;
picnum--;
for(i=0;i<=picnum;i++)
{
sprintf(cStr[i],"E:\DancingMiku\Picture\%s",File[i].name);
}
}
voidCALLBACKTimer(HWNDhWnd,UINTnMsg,UINTnTimerid,DWORDdwTime)
{
hBitmap=LoadImage(NULL,cStr[filenum%picnum],IMAGE_BITMAP,204,200,LR_LOADFROMFILE);
SelectObject(mDC,hBitmap);
BitBlt(hDC,0,0,204,200,mDC,0,0,SRCCOPY);
DeleteObject(hBitmap);
//DeleteDC(mDC);
//DeleteDC(hDC);
filenum++;
return;
}
voidCALLBACKTimer2(HWNDhWnd,UINTnMsg,UINTnTimerid,DWORDdwTime)
{
KillTimer(hWnd,1);
SetTimer(hWnd,1,30,Timer);
}
LRESULTCALLBACKWinProc(HWNDhWnd,UINTMsg,WPARAMwParam,LPARAMlParam)
{
switch(Msg)
{
caseWM_RBUTTONUP:
{
if(First_Run==0)
{
First_Run=1;
MessageBox(NULL,"這是一個測試的軟體,你的點擊意味著某些事情即將發生,至於是什麼事情,哼哼...","提醒",MB_OKCANCEL);
}
break;
}
caseWM_LBUTTONDOWN:
{
POINTtempos;
GetCursorPos(&tempos);
ScreenToClient(hWnd,&tempos);
xpos=tempos.x;
ypos=tempos.y;
SetTimer(hWnd,3,0,movewindow);
break;
}
caseWM_LBUTTONUP:
{
KillTimer(hWnd,3);
break;
}
}
returnDefWindowProc(hWnd,Msg,wParam,lParam);
}
intWINAPIWinMain(HINSTANCEhInstance,
HINSTANCEhPreInstance,
LPSTRIpCmdLine,
intnShowCmd)
{
char*cName="DancingMiku";
WNDCLASSEXmywindow;
HWNDhWnd;
MSGMsg;
HMENUhMenu;
mywindow.cbClsExtra=0;
mywindow.cbWndExtra=0;
mywindow.cbSize=sizeof(WNDCLASSEX);
mywindow.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
mywindow.hCursor=LoadImage(NULL,"E:\DancingMiku\Pointer\Arrow.ani",IMAGE_CURSOR,0,0,LR_LOADFROMFILE);
mywindow.hIcon=LoadImage(NULL,"E:\DancingMiku\Icon\01.ico",IMAGE_ICON,0,0,LR_LOADFROMFILE);
mywindow.hIconSm=LoadImage(NULL,"E:\DancingMiku\Icon\01.ico",IMAGE_ICON,0,0,LR_LOADFROMFILE);
mywindow.hInstance=hInstance;
mywindow.lpfnWndProc=WinProc;
mywindow.lpszClassName=(LPSTR)cName;
mywindow.lpszMenuName=NULL;
mywindow.style=CS_HREDRAW;
RegisterClassEx(&mywindow);
// hMenu=CreatePopupMenu();
// AppendMenu(hMenu,MF_ENABLED,MF_STRING,"New");
intheight,width;
height=GetSystemMetrics(SM_CYSCREEN);
width=GetSystemMetrics(SM_CXSCREEN);
hWnd=CreateWindowEx((WS_EX_TOPMOST|WS_EX_TOOLWINDOW|WS_EX_LAYERED),cName,"DancingMiku",(WS_SIZEBOX|WS_THICKFRAME),(width-50),100,371,324,NULL,NULL,hInstance,NULL);
if(hWnd==NULL)
{
MessageBox(hWnd,"Thereisaerror!!","warn",MB_OKCANCEL);
return0;
}
//SetLayeredWindowAttributes(hWnd,RGB(165,201,247),210,LWA_COLORKEY);
SetLayeredWindowAttributes(hWnd,RGB(255,255,255),200,(LWA_ALPHA|LWA_COLORKEY));
SetWindowLong(hWnd,GWL_STYLE,GetWindowLong(hWnd,GWL_STYLE)&(~(WS_CAPTION|WS_SIZEBOX)));
ShowWindow(hWnd,nShowCmd);
//下面是搜索文件的
if(_access("E:\DancingMiku",0)==-1)
{
intresult;
result=MessageBox(hWnd,"Thereisaerror!ShouldIcreatefilesformyself?","warn",MB_YESNO);
switch(result)
{
caseIDYES:
{
_mkdir("E:\DancingMiku\");
_mkdir("E:\DancingMiku\Picture\");
_mkdir("E:\DancingMiku\Pointer\");
_mkdir("E:\DancingMiku\Icon\");
break;
}
caseIDNO:
{
MessageBox(hWnd,"Nofilemeansnoruning!","Warn",MB_OK);
return0;
}
}
}
elsefindbmp();
if(hDC==NULL)
hDC=GetDC(hWnd);
if(mDC==NULL)
mDC=CreateCompatibleDC(hDC);
SetTimer(hWnd,2,1000,Timer2);
UpdateWindow(hWnd);
while(GetMessage(&Msg,NULL,0,0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
returnMsg.message;
}
Ⅱ 誰有能在vc++6.0上運行的c語言小游戲代碼
最基礎的貪吃蛇的代碼
#include<stdio.h>
#include<windows.h>//基本型態定義。支援型態定義函數。使用者界面函數 圖形裝置界面函數。
#include<conio.h> //用戶通過按鍵盤產生的對應操作 (控制台)
#include<stdlib.h>
#include<time.h> //日期和時間頭文件
#define LEN 30
#define WID 25
int Snake[LEN][WID] = {0}; //數組的元素代表蛇的各個部位
char Sna_Hea_Dir = 'a';//記錄蛇頭的移動方向
int Sna_Hea_X, Sna_Hea_Y;//記錄蛇頭的位置
int Snake_Len = 3;//記錄蛇的長度
clock_t Now_Time;//記錄當前時間,以便自動移動
int Wait_Time ;//記錄自動移動的時間間隔
int Eat_Apple = 1;//吃到蘋果表示為1
int Level ;
int All_Score = -1;
int Apple_Num = -1;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); //獲取標准輸出的句柄 <windows.h>
//句柄 :標志應用程序中的不同對象和同類對象中的不同的實例 方便操控,
void gotoxy(int x, int y)//設置游標位置
{
COORD pos = {x,y}; //定義一個字元在控制台屏幕上的坐標POS
SetConsoleCursorPosition(hConsole, pos); //定位游標位置的函數<windows.h>
}
void Hide_Cursor()//隱藏游標 固定函數
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(hConsole, &cursor_info);
}
void SetColor(int color)//設置顏色
{
SetConsoleTextAttribute(hConsole, color);
//是API設置字體顏色和背景色的函數 格式:SetConsoleTextAttribute(句柄,顏色);
}
void Print_Snake()//列印蛇頭和蛇的脖子和蛇尾
{
int iy, ix, color;
for(iy = 0; iy < WID; ++iy)
for(ix = 0; ix < LEN; ++ix)
{
if(Snake[ix][iy] == 1)//蛇頭
{
SetColor(0xf); //oxf代表分配的內存地址 setcolor:34行自定義設置顏色的函數
gotoxy(ix*2, iy);
printf("※");
}
if(Snake[ix][iy] == 2)//蛇的脖子
{
color = rand()%15 + 1; //rand()函數是產生隨機數的一個隨機函數。C語言里還有 srand()函數等。
//頭文件:stdlib.h
if(color == 14)
color -= rand() % 13 + 1; //變色
SetColor(color);
gotoxy(ix*2, iy);
printf("■");
}
if(Snake[ix][iy] == Snake_Len)
{
gotoxy(ix*2, iy);
SetColor(0xe);
printf("≈");
}
}
}
void Clear_Snake()//擦除貪吃蛇
{
int iy, ix;
for(iy = 0; iy < WID; ++iy)
for(ix = 0; ix < LEN; ++ix)
{
gotoxy(ix*2, iy);
if(Snake[ix][iy] == Snake_Len)
printf(" ");
}
}
void Rand_Apple()//隨機產生蘋果
{
int ix, iy;
do
{
ix = rand() % LEN;
iy = rand() % WID;
}while(Snake[ix][iy]);
Snake[ix][iy] = -1;
gotoxy(ix*2, iy);
printf("⊙");
Eat_Apple = 0;
}
void Game_Over()//蛇死掉了
{
gotoxy(30, 10);
printf("Game Over");
Sleep(3000);
system("pause > nul");
exit(0);
}
void Move_Snake()//讓蛇動起來
{
int ix, iy;
for(ix = 0; ix < LEN; ++ix)//先標記蛇頭
for(iy = 0; iy < WID; ++iy)
if(Snake[ix][iy] == 1)
{
switch(Sna_Hea_Dir)//根據新的蛇頭方向標志蛇頭
{
case 'w':
if(iy == 0)
Game_Over();
else
Sna_Hea_Y = iy - 1;
Sna_Hea_X = ix;
break;
case 's':
if(iy == (WID -1))
Game_Over();
else
Sna_Hea_Y = iy + 1;
Sna_Hea_X = ix;
break;
case 'a':
if(ix == 0)
Game_Over();
else
Sna_Hea_X = ix - 1;
Sna_Hea_Y = iy;
break;
case 'd':
if(ix == (LEN - 1))
Game_Over();
else
Sna_Hea_X = ix + 1;
Sna_Hea_Y = iy;
break;
default:
break;
}
}
if(Snake[Sna_Hea_X][Sna_Hea_Y]!=1&&Snake[Sna_Hea_X][Sna_Hea_Y]!=0&&Snake[Sna_Hea_X][Sna_Hea_Y]!=-1)
Game_Over();
if(Snake[Sna_Hea_X][Sna_Hea_Y] < 0)//吃到蘋果
{
++Snake_Len;
Eat_Apple = 1;
}
for(ix = 0; ix < LEN; ++ix)//處理蛇尾
for(iy = 0; iy < WID; ++iy)
{
if(Snake[ix][iy] > 0)
{
if(Snake[ix][iy] != Snake_Len)
Snake[ix][iy] += 1;
else
Snake[ix][iy] = 0;
}
}
Snake[Sna_Hea_X][Sna_Hea_Y] = 1;//處理蛇頭
}
void Get_Input()//控制蛇的移動方向
{
if(kbhit())
{
switch(getch())
{
case 87:
Sna_Hea_Dir = 'w';
break;
case 83:
Sna_Hea_Dir = 's';
break;
case 65:
Sna_Hea_Dir = 'a';
break;
case 68:
Sna_Hea_Dir = 'd';
break;
default:
break;
}
}
if(clock() - Now_Time >= Wait_Time)//蛇到時間自動行走
{
Clear_Snake();
Move_Snake();
Print_Snake();
Now_Time = clock();
}
}
void Init()//初始化
{
system("title 貪吃毛毛蛇");
system("mode con: cols=80 lines=25");
Hide_Cursor();
gotoxy(61, 4);
printf("You Score:");
gotoxy(61, 6);
printf("You Level:");
gotoxy(61, 8);
printf("The Lenght:");
gotoxy(61, 10);
printf("The Speed:");
gotoxy(61, 12);
printf("Apple Num:");
int i;
for(i = 0; i < Snake_Len; ++i)//生成蛇
Snake[10+i][15] = i+1;
int iy, ix;//列印蛇
for(iy = 0; iy < WID; ++iy)
for(ix = 0; ix < LEN; ++ix)
{
if(Snake[ix][iy])
{
SetColor(Snake[ix][iy]);
gotoxy(ix*2, iy);
printf("■");
}
}
}
void Pri_News()//列印信息
{
SetColor(0xe);
gotoxy(73,4);
All_Score += Level;
printf("%3d", All_Score);
gotoxy(73, 6);
printf("%3d", Level);
gotoxy(73, 8);
printf("%3d",Snake_Len);
gotoxy(73, 10);
printf("0.%3ds", Wait_Time/10);
gotoxy(73, 12);
printf("%d", Apple_Num);
}
void Lev_Sys()//等級系統
{
if(((Apple_Num-1) / 10) == Level)
{
++Level;
if(Wait_Time > 50)
Wait_Time -= 50;
else
if(Wait_Time > 10)
Wait_Time -= 10;
else
Wait_Time -= 1;
}
}
int main(void)
{
Init();
srand((unsigned)time(NULL));//設置隨機數的種子
Now_Time = clock();
int speed1=1000,speed2,a;
printf("\n");
printf("請輸入你想要的速度\n");
scanf("%d",&speed2);
Level=1;
Wait_Time=speed1-speed2;
printf("請輸入你想要的蘋果數\n");
scanf("%d",&a);
while(a--)
Rand_Apple();
while(1)
{
if(Eat_Apple)
{
++Apple_Num;
Rand_Apple();
Lev_Sys();
Pri_News();
}
Get_Input();
Sleep(10);
}
return 0;
}
Ⅲ 求用C語言編寫一個簡單游戲的代碼
太長了,發不上去,LZ給個郵箱什麼的唄,。
Ⅳ c語言小游戲代碼
「貪吃蛇」C代碼,在dev C++試驗通過(用4個方向鍵控制)
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <Windows.h>
#define W 78 //游戲框的寬,x軸
#define H 26 //游戲框的高,y軸
int dir=3; //方向變數,初值3表示向「左」
int Flag=0; //吃了食物的標志(1是0否)
int score=0; //玩家得分
struct food{ int x; //食物的x坐標
int y; //食物的y坐標
}fod; //結構體fod有2個成員
struct snake{ int len; //蛇身長
int speed; //移動速度
int x[100]; //蛇身某節x坐標
int y[100]; //蛇身某節y坐標
}snk; //結構體snk有4個成員
void gtxy( int x,int y) //控制游標移動的函數
{ COORD coord;
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void gtxy( int x,int y); //以下聲明要用到的幾個自編函數
void csh( ); //初始化界面
void keymove( ); //按鍵操作移動蛇
void putFod( ); //投放食物
int Over( ); //游戲結束(1是0否)
void Color(int a); //設定顯示顏色的函數
int main( ) //主函數
{ csh( );
while(1)
{ Sleep(snk.speed);
keymove( );
putFod( );
if(Over( ))
{ system(「cls」);
gtxy(W/2+1,H/2); printf(「游戲結束!T__T」);
gtxy(W/2+1,H/2+2); printf(「玩家總分:%d分」,score);
getch( );
break;
}
}
return 0;
}
void csh( ) //初始化界面
{ int i;
gtxy(0,0);
CONSOLE_CURSOR_INFO cursor_info={1,0}; //以下兩行是隱藏游標的設置
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
for(i=0;i<=W;i=i+2) //橫坐標要為偶數,因為這個要列印的字元佔2個位置
{Color(2); //設定列印顏色為綠色
gtxy(i,0); printf("■"); //列印上邊框
gtxy(i,H); printf("■"); //列印下邊框
}
for(i=1;i<H;i++)
{ gtxy(0,i); printf("■"); //列印左邊框
gtxy(W,i); printf("■"); //列印右邊框
}
while(1)
{ srand((unsigned)time(NULL)); //初始化隨機數發生器srand( )
fod.x=rand()%(W-4)+2; //隨機函數rand( )產生一個從0到比」(W-4)」小1的數再加2
fod.y=rand()%(H-2)+1; //隨機函數rand( )產生一個從0到比」(H-2)」小1的數再加1
if (fod.x%2==0) break; //fod.x是食物的橫坐標,要是2的倍數(為偶數)
}
Color(12); //設定列印顏色為淡紅
gtxy(fod.x,fod.y); printf("●"); //到食物坐標處列印初試食物
snk.len=3; //蛇身長初值為3節
snk.speed=350; //刷新蛇的時間,即移動速度初值為350毫秒
snk.x[0]=W/2+1; //蛇頭橫坐標要為偶數(因為W/2=39)
snk.y[0]=H/2; //蛇頭縱坐標
Color(9); //設定列印顏色為淡藍
gtxy(snk.x[0], snk.y[0]); printf("■"); //列印蛇頭
for(i=1;i<snk.len;i++)
{ snk.x[i]=snk.x[i-1]+2; snk.y[i]=snk.y[i-1];
gtxy(snk.x[i],snk.y[i]); printf("■"); //列印蛇身
}
Color(7, 0); //恢復默認的白字黑底
return;
}
void keymove( ) //按鍵操作移動蛇
{ int key;
if( kbhit( ) ) //如有按鍵輸入才執行下面操作
{ key=getch( );
if (key==224) //值為224表示按下了方向鍵,下面要再次獲取鍵值
{ key=getch( );
if(key==72&&dir!=2)dir=1; //72表示按下了向上方向鍵
if(key==80&&dir!=1)dir=2; //80為向下
if(key==75&&dir!=4)dir=3; //75為向左
if(key==77&&dir!=3)dir=4; //77為向右
}
if (key==32)
{ while(1) if((key=getch( ))==32) break; } //32為空格鍵,這兒用來暫停
}
if (Flag==0) //如沒吃食物,才執行下面操作擦掉蛇尾
{ gtxy(snk.x[snk.len-1],snk.y[snk.len-1]); printf(" "); }
int i;
for (i = snk.len - 1; i > 0; i--) //從蛇尾起每節存儲前一節坐標值(蛇頭除外)
{ snk.x[i]=snk.x[i-1]; snk.y[i]=snk.y[i-1]; }
switch (dir) //判斷蛇頭該往哪個方向移動,並獲取最新坐標值
{ case 1: snk.y[0]--; break; //dir=1要向上移動
case 2: snk.y[0]++; break; //dir=2要向下移動
case 3: snk.x[0]-=2; break; //dir=3要向左移動
case 4: snk.x[0]+=2; break; //dir=4要向右移動
}
Color(9);
gtxy(snk.x[0], snk.y[0]); printf("■"); //列印蛇頭
if (snk.x[0] == fod.x && snk.y[0] == fod.y) //如吃到食物則執行以下操作
{ printf("7"); snk.len++; score += 100; snk.speed -= 5; Flag = 1; } //7是響鈴
else Flag = 0; //沒吃到食物Flag的值為0
if(snk.speed<150) snk.speed= snk.speed+5; //作弊碼,不讓速度無限加快
}
void putFod( ) //投放食物
{ if (Flag == 1) //如吃到食物才執行以下操作,生成另一個食物
{ while (1)
{ int i,n= 1;
srand((unsigned)time(NULL)); //初始化隨機數發生器srand( )
fod.x = rand( ) % (W - 4) + 2; //產生在游戲框范圍內的一個x坐標值
fod.y = rand( ) % (H - 2) + 1; //產生在游戲框范圍內的一個y坐標值
for (i = 0; i < snk.len; i++) //隨機生成的食物不能在蛇的身體上
{ if (fod.x == snk.x[i] &&fod.y == snk.y[i]) { n= 0; break;} }
if (n && fod.x % 2 == 0) break; //n不為0且橫坐標為偶數,則食物坐標取值成功
}
Color(12); //設定字元為紅色
gtxy(fod.x, fod.y); printf("●"); //游標到取得的坐標處列印食物
}
return;
}
int Over( ) //判斷游戲是否結束的函數
{ int i;
Color(7);
gtxy(2,H+1); printf(「暫停鍵:space.」); //以下列印一些其它信息
gtxy(2,H+2); printf(「游戲得分:%d」,score);
if (snk.x[0] == 0 || snk.x[0] == W) return 1; //蛇頭觸碰左右邊界
if (snk.y[0] == 0 || snk.y[0] == H) return 1; //蛇頭觸碰上下邊界
for (i = 1; i < snk.len; i++)
{ if (snk.x[0] == snk.x[i] && snk.y[0] == snk.y[i]) return 1; } //蛇頭觸碰自身
return 0; //沒碰到邊界及自身時就返回0
}
void Color(int a) //設定顏色的函數
{ SetConsoleTextAttribute(GetStdHandle( STD_OUTPUT_HANDLE ),a ); }
Ⅳ C語言 游戲 代碼
「坑人的無限」(一):
#include<iostream>
#include<windows.h>
#include<ctime>
#include<cstdlib>
#include<conio.h>
using namespace std;
int a;
class Screen
{
private:
int n;
public:
Screen()
{
n=5;
}
void move1()//注意只是循環輸出各個數字,不能對循環輸出再進行循環(如果對循環輸出0123456789再進行循環,則move1就變成一個無限循環的函數,則下面的screen循環就進行不下去了)
{
for(int i=0;i<10;++i)
{
cout<<char(1)<<" ";
}
}
void move2()
{
char i;
for(i='a';i<='z';++i)
{
cout<<char(1)<<" ";
}
}
void screen()
{
int t;
while(!kbhit())
{
t=time(0)%(2*n);//如果是放在循環外面的話,time(0)的值就一直不變,放在循環裡面,一秒鍾進行一次判斷,一秒鍾進行一次循環
if(t<n)
move1();
else
move2();
}
}
};
int main(){
cout<<"歡迎來到「無限 」游戲"<<char(1)<<endl;
cout<<"下面會輸出無限個笑臉"<<char(1)<<endl;
cout<<"按'enter'取消"<<endl;
Sleep(4000);
Screen s;
s.screen();
cout<<endl<<"哈哈!!控制不住了吧!"<<char(1)<<endl;
cout<<"接下來會更讓你喪心病狂的!"<<char(1)<<endl;
cout<<"但是堅持過後必有彩蛋!!!!!!加油!!";
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
Sleep(10000);
for(int as=0;as<=50;as++){
for(int i=0;i<=100;i++){
for(int j=0;j<=10;j++){
cout<<char(2)<<" ";
}
cout<<endl;
}
for(int i=0;i<=100;i++){
for(int j=0;j<=10;j++){
cout<<char(1)<<" ";
}
cout<<endl;
}
}
cout<<"你居然堅持下來了!"<<char(1)<<" "<<char(2)<<endl<<"不可思議呀!"<<endl;
for(int i=0;i<=10;i++){
cout<<"-----------------------------------------------"<<endl;
}
cout<<"敬請期待!等待無限游戲(二)!";
return 0;
}
Ⅵ 求C語言小游戲源程序
簡易「推箱子」游戲C代碼:
#include <stdio.h>
#include <conio.h>
#include<stdlib.h>
#include<windows.h>
int m =0; //m代表第幾關
struct maps{short a[9][11]; };
struct maps map[5]={ 0,0,0,0,0,0,0,0,0,0,0, //共5關,每關9行
0,1,1,1,1,1,1,1,0,0,0,
0,1,0,0,0,0,0,1,1,1,0,
1,1,4,1,1,1,0,0,0,1,0, //0空地,1牆
1,5,0,0,4,0,0,4,0,1,0, //4是箱子,5是人
1,0,3,3,1,0,4,0,1,1,0, //3是目的地
1,1,3,3,1,0,0,0,1,0,0, //7是箱子在目的地(4+3)
0,1,1,1,1,1,1,1,1,0,0, //8是人在目的地(5+3)
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,1,1,1,1,0,0,0,0,0,
0,0,1,5,0,1,1,1,0,0,0,
0,0,1,0,4,0,0,1,0,0,0,
0,1,1,1,0,1,0,1,1,0,0,
0,1,3,1,0,1,0,0,1,0,0,
0,1,3,4,0,0,1,0,1,0,0,
0,1,3,0,0,0,4,0,1,0,0,
0,1,1,1,1,1,1,1,1,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,1,1,1,1,1,1,1,0,
0,0,1,1,0,0,1,0,5,1,0,
0,0,1,0,0,0,1,0,0,1,0,
0,0,1,4,0,4,0,4,0,1,0,
0,0,1,0,4,1,1,0,0,1,0,
1,1,1,0,4,0,1,0,1,1,0,
1,3,3,3,3,3,0,0,1,0,0,
1,1,1,1,1,1,1,1,1,0,0,
0,1,1,1,1,1,1,1,1,1,0,
0,1,0,0,1,1,0,0,0,1,0,
0,1,0,0,0,4,0,0,0,1,0,
0,1,4,0,1,1,1,0,4,1,0,
0,1,0,1,3,3,3,1,0,1,0,
1,1,0,1,3,3,3,1,0,1,1,
1,0,4,0,0,4,0,0,4,0,1,
1,0,0,0,0,0,1,0,5,0,1,
1,1,1,1,1,1,1,1,1,1,1,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,1,1,1,1,1,1,0,0,
0,1,1,1,0,0,0,0,1,0,0,
1,1,3,0,4,1,1,0,1,1,0,
1,3,3,4,0,4,0,0,5,1,0,
1,3,3,0,4,0,4,0,1,1,0,
1,1,1,1,1,1,0,0,1,0,0,
0,0,0,0,0,1,1,1,1,0,0,
0,0,0,0,0,0,0,0,0,0,0 };
void DrMap( ) //繪制地圖
{ CONSOLE_CURSOR_INFO cursor_info={1,0}; //隱藏游標的設置
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
printf(" 推箱子");
printf(" ");
for (int i = 0; i < 9; i++)
{for (int j = 0; j < 11; j++)
{switch (map[m].a[i][j])
{case 0: printf(" "); break;
case 1: printf("■"); break;
case 3: printf("◎");break;
case 4: printf("□"); break;
case 5: printf("♀"); break; //5是人
case 7: printf("□"); break; //4 + 3箱子在目的地中
case 8: printf("♀");break; // 5 + 3人在目的地中
}
}
printf(" ");
}
}
void gtxy(int x, int y) //控制游標位置的函數
{ COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void start( ) //開始游戲
{ int r, c; //存儲人的下標
for (int i = 0; i < 9; i++)
{ for (int j = 0; j < 11; j++)
{if (map[m].a[i][j] == 5||map[m].a[i][j]==8) { r = i; c = j; } } //i j 人的下標
}
char key;
key = getch( );
switch (key)
{case 'W':
case 'w':
case 72:
if (map[m]. a[r - 1][c] == 0|| map[m]. a [r - 1][c] == 3)
{ gtxy(2*c+8,r-1+3); printf("♀"); // gtxy(2*c+8,r-1+3)是到指定位置輸出字元
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r - 1][c] += 5; map[m]. a [r][c] -= 5; }
else if (map[m]. a [r - 1][c] == 4 || map[m]. a [r - 1][c] == 7)
{ if (map[m]. a [r - 2][c] == 0 || map[m]. a [r - 2][c] == 3)
{ gtxy(2*c+8,r-2+3); printf("□"); gtxy(2*c+8,r-1+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r - 2][c] += 4; map[m]. a [r - 1][c] += 1;
map[m]. a [r][c] -= 5; }
} break;
case 'S':
case 's':
case 80:
if (map[m]. a [r + 1][c] == 0 || map[m]. a [r + 1][c] == 3)
{ gtxy(2*c+8,r+1+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r + 1][c] += 5; map[m]. a [r][c] -= 5; }
else if (map[m]. a [r + 1][c] == 4 || map[m]. a [r+ 1][c] == 7)
{ if (map[m]. a [r + 2][c] == 0 || map[m]. a [r + 2][c] == 3)
{ gtxy(2*c+8,r+2+3); printf("□"); gtxy(2*c+8,r+1+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r + 2][c] += 4; map[m]. a [r + 1][c] += 1;
map[m]. a [r][c] -= 5; }
}break;
case 'A':
case 'a':
case 75:
if (map[m]. a [r ][c - 1] == 0 || map[m]. a [r ][c - 1] == 3)
{ gtxy(2*(c-1)+8,r+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r ][c - 1] += 5; map[m]. a [r][c] -= 5; }
else if (map[m]. a [r][c - 1] == 4 || map[m]. a [r][c - 1] == 7)
{if (map[m]. a [r ][c - 2] == 0 || map[m]. a [r ][c - 2] == 3)
{ gtxy(2*(c-2)+8,r+3); printf("□"); gtxy(2*(c-1)+8,r+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r ][c - 2] += 4; map[m]. a [r ][c - 1] += 1;
map[m]. a [r][c] -= 5; }
}break;
case 'D':
case 'd':
case 77:
if (map[m]. a [r][c + 1] == 0 || map[m]. a [r][c + 1] == 3)
{ gtxy(2*(c+1)+8,r+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8) {gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r][c + 1] += 5; map[m]. a [r][c] -= 5; }
else if (map[m]. a [r][c + 1] == 4 || map[m]. a [r][c + 1] == 7)
{ if (map[m]. a [r][c + 2] == 0 || map[m]. a [r][c + 2] == 3)
{ gtxy(2*(c+2)+8,r+3); printf("□"); gtxy(2*(c+1)+8,r+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r][c + 2] += 4; map[m]. a [r][c + 1] += 1;
map[m]. a [r][c] -= 5; }
}break;
}
}
int ifwan( ) //是否完成(1是0否)
{ if(m==0){if(map[m].a[5][2]==7&& map[m].a[5][3]==7&&
map[m].a[6][2]==7&& map[m].a[6][3]==7) return 1;}
if(m==1){if(map[m].a[5][2]==7&& map[m].a[6][2]==7&&
map[m].a[7][2]==7) return 1;}
if(m==2){if(map[m].a[7][1]==7&& map[m].a[7][2]==7&& map[m].a[7][3]==7&&
map[m].a[7][4]==7&& map[m].a[7][5]==7) return 1;}
if(m==3){if(map[m].a[4][4]==7&& map[m].a[4][5]==7&& map[m].a[4][6]==7&&
map[m].a[5][4]==7&& map[m].a[5][5]==7&& map[m].a[5][6]==7) return 1;}
if(m==4){if(map[m].a[3][2]==7&& map[m].a[4][1]==7&& map[m].a[4][2]==7&&
map[m].a[5][1]==7&& map[m].a[5][2]==7) return 1;}
return 0;
}
int main( ) //主函數
{ while (1)
{ system("cls");
DrMap( );
while (1)
{ start( );
if(ifwan()){printf("