c语言双人贪吃蛇-基于图形库实现
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                c语言双人贪吃蛇-基于图形库实现
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                
                            
                            
                             
/*
蛇蛇大作戰(zhàn)
作者:施瑞文
*/#include <conio.h>
#include <graphics.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")#define R 30      
#define fram_width 30    //寬度
#define fram_height 30   //高度
#define SIZE 16            //方格邊長(zhǎng)//玩家1
#define UP 'w' //72      上
#define DOWN 's' //80  下
#define LEFT 'a' //75   左
#define RIGHT 'd' //77  右
//玩家2
#define DOWN2 'k'    //72,80,75,77是方向鍵對(duì)應(yīng)的鍵值  下
#define LEFT2 'j'   //左
#define RIGHT2 'l'   //右
#define UP2 'i'   //上char ch = 'a';//用于記錄方向,傳統(tǒng)模式
char c = LEFT2;   //記錄玩家2方向
int m;     //全局變量,用于循環(huán)計(jì)數(shù)
int score = 0;    //玩家1分?jǐn)?shù)和傳統(tǒng)模式分?jǐn)?shù)
int score2 = 0;    //玩家2分?jǐn)?shù)
char maxScore[5] = "0";   //記錄傳統(tǒng)模式歷史最高分struct Food     //食物結(jié)構(gòu)體
{int x;     //食物的橫坐標(biāo)int y;     //食物的縱坐標(biāo)
}food;struct Snake {   //玩家1貪吃蛇int len;    //蛇的長(zhǎng)度int x[780];   //蛇的每一節(jié)的橫坐標(biāo)int y[780];   //蛇的每一節(jié)的縱坐標(biāo)int count;    //蛇吃到的食物數(shù)量int speed;    //蛇的速度
}snake;struct newSnake   //玩家2 
{int len;int x[780];int y[780];int count;int speed;
}new_snake;void initmap();   //畫邊框
void menu();      //菜單
void getfood();   //隨機(jī)產(chǎn)生食物
void chushihua();   //初始化蛇
void eatfood();     //判斷蛇是否吃到食物
int die();            //判斷蛇是否死亡
void move();     //遷移蛇的坐標(biāo),移動(dòng)蛇
void turn(char t);    //轉(zhuǎn)向
void print();           //打印蛇
void play();            //開始傳統(tǒng)模式游戲
void start();           //開始特效
int StringToInt(char a[], int n);   //將字符串轉(zhuǎn)化為整數(shù)
void wall();           //傳統(tǒng)模式的障礙物
void score_rule();   //顯示分?jǐn)?shù)和規(guī)則//雙人PK模式
void double_initmap();   //雙人PK地圖
void new_chushihua();   //初始化玩家2的蛇
void double_play();     //開始雙人PK模式游戲
void double_eatfood();   //判斷是否吃到食物
void double_turn(char t);  //轉(zhuǎn)向
void double_print();     //打印玩家2的蛇
void double_move1(); //雙人模式玩家1移動(dòng)
void double_move2();  //雙人模式玩家2移動(dòng)
void double_getfood();//雙人模式隨機(jī)產(chǎn)生食物
void double_score_rule();//雙人模式  顯示分?jǐn)?shù)和操作規(guī)則
int double_die();    //雙人模式判斷玩家1是否死亡
int double_die2();  //雙人模式判斷玩家2是否死亡
int win(int grade);   //判斷贏家void main()
{do{initgraph(640, 480);   //產(chǎn)生畫板PlaySound("F:\\Snake_bgm\\start.WAV", NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);//播放音樂(lè)menu();   //進(jìn)入菜單closegraph();  //關(guān)閉畫板} while (1);system("pause");
}int StringToInt(char a[], int n)//將字符串轉(zhuǎn)化為整數(shù)
{if (n == 1)return a[0] - 48;elsereturn StringToInt(a, n - 1) * 10 + a[n - 1] - 48;
}void start()//開始特效
{int x, y, i;for (i = 0; i<30; i++){x = i;for (y = x; y<R - x; y++){setfillcolor(BROWN);fillrectangle(x*SIZE, y*SIZE, x*SIZE + SIZE, y*SIZE + SIZE);}x = (R - 1 - i);for (y = i; y <= x; y++){setfillcolor(BROWN);fillrectangle(x*SIZE, y*SIZE, x*SIZE + SIZE, y*SIZE + SIZE);}x = i;for (y = x; y<R - x; y++){setfillcolor(BROWN);fillrectangle(y*SIZE, x*SIZE, y*SIZE + SIZE, x*SIZE + SIZE);}x = (R - 1 - i);for (y = i; y <= x; y++){setfillcolor(BROWN);fillrectangle(y*SIZE, x*SIZE, y*SIZE + SIZE, x*SIZE + SIZE);}Sleep(50);}for (i = 0; i<30; i++){x = i;for (y = x; y<R - x; y++){setfillcolor(BLACK);solidrectangle(x*SIZE, y*SIZE, x*SIZE + SIZE, y*SIZE + SIZE);}x = (R - 1 - i);for (y = i; y <= x; y++){setfillcolor(BLACK);solidrectangle(x*SIZE, y*SIZE, x*SIZE + SIZE, y*SIZE + SIZE);}x = i;for (y = x; y<R - x; y++){setfillcolor(BLACK);solidrectangle(y*SIZE, x*SIZE, y*SIZE + SIZE, x*SIZE + SIZE);}x = (R - 1 - i);for (y = i; y <= x; y++){setfillcolor(BLACK);solidrectangle(y*SIZE, x*SIZE, y*SIZE + SIZE, x*SIZE + SIZE);}Sleep(50);}
}void double_getfood()//雙人模式隨機(jī)產(chǎn)生食物
{int flag;while (1){flag = 1;food.x = rand() % (fram_width - 2) + 1;food.y = rand() % (fram_height - 2) + 1;for (m = 0; m<snake.len; m++){if (food.x == snake.x[m] && food.y == snake.y[m]) //判斷食物是否落到蛇身上{flag = 0;break;}}if (flag == 0)continue;for (m = 0; m<new_snake.len; m++){if (food.x == new_snake.x[m] && food.y == new_snake.y[m]){flag = 0;break;}}if (flag == 0)continue;if (flag == 1){if ((new_snake.count + snake.count) % 5 == 0 && (new_snake.count + snake.count) != 0)//每產(chǎn)生5個(gè)小食物后產(chǎn)生1個(gè)大食物{setfillcolor(WHITE);fillcircle(food.x*SIZE + SIZE / 2, food.y*SIZE + SIZE / 2, SIZE / 2);}else{setfillcolor(WHITE);fillcircle(food.x*SIZE + SIZE / 2, food.y*SIZE + SIZE / 2, SIZE / 4);}break;}}
}void getfood()//產(chǎn)生食物
{int flag;while (1){flag = 1;food.x = rand() % (fram_width - 2) + 1;food.y = rand() % (fram_height - 2) + 1;for (m = 1; m<fram_width / 3; m++){if ((food.x == m && food.y == fram_height / 4) || (food.x == m && food.y == 3 * fram_height / 4))//判斷食物是否落到蛇身上{flag = 0;break;}}if (flag == 0)continue;for (m = 2 * fram_width / 3; m<fram_width; m++){if (food.x == m && food.y == fram_height / 2)//判斷食物是否落到障礙物上{flag = 0;break;}}if (flag == 0)continue;for (m = 0; m<snake.len; m++){if (food.x == snake.x[m] && food.y == snake.y[m]){flag = 0;break;}}if (flag == 0)continue;if (flag == 1){if (snake.count % 5 == 0 && snake.count != 0){setfillcolor(WHITE);fillcircle(food.x*SIZE + SIZE / 2, food.y*SIZE + SIZE / 2, SIZE / 2);}else{setfillcolor(WHITE);fillcircle(food.x*SIZE + SIZE / 2, food.y*SIZE + SIZE / 2, SIZE / 4);}break;}}
}void double_eatfood()//雙人模式判斷是否吃到食物
{if (snake.x[0] == food.x&&snake.y[0] == food.y)//如果玩家1吃到食物{snake.len++;if ((snake.count + new_snake.count) % 5 == 0 && (snake.count + new_snake.count) != 0){score += 15;}elsescore += 5;snake.count++;double_getfood();}else if (new_snake.x[0] == food.x&&new_snake.y[0] == food.y)//如果玩家2吃到食物{new_snake.len++;if ((snake.count + new_snake.count) % 5 == 0 && (snake.count + new_snake.count) != 0){score2 += 15;}elsescore2 += 5;new_snake.count++;double_getfood();}
}void eatfood()//傳統(tǒng)模式  判斷是否吃到食物
{if (snake.x[0] == food.x&&snake.y[0] == food.y){snake.len++;if (snake.count % 5 == 0 && snake.count != 0){score += 20;if (snake.speed>100)snake.speed -= 50;elsesnake.speed = 100;}elsescore += 5;snake.count++;getfood();//吃完還有}
}void new_chushihua()//初始化玩家2
{//產(chǎn)生蛇頭new_snake.x[0] = (fram_width) / 3;new_snake.y[0] = (fram_height) / 3;new_snake.speed = 300;moveto(new_snake.x[0] * SIZE, new_snake.y[0] * SIZE);setfillcolor(BLUE);fillcircle(new_snake.x[0] * SIZE + SIZE / 2, new_snake.y[0] * SIZE + SIZE / 2, SIZE / 2);//產(chǎn)生蛇身fillcircle(new_snake.x[0] * SIZE + SIZE / 2, new_snake.y[0] * SIZE + SIZE / 2, SIZE / 2);new_snake.len = 4;for (int k = 1; k < new_snake.len; k++)//依次給后一節(jié)蛇身賦值{//將前一節(jié)坐標(biāo)賦給后一節(jié)new_snake.x[k] = new_snake.x[k - 1] + 1;new_snake.y[k] = new_snake.y[k - 1];moveto(new_snake.x[k] * SIZE, new_snake.y[k] * SIZE);setfillcolor(YELLOW);//填充顏色fillcircle(new_snake.x[k]*SIZE+SIZE/2, new_snake.y[k]*SIZE+SIZE/2, SIZE/2);//畫蛇}
}void chushihua()
{//產(chǎn)生蛇頭snake.x[0] = (fram_width) / 2;snake.y[0] = (fram_height) / 2;snake.speed = 300;moveto(snake.x[0] * SIZE, snake.y[0] * SIZE);setfillcolor(GREEN);fillcircle(snake.x[0] * SIZE + SIZE / 2, snake.y[0] * SIZE + SIZE / 2, SIZE / 2);//產(chǎn)生蛇身//fillcircle(snake.x[0] * SIZE + SIZE / 2, snake.y[0] * SIZE + SIZE / 2, SIZE / 2);snake.len = 4;for (int k = 1; k < snake.len; k++){//將前一節(jié)坐標(biāo)賦給后一節(jié)snake.x[k] = snake.x[k - 1] + 1;snake.y[k] = snake.y[k - 1];moveto(snake.x[k] * SIZE, snake.y[k] * SIZE);setfillcolor(RED);//填充顏色fillcircle(snake.x[k]*SIZE+SIZE/2, snake.y[k]*SIZE+SIZE/2, SIZE/2);//畫蛇}
}void move()//遷移坐標(biāo),移動(dòng)蛇
{//每次移動(dòng)將蛇尾巴畫為背景色moveto(snake.x[snake.len - 1] * SIZE, snake.y[snake.len - 1] * SIZE);setfillcolor(BLACK);solidrectangle(snake.x[snake.len - 1] * SIZE, snake.y[snake.len - 1] * SIZE, snake.x[snake.len - 1] * SIZE + SIZE, snake.y[snake.len - 1] * SIZE + SIZE);if (snake.y[0] == 0)     //穿墻snake.y[0] = fram_height - 2;else if (snake.y[0] == fram_height - 1)snake.y[0] = 0;for (m = snake.len - 1; m > 0; m--){//將后一節(jié)坐標(biāo)賦值給前一節(jié)坐標(biāo)snake.x[m] = snake.x[m - 1];snake.y[m] = snake.y[m - 1];}
}void double_move1()//雙人模式移動(dòng)玩家1
{//每次移動(dòng)將蛇尾巴畫為背景色moveto(snake.x[snake.len - 1] * SIZE, snake.y[snake.len - 1] * SIZE);setfillcolor(BLACK);solidrectangle(snake.x[snake.len - 1] * SIZE, snake.y[snake.len - 1] * SIZE, snake.x[snake.len - 1] * SIZE + SIZE, snake.y[snake.len - 1] * SIZE + SIZE);if (snake.y[0] == 0)     //穿墻snake.y[0] = fram_height - 2;else if (snake.y[0] == fram_height - 1)snake.y[0] = 0;else if (snake.x[0] == 0)snake.x[0] = fram_width - 2;else if (snake.x[0] == fram_width - 1)snake.x[0] = 0;for (m = snake.len - 1; m > 0; m--){//將后一節(jié)坐標(biāo)賦值給前一節(jié)坐標(biāo)snake.x[m] = snake.x[m - 1];snake.y[m] = snake.y[m - 1];}
}void double_move2()//雙人模式移動(dòng)玩家2
{//int k;//每次移動(dòng)將蛇尾巴畫為背景色moveto(new_snake.x[new_snake.len - 1] * SIZE, new_snake.y[new_snake.len - 1] * SIZE);setfillcolor(BLACK);solidrectangle(new_snake.x[new_snake.len - 1] * SIZE, new_snake.y[new_snake.len - 1] * SIZE, new_snake.x[new_snake.len - 1] * SIZE + SIZE, new_snake.y[new_snake.len - 1] * SIZE + SIZE);if (new_snake.y[0] == 0)     //穿墻new_snake.y[0] = fram_height - 2;else if (new_snake.y[0] == fram_height - 1)new_snake.y[0] = 0;else if (new_snake.x[0] == 0)new_snake.x[0] = fram_width - 2;else if (new_snake.x[0] == fram_width - 1)new_snake.x[0] = 0;for (m = new_snake.len - 1; m > 0; m--){//將后一節(jié)坐標(biāo)賦值給前一節(jié)坐標(biāo)new_snake.x[m] = new_snake.x[m - 1];new_snake.y[m] = new_snake.y[m - 1];}
}void double_turn(char t)
{if (t == UP2)new_snake.y[0]--;else if (t == DOWN2)new_snake.y[0]++;else if (t == LEFT2)new_snake.x[0]--;else if (t == RIGHT2)new_snake.x[0]++;
}void turn(char t)
{if (t == UP)snake.y[0]--;else if (t == DOWN)snake.y[0]++;else if (t == LEFT)snake.x[0]--;else if (t == RIGHT)snake.x[0]++;
}void print()//打印蛇
{//打印蛇頭moveto(snake.x[0] * SIZE, snake.y[0] * SIZE);setfillcolor(GREEN);fillcircle(snake.x[0] * SIZE + SIZE / 2, snake.y[0] * SIZE + SIZE / 2, SIZE / 2);//打印蛇身for (m = 1; m<snake.len; m++){setfillcolor(RED);fillcircle(snake.x[m] * SIZE + SIZE / 2, snake.y[m] * SIZE + SIZE / 2, SIZE / 2);}
}void double_print()//雙人模式   同時(shí)打印兩條蛇
{int len = new_snake.len<snake.len ? new_snake.len : snake.len;//len取兩者中的較小值moveto(new_snake.x[0] * SIZE, new_snake.y[0] * SIZE);setfillcolor(BLUE);fillcircle(new_snake.x[0] * SIZE + SIZE / 2, new_snake.y[0] * SIZE + SIZE / 2, SIZE / 2);//畫玩家2的蛇頭moveto(snake.x[0] * SIZE, snake.y[0] * SIZE);setfillcolor(GREEN);fillcircle(snake.x[0] * SIZE + SIZE / 2, snake.y[0] * SIZE + SIZE / 2, SIZE / 2);//畫玩家1的蛇頭for (m = 1; m<len; m++)//同時(shí)畫玩家1和玩家2的蛇身{setfillcolor(RED);fillcircle(snake.x[m] * SIZE + SIZE / 2, snake.y[m] * SIZE + SIZE / 2, SIZE / 2);setfillcolor(YELLOW);fillcircle(new_snake.x[m] * SIZE + SIZE / 2, new_snake.y[m] * SIZE + SIZE / 2, SIZE / 2);}for (m = len; m<(new_snake.len>snake.len ? new_snake.len : snake.len); m++){if (new_snake.len>snake.len)//如果玩家2的蛇比玩家1的蛇長(zhǎng),則把玩家2比玩家1多處的那部分補(bǔ)全{setfillcolor(YELLOW);fillcircle(new_snake.x[m] * SIZE + SIZE / 2, new_snake.y[m] * SIZE + SIZE / 2, SIZE / 2);}else//如果玩家1的蛇比玩家2的蛇長(zhǎng),則把玩家1比玩家2多處的那部分補(bǔ)全{setfillcolor(RED);fillcircle(snake.x[m] * SIZE + SIZE / 2, snake.y[m] * SIZE + SIZE / 2, SIZE / 2);}}
}int win(int grade)//判斷輸贏
{if (score >= grade)//如果玩家1率先達(dá)到50分,則玩家1勝利return 1;else if (score2 >= grade)//否則玩家2勝利return 2;
}void wall()//畫障礙物
{for (m = 1; m<fram_width / 3; m++){setfillcolor(BROWN);fillrectangle(m*SIZE, fram_height / 4 * SIZE, m*SIZE + SIZE, fram_height / 4 * SIZE + SIZE);}for (m = 2 * fram_width / 3; m<fram_width; m++){setfillcolor(BROWN);fillrectangle(m*SIZE, fram_height / 2 * SIZE, m*SIZE + SIZE, fram_height / 2 * SIZE + SIZE);}for (m = 1; m<fram_width / 3; m++){setfillcolor(BROWN);fillrectangle(m*SIZE, 3 * fram_height / 4 * SIZE, m*SIZE + SIZE, 3 * fram_height / 4 * SIZE + SIZE);}
}void double_score_rule()//分?jǐn)?shù)與規(guī)則
{settextcolor(WHITE);outtextxy(31 * SIZE, 3 * SIZE, "玩家1:");setfillcolor(GREEN);fillcircle(34 * SIZE + SIZE / 2, 3 * SIZE + SIZE / 2, SIZE / 2);//產(chǎn)生蛇身for (int k = 35; k <38; k++){setfillcolor(RED);fillcircle(k * SIZE + SIZE / 2, 3 * SIZE + SIZE / 2, SIZE / 2);}char count1[5];itoa(score, count1, 10);//將整數(shù)轉(zhuǎn)化為字符串settextcolor(WHITE);outtextxy(31 * SIZE, 5 * SIZE, "分?jǐn)?shù):");setfillcolor(BLACK);solidrectangle(34 * SIZE, 5 * SIZE, 38 * SIZE + SIZE, 5 * SIZE + SIZE);outtextxy(34 * SIZE, 5 * SIZE, count1);settextcolor(WHITE);outtextxy(31 * SIZE, 7 * SIZE, "玩家2:");setfillcolor(BLUE);fillcircle(new_snake.x[0] * SIZE + SIZE / 2, new_snake.y[0] * SIZE + SIZE / 2, SIZE / 2);//產(chǎn)生蛇身fillcircle(34 * SIZE + SIZE / 2, 7 * SIZE + SIZE / 2, SIZE / 2);for (int j = 35; j < 38; j++){setfillcolor(YELLOW);fillcircle(j * SIZE + SIZE / 2, 7 * SIZE + SIZE / 2, SIZE / 2);}char count2[5];itoa(score2, count2, 10);settextcolor(WHITE);outtextxy(31 * SIZE, 9 * SIZE, "分?jǐn)?shù):");setfillcolor(BLACK);solidrectangle(34 * SIZE, 9 * SIZE, 38 * SIZE + SIZE, 9 * SIZE + SIZE);outtextxy(34 * SIZE, 9 * SIZE, count2);line(30 * SIZE, 11 * SIZE, 40 * SIZE, 11 * SIZE);settextcolor(RED);outtextxy(31 * SIZE, 13 * SIZE, "玩家1:");settextcolor(GREEN);outtextxy(31 * SIZE, 14 * SIZE, "  w: 上   s: 下");outtextxy(31 * SIZE, 15 * SIZE, "  a: 左   d: 右");settextcolor(RED);outtextxy(31 * SIZE, 17 * SIZE, "玩家2:");settextcolor(GREEN);outtextxy(31 * SIZE, 18 * SIZE, "  i: 上   k: 下");outtextxy(31 * SIZE, 19 * SIZE, "  j: 左   l: 右");settextcolor(RED);outtextxy(31 * SIZE, 21 * SIZE, "規(guī)則:");settextcolor(WHITE);outtextxy(31 * SIZE, 23 * SIZE, "1.率先達(dá)到目標(biāo)分者");outtextxy(31 * SIZE, 24 * SIZE, "  勝利。");outtextxy(31 * SIZE, 25 * SIZE, "2.游戲過(guò)程中若碰到");outtextxy(31 * SIZE, 26 * SIZE, "  對(duì)方身體,則減15");outtextxy(31 * SIZE, 27 * SIZE, "  分,并在初始位置");outtextxy(31 * SIZE, 28 * SIZE, "  復(fù)活");
}void score_rule()
{char count[5];FILE *fp;fp = fopen("maxscore", "a+");//讀取文件中的內(nèi)容if (fp != NULL){settextcolor(GREEN);while (fgets(maxScore, 5, fp) != NULL)//將文件中的內(nèi)容寫入maxScore數(shù)組中outtextxy(35 * SIZE, 5 * SIZE, maxScore);//將最高分顯示在畫板上fclose(fp);}itoa(score, count, 10);//將整數(shù)轉(zhuǎn)化為字符串settextcolor(WHITE);outtextxy(31 * SIZE, 3 * SIZE, "分?jǐn)?shù):");setfillcolor(BLACK);solidrectangle(34 * SIZE, 3 * SIZE, 38 * SIZE + SIZE, 3 * SIZE + SIZE);outtextxy(34 * SIZE, 3 * SIZE, count);outtextxy(31 * SIZE, 5 * SIZE, "最高分:");line(30 * SIZE, 9 * SIZE, 40 * SIZE, 9 * SIZE);outtextxy(31 * SIZE, 11 * SIZE, "  w:     上");outtextxy(31 * SIZE, 13 * SIZE, "  s:     下");outtextxy(31 * SIZE, 15 * SIZE, "  a:     左");outtextxy(31 * SIZE, 17 * SIZE, "  d:     右");
}void double_initmap()
{for (int i = 1; i < fram_width - 1; i++){setfillcolor(BLACK);fillrectangle(i*SIZE, 0, i*SIZE + SIZE, SIZE);setfillcolor(BLACK);fillrectangle(i*SIZE, SIZE*(fram_width - 1), i*SIZE + SIZE, SIZE*(fram_width - 1) + SIZE);}for (int j = 0; j < fram_height; j++){setfillcolor(BLACK);fillrectangle(0, j*SIZE, SIZE, j*SIZE + SIZE);setfillcolor(BLACK);fillrectangle(SIZE*(fram_height - 1), j*SIZE, SIZE*(fram_height - 1) + SIZE, j*SIZE + SIZE);}
}void initmap()
{//產(chǎn)生圍欄for (int i = 1; i < fram_width - 1; i++){setfillcolor(BLACK);fillrectangle(i*SIZE, 0, i*SIZE + SIZE, SIZE);setfillcolor(BLACK);fillrectangle(i*SIZE, SIZE*(fram_width - 1), i*SIZE + SIZE, SIZE*(fram_width - 1) + SIZE);}for (int j = 0; j < fram_height; j++){setfillcolor(BROWN);fillrectangle(0, j*SIZE, SIZE, j*SIZE + SIZE);setfillcolor(BROWN);fillrectangle(SIZE*(fram_height - 1), j*SIZE, SIZE*(fram_height - 1) + SIZE, j*SIZE + SIZE);}}int double_die1()
{for (int i = 1; i<new_snake.len; i++){if (snake.x[0] == new_snake.x[i] && snake.y[0] == new_snake.y[i]){return 1;}}return 0;
}int double_die2()
{for (int i = 1; i<snake.len; i++){if (new_snake.x[0] == snake.x[i] && new_snake.y[0] == snake.y[i]){return 1;}}return 0;
}int die()
{for (int i = 1; i<snake.len; i++){if (snake.x[0] == snake.x[i] && snake.y[0] == snake.y[i]){return 1;}}for (m = 1; m<fram_width / 3; m++){if ((snake.x[0] == m && snake.y[0] == fram_height / 4) || (snake.x[0] == m && snake.y[0] == 3 * fram_height / 4)){return 1;}}for (m = 2 * fram_width / 3; m<fram_width; m++){if (snake.x[0] == m && snake.y[0] == fram_height / 2){return 1;}}if (snake.x[0] == 0 || snake.x[0] == fram_width - 1){return 1;}return 0;
}void menu()
{char str[100];InputBox(str, 100, "請(qǐng)選擇:\n\n 1.傳統(tǒng)模式\n\n 2.雙人PK\n\n 3.退出游戲", "蛇蛇大作戰(zhàn)", "", 250, 100, false);if (strcmp(str, "1") == 0){PlaySound("F:\\Snake_bgm\\bgm1.WAV", NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);start();initmap();wall();score_rule();srand(time(NULL));play();}else if (strcmp(str, "2") == 0){PlaySound("F:\\Snake_bgm\\double_play.wav", NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);start();double_initmap();srand(time(NULL));double_play();}else if (strcmp(str, "3") == 0){exit(0);}
}void double_play()//開始雙人PK游戲
{char str[5];int len;int grade;while(1){int flag=1;InputBox(str, 5, "請(qǐng)輸入目標(biāo)分?jǐn)?shù)(1~1000)\n只能輸入數(shù)字", "蛇蛇大作戰(zhàn)", "", 200, 100, false);len=strlen(str);str[len]='\0';if(str[0]==NULL)continue;for(int i=0;i<len;i++)if(str[i]<'0'||str[i]>'9'){flag=0;break;}if(!flag)continue;grade=StringToInt(str,len);if(grade>0&&grade<=1000)break;}settextcolor(WHITE);outtextxy(31 * SIZE, 1 * SIZE, "目標(biāo)分?jǐn)?shù):");settextcolor(BROWN);outtextxy(36 * SIZE, 1 * SIZE, str);char k = 'a';char t = LEFT2;  //k和t分別記錄蛇前一時(shí)刻移動(dòng)的方向new_snake.count = 0;snake.count = 0;score = 0;score2 = 0;new_chushihua();chushihua();  //初始化int move1 = 0, move2 = 0;//標(biāo)記按鍵的歸屬char key, key1 = LEFT, key2 = LEFT2;//初始方向double_getfood();//產(chǎn)生食物while (1){double_eatfood();//判斷是否吃到食物double_move2();double_move1();//移動(dòng)蛇move1 = 0;move2 = 0;if (kbhit())//如果有按鍵{key = getch();//獲取按鍵值switch (key)//判斷按鍵{case UP2:case DOWN2:case LEFT2:case RIGHT2:key2 = key; move2 = 1; break;//如果按鍵屬于玩家2,move2=1;case UP:case DOWN:case LEFT:case RIGHT:key1 = key; move1 = 1; break;//如果按鍵屬于玩家1,move1=1;}}if (move1 == 1)//如果move1=1,即按鍵屬于玩家1{if (k == LEFT && key1 == RIGHT)    //防止反向咬到自己key1 = LEFT;else if (k == UP && key1 == DOWN)key1 = UP;else if (k == RIGHT && key1 == LEFT)key1 = RIGHT;else if (k == DOWN && key1 == UP)key1 = DOWN;turn(key1);//轉(zhuǎn)向}if (move2 == 1)//如果move2=1,即按鍵屬于玩家2{if (t == UP2 && key2 == DOWN2)    //防止反向咬到自己key2 = UP2;else if (t == DOWN2 && key2 == UP2)key2 = DOWN2;else if (t == LEFT2 && key2 == RIGHT2)key2 = LEFT2;else if (t == RIGHT2 && key2 == LEFT2)key2 = RIGHT2;double_turn(key2);//轉(zhuǎn)向}if (move2 == 0)//如果按鍵屬于玩家1,則玩家2的蛇繼續(xù)維持上一時(shí)刻的方向double_turn(t);if (move1 == 0)//如果按鍵屬于玩家2,則玩家1的蛇繼續(xù)維持上一時(shí)刻的方向turn(k);k = key1;t = key2;//獲取上一時(shí)刻的方向if (double_die1())//判斷玩家1是否死亡{if (score >= 15)//如果分?jǐn)?shù)大于15分score -= 15;else      //如果分?jǐn)?shù)小于15分,則分?jǐn)?shù)清零score = 0;for (m = 0; m<snake.len; m++)//死亡后,將遺體用背景色覆蓋{setfillcolor(BLACK);solidrectangle(snake.x[m] * SIZE, snake.y[m] * SIZE, snake.x[m] * SIZE + SIZE, snake.y[m] * SIZE + SIZE);}k=key1=LEFT;chushihua();//初始化蛇}if (double_die2())//如果玩家2死亡{if (score2 >= 15)score2 -= 15;elsescore2 = 0;for (m = 0; m<new_snake.len; m++){setfillcolor(BLACK);solidrectangle(new_snake.x[m] * SIZE, new_snake.y[m] * SIZE, new_snake.x[m] * SIZE + SIZE, new_snake.y[m] * SIZE + SIZE);}t=key2=LEFT2;new_chushihua();}double_print();//畫蛇double_initmap();double_score_rule();if (win(grade) == 1)//如果玩家1勝利{PlaySound("F:\\Snake_bgm\\win.WAV", NULL, SND_FILENAME | SND_ASYNC);settextcolor(YELLOW);LOGFONT f;gettextstyle(&f);                     // 獲取當(dāng)前字體設(shè)置f.lfHeight = 48;                      // 設(shè)?米痔甯叨任? 48_tcscpy(f.lfFaceName, _T("黑體"));    // 設(shè)置字體為“黑體”(高版本 VC 推薦使用 _tcscpy_s 函數(shù))f.lfQuality = ANTIALIASED_QUALITY;    // 設(shè)置輸出效果為抗鋸齒  settextstyle(&f);                     // 設(shè)置字體樣式outtextxy(8 * SIZE, 12 * SIZE, _T("玩家1勝利!"));outtextxy(8 * SIZE, 15 * SIZE, _T("按空格鍵繼續(xù)!"));while (getch() != ' '){;}break;}else if (win(grade) == 2)//如果玩家2勝利{PlaySound("F:\\Snake_bgm\\win.WAV", NULL, SND_FILENAME | SND_ASYNC);settextcolor(YELLOW);LOGFONT f;gettextstyle(&f);                     // 獲取當(dāng)前字體設(shè)置f.lfHeight = 48;                      // 設(shè)?米痔甯叨任? 48_tcscpy(f.lfFaceName, _T("黑體"));    // 設(shè)置字體為“黑體”(高版本 VC 推薦使用 _tcscpy_s 函數(shù))f.lfQuality = ANTIALIASED_QUALITY;    // 設(shè)置輸出效果為抗鋸齒  settextstyle(&f);                     // 設(shè)置字體樣式outtextxy(8 * SIZE, 12 * SIZE, _T("玩家2勝利!"));outtextxy(8 * SIZE, 15 * SIZE, _T("按空格鍵繼續(xù)!"));while (getch() != ' '){;}break;}Sleep(150);}
}void play()   //開始傳統(tǒng)模式游戲
{char k = 'a';//k記錄前一時(shí)刻移動(dòng)的方向char ch = 'a';snake.count = 0;score = 0;chushihua();getfood();while (1){if (kbhit()){while (1)//如果按其他鍵,則暫停{ch = getch();if (ch == 'w' || ch == 'a' || ch == 's' || ch == 'd')break;elsecontinue;}}eatfood();move();if (k == 'a'&&ch == 'd')    //防止反向咬到自己ch = 'a';else if (k == 'w'&&ch == 's')ch = 'w';else if (k == 'd'&&ch == 'a')ch = 'd';else if (k == 's'&&ch == 'w')ch = 's';turn(ch);k = ch;if (die()){PlaySound("F:\\Snake_bgm\\gameover.WAV", NULL, SND_FILENAME | SND_ASYNC);settextcolor(YELLOW);LOGFONT f;gettextstyle(&f);                     // 獲取當(dāng)前字體設(shè)置f.lfHeight = 48;                      // 設(shè)?米痔甯叨任? 48_tcscpy(f.lfFaceName, _T("黑體"));    // 設(shè)置字體為“黑體”(高版本 VC 推薦使用 _tcscpy_s 函數(shù))f.lfQuality = ANTIALIASED_QUALITY;    // 設(shè)置輸出效果為抗鋸齒  settextstyle(&f);                     // 設(shè)置字體樣式outtextxy(8 * SIZE, 12 * SIZE, _T("Game Over!"));outtextxy(8 * SIZE, 15 * SIZE, _T("按空格鍵繼續(xù)!"));FILE *fp;int len;len = strlen(maxScore);maxScore[len] = '\0';int maxscore;char ms[5];maxscore = StringToInt(maxScore, len);//將字符串轉(zhuǎn)化為整數(shù)if (score>maxscore)//如果破紀(jì)錄{fp = fopen("maxscore", "w");//將新紀(jì)錄寫入文件,并將文件中的原內(nèi)容清空if (fp != NULL){itoa(score, ms, 10);fputs(ms, fp);}fclose(fp);}while (getch() != ' '){;}break;}print();initmap();//畫邊框wall();//畫障礙物score_rule();//顯示分?jǐn)?shù)和規(guī)則Sleep(snake.speed);//速度}
} 
                        
                        
                        ?
?
?
總結(jié)
以上是生活随笔為你收集整理的c语言双人贪吃蛇-基于图形库实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
 
                            
                        - 上一篇: Zhong__xlrd基本使用
- 下一篇: smarty模版手册
