本文实例为大家分享了C语言基于图形库实现双人贪吃蛇的具体代码,供大家参考,具体内容如下
/* 蛇蛇大作战 作者:施瑞文 */ #include#include #include #include #include #include #include #pragma comment(lib,"winmm.lib") #define R 30 #define fram_width 30 //宽度 #define fram_height 30 //高度 #define SIZE 16 //方格边长 //玩家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是方向键对应的键值 下 #define LEFT2 'j' //左 #define RIGHT2 'l' //右 #define UP2 'i' //上 char ch = 'a';//用于记录方向,传统模式 char c = LEFT2; //记录玩家2方向 int m; //全局变量,用于循环计数 int score = 0; //玩家1分数和传统模式分数 int score2 = 0; //玩家2分数 char maxScore[5] = "0"; //记录传统模式历史最高分 struct Food //食物结构体 { int x; //食物的横坐标 int y; //食物的纵坐标 }food; struct Snake { //玩家1贪吃蛇 int len; //蛇的长度 int x[780]; //蛇的每一节的横坐标 int y[780]; //蛇的每一节的纵坐标 int count; //蛇吃到的食物数量 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(); //随机产生食物 void chushihua(); //初始化蛇 void eatfood(); //判断蛇是否吃到食物 int die(); //判断蛇是否死亡 void move(); //迁移蛇的坐标,移动蛇 void turn(char t); //转向 void print(); //打印蛇 void play(); //开始传统模式游戏 void start(); //开始特效 int StringToInt(char a[], int n); //将字符串转化为整数 void wall(); //传统模式的障碍物 void score_rule(); //显示分数和规则 //双人PK模式 void double_initmap(); //双人PK地图 void new_chushihua(); //初始化玩家2的蛇 void double_play(); //开始双人PK模式游戏 void double_eatfood(); //判断是否吃到食物 void double_turn(char t); //转向 void double_print(); //打印玩家2的蛇 void double_move1(); //双人模式玩家1移动 void double_move2(); //双人模式玩家2移动 void double_getfood();//双人模式随机产生食物 void double_score_rule();//双人模式 显示分数和操作规则 int double_die(); //双人模式判断玩家1是否死亡 int double_die2(); //双人模式判断玩家2是否死亡 int win(int grade); //判断赢家 void main() { do { initgraph(640, 480); //产生画板 PlaySound("F:\\Snake_bgm\\start.WAV", NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);//播放音乐 menu(); //进入菜单 closegraph(); //关闭画板 } while (1); system("pause"); } int StringToInt(char a[], int n)//将字符串转化为整数 { if (n == 1) return a[0] - 48; else return 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 100) snake.speed -= 50; else snake.speed = 100; } else score += 5; snake.count++; getfood();//吃完还有 } } void new_chushihua()//初始化玩家2 { //产生蛇头 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); //产生蛇身 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++)//依次给后一节蛇身赋值 { //将前一节坐标赋给后一节 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() { //产生蛇头 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); //产生蛇身 //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++) { //将前一节坐标赋给后一节 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()//迁移坐标,移动蛇 { //每次移动将蛇尾巴画为背景色 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--) { //将后一节坐标赋值给前一节坐标 snake.x[m] = snake.x[m - 1]; snake.y[m] = snake.y[m - 1]; } } void double_move1()//双人模式移动玩家1 { //每次移动将蛇尾巴画为背景色 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--) { //将后一节坐标赋值给前一节坐标 snake.x[m] = snake.x[m - 1]; snake.y[m] = snake.y[m - 1]; } } void double_move2()//双人模式移动玩家2 { //int k; //每次移动将蛇尾巴画为背景色 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--) { //将后一节坐标赋值给前一节坐标 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 ? new_snake.len : snake.len); m++) { if (new_snake.len>snake.len)//如果玩家2的蛇比玩家1的蛇长,则把玩家2比玩家1多处的那部分补全 { setfillcolor(YELLOW); fillcircle(new_snake.x[m] * SIZE + SIZE / 2, new_snake.y[m] * SIZE + SIZE / 2, SIZE / 2); } else//如果玩家1的蛇比玩家2的蛇长,则把玩家1比玩家2多处的那部分补全 { 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率先达到50分,则玩家1胜利 return 1; else if (score2 >= grade)//否则玩家2胜利 return 2; } void wall()//画障碍物 { for (m = 1; m '9') { flag=0; break; } if(!flag) continue; grade=StringToInt(str,len); if(grade>0&&grade<=1000) break; } settextcolor(WHITE); outtextxy(31 * SIZE, 1 * SIZE, "目标分数:"); settextcolor(BROWN); outtextxy(36 * SIZE, 1 * SIZE, str); char k = 'a'; char t = LEFT2; //k和t分别记录蛇前一时刻移动的方向 new_snake.count = 0; snake.count = 0; score = 0; score2 = 0; new_chushihua(); chushihua(); //初始化 int move1 = 0, move2 = 0;//标记按键的归属 char key, key1 = LEFT, key2 = LEFT2;//初始方向 double_getfood();//产生食物 while (1) { double_eatfood();//判断是否吃到食物 double_move2(); double_move1();//移动蛇 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);//转向 } 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);//转向 } if (move2 == 0)//如果按键属于玩家1,则玩家2的蛇继续维持上一时刻的方向 double_turn(t); if (move1 == 0)//如果按键属于玩家2,则玩家1的蛇继续维持上一时刻的方向 turn(k); k = key1; t = key2;//获取上一时刻的方向 if (double_die1())//判断玩家1是否死亡 { if (score >= 15)//如果分数大于15分 score -= 15; else //如果分数小于15分,则分数清零 score = 0; for (m = 0; m = 15) score2 -= 15; else score2 = 0; for (m = 0; m maxscore)//如果破纪录 { fp = fopen("maxscore", "w");//将新纪录写入文件,并将文件中的原内容清空 if (fp != NULL) { itoa(score, ms, 10); fputs(ms, fp); } fclose(fp); } while (getch() != ' ') { ; } break; } print(); initmap();//画边框 wall();//画障碍物 score_rule();//显示分数和规则 Sleep(snake.speed);//速度 } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。