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;
}
這個代碼可以運行的,記得給分啦