1. 贪吃蛇程序设计报告
#include<graphics.h>
#include<stdlib.h>
#include<dos.h>
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
int i,key;
int score=0;
int gamespeed=32000;
struct Food /*食物的结构体*/
{
int x; /*食物的横坐标*/
int y; /*食物的纵坐标*/
int yes; /*食物是否出现的变量*/
}food;
struct Snack /*蛇的结构体*/
{
int x[N];
int y[N];
int node; /*蛇的节数*/
int direction; /*蛇的方向*/
int life; /*蛇的生命,0活着,1死亡*/
}snake;
void Init(void); /*图形驱动*/
void Close(void); /*关闭游戏函数*/
void DrawK(void); /*画图函数*/
void GameOver(void);/*输出失败函数*/
void GamePlay(); /*游戏控制函数 主要程序*/
void PrScore(void); /*分数输出函数*/
DELAY(char ch)/*调节游戏速度*/
{
if(ch=='3')
{
delay(gamespeed); /*delay是延迟函数*/
delay(gamespeed);
}
else if(ch=='2')
{
delay(gamespeed);
}
}
Menu()/*游戏开始菜单*/
{
char ch;
printf("Please choose the gamespeed:\n");
printf("1-Fast 2-Normal 3-Slow\n");
printf("\nPlease Press The numbers..\n");
do
{ch=getch();}
while(ch!='1'&&ch!='2'&&ch!='3');
clrscr();
return(ch);
}
/*主函数*/
void main(void)
{
int ch;
ch=Menu();
Init();
DrawK();
GamePlay(ch);
Close();
}
void Init(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
cleardevice();
}
void DrawK(void)
{
setcolor(11);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);
for(i=50;i<=600;i+=10)
{
rectangle(i,40,i+10,49); /*画出上边框*/
rectangle(i,451,i+10,460); /*画出下边框*/
}
for(i=40;i<=450;i+=10)
{
rectangle(50,i,59,i+10); /*画出左边框*/
rectangle(601,i,610,i+10); /*画出右边框*/
}
}
void GamePlay(char ch)
{
randomize(); /*随机数发生器*/
food.yes=1; /*1代表要出现食物,0表示以存在食物*/
snake.life=0;
snake.direction=1;
snake.x[0]=100;snake.y[0]=100;
snake.x[1]=110;snake.y[1]=100;
snake.node=2;
PrScore();
while(1) /*可以重复游戏*/
{
while(!kbhit()) /*在没有按键的情况下蛇自己移动*/
{
if(food.yes==1) /*需要食物*/
{
food.x=rand()%400+60;
food.y=rand()%350+60; /*使用rand函数随机产生食物坐标*/
while(food.x%10!=0)
food.x++;
while(food.y%10!=0)
food.y++; /*判断食物是否出现在整格里*/
food.yes=0; /*现在有食物了*/
}
if(food.yes==0) /*有食物了就要显示出来*/
{
setcolor(GREEN);
rectangle(food.x,food.y,food.x+10,food.y-10);
}
for(i=snake.node-1;i>0;i--) /*贪吃蛇的移动算法*/
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1]; /*贪吃蛇的身体移动算法*/
}
switch(snake.direction) /*贪吃蛇的头部移动算法,以此来控制移动*/
{
case 1:snake.x[0]+=10;break;
case 2:snake.x[0]-=10;break;
case 3:snake.y[0]-=10;break;
case 4:snake.y[0]+=10;break;
}
for(i=3;i<snake.node;i++) /*判断是否头部与身体相撞*/
{
if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0])
{
GameOver();
snake.life=1;
break;
}
}
/*下面是判断是否撞到墙壁*/
if(snake.x[0]<55||snake.x[0]>595||snake.y[0]<55||snake.y[0]>455)
{
GameOver();
snake.life=1;
}
if(snake.life==1) /*如果死亡就退出循环*/
break;
if(snake.x[0]==food.x&&snake.y[0]==food.y) /*判断蛇是否吃到食物*/
{
setcolor(0);
rectangle(food.x,food.y,food.x+10,food.y-10); /*吃的食物后用黑色将食物擦去*/
snake.x[snake.node]=-20;snake.y[snake.node]=-20; /*现把增加的一节放到看不到的地方去*/
snake.node++;
food.yes=1;
score+=10;
PrScore();
}
setcolor(4); /*每次移动后将后面的身体擦去*/
for(i=0;i<snake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);
delay(gamespeed);
DELAY(ch);
setcolor(0);
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
}
if(snake.life==1)
break;
key=bioskey(0); /*接受按键*/
if(key==ESC)
break;
else
if(key==UP&&snake.direction!=4)/*判断是否改变方向*/
snake.direction=3;
else
if(key==RIGHT&&snake.direction!=2)
snake.direction=1;
else
if(key==LEFT&&snake.direction!=1)
snake.direction=2;
else
if(key==DOWN&&snake.direction!=3)
snake.direction=4;
}
}
void GameOver(void)
{
cleardevice();
setcolor(RED);
settextstyle(0,0,4);
outtextxy(200,200,"GAME OVER");
getch();
}
void PrScore(void)
{
char str[10];
setfillstyle(SOLID_FILL,YELLOW);
bar(50,15,220,35);
setcolor(6);
settextstyle(0,0,2);
sprintf(str,"scord:%d",score);
outtextxy(55,20,str);
}
void Close(void)
{
getch();
closegraph();
}
贪吃蛇
#include "graphics.h"
#include "stdio.h"
#define MAX 200
#define MAXX 30
#define MAXY 30
#define UP 18432
#define DOWN 20480
#define LEFT 19200
#define RIGHT 19712
#define ESC 283
#define ENTER 7181
#define PAGEUP 18688
#define PAGEDOWN 20736
#define KEY_U 5749
#define KEY_K 9579
#define CTRL_P 6512
#define TRUE 1
#define FALSE 0
#define GAMEINIT 1
#define GAMESTART 2
#define GAMEHAPPY 3
#define GAMEOVER 4
struct SPlace
{
int x;
int y;
int st;
} place[MAX];
int speed;
int count;
int score;
int control;
int head;
int tear;
int x,y;
int babyx,babyy;
int class;
int eat;
int game;
int gamedelay[]={5000,4000,3000,2000,1000,500,250,100};
int gamedelay2[]={1000,1};
static int hitme=TRUE,hit = TRUE;
void init(void);
void nextstatus(void);
void draw(void);
void init(void)
{
int i;
for(i=0;i<MAX;i++)
{
place[i].x = 0;
place[i].y = 0;
place[i].st = FALSE;
}
place[0].st = TRUE;
place[1].st = TRUE;
place[1].x = 1;
speed = 9;
count = 0;
score = 0;
control = 4;
head = 1;
tear = 0;
x = 1;
y = 0;
babyx = rand()%MAXX;
babyy = rand()%MAXY;
eat = FALSE;
game = GAMESTART;
}
void nextstatus(void)
{
int i;
int exit;
int xx,yy;
xx = x;
yy = y;
switch(control)
{
case 1: y--; yy = y-1; break;
case 2: y++; yy = y+1; break;
case 3: x--; xx = x-1; break;
case 4: x++; xx = x+1; break;
}
hit = TRUE;
if ( ((control == 1) || (control ==2 )) && ( (y < 1) ||(y >= MAXY-1)) ||
(((control == 3) || (control == 4)) && ((x < 1) ||(x >= MAXX-1) ) ) )
{
hit = FALSE;
}
if ( (y < 0) ||(y >= MAXY) ||
(x < 0) ||(x >= MAXX) )
{
game = GAMEOVER;
control = 0;
return;
}
for (i = 0; i < MAX; i++)
{
if ((place[i].st) &&
(x == place[i].x) &&
(y == place[i].y) )
{
game = GAMEOVER;
control = 0;
return;
}
if ((place[i].st) &&
(xx == place[i].x) &&
(yy == place[i].y) )
{
hit = FALSE;
goto OUT;
}
}
OUT:
if ( (x == babyx) && (y == babyy) )
{
eat = TRUE;
count ++;
score += (1+class) * 10;
}
head ++;
if (head >= MAX) head = 0;
place[head].x = x;
place[head].y = y;
place[head].st= TRUE;
if (eat == FALSE)
{
place[tear].st = FALSE;
tear ++;
if (tear >= MAX) tear = 0;
}
else
{
eat = FALSE;
exit = TRUE;
while(exit)
{
babyx = rand()%MAXX;
babyy = rand()%MAXY;
exit = FALSE;
for( i = 0; i< MAX; i++ )
if( (place[i].st)&&( place[i].x == babyx) && (place[i].y == babyy))
exit ++;
}
}
if (head == tear) game = GAMEHAPPY;
}
void draw(void)
{
char temp[50];
int i,j;
for (i = 0; i < MAX; i++ )
{
setfillstyle(1,9);
if (place[i].st)
bar(place[i].x*15+1,place[i].y*10+1,place[i].x*15+14,place[i].y*10+9);
}
setfillstyle(1,4);
bar(babyx*15+1,babyy*10+1,babyx*15+14,babyy*10+9);
setcolor(8);
setfillstyle(1,8);
bar(place[head].x*15+1,place[head].y*10+1,place[head].x*15+14,place[head].y*10+9);
/* for( i = 0; i <= MAXX; i++ )
line( i*15,0, i*15, 10*MAXY);
for( j = 0; j <= MAXY; j++ )
line( 0, j*10, 15*MAXX, j*10);
*/
rectangle(0,0,15*MAXX,10*MAXY);
sprintf(temp,"Count: %d",count);
settextstyle(1,0,2);
setcolor(8);
outtextxy(512,142,temp);
setcolor(11);
outtextxy(510,140,temp);
sprintf(temp,"1P: %d",score);
settextstyle(1,0,2);
setcolor(8);
outtextxy(512,102,temp);
setcolor(12);
outtextxy(510,100,temp);
sprintf(temp,"Class: %d",class);
setcolor(8);
outtextxy(512,182,temp);
setcolor(11);
outtextxy(510,180,temp);
}
main()
{
int pause = 0;
char temp[50];
int d,m;
int key;
int p;
static int keydown = FALSE;
int exit = FALSE;
int stchange = 0;
d = VGA;
m = VGAMED;
initgraph( &d, &m, "" );
setbkcolor(3);
class = 3;
init();
p = 1;
while(!exit)
{
if (kbhit())
{
key = bioskey(0);
switch(key)
{
case UP: if( (control != 2)&& !keydown)
control = 1;
keydown = TRUE;
break;
case DOWN: if( (control != 1)&& !keydown)
control = 2;
keydown = TRUE;
break;
case LEFT: if( (control != 4)&& !keydown)
control = 3;
keydown = TRUE;
break;
case RIGHT: if( (control != 3)&& !keydown)
control = 4;
keydown = TRUE;
break;
case ESC: exit = TRUE;break;
case ENTER: init();break;
case PAGEUP: class --; if (class<0) class = 0; break;
case PAGEDOWN: class ++;if (class>7) class = 7; break;
case KEY_U: if( ( (control ==1) ||(control ==2))&& !keydown)
control = 3;
else if(( (control == 3) || (control == 4))&& !keydown)
control = 1;
keydown = TRUE;
break;
case KEY_K: if( ( (control ==1) ||(control ==2))&& !keydown)
control = 4;
else if(( (control == 3) || (control == 4))&& !keydown)
control = 2;
keydown = TRUE;
break;
case CTRL_P:pause = 1 - pause; break;
}
}
stchange ++ ;
putpixel(0,0,0);
if (stchange > gamedelay[class] + gamedelay2[hit])
{
stchange = 0;
keydown = FALSE;
p = 1 - p;
setactivepage(p);
cleardevice();
if (!pause)
nextstatus();
else
{
settextstyle(1,0,4);
setcolor(12);
outtextxy(250,100,"PAUSE");
}
draw();
if(game==GAMEOVER)
{
settextstyle(0,0,6);
setcolor(8);
outtextxy(101,101,"GAME OVER");
setcolor(15);
outtextxy(99,99,"GAME OVER");
setcolor(12);
outtextxy(100,100,"GAME OVER");
sprintf(temp,"Last Count: %d",count);
settextstyle(0,0,2);
outtextxy(200,200,temp);
}
if(game==GAMEHAPPY)
{
settextstyle(0,0,6);
setcolor(12);
outtextxy(100,300,"YOU WIN");
sprintf(temp,"Last Count: %d",count);
settextstyle(0,0,2);
outtextxy(200,200,temp);
}
setvisualpage(p);
}
}
closegraph();
}
具体的编译和界面还是要靠你。
2. c语言写贪吃蛇
#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
#include <dos.h> /*引用的库函数*/
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b/*宏定义键名*/
#define N 200
int i,key;
int level;/*游戏等级*/
int score=0;/*得分*/
int gamespeed;/*游戏速度*/
struct Food
{
int x;/*食物的横坐标*/
int y;/*食物的纵坐标*/
int yes;/*判断是否要出现食物的变量*/
}food;/*食物的结构体*/
struct Snake
{
int x[N];
int y[N];
int node;/*蛇的节数*/
int direction;/*蛇移动方向*/
int life;/* 蛇的生命,0活着,1死亡*/
}snake;/*蛇的结构体*/
void Choicelevle(void);/*选择游戏等级*/
void Init(void);/*图形驱动*/
void Close(void);/*图形结束*/
void DRAW(void);/*游戏区域*/
void GameOver(void);/*结束游戏*/
void GamePlay(void);/*玩游戏具体过程*/
void PrScore(void);/*输出成绩*/
/*主函数*/
void main(void)
{
Init();/*图形驱动*/
Choicelevle();/*选择游戏等级*/
DRAW();/*游戏区域*/
GamePlay();/*玩游戏具体过程*/
Close();/*图形结束*/
}
/*图形驱动*/
void Init(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"\\turboc2"); /*初始化图形系统*/
cleardevice(); /*清除图形界面*/
}
/*选择游戏等级*/
void Choicelevle(void)
{char
name[20];setcolor(YELLOW);settextstyle(0,0,6);outtextxy(150,150,"Snake");setcolor(GREEN);settextstyle(0,0,1);outtextxy(200,250,"please
put in your English name:");outtextxy(200,270,"Choice levle from
1-9.");outtextxy(300,320,"name:yangzilong");/*制作人姓名*
/outtextxy(300,350,"number:0902060226");/*制作人学号*
/outtextxy(300,380,"class:computer science
0602");/*制作人班级*/getch();printf("please putin your
name:");gets(name);printf("please choice
levle:");scanf("%d",&level);gamespeed=100000-400*level-300*level*level;if(level>9||level<1){cleardevice();
/*清除图形界面*/setcolor(YELLOW); /*设置字体颜色*/settextstyle(0,0,2);
/*设置字体类型*/outtextxy(150,200,"level input error");
/*显示文本*/getch();level=1;}
}
void DRAW(void)
{cleardevice(); /*清屏*/setcolor(2);setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*设置线型*/rectangle(45,45,465,325);}
/*玩游戏具体过程*/
void GamePlay(void)
{setcolor(5);setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*
设置线型*/randomize();/*随机数发生器*/food.yes=1;/*1表示需要出现新食物,0表示已经存在食物*
/snake.life=0;/*活着*/snake.direction=1;/*方向往右*/snake.x[0]=320;
snake.y[0]=240;/*蛇头*/snake.x[1]=330;snake.y[1]=240;
/*蛇的第二节位置*/snake.node=3;/*节数*/PrScore();/*输出得分*/while(1)/*可以重复玩游戏,压ESC键结
束*/{while(!kbhit())/*在没有按键的情况下,蛇自己移动身体*/{if(food.yes==1)/*需要出现新食物*
/{food.x=rand()%360+70;food.y=rand()%250+60;while(food.x%10!=0)/*食物随机出现后
必须让食物能够在整格内,这样才可以让蛇吃到*/food.x++;while(food.y%10!=0)food.y++;food.yes=0;
/*画面上有食物了*/}
if(food.yes==0)/*画面上有食物了就要显示*/
{
setcolor(GREEN);
rectangle(food.x,food.y,food.x+10,food.y-10);
}
for(i=snake.node-1;i>0;i--)/*蛇的每个环节往前移动*/
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
/*1,2,3,4表示右,左,上,下四个方向,通过这个控制来移动蛇头*/
switch(snake.direction)
{
case 1: snake.x[0]+=10;break;
case 2: snake.x[0]-=10;break;
case 3: snake.y[0]-=10;break;
case 4: snake.y[0]+=10;break;
}
for(i=3;i<snake.node;i++)/*从蛇的第四节开始判断是否撞到自己了,因为蛇头为两节,第三节不可能拐过来*/
{
if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0])
{
GameOver();/*显示失败*/
snake.life=1; /*蛇死*/
break;
}
}
/*如果蛇头碰到墙壁,蛇头从对面墙出来*/
if(snake.x[0]<50)
{snake.x[0]=450;
/*如果蛇头越过左边界,则从右边界进入*/snake.y[0]=snake.y[0];/*纵坐标不变*/for(i=snake.node-1;
i>0;i--){snake.x[i]=snake.x[i-1];snake.y[i]=snake.y[i-1];
/*蛇的其他节数向前推进*/}
{
setfillstyle(SOLID_FILL,0); /*设置填充模式和颜色,0表示黑色*/
bar(50,55,455,315);/*bar是表示填充的范围的函数*/
}
}
else
if(snake.x[0]>450)
{snake.x[0]=50;
/*如果蛇头越过右边界,则蛇头从左边界进入*/snake.y[0]=snake.y[0];/*纵坐标不变*/for(i=snake.node-
1;i>0;i--){snake.x[i]=snake.x[i-1];snake.y[i]=snake.y[i-1];
/*蛇的其他节数向前推进*/}
{
setfillstyle(SOLID_FILL,0); /*设置填充模式和颜色,0表示黑色*/
bar(50,55,455,315);/*bar是表示填充的范围的函数*/
}
}
else
if(snake.y[0]<60)
{snake.y[0]=320;
/*如果蛇头越过上边界,则从下边界进入*/snake.x[0]=snake.x[0];/*横坐标不变*/for(i=snake.node-1;
i>0;i--){snake.x[i]=snake.x[i-1];snake.y[i]=snake.y[i-1];
/*蛇的其他节数向前推进*/}
{
setfillstyle(SOLID_FILL,0); /*设置填充模式和颜色,0表示黑色*/
bar(50,55,455,315);/*bar是表示填充的范围的函数*/
}
}
else
if(snake.y[0]>320)
{snake.y[0]=60;
/*如果蛇头越过下边界,则从上边界进入*/snake.x[0]=snake.x[0];/*横坐标不变*/for(i=snake.node-1;
i>0;i--){snake.x[i]=snake.x[i-1];snake.y[i]=snake.y[i-1];
/*蛇的其他节数向前推进*/}
{
setfillstyle(SOLID_FILL,0); /*设置填充模式和颜色,0表示黑色*/
bar(50,55,455,315);/*bar是表示填充的范围的函数*/
}
}
if(snake.life==1)/*如果蛇死就跳出内循环,重新开始*/
break;
if(snake.x[0]==food.x&&snake.y[0]==food.y)/*吃到食物以后*/
{
setcolor(0);/*把画面上的食物东西去掉*/
rectangle(food.x,food.y,food.x+10,food.y-10); /*用当前线型和颜色画一矩形*/
snake.x[snake.node]=-20;snake.y[snake.node]=-20;
/*新的一节先放在看不见的位置,下次循环就取前一节的位置*/
snake.node++;/*蛇的身体长一节*/
food.yes=1;/*画面上需要出现新的食物*/
score+=10; /*每吃掉一食物,得分累加10分*/
if(score%100==0)
{level++;gamespeed=100000-400*level-
300*level*level;/*每吃掉10食物提升一级,速度加快*/PrScore();/*输出新得分*/setcolor(YELLOW);
/*设置字体颜色*/settextstyle(0,0,4); /*设置字体类型*/outtextxy(150,200,"LEVEL UP");
/*显示文本*/if(level==10){level=1,gamespeed=100000-400*level-
300*level*level;}
delay(6000000);
delay(6000000);
delay(6000000);
delay(6000000);
delay(6000000);
delay(6000000);
delay(6000000);
bar(50,55,455,315);/*bar是表示填充的范围的函数*/
}
PrScore();/*输出新得分*/
}
setcolor(4);/*画出蛇*/
for(i=0;i<snake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,
snake.y[i]-10);
delay(gamespeed); /*控制游戏速度*/
setcolor(0);
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
} /*endwhile(!kbhit)*/ /*用黑色去除蛇的的最后一节*/
if(snake.life==1)/*如果蛇死就跳出循环*/
break;
key=bioskey(0);/*接收按键*/
if(key==ESC)/*按ESC键退出*/
break;
else
if(key==UP&&snake.direction!=4)
/*判断是否往相反的方向移动*/
snake.direction=3;
else
if(key==RIGHT&&snake.direction!=2)
snake.direction=1;
else
if(key==LEFT&&snake.direction!=1)
snake.direction=2;
else
if(key==DOWN&&snake.direction!=3)
snake.direction=4;
}/*endwhile(1)*/
}
/*游戏结束*/
void GameOver(void)
{
cleardevice(); /*清屏*/
PrScore();
setcolor(RED); /*设置字体颜色*/
settextstyle(0,0,4); /*设置字体类型*/
outtextxy(200,200,"GAME OVER"); /*显示文本*/
getch();
}
/*输出成绩及游戏等级*/
void PrScore(void)
{
char str1[20];/*设置字符型数组*/
setfillstyle(SOLID_FILL,0);
bar(50,15,390,35); /*填充矩形框*/
setcolor(6); /*设置文本颜色*/
settextstyle(0,0,2); /*设置数组显示位置*/
sprintf(str1,"score %d level %d",score,level);/*显示数组内容*/
outtextxy(55,20,str1);
setcolor(YELLOW); /*设置字体颜色*/
settextstyle(0,0,2); /*设置字体类型*/
outtextxy(250,400,"EXIT=ESC ");/*显示文本*/
}
void Close(void)
{
closegraph();
}
3. c语言 贪吃蛇 程序
基本思路:
蛇每吃一个食物蛇身子就增加一格,用UP, DOWN, LEFT, RIGHT控制蛇头的运动,而蛇身子跟着蛇头走,每后一格蛇身子下一步走到上一格蛇身子的位置,以此类推。
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#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;
}
(3)贪吃蛇编程C语言程序设计流程图扩展阅读:
实现逻辑
1,可以设置光标,就能实现制定位置打印制定符号。
2,涉及一个结构体,包含两个元素坐标元素和一个结构体指针。
3,结构体串联形成链表,遍历获取成员坐标,打印符号得到蛇身。
4,不断的加头,去尾,重新遍历坐标,再打印形成蛇的移动。
5,食物产生的位置判定,不能越界,也不能与蛇身体重合。
6,蛇的转向判定,一条规则,不允许倒退。
7,转向的实现,跟行进方向决定新的关节坐标(当前头的上下左右)
8,死亡检测,是否头节点坐标是否与墙壁重合,是否与身体其他关节重合。
9,加速减速,设置刷新休眠时间实现。
4. 在dos环境下c语言编程编一个贪吃蛇游戏
程序设计及说明
1
、边墙(
Wall
)
该类规定游戏的范围大小。
2
、蛇类(
Snake
)
用该类生成一个实例蛇
snake
。
3
、移动(
Move
)
该类用于实现对蛇的操作控制,即蛇头方向的上下左右的移动操作。
4
、食物类(
Food
)
该类是游戏过程中食物随机产生的控制和显示。
5
、判断死亡(
Dead
)
该类是对游戏过程中判断玩家操作是否导致蛇的死亡,其中包括蛇头咬食自己身体和蛇头是否触
及游戏“边墙”。
6
、蛇结点(
SnakeNode
)
该类是蛇吃下随机产生的食物从而增加长度的控制类,其中包括蛇长度增加和尾部的变化。
7
、计分统计(
Score
)
该类由于玩家的游戏成绩记录,及游戏结束时的得分输出。
...
部分函数及说明
1.Char menu();
/*
用于玩家选择的游戏速度,返回一个
char
值
*/
2.DELAY(char ch1);
/*
用于控制游戏速度
*/
3.void drawmap();
/*
绘制游戏地图函数
*
4
、
void menu()
/*
游戏帮助信息的输出
*
...
部分类细节解说
1
、蛇的构建
—
Snake
class Snake{
public:
int x[n]
;
int y[n];
int node;
//
蛇身长度
int direction;//
蛇运动方向
int
life;//
蛇生命,判断死亡
}
2
、随机食物
Food
利用
rand(
)函数进行随机数产生,然后就行坐标定位
void Food(void){
...
int pos_x = 0;
int pos_y = 0;
pos_x = rand() % length;//x
坐标的确定
pos_y = rand() % (width-1);//y
坐标的确定
...
}
3
、蛇头方向确定
利用
switch
语句进行方向确定
...
switch(){
case VK_UP:{
OutChar2.Y--;
y--;
break;
}
case VK_LEFT:{
OutChar2.Y++;
y++;
break;
}
case VK_DOWN:{
OutChar2.X---;
x--;
break;
}
case 'VK_RIGHT:{
OutChar2.X++;
x++;
break;
}
}
代码
#include <iostream>
#include <ctime>
#include <conio.h>
#include <windows.h>
#include <time.h>
using namespace std;
int score=0,t=300,f=1;//
得分与时间间隔
/ms
(控制贪吃蛇的速度)
double ss=0,tt=0;//
统计时间所用参数
class Node
{
Node(): x(0), y(0), prior(0), next(0) { }
int x;
int y;
Node *prior;
Node *next;
friend class Snake;
};
class Snake
{
public:
Snake();
~Snake();
void output();
void move();
void change_point(char);
private:
Node *head;
Node *tail;
enum p{ UP
, RIGHT, DOWN, LEFT }point; //
方向
int food_x, food_y; //
食物的坐标
static const int N = 23;
int game[N][N];
void add_head(int, int); //
添加坐标为
a,b
的结点
void delete_tail(); //
删除最后一个结点
void greate_food(); //
产生食物
void gotoxy(int, int);
};
void menu();
//
游戏操作菜单
int main()
{
system("color a");
//
初始
cmd
窗口颜色为黑(背景)淡绿(文字)
cout<<"\n\n\n\n\n\n
";
for(int i=0;i<23;i++)
{char star[]={"Welcome To Snake Game!"};
cout<<star[i];
Sleep(170);}
cout<<"\n\n
祝你好运!
"<<endl;
Sleep(3000);
if(kbhit()){char kk=getch();if(kk==9)f=5;} //
如果执行,吃一颗星加
5
分
system("cls");
Snake s;
menu();
system("color 1a");
s.output();
while (true)
{
char keydown = getch();
if(keydown==32)getch();
if(keydown==27)return 0;
s.change_point(keydown);
while (!kbhit())
{clock_t start,end;start=clock();
s.move();
s.output();
Sleep(t);
end=clock();tt=(double)(end-start)/CLOCKS_PER_SEC;ss+=tt;
cout<<"
时间:
"<<(int)ss;
5. C语言做贪吃蛇用什么软件编写
问题上已经说了,是用C语言做的贪吃蛇,那么编写它的软件就是:C语言(一门通用计算机编程语言,广泛应用于底层开发)。
6. 贪吃蛇游戏的C语言编程
#include <windows.h>
#include <ctime>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
#ifndef SNAKE_H
#define SNAKE_H
class Cmp
{
friend class Csnake;
int rSign; //横坐标
int lSign; //竖坐标
public:
// friend bool isDead(const Cmp& cmp);
Cmp(int r,int l){setPoint(r,l);}
Cmp(){}
void setPoint(int r,int l){rSign=r;lSign=l;}
Cmp operator-(const Cmp &m)const
{
return Cmp(rSign-m.rSign,lSign-m.lSign);
}
Cmp operator+(const Cmp &m)const
{
return Cmp(rSign+m.rSign,lSign+m.lSign);
}
};
const int maxSize = 5; //初始蛇身长度
class Csnake
{
Cmp firstSign; //蛇头坐标
Cmp secondSign;//蛇颈坐标
Cmp lastSign; //蛇尾坐标
Cmp nextSign; //预备蛇头
int row; //列数
int line; //行数
int count; //蛇身长度
vector<vector<char> > snakeMap;//整个游戏界面
queue<Cmp> snakeBody; //蛇身
public:
int GetDirections()const;
char getSymbol(const Cmp& c)const
//获取指定坐标点上的字符
{
return snakeMap[c.lSign][c.rSign];
}
Csnake(int n)
//初始化游戏界面大小
{
if(n<20)line=20+2;
else if(n>30)line=30+2;
else line=n+2;
row=line*3+2;
}
bool isDead(const Cmp& cmp)
{
return ( getSymbol(cmp)=='@' || cmp.rSign == row-1
|| cmp.rSign== 0 || cmp.lSign == line-1 ||
cmp.lSign == 0 );
}
void InitInstance(); //初始化游戏界面
bool UpdataGame(); //更新游戏界面
void ShowGame(); //显示游戏界面
};
#endif // SNAKE_H
using namespace std;
//测试成功
void Csnake::InitInstance()
{
snakeMap.resize(line); // snakeMap[竖坐标][横坐标]
for(int i=0;i<line;i++)
{
snakeMap[i].resize(row);
for(int j=0;j<row;j++)
{
snakeMap[i][j]=' ';
}
}
for(int m=1;m<maxSize+1;m++)
{
//初始蛇身
snakeMap[line/2][m]='@';
//将蛇身坐标压入队列
snakeBody.push(Cmp(m,(line/2)));
//snakeBody[横坐标][竖坐标]
}
//链表头尾
firstSign=snakeBody.back();
secondSign.setPoint(maxSize-1,line/2);
}
//测试成功
int Csnake::GetDirections()const
{
if(GetKeyState(VK_UP)<0) return 1; //1表示按下上键
if(GetKeyState(VK_DOWN)<0) return 2; //2表示按下下键
if(GetKeyState(VK_LEFT)<0) return 3; //3表示按下左键
if(GetKeyState(VK_RIGHT)<0)return 4; //4表示按下右键
return 0;
}
bool Csnake::UpdataGame()
{
//-----------------------------------------------
//初始化得分0
static int score=0;
//获取用户按键信息
int choice;
choice=GetDirections();
cout<<"Total score: "<<score<<endl;
//随机产生食物所在坐标
int r,l;
//开始初始已经吃食,产生一个食物
static bool eatFood=true;
//如果吃了一个,才再出现第2个食物
if(eatFood)
{
do
{
//坐标范围限制在(1,1)到(line-2,row-2)对点矩型之间
srand(time(0));
r=(rand()%(row-2))+1; //横坐标
l=(rand()%(line-2))+1;//竖坐标
//如果随机产生的坐标不是蛇身,则可行
//否则重新产生坐标
if(snakeMap[l][r]!='@')
{snakeMap[l][r]='*';}
}while (snakeMap[l][r]=='@');
}
switch (choice)
{
case 1://向上
//如果蛇头和社颈的横坐标不相同,执行下面操作
if(firstSign.rSign!=secondSign.rSign)nextSign.setPoint(firstSign.rSign,firstSign.lSign-1);
//否则,如下在原本方向上继续移动
else nextSign=firstSign+(firstSign-secondSign);
break;
case 2://向下
if(firstSign.rSign!=secondSign.rSign)nextSign.setPoint(firstSign.rSign,firstSign.lSign+1);
else nextSign=firstSign+(firstSign-secondSign);
break;
case 3://向左
if(firstSign.lSign!=secondSign.lSign)nextSign.setPoint(firstSign.rSign-1,firstSign.lSign);
else nextSign=firstSign+(firstSign-secondSign);
break;
case 4://向右
if(firstSign.lSign!=secondSign.lSign)nextSign.setPoint(firstSign.rSign+1,firstSign.lSign);
else nextSign=firstSign+(firstSign-secondSign);
break;
default:
nextSign=firstSign+(firstSign-secondSign);
}
//----------------------------------------------------------
if(getSymbol(nextSign)!='*' && !isDead(nextSign))
//如果没有碰到食物(且没有死亡的情况下),删除蛇尾,压入新的蛇头
{
//删除蛇尾
lastSign=snakeBody.front();
snakeMap[lastSign.lSign][lastSign.rSign]=' ';
snakeBody.pop();
//更新蛇头
secondSign=firstSign;
//压入蛇头
snakeBody.push(nextSign);
firstSign=snakeBody.back();
snakeMap[firstSign.lSign][firstSign.rSign]='@';
//没有吃食
eatFood=false;
return true;
}
//-----吃食-----
else if(getSymbol(nextSign)=='*' && !isDead(nextSign))
{
secondSign=firstSign;
snakeMap[nextSign.lSign][nextSign.rSign]='@';
//只压入蛇头
snakeBody.push(nextSign);
firstSign=snakeBody.back();
eatFood=true;
//加分
score+=20;
return true;
}
//-----死亡-----
else {cout<<"Dead"<<endl;cout<<"Your last total score is "<<score<<endl; return false;}
}
void Csnake::ShowGame()
{
for(int i=0;i<line;i++)
{
for(int j=0;j<row;j++)
cout<<snakeMap[i][j];
cout<<endl;
}
Sleep(1);
system("cls");
}
int main()
{
Csnake s(20);
s.InitInstance();
//s.ShowGame();
int noDead;
do
{
s.ShowGame();
noDead=s.UpdataGame();
}while (noDead);
system("pause");
return 0;
}
这个代码可以运行的,记得给分啦