您好,登錄后才能下訂單哦!
本文實例為大家分享了C語言自定義軍旗游戲的具體代碼,供大家參考,具體內容如下
#include <graphics.h> #include <time.h> #define CHESIZE 40 // 棋盤尺寸,不能隨意調整 #define RESETX 170 #define RESETY 350 // 重置原點 typedef enum // 要用到的棋子ID { si, jun, shi, lv, tuan, ying, lian, pai, ban, gong, fei, chao, zha, qi, lei, bian, xian, sheng, shen }CHESSID; typedef enum // 攻擊類型 { comatt, preatt, noatt }ATTSTYLE; typedef enum // 當前游戲方和棋子所屬方 { blue, red, white }TEAM; typedef enum // 選中與未選中 { alchoose, unchoose }CHOOSESTATE; typedef enum // 區域狀態 { unknow, empty, exist }STATE; typedef struct // 坐標 { int x; int y; }COOR; typedef struct // 棋子 { CHESSID id; // 棋子的ID int power; // 棋子的等級 TEAM team; // 所屬方 char *image; // 該棋子的圖片,考慮到運行問題,本程序用字代替 int scoopc; // 工兵是挖到的地雷數 }CHESS; typedef struct // 區域 { COOR crdld; // 區域的左下坐標 CHESS chess; // 區域的棋子 STATE state; // 區域狀態 }AREA; typedef struct // 用戶的選擇信息 { int i; int j; CHOOSESTATE state; // 選擇狀態 }CHOOSE; IMAGE image; AREA area[6][6]; // 定義棋盤大小 CHESS datachess[19]; // 幾種基本棋子類型 CHOOSE choose; // 用戶選擇信息 MOUSEMSG mmsg; // 鼠標信息 TEAM user; // 執棋方 int lockchessboard = 0; // 是否鎖定棋盤 int i; // 當前鼠標所在區域的坐標 int j; char *str[]={"工","班","排","連","營","團","旅","師","軍","司","棋","炸","變","雷","飛","超","升","神","仙"}; void init(); void initchessbute(); // 給初始化棋子基本參數 void initvalue(); void drawboard(); // 畫棋盤 void randomarr(int *); // 實現棋的隨機排列 void judge(); void getpreij(); // 獲得當前鼠標所在區域坐標 int checkij(); // 檢查當鼠標所在區域 void open(); // 打開所在區域 int whemove(); // 判斷是否能移動 void move(); // 移動 int judgeunknow(); // 檢測當前未翻開棋子數 ATTSTYLE wheattack(); // 判斷是否能攻擊 void kill(); // 殺死當前選擇的棋 void killself(); // 自殺 void perishtogether(); // 同歸于盡 void getteam(); // 用作改變棋子類型時,對棋子所屬方賦值 void userchange(); // 交換執棋方 void judgebunko(); // 判斷輸贏 void choosearea(); // 選定區域 void cancelchoose(); // 取消選定 void change(); // 變身 void bluewin(); // 藍方勝利 void redwin(); // 紅方勝利 void gamehelp(); // 規則說明 void quit(); // 退出游戲 void peace(); // 和棋 void surrender(); // 投降 void resetchessboard(); // 重置 // 下面幾個函數為判斷棋子的攻擊類型 ATTSTYLE judgegong(); // 判斷工兵 ATTSTYLE judgecom(); // 判普通人物 ATTSTYLE judgezha(); // 判斷炸彈 void main() // 主函數 { init(); while (true) { mmsg = GetMouseMsg(); getpreij(); if (mmsg.uMsg == WM_LBUTTONDOWN) //單擊左鍵 { judge(); } else if (mmsg.uMsg == WM_RBUTTONDOWN && choose.state==alchoose) //單擊右鍵 { cancelchoose(); } else if (mmsg.uMsg == WM_MBUTTONDOWN && choose.state == alchoose && area[choose.i][choose.j].chess.id != zha) //單擊中鍵 { killself(); cancelchoose(); userchange(); judgebunko(); } } } void init() { initgraph(640, 480); setorigin(RESETX, RESETY); // 重置原點 setaspectratio(1, -1); // 把 y 軸上方設為正半軸 drawboard(); initvalue(); } void drawboard() // 畫棋盤 { int i1; setlinecolor(WHITE); for (i1=0; i1<7; i1++) { line(i1*CHESIZE, 0, i1*CHESIZE, CHESIZE*6); } for (i1=0; i1<7; i1++) { line(0, i1*CHESIZE, CHESIZE*6, i1*CHESIZE); } setlinecolor(WHITE); setfillcolor(RED); rectangle(-10, -10, CHESIZE*6+10, CHESIZE*6+10); floodfill(-1, -1, WHITE); rectangle(7*CHESIZE, CHESIZE, 9*CHESIZE, 6*CHESIZE); line(7*CHESIZE, 5*CHESIZE, 9*CHESIZE, 5*CHESIZE); line(7*CHESIZE, 4*CHESIZE, 9*CHESIZE, 4*CHESIZE); line(7*CHESIZE, 3*CHESIZE, 9*CHESIZE, 3*CHESIZE); line(7*CHESIZE, 2*CHESIZE, 9*CHESIZE, 2*CHESIZE); setaspectratio(1, 1); settextstyle(35, 18, "黑體"); settextcolor(RED); outtextxy(7*CHESIZE+2, -6*CHESIZE+2, "幫助"); settextcolor(BROWN); outtextxy(7*CHESIZE+2, -5*CHESIZE+2, "投降"); settextcolor(GREEN); outtextxy(7*CHESIZE+2, -4*CHESIZE+2, "和棋"); settextcolor(YELLOW); outtextxy(7*CHESIZE+2, -3*CHESIZE+2, "重置"); settextcolor(CYAN); outtextxy(7*CHESIZE+2, -2*CHESIZE+2, "退出"); settextcolor(LIGHTMAGENTA); settextstyle(50, 20, "黑體"); outtextxy(CHESIZE, -CHESIZE*8, "兩國軍旗"); setaspectratio(1, -1); } void initchessbute() // 設置棋子基本參數 { datachess[0].id = gong; datachess[0].power = 1; datachess[0].image = str[0]; datachess[0].scoopc = 0; datachess[1].id = ban; datachess[1].power = 2; datachess[1].image = str[1]; datachess[1].scoopc = 0; datachess[2].id = pai; datachess[2].power = 3; datachess[2].image = str[2]; datachess[2].scoopc = 0; datachess[3].id = lian; datachess[3].power = 4; datachess[3].image = str[3]; datachess[3].scoopc = 0; datachess[4].id = ying; datachess[4].power = 5; datachess[4].image = str[4]; datachess[4].scoopc = 0; datachess[5].id = tuan; datachess[5].power = 6; datachess[5].image = str[5]; datachess[5].scoopc = 0; datachess[6].id = lv; datachess[6].power = 7; datachess[6].image = str[6]; datachess[6].scoopc = 0; datachess[7].id = shi; datachess[7].power = 8; datachess[7].image = str[7]; datachess[7].scoopc = 0; datachess[8].id = jun; datachess[8].power = 9; datachess[8].image = str[8]; datachess[8].scoopc = 0; datachess[9].id = si; datachess[9].power = 10; datachess[9].image = str[9]; datachess[9].scoopc = 0; datachess[10].id = qi; datachess[10].power = 100; datachess[10].image = str[10]; datachess[10].scoopc = 0; datachess[11].id = zha; datachess[11].power = 99; datachess[11].image = str[11]; datachess[11].scoopc = 0; datachess[12].id = bian; datachess[12].power = 0; datachess[12].image = str[12]; datachess[12].scoopc = 0; datachess[13].id = lei; datachess[13].power = 98; datachess[13].image = str[13]; datachess[13].scoopc = 0; datachess[14].id = fei; datachess[14].power = 9; datachess[14].image = str[14]; datachess[14].scoopc = 0; datachess[15].id = chao; datachess[15].power = 11; datachess[15].image = str[15]; datachess[15].scoopc = 0; datachess[16].id = sheng; datachess[16].power = 10; datachess[16].image = str[16]; datachess[16].scoopc = 0; datachess[17].id = shen; datachess[17].power = 11; datachess[17].image = str[17]; datachess[17].scoopc = 0; datachess[18].id = xian; datachess[18].power = 11; datachess[18].image = str[18]; datachess[18].scoopc = 0; } void initvalue() // 初始化值 { CHESS chess[36]; int random[36]; int count; int i1, j1; initchessbute(); randomarr(random); for (i1=0; i1<=11; i1++) { chess[i1] = datachess[i1]; chess[i1].team = red; } chess[i1] = datachess[11]; chess[i1].team = red; chess[i1+1] = datachess[0]; chess[i1+1].team = red; for (i1=0; i1<=11; i1++) { chess[i1+14] = datachess[i1]; chess[i1+14].team = blue; } chess[i1+14] = datachess[11]; chess[i1+14].team = blue; chess[i1+15] = datachess[0]; chess[i1+15].team = blue; for (i1=0; i1<4; i1++) { chess[i1+28] = datachess[12]; chess[i1+28].team = white; chess[i1+32] = datachess[13]; chess[i1+32].team = white; } setfillcolor(YELLOW); for (count=0, i1=0; i1<6; i1++) { for (j1=0; j1<6; j1++, count++) { area[i1][j1].chess = chess[random[count]]; area[i1][j1].crdld.x = i1 * CHESIZE + 1; area[i1][j1].crdld.y = j1 * CHESIZE + 1; area[i1][j1].state = unknow; floodfill(area[i1][j1].crdld.x, area[i1][j1].crdld.y, WHITE); } } user = red; choose.state = unchoose; } void randomarr(int random[]) // 得到0~36數字的隨機排列 { int i1, j1; int flag = 0; srand(time(NULL)); random[0] = rand() % 36 ; for (i1=1; i1<36; i1++) { while (1) { random[i1] = rand() % 36 ; for (j1=0; j1<i1; j1++) { if (random[j1] == random[i1]) { flag = 1; break; } } if (flag) { flag = 0; } else { break; } } } } void judge() // 判斷當前要進行的操作 { ATTSTYLE attstyle; // 攻擊類型 getpreij(); if (checkij()) { if (area[i][j].state==unknow && choose.state==unchoose) // 打開 { open(); userchange(); } else if(area[i][j].state == empty) { if (choose.state == alchoose) // 移動 { if (whemove()) { move(); cancelchoose(); userchange(); } } } else { if (choose.state == unchoose) { if (area[i][j].chess.team==user && area[i][j].chess.id!=qi) //選定 { choosearea(); } } else { if (area[i][j].state!=unknow) // 攻擊 { attstyle = wheattack(); if (attstyle == comatt) { kill(); cancelchoose(); userchange(); } else if (attstyle == preatt) { perishtogether(); cancelchoose(); userchange(); } else { ; } } } } if (!judgeunknow()) // 在所有棋子都翻開的情況下判斷輸贏 { judgebunko(); } } } int judgeunknow() { int i1, i2; int num = 0; for (i1=0; i1<6; i1++) { for (i2=0; i2<6; i2++) { if (area[i1][i2].state == unknow) { num++; } } } return num; } // 選擇區域 void choosearea() { choose.i = i; choose.j = j; choose.state = alchoose; setlinecolor(GREEN); rectangle(choose.i*CHESIZE, choose.j*CHESIZE, choose.i*CHESIZE+CHESIZE, choose.j*CHESIZE+CHESIZE); } // 取消選定 void cancelchoose() { setlinecolor(WHITE); rectangle(choose.i*CHESIZE, choose.j*CHESIZE, choose.i*CHESIZE+CHESIZE, choose.j*CHESIZE+CHESIZE); choose.state = unchoose; } // 當前鼠標所在區域 void getpreij() { i = (mmsg.x-RESETX) / CHESIZE; j = -(mmsg.y-RESETY) / CHESIZE; } // 檢查鼠標是否在有效區域內 int checkij() { if ((i==7 || i==8) && j==5) { gamehelp(); return 0; } else if ((i==7 || i==8) && j==4) { if (!lockchessboard) { surrender(); } return 0; } else if ((i==7 || i==8) && j==3) { if (!lockchessboard) { peace(); } return 0; } else if ((i==7 || i==8) && j==2) { resetchessboard(); lockchessboard = 0; return 0; } else if ((i==7 || i==8) && j==1) { quit(); return 0; } else { if (!lockchessboard) { if ((i>=0 && i<=5 && j>=0 && j<=5 && (mmsg.x-RESETX)>0 && -(mmsg.y-RESETY)>0)) { return 1; } else { return 0; } } else { return 0; } } } // 打開操作 void open() { setfillcolor(BLACK); floodfill(area[i][j].crdld.x, area[i][j].crdld.y, WHITE); setaspectratio(1, 1); if (area[i][j].chess.team == blue) { settextcolor(BLUE); } else if (area[i][j].chess.team == red) { settextcolor(RED); } else { settextcolor(MAGENTA); } settextstyle(35, 18, "黑體"); outtextxy(area[i][j].crdld.x, -area[i][j].crdld.y-CHESIZE+2, area[i][j].chess.image); area[i][j].state = exist; setaspectratio(1, -1); } // 判斷是否能移動 int whemove() { if (area[choose.i][choose.j].chess.id==fei || area[choose.i][choose.j].chess.id==sheng || area[choose.i][choose.j].chess.id==shen) { if (choose.i==i && abs(choose.j-j)<=5 || choose.j==j && abs(choose.i-i)<=5) { return 1; } else { return 0; } } else if (area[choose.i][choose.j].chess.id == xian) { return 1; } else { if (choose.i==i && abs(choose.j-j)==1 || choose.j==j && abs(choose.i-i)==1) { return 1; } else { return 0; } } } // 移動 void move() { setfillcolor(BLACK); floodfill(area[choose.i][choose.j].crdld.x, area[choose.i][choose.j].crdld.y, GREEN); setaspectratio(1, 1); if (area[choose.i][choose.j].chess.id==gong && area[choose.i][choose.j].chess.scoopc>0) { if (area[choose.i][choose.j].chess.team == blue) { settextcolor(LIGHTBLUE); } else { settextcolor(LIGHTRED); } } else { if (user == blue) { settextcolor(BLUE); } else { settextcolor(RED); } } settextstyle(35, 18, "黑體"); outtextxy(area[i][j].crdld.x, -area[i][j].crdld.y-CHESIZE+2, area[choose.i][choose.j].chess.image); area[choose.i][choose.j].state = empty; area[i][j].state = exist; area[i][j].chess = area[choose.i][choose.j].chess; setaspectratio(1, -1); } // 判斷是否能攻擊,并返回攻擊類型 ATTSTYLE wheattack() { if (whemove()) { if (area[choose.i][choose.j].chess.id == gong) { return judgegong(); } else if (area[choose.i][choose.j].chess.id == zha) { return judgezha(); } else { return judgecom(); } } else { return noatt; } } // 判斷工兵 ATTSTYLE judgegong() { if (area[i][j].chess.team != white) { if (area[choose.i][choose.j].chess.team != area[i][j].chess.team) { if (area[i][j].chess.id==gong || area[i][j].chess.id==zha) { return preatt; } else if (area[i][j].chess.id == qi) { if (area[choose.i][choose.j].chess.scoopc == 0) { return noatt; } else if (area[choose.i][choose.j].chess.scoopc == 1) { area[choose.i][choose.j].chess = datachess[14]; getteam(); return comatt; } else if (area[choose.i][choose.j].chess.scoopc == 2) { area[choose.i][choose.j].chess = datachess[16]; getteam(); return comatt; } else if (area[choose.i][choose.j].chess.scoopc == 3) { area[choose.i][choose.j].chess = datachess[17]; getteam(); return comatt; } else { area[choose.i][choose.j].chess = datachess[18]; getteam(); return comatt; } } else { return noatt; } } else { return noatt; } } else { if (area[i][j].chess.id == lei) { area[choose.i][choose.j].chess.scoopc++; return comatt; } else { change(); return comatt; } } } // 判斷炸彈 ATTSTYLE judgezha() { if (area[choose.i][choose.j].chess.team != area[i][j].chess.team) { if (area[i][j].chess.id != qi) { return preatt; } else { return noatt; } } else { return noatt; } } // 判斷普通人物 ATTSTYLE judgecom() { if (area[i][j].chess.team != white) { if (area[choose.i][choose.j].chess.team != area[i][j].chess.team) { if (area[choose.i][choose.j].chess.power==area[i][j].chess.power || area[i][j].chess.id==zha) { return preatt; } else if (area[choose.i][choose.j].chess.power > area[i][j].chess.power) { return comatt; } else { return noatt; } } else { return noatt; } } else { if (area[i][j].chess.id == lei) { return noatt; } else { change(); return comatt; } } } // 變身 void change() { int x; x = rand() % 50; if (x == 6) { area[choose.i][choose.j].chess = datachess[15]; getteam(); } else { x = rand() % 4; if (x == 3) { x = rand() % 2; if (x == 0) { area[choose.i][choose.j].chess = datachess[7]; } else { area[choose.i][choose.j].chess = datachess[8]; } getteam(); } else { x = rand() % 6; area[choose.i][choose.j].chess = datachess[x]; getteam(); } } } // 對棋子所屬方賦值 void getteam() { if (user == blue) { area[choose.i][choose.j].chess.team = blue; } else { area[choose.i][choose.j].chess.team = red; } } // 殺死對方 void kill() { move(); } // 自殺 void killself() { setfillcolor(BLACK); floodfill(area[choose.i][choose.j].crdld.x, area[choose.i][choose.j].crdld.y, GREEN); area[choose.i][choose.j].state = empty; } // 同歸于盡 void perishtogether() { setfillcolor(BLACK); cancelchoose(); floodfill(area[choose.i][choose.j].crdld.x, area[choose.i][choose.j].crdld.y, WHITE); floodfill(area[i][j].crdld.x, area[i][j].crdld.y, WHITE); area[choose.i][choose.j].state = empty; area[i][j].state = empty; } // 切換執棋方 void userchange() { if (user == blue) { user = red; setfillcolor(RED); floodfill(-1, -1, WHITE); } else { user = blue; setfillcolor(BLUE); floodfill(-1, -1, WHITE); } } // 判斷輸贏 void judgebunko() { int i1, j1; int num1 = 0, num2 = 0; for (i1=0; i1<6; i1++) { for (j1=0; j1<6; j1++) { if (area[i1][j1].state != empty) { if (area[i1][j1].chess.team==red && area[i1][j1].chess.id!=qi) { num1++; } else if(area[i1][j1].chess.team==blue && area[i1][j1].chess.id!=qi) { num2++; } } } } if (num1==0 && num2!=0) { bluewin(); } if (num2==0 && num1!=0) { redwin(); } if (num1==0 && num2==0) { peace(); } } // 藍方勝 void bluewin() { setaspectratio(1, 1); settextcolor(BLUE); settextstyle(50, 20, "黑體"); outtextxy(CHESIZE, -CHESIZE*8, "藍方勝利"); setaspectratio(1, -1); setfillcolor(BLUE); floodfill(-1, -1, WHITE); lockchessboard = 1; //鎖定棋盤 } // 紅方勝 void redwin() { setaspectratio(1, 1); settextcolor(RED); settextstyle(50, 20, "黑體"); outtextxy(CHESIZE, -CHESIZE*8, "紅方勝利"); setaspectratio(1, -1); setfillcolor(RED); floodfill(-1, -1, WHITE); lockchessboard = 1; } // 和棋 void peace() { setaspectratio(1, 1); settextcolor(GREEN); settextstyle(50, 20, "黑體"); outtextxy(CHESIZE, -CHESIZE*8, "握手言和"); setaspectratio(1, -1); setfillcolor(GREEN); floodfill(-1, -1, WHITE); lockchessboard = 1; } // 投降 void surrender() { if (user == blue) { redwin(); } else { bluewin(); } } // 重置 void resetchessboard() { cleardevice(); init(); } // 游戲說明 void gamehelp() { getimage(&image, -10, -10, 500, 350); cleardevice(); setorigin(50, 0); setaspectratio(1, 1); settextcolor(RED); settextstyle(14, 0, "黑體"); outtextxy(-50, 0, "注:單擊鼠標左鍵回到游戲界面"); settextcolor(WHITE); settextstyle(24, 0, "黑體"); outtextxy(230, 5, "游戲說明"); settextstyle(12, 0, "宋體"); outtextxy(0, 35, "棋盤大小:6*6; 棋子總數:36; 敵對雙方:紅,藍"); outtextxy(0, 60, "棋子類別:紅棋(紅方操作,14個) 藍棋(藍方操作,14個) 紫棋(功能棋,8個)"); outtextxy(0, 85, "紅棋(藍棋)類型:司令,軍長,師長,旅長,團長,營長,連長,班長,軍旗,工兵*2,炸彈*2."); outtextxy(0, 100, "紫棋類型:地雷*4,變身棋*4. 注:'*'后面表示該棋的數量,沒注則只有一個"); outtextxy(0, 125, "規則說明:1.司令最大,工兵最小,大的吃小的,一樣就同歸于盡,"); outtextxy(textwidth("規則說明:1."), 140, "炸彈能炸紫棋和敵方除軍旗外所有的棋(炸彈也會消失)." ); outtextxy(textwidth("規則說明:"), 155, "2.工兵可挖地雷,挖完后可扛對方棋變身(挖的雷越多,變成的人物越厲害)."); outtextxy(textwidth("規則說明:"), 170, "3.人物棋可吃變,吃后能變成工兵~軍長中的一種,有一定幾率變成隱藏BOSS."); outtextxy(textwidth("規則說明:"), 185, "4.人物棋可自殺(算一次操作)."); outtextxy(textwidth("規則說明:"), 200, "5.執棋方進行完一次有效操作后,就換對方執棋(邊框顏色表當前執棋方)."); outtextxy(textwidth("規則說明:"), 215, "6.一方棋子(軍旗除外)全被消滅,就算輸; 同時全部沒有,則和棋."); outtextxy(0, 240, "執棋方能進行的操作:操作1:打開棋子(算一次操作)."); outtextxy(textwidth("執棋方能進行的操作:"), 255, "操作2:攻擊."); outtextxy(textwidth("執棋方能進行的操作:"), 270, "操作3:移動."); outtextxy(textwidth("執棋方能進行的操作:"), 285, "操作4:工兵(已挖雷)扛旗."); outtextxy(textwidth("執棋方能進行的操作:"), 300, "操作5:吃變身卡."); outtextxy(textwidth("執棋方能進行的操作:"), 315, "操作6:自殺."); outtextxy(0, 340, "實施游戲操作說明(鼠標操作):實施操作1:選擇要打開棋子所在的區域,單擊."); outtextxy(textwidth("實施游戲操作說明(鼠標操作):"), 355, "實施操作2~5:單擊選中主動方(棋子邊框會變綠)"); outtextxy(textwidth("實施游戲操作說明(鼠標操作):實施操作2~5:"), 370, "再單擊選中被動方."); outtextxy(textwidth("實施游戲操作說明(鼠標操作):"), 385, "實施操作6:選中己方棋子,單機鼠標的中鍵."); settextcolor(RED); outtextxy(textwidth("實施游戲操作說明(鼠標操作):"), 400,"注:要進行其他操作,必先撤銷當前選定(單擊右鍵撤銷)"); settextcolor(WHITE); setlinecolor(WHITE); line(-30, 420, 570, 420); outtextxy(0, 425, "人物棋等級一覽(等高殺等小):工1 班2 連3 營4 團5 旅6 師7"); outtextxy(textwidth("人物棋等級一覽(等高殺等小):"), 440, "軍8 飛8 司9 升9 神10 仙10"); outtextxy(0, 455, "注:'飛' '升' '神' '仙' 都為工兵挖雷后扛旗所變,'飛''升''神'能直線飛,'仙'能滿天飛"); while (true) { mmsg = GetMouseMsg(); if (mmsg.uMsg == WM_LBUTTONDOWN) { break; } } cleardevice(); setorigin(RESETX, RESETY); setaspectratio(1, -1); putimage(-10, -10, &image); } // 退出游戲 void quit() { closegraph(); }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。