欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C語(yǔ)言基于圖形庫(kù)實(shí)現(xiàn)雙人貪吃蛇

 更新時(shí)間:2022年05月30日 08:55:07   作者:Sriven  
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言基于圖形庫(kù)實(shí)現(xiàn)雙人貪吃蛇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C語(yǔ)言基于圖形庫(kù)實(shí)現(xiàn)雙人貪吃蛇的具體代碼,供大家參考,具體內(nèi)容如下

/*
蛇蛇大作戰(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(); ? //畫(huà)邊框
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(); ? ? ? ? ? ?//開(kāi)始傳統(tǒng)模式游戲
void start(); ? ? ? ? ? //開(kāi)始特效
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(); ? ? //開(kāi)始雙人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)生畫(huà)板
?? ??? ?PlaySound("F:\\Snake_bgm\\start.WAV", NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);//播放音樂(lè)
?? ??? ?menu(); ? //進(jìn)入菜單
?? ??? ?closegraph(); ?//關(guān)閉畫(huà)板
?? ?} while (1);
?? ?system("pause");
}
?
int StringToInt(char a[], int n)//將字符串轉(zhuǎn)化為整數(shù)
{
?? ?if (n == 1)
?? ??? ?return a[0] - 48;
?? ?else
?? ??? ?return StringToInt(a, n - 1) * 10 + a[n - 1] - 48;
}
?
void start()//開(kāi)始特效
{
?? ?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;
?? ??? ?}
?? ??? ?else
?? ??? ??? ?score += 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;
?? ??? ?}
?? ??? ?else
?? ??? ??? ?score2 += 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;
?? ??? ??? ?else
?? ??? ??? ??? ?snake.speed = 100;
?? ??? ?}
?? ??? ?else
?? ??? ??? ?score += 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);//畫(huà)蛇
?? ?}
}
?
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);//畫(huà)蛇
?? ?}
}
?
void move()//遷移坐標(biāo),移動(dòng)蛇
{
?? ?//每次移動(dòng)將蛇尾巴畫(huà)為背景色
?? ?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)將蛇尾巴畫(huà)為背景色
?? ?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)將蛇尾巴畫(huà)為背景色
?? ?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);//畫(huà)玩家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);//畫(huà)玩家1的蛇頭
?? ?for (m = 1; m<len; m++)//同時(shí)畫(huà)玩家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()//畫(huà)障礙物
{
?? ?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)容寫(xiě)入maxScore數(shù)組中
?? ??? ??? ?outtextxy(35 * SIZE, 5 * SIZE, maxScore);//將最高分顯示在畫(huà)板上
?? ??? ?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()//開(kāi)始雙人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;
?? ??? ??? ?else
?? ??? ??? ??? ?score2 = 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();//畫(huà)蛇
?? ??? ?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() ? //開(kāi)始傳統(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;
?? ??? ??? ??? ?else
?? ??? ??? ??? ??? ?continue;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?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ì)錄寫(xiě)入文件,并將文件中的原內(nèi)容清空
?? ??? ??? ??? ?if (fp != NULL)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?itoa(score, ms, 10);
?? ??? ??? ??? ??? ?fputs(ms, fp);
?? ??? ??? ??? ?}
?? ??? ??? ??? ?fclose(fp);
?? ??? ??? ?}
?? ??? ??? ?while (getch() != ' ')
?? ??? ??? ?{
?? ??? ??? ??? ?;
?? ??? ??? ?}
?? ??? ??? ?break;
?? ??? ?}
?? ??? ?print();
?? ??? ?initmap();//畫(huà)邊框
?? ??? ?wall();//畫(huà)障礙物
?? ??? ?score_rule();//顯示分?jǐn)?shù)和規(guī)則
?? ??? ?Sleep(snake.speed);//速度
?? ?}
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • FFmpeg實(shí)戰(zhàn)之分離出PCM數(shù)據(jù)

    FFmpeg實(shí)戰(zhàn)之分離出PCM數(shù)據(jù)

    PCM(Pulse?Code?Modulation,脈沖編碼調(diào)制)音頻數(shù)據(jù)是未經(jīng)壓縮的音頻采樣數(shù)據(jù)裸流,它是由模擬信號(hào)經(jīng)過(guò)采樣、量化、編碼轉(zhuǎn)換成的標(biāo)準(zhǔn)數(shù)字音頻數(shù)據(jù)。本文將通過(guò)FFmpeg實(shí)現(xiàn)分離PCM數(shù)據(jù),感興趣的可以了解一下
    2023-02-02
  • 一文快速掌握C++雙端數(shù)組容器deque的使用

    一文快速掌握C++雙端數(shù)組容器deque的使用

    本文和大家分享的是和vector容器功能很像的容器——deque,了解deque容器的本質(zhì),使用方法以及與vector容器的不同之處,感興趣的快跟隨小編一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • C++深入探究用NULL來(lái)初始化空指針是否合適

    C++深入探究用NULL來(lái)初始化空指針是否合適

    在C++11新特性中,我們用nullptr來(lái)表示指針空值,這是為什么呢?好好地NULL為什么不繼續(xù)使用呢?說(shuō)明在創(chuàng)造C++的大佬們一定發(fā)現(xiàn)了什么Bug,本篇我們就一起來(lái)討論一下吧
    2022-05-05
  • 有關(guān)C++頭文件的包含順序研究

    有關(guān)C++頭文件的包含順序研究

    下面小編就為大家?guī)?lái)一篇有關(guān)C++頭文件的包含順序研究。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-01-01
  • C++文件流讀寫(xiě)操作詳解

    C++文件流讀寫(xiě)操作詳解

    本文詳細(xì)講解了C++文件流讀寫(xiě)操作的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-11-11
  • C語(yǔ)言中查找字符在字符串中出現(xiàn)的位置的方法

    C語(yǔ)言中查找字符在字符串中出現(xiàn)的位置的方法

    這篇文章主要介紹了C語(yǔ)言中查找字符在字符串中出現(xiàn)的位置的方法,分別是strchr()函數(shù)和strrchr()函數(shù)的使用,需要的朋友可以參考下
    2015-08-08
  • 詳解c++ libuv工作隊(duì)列

    詳解c++ libuv工作隊(duì)列

    這篇文章主要介紹了c++ libuv工作隊(duì)列的相關(guān)資料,幫助大家更好的理解和使用libuv,感興趣的朋友可以了解下
    2021-02-02
  • Qt線程池QThreadPool的使用詳解

    Qt線程池QThreadPool的使用詳解

    本文主要介紹了Qt線程池QThreadPool的使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • C語(yǔ)言趣味編程之水仙花數(shù)

    C語(yǔ)言趣味編程之水仙花數(shù)

    這篇文章介紹了C語(yǔ)言趣味編程之水仙花數(shù),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-11-11
  • 詳解C++中特殊類設(shè)計(jì)

    詳解C++中特殊類設(shè)計(jì)

    這篇文章主要為大家詳細(xì)介紹了C++中關(guān)于特殊類設(shè)計(jì)的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C++有一定的幫助,感興趣的可以了解一下
    2023-07-07

最新評(píng)論