1.完美的贪吃蛇
#include
#include
#include
#include
#define N 21
#include
using namespace std;
void gotoxy(int x,int y)//位置函数
{
COORD pos;
pos.X=2*x;
pos.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void color(int a)//颜色函数
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void init(int apple[2])//初始化函数(初始化围墙、显示信息、苹果)
{
int i,j;//初始化围墙
int wall[N+2][N+2]={{0}};
for(i=1;i<=N;i++)
{
for(j=1;j<=N;j++)
wall[i][j]=1;
}
color(11);
for(i=0;i0;i--)
{
snake[i][0]=snake[i-1][0];
snake[i][1]=snake[i-1][1];
gotoxy(snake[i][0],snake[i][1]);
color(14);
cout<<"★"<
2.有缺陷的贪吃蛇
#include
#include
#include
#include
//游戏窗口
#define FrameX 4 //游戏窗口左上角的X轴坐标
#define FrameY 4 //游戏窗口左上角的Y轴坐标
#define Frame_height 20 //游戏窗口的高度
#define Frame_width 20 //游戏窗口的宽度
//定义全局变量
int i,j;
int a[2]; //用于记住蛇尾坐标,其中a[0]、a[1]分别表示横、竖坐标
//声明蛇的结构体
struct Snake
{
int x[100]; //蛇的横坐标,其中x[0]表示蛇尾的横坐标,x[N-1]表示蛇头的横坐标
int y[100]; //蛇的竖坐标,其中y[0]表示蛇尾的竖坐标,y[N-1]表示蛇头的竖坐标
int count; //蛇吃食物的个数
int length; //蛇的长度
int speed; //蛇的速度
};
//声明食物的结构体
struct Food
{
int x; //食物的横坐标
int y; //食物的竖坐标
};
/******光标移到指定位置**************************************************************/
void gotoxy(HANDLE hOut, int x, int y)
{
COORD pos;
pos.X = x; //横坐标
pos.Y = y; //纵坐标
SetConsoleCursorPosition(hOut, pos);
}
/******设置文本为绿色*****************************************************************/
void Set_TextColor_Green (void)
{
HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(Handle, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
}
/******制作游戏窗口******************************************************************/
void make_frame()
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); //定义显示器句柄变量
gotoxy(hOut,FrameX+2*Frame_width+3,FrameY+13); //打印选择菜单
printf("Esc 退出游戏");
gotoxy(hOut,FrameX+2*Frame_width+3,FrameY+15);
printf("长按方向键:加速");
gotoxy(hOut,FrameX,FrameY); //打印框角
printf("╔");
gotoxy(hOut,FrameX+2*Frame_width-2,FrameY);
printf("╗");
gotoxy(hOut,FrameX,FrameY+Frame_height);
printf("╚");
gotoxy(hOut,FrameX+2*Frame_width-2,FrameY+Frame_height);
printf("╝");
for(i=2;i<2*Frame_width-2;i+=2)
{
gotoxy(hOut,FrameX+i,FrameY);
printf("═"); //打印上横框
}
for(i=2;i<2*Frame_width-2;i+=2)
{
gotoxy(hOut,FrameX+i,FrameY+Frame_height);
printf("═"); //打印下横框
}
for(i=1;icount/5+1); //打印游戏等级
gotoxy(hOut,FrameX+2*Frame_width+3,FrameY+3);
printf("score : %d",10*snake->count); //打印游戏得分
gotoxy(hOut,FrameX+2*Frame_width+3,FrameY+5);
printf("eat food : %d",snake->count); //打印产生食物个数
gotoxy(hOut,FrameX+2*Frame_width+3,FrameY+7);
printf("speed : %dms",snake->speed); //打印游戏速度
gotoxy(hOut,FrameX+2*Frame_width+3,FrameY+9);
printf("foodX : %d",food->x); //打印食物的横坐标
gotoxy(hOut,FrameX+2*Frame_width+3,FrameY+11);
printf("foodY : %d",food->y); //打印食物的竖坐标
}
/******初始化蛇**********************************************************************/
void init_snake(struct Snake *snake)
{
snake->x[0]=FrameX+2; //初始化蛇的横坐标
snake->y[0]=FrameY+Frame_height/2; //初始化蛇的竖坐标
snake->speed=300; //初始化蛇的速度为300ms
snake->length=3; //初始化蛇的长度为3节
snake->count=0; //初始化蛇吃的个数为0
for(i=1;ilength;i++)
{/* 依次得到蛇身、蛇头的坐标 */
snake->x[i]=snake->x[i-1]+2;
snake->y[i]=snake->y[i-1];
}
}
/******移动蛇*************************************************************************/
void move_snake(HANDLE hOut,struct Snake *snake)
{
gotoxy(hOut,snake->x[0],snake->y[0]);
printf(" "); /* 清除蛇尾*/
for(i=1;ilength;i++)
{/* 后一节的坐标依次取代前一节的坐标 */
snake->x[i-1]=snake->x[i];
snake->y[i-1]=snake->y[i];
}
}
/******打印蛇*************************************************************************/
void print_snake(HANDLE hOut,struct Snake *snake)
{
for(i=0;ilength;i++)
{
gotoxy(hOut,snake->x[i],snake->y[i]);
if(i==0)
{
printf("○"); //打印蛇尾
}
else if(i==snake->length-1)
{
printf("¤"); //打印蛇头
}
else
{
printf("⊙"); //打印蛇身
}
}
}
/******随机产生食物*******************************************************************/
void get_food(HANDLE hOut,struct Snake *snake,struct Food *food)
{
srand((unsigned)time(NULL)); //初始化随机数
while(1)
{/* 产生食物的条件:1.在游戏窗口内 2.不在蛇的身上 */
food->x = rand() % (Frame_width-1);
food->y = rand() % Frame_height;
if( food->x==0 || food->y==0 )
{
continue;
}
food->x = 2*food->x + FrameX; //得到食物的横坐标
food->y+=FrameY; //得到食物的竖坐标
for(i=0;ilength;i++)
{/* 判断食物是否在蛇的身上,如果在蛇身上,则重新产生;否则,打印蛇身 */
if( food->x==snake->x[i] && food->y==snake->y[i] )
{
break;
}
}
if(i==snake->length)
{
gotoxy(hOut,food->x,food->y);
printf("⊙");
break;
}
}
}
/******吃食物***************************************************************************/
void eat_food(HANDLE hOut,struct Snake *snake,struct Food *food)
{
if( snake->x[snake->length-1]==food->x && snake->y[snake->length-1]==food->y )
{/* 如果蛇头位置与食物位置相同,吃食物 */
snake->length++; //吃一个食物,蛇身增长一节
for(i=snake->length-1;i>=1;i--)
{/* 蛇后节坐标依次赋值给蛇前一节的坐标,依次得到蛇身及蛇头的坐标 */
snake->x[i]=snake->x[i-1];
snake->y[i]=snake->y[i-1];
}
snake->x[0]=a[0]; //得到蛇尾移动前的横坐标
snake->y[0]=a[1]; //得到蛇尾移动前的竖坐标
get_food(hOut,snake,food); //重新产生食物
snake->count++; //食物的个数增1
if( snake->count%5==0 )
{/* 当蛇吃Up_level个食物时,速度加快Up_speed毫秒并且升一级 */
snake->speed-=50;
}
}
}
/******穿墙**********************************************************************************/
void through_wall(HANDLE hOut,struct Snake *snake,char ch)
{
if( ch==72 && snake->y[snake->length-1]==FrameY)
{
snake->y[snake->length-1] = FrameY+Frame_height-1; //如果蛇在上框且向上移动,穿墙
}
if( ch==80 && snake->y[snake->length-1]==FrameY+Frame_height )
{
snake->y[snake->length-1] = FrameY+1; //如果蛇在下框且向下移动,穿墙
}
if( ch==75 && snake->x[snake->length-1]==FrameX )
{
snake->x[snake->length-1] = FrameX+2*Frame_width-4; //如果蛇在左框且向左移动,穿墙
}
if( ch==77 && snake->x[snake->length-1]==FrameX+2*Frame_width-2 )
{
snake->x[snake->length-1] = FrameX+2; //如果蛇在右框且向右移动,穿墙
}
}
/******判断蛇是否死**************************************************************************/
int if_die(struct Snake *snake)
{/* 当蛇头碰到自身时,蛇死 ,返回值为0 */
for(i=0;ilength-1;i++)
{
if( snake->x[snake->length-1]==snake->x[i] && snake->y[snake->length-1]==snake->y[i] )
{
return 0;
}
}
return 1;
}
/******开始游戏*******************************************************************************/
void start_game()
{
unsigned char ch=77; //定义用于接收键盘输入的字符变量
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); //定义显示器句柄变量
struct Snake s,*snake=&s; //定义蛇的结构体指针并指向蛇的结构体
struct Food f,*food=&f; //定义食物的结构体指针并指向食物的结构体
make_frame(); //制作游戏窗口
init_snake(snake); //初始化蛇
get_food(hOut,snake,food); //随机产生食物
while(1)
{
print_information(hOut,snake,food); //打印菜单信息
a[0]=snake->x[0]; //记住蛇尾的横坐标
a[1]=snake->y[0]; //记住蛇尾的竖坐标
j=0;
if(kbhit())
{/* 判断是否按下键盘,如果按下,ch接收键盘输入 */
ch=getch();
if(kbhit())
{/* 如果长按键盘,则加速 */
Sleep(20);
j=1;
}
}
switch(ch)
{
case 72:
{/* 向上移动 */
move_snake(hOut,snake); //移动蛇
snake->y[snake->length-1]-=1; //蛇头的竖坐标向上移,即减1
break;
}
case 80:
{/* 向下移动 */
move_snake(hOut,snake); //移动蛇
snake->y[snake->length-1]+=1; //蛇头的竖坐标向下移,即加1
break;
}
case 75:
{/* 向左移动 */
move_snake(hOut,snake); //移动蛇
snake->x[snake->length-1]-=2; //蛇头的横坐标向左移,即减2
break;
}
case 77:
{/* 向右移动 */
move_snake(hOut,snake); //移动蛇
snake->x[snake->length-1]+=2; //蛇头的横坐标向右移,即加2
break;
}
}
through_wall(hOut,snake,ch); //穿墙
eat_food(hOut,snake,food); //吃食物
print_snake(hOut,snake); //打印蛇
if( if_die(snake)==0 || ch==27 || snake->speed==50 )
{/* 游戏结束条件:1.蛇碰到自身 2.按Esc键 3.速度为50ms */
gotoxy(hOut,FrameX+Frame_width-2,FrameY+Frame_height/2-1);
printf("Game Over");
Sleep(2000);
break;
}
if(j==0)
{
Sleep(snake->speed); //延迟时间
}
else
{
Sleep(1000);
}
}
}
int main()
{
system("color 0D"); //设置文本为粉红色
start_game(); //开始游戏
over_game();
return 0; //结束游戏
}
3.贪吃蛇的简单移动
#include
#include
#include
#define BEG_X 2
#define BEG_Y 1
#define WID 20
#define HEI 20
HANDLE hout;
typedef enum {UP, DOWN, LEFT, RIGHT} DIR;
typedef struct Snake_body
{
COORD pos;//蛇身的位置
struct Snake_body *next;//下一个蛇身
struct Snake_body *prev;//前一个蛇身
}SNAKE, *PSNAKE;
PSNAKE head = NULL;//蛇头
PSNAKE tail = NULL;//蛇尾
//画游戏边框的函数
void DrawBorder()
{
int i, j;
COORD pos = {BEG_X, BEG_Y};
for(i = 0; i < HEI; ++i)
{
SetConsoleCursorPosition(hout, pos);
for(j = 0; j < WID; ++j)
{
if(i == 0)//第一行
{
if(j == 0)
printf("┏");
else if(j == WID - 1)
printf("┓");
else
printf("━");
}
else if(i == HEI - 1)//最后一行
{
if(j == 0)
printf("┗");
else if(j == WID - 1)
printf("┛");
else
printf("━");
}
else if(j == 0 || j == WID - 1)//第一列或最后一列
printf("┃");
else
printf(" ");
}
++pos.Y;
}
}
//添加蛇身的函数
void AddBody(COORD pos)
{
PSNAKE pnew = (PSNAKE)calloc(1, sizeof(SNAKE));
pnew->pos = pos;
if(!head)
{
head = tail = pnew;
}
else
{
pnew->next = head;//新创建蛇身的next指向原先的蛇头
head->prev = pnew;//原先的蛇头的prev指向新创建的蛇身
head = pnew;//把新创建的蛇身作为新的蛇头
}
SetConsoleCursorPosition(hout, head->pos);
printf("◎");
}
//蛇身移动的函数
void MoveBody(DIR dir)
{
PSNAKE ptmp;
COORD pos = head->pos;
switch(dir)
{
case UP:
if(head->pos.Y > BEG_Y + 1)
--pos.Y;
else
return;
break;
case DOWN:
if(head->pos.Y < BEG_Y + HEI - 2)
++pos.Y;
else
return;
break;
case LEFT:
if(head->pos.X > BEG_X + 2)
pos.X -= 2;
else
return;
break;
case RIGHT:
if(head->pos.X < BEG_X + (WID - 2) * 2)
pos.X += 2;
else
return;
break;
}
AddBody(pos);//添加了一个新的蛇头
ptmp = tail;//保存当前的蛇尾
tail = tail->prev;
if(tail)
tail->next = NULL;
SetConsoleCursorPosition(hout, ptmp->pos);
printf(" ");
free(ptmp);
}
int main()
{
int ctrl;
DIR dir = RIGHT;//初始蛇的方向是向右的
COORD pos = {BEG_X + 2, BEG_Y + HEI / 2};
system("color 0E");
system("mode con cols=90 lines=30");
hout = GetStdHandle(STD_OUTPUT_HANDLE);
printf(" ------------贪吃蛇的移动------------");
DrawBorder();
//自定义几个蛇的身体
AddBody(pos);
pos.X += 2;
AddBody(pos);
pos.X += 2;
AddBody(pos);
pos.X += 2;
AddBody(pos);
pos.X += 2;
AddBody(pos);
pos.X += 2;
AddBody(pos);
pos.X += 2;
AddBody(pos);
//控制蛇的移动
while(ctrl = getch())
{
switch(ctrl)
{
case 'w':
if(dir == DOWN)
continue;
dir = UP;
break;
case 's':
if(dir == UP)
continue;
dir = DOWN;
break;
case 'a':
if(dir == RIGHT)
continue;
dir = LEFT;
break;
case 'd':
if(dir == LEFT)
continue;
dir = RIGHT;
break;
case 'q':
return 0;
}
MoveBody(dir);
}
return 0;
}
#include
#include
#include
#include
#define U 1
#define D 2
#define L 3
#define R 4 //蛇的状态,U:上 ;D:下;L:左 R:右
typedef struct SNAKE { //蛇身的一个节点
int x;
int y;
struct SNAKE *next;
} snake;
//全局变量//
int score=0,add=10;//总得分与每次吃食物得分。
int status,sleeptime=200;//每次运行的时间间隔
snake *head, *food;//蛇头指针,食物指针
snake *q;//遍历蛇的时候用到的指针
int endgamestatus=0; //游戏结束的情况,1:撞到墙;2:咬到自己;3:主动退出游戏。
//声明全部函数//
void Pos();
void creatMap();
void initsnake();
int biteself();
void createfood();
void cantcrosswall();
void snakemove();
void pause();
void gamecircle();
void welcometogame();
void endgame();
void gamestart();
void Pos(int x,int y) { //设置光标位置
COORD pos;
HANDLE hOutput;
pos.X=x;
pos.Y=y;
hOutput=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutput,pos);
}
void creatMap() { //创建地图
int i;
for(i=0; i<58; i+=2) { //打印上下边框
Pos(i,0);
printf("■");
Pos(i,26);
printf("■");
}
for(i=1; i<26; i++) { //打印左右边框
Pos(0,i);
printf("■");
Pos(56,i);
printf("■");
}
}
void initsnake() { //初始化蛇身
snake *tail;
int i;
tail=(snake*)malloc(sizeof(snake));//从蛇尾开始,头插法,以x,y设定开始的位置//
tail->x=24;
tail->y=5;
tail->next=NULL;
for(i=1; i<=4; i++) {
head=(snake*)malloc(sizeof(snake));
head->next=tail;
head->x=24+2*i;
head->y=5;
tail=head;
}
while(tail!=NULL) { //从头到为,输出蛇身
Pos(tail->x,tail->y);
printf("■");
tail=tail->next;
}
}
int biteself() { //判断是否咬到了自己
snake *self;
self=head->next;
while(self!=NULL) {
if(self->x==head->x && self->y==head->y) {
return 1;
}
self=self->next;
}
return 0;
}
void createfood() { //随机出现食物
snake *food_1;
srand((unsigned)time(NULL));
food_1=(snake*)malloc(sizeof(snake));
while((food_1->x%2)!=0) { //保证其为偶数,使得食物能与蛇头对其
food_1->x=rand()%52+2;
}
food_1->y=rand()%24+1;
q=head;
while(q->next==NULL) {
if(q->x==food_1->x && q->y==food_1->y) { //判断蛇身是否与食物重合
free(food_1);
createfood();
}
q=q->next;
}
Pos(food_1->x,food_1->y);
food=food_1;
printf("■");
}
void cantcrosswall() { //不能穿墙
if(head->x==0 || head->x==56 ||head->y==0 || head->y==26) {
endgamestatus=1;
endgame();
}
}
void snakemove() { //蛇前进,上U,下D,左L,右R
snake * nexthead;
cantcrosswall();
nexthead=(snake*)malloc(sizeof(snake));
if(status==U) {
nexthead->x=head->x;
nexthead->y=head->y-1;
if(nexthead->x==food->x && nexthead->y==food->y) { //如果下一个有食物//
nexthead->next=head;
head=nexthead;
q=head;
while(q!=NULL) {
Pos(q->x,q->y);
printf("■");
q=q->next;
}
score=score+add;
createfood();
} else { //如果没有食物//
nexthead->next=head;
head=nexthead;
q=head;
while(q->next->next!=NULL) {
Pos(q->x,q->y);
printf("■");
q=q->next;
}
Pos(q->next->x,q->next->y);
printf(" ");
free(q->next);
q->next=NULL;
}
}
if(status==D) {
nexthead->x=head->x;
nexthead->y=head->y+1;
if(nexthead->x==food->x && nexthead->y==food->y) { //有食物
nexthead->next=head;
head=nexthead;
q=head;
while(q!=NULL) {
Pos(q->x,q->y);
printf("■");
q=q->next;
}
score=score+add;
createfood();
} else { //没有食物
nexthead->next=head;
head=nexthead;
q=head;
while(q->next->next!=NULL) {
Pos(q->x,q->y);
printf("■");
q=q->next;
}
Pos(q->next->x,q->next->y);
printf(" ");
free(q->next);
q->next=NULL;
}
}
if(status==L) {
nexthead->x=head->x-2;
nexthead->y=head->y;
if(nexthead->x==food->x && nexthead->y==food->y) { //有食物
nexthead->next=head;
head=nexthead;
q=head;
while(q!=NULL) {
Pos(q->x,q->y);
printf("■");
q=q->next;
}
score=score+add;
createfood();
} else { //没有食物
nexthead->next=head;
head=nexthead;
q=head;
while(q->next->next!=NULL) {
Pos(q->x,q->y);
printf("■");
q=q->next;
}
Pos(q->next->x,q->next->y);
printf(" ");
free(q->next);
q->next=NULL;
}
}
if(status==R) {
nexthead->x=head->x+2;
nexthead->y=head->y;
if(nexthead->x==food->x && nexthead->y==food->y) { //有食物
nexthead->next=head;
head=nexthead;
q=head;
while(q!=NULL) {
Pos(q->x,q->y);
printf("■");
q=q->next;
}
score=score+add;
createfood();
} else { //没有食物
nexthead->next=head;
head=nexthead;
q=head;
while(q->next->next!=NULL) {
Pos(q->x,q->y);
printf("■");
q=q->next;
}
Pos(q->next->x,q->next->y);
printf(" ");
free(q->next);
q->next=NULL;
}
}
if(biteself()==1) { //判断是否会咬到自己
endgamestatus=2;
endgame();
}
}
void pause() { //暂停
while(1) {
Sleep(300);
if(GetAsyncKeyState(VK_SPACE)) {
break;
}
}
}
void gamecircle() { //控制游戏
Pos(64,15);
printf("不能穿墙,不能咬到自己\n");
Pos(64,16);
printf("用↑.↓.←.→分别控制蛇的移动.");
Pos(64,17);
printf("F1 为加速,F2 为减速\n");
Pos(64,18);
printf("ESC :退出游戏.space:暂停游戏.");
Pos(64,20);
printf("C语言研究中心 www.clang.cc");
status=R;
while(1) {
Pos(64,10);
printf("得分:%d ",score);
Pos(64,11);
printf("每个食物得分:%d分",add);
if(GetAsyncKeyState(VK_UP) && status!=D) {
status=U;
} else if(GetAsyncKeyState(VK_DOWN) && status!=U) {
status=D;
} else if(GetAsyncKeyState(VK_LEFT)&& status!=R) {
status=L;
} else if(GetAsyncKeyState(VK_RIGHT)&& status!=L) {
status=R;
} else if(GetAsyncKeyState(VK_SPACE)) {
pause();
} else if(GetAsyncKeyState(VK_ESCAPE)) {
endgamestatus=3;
break;
} else if(GetAsyncKeyState(VK_F1)) {
if(sleeptime>=50) {
sleeptime=sleeptime-30;
add=add+2;
if(sleeptime==320) {
add=2;//防止减到1之后再加回来有错
}
}
} else if(GetAsyncKeyState(VK_F2)) {
if(sleeptime<350) {
sleeptime=sleeptime+30;
add=add-2;
if(sleeptime==350) {
add=1; //保证最低分为1
}
}
}
Sleep(sleeptime);
snakemove();
}
}
void welcometogame() { //开始界面
Pos(40,12);
system("title C语言研究中心 www.clang.cc");
printf("欢迎来到贪食蛇游戏!");
Pos(40,25);
printf(" C语言研究中心 www.clang.cc.\n");
system("pause");
system("cls");
Pos(25,12);
printf("用↑.↓.←.→分别控制蛇的移动, F1 为加速,2 为减速\n");
Pos(25,13);
printf("加速将能得到更高的分数。\n");
system("pause");
system("cls");
}
void endgame() { //结束游戏
system("cls");
Pos(24,12);
if(endgamestatus==1) {
printf("对不起,您撞到墙了。游戏结束.");
} else if(endgamestatus==2) {
printf("对不起,您咬到自己了。游戏结束.");
} else if(endgamestatus==3) {
printf("您的已经结束了游戏。");
}
Pos(24,13);
printf("您的得分是%d\n",score);
exit(0);
}
void gamestart() { //游戏初始化
system("mode con cols=100 lines=30");
welcometogame();
creatMap();
initsnake();
createfood();
}
int main() {
gamestart();
gamecircle();
endgame();
return 0;
}