用C语言,能在100行之内实现贪吃蛇吗?恕我直言贪吃蛇这个游戏虽小但涉及的方面还是不少的,想要做一个完整的贪吃蛇100行应该不好实现。建议楼主试试这个。#include<stdio.h>#in
用C语言,能在100行之内实现贪吃蛇吗?
恕我直言贪吃蛇这个游戏虽小但涉及的方面还是不少的,想要做一个完整的贪吃蛇100行应该不好实现。建议楼主试试这个(繁体:個)。
#include
#include
#include
#include
#define U 1
#define D 2
#define L 3
#define R 4 //蛇的状(繁:狀)态,U:上 ;D:下;L:左 R:右
typedef struct SNAKE //蛇(练:shé)身的一个节点
{
int x
int y
struct SNAKE *next
}snake
//全局(繁:侷)变量//
int score=0,add=10//总得分[pinyin:fēn]与每次吃食物得分。
int status,sleeptime=200//每{拼音:měi}次运行的时间间隔
snake *head, *food//蛇头指针,食[shí]物指针
snake *q//遍历蛇的时【pinyin:shí】候用到的指针
int endgamestatus=0 //游戏结束的情况,1:撞到墙;2:咬《yǎo》到自己{读:jǐ};3:主动{练:dòng}退出游戏。
//声[繁体:聲]明全部函数//
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()//创建地图[tú]
{
int i
for(i=0i<58i =2)//打(拼音:dǎ)印上下边框
{
Pos(i,0)
printf("■")
Pos(i,26)
printf("■")
}
for(i=1i<26i )//打印左《pinyin:zuǒ》右边框
{
Pos(0,i)
printf("■")
Pos(56,i)
printf("■")
}
}
void initsnake()//初[读:chū]始化蛇身
{
snake *tail
int i
tail=(snake*)malloc(sizeof(snake))//从蛇尾开始,头(繁体:頭)插法,以x,y设定开始的位置//
tail->x=24
tail->y=5
tail->next=NULL
for(i=1i<=4i )
{
head=(snake*)malloc(sizeof(snake))
head->next=tail
head->x=24 2*i
head->y=5
tail=head
}
while(tail!=NULL)//从头到[拼音:dào]为,输出蛇身
{
Pos(tail->x,tail->y)
printf("■")
tail=tail->next
}
}
int biteself()//判断是否咬(读:yǎo)到了自己
{
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()//随机出现食物《拼音:wù》
{
snake *food_1
srand((unsigned)time(NULL))
food_1=(snake*)malloc(sizeof(snake))
while((food_1->x%2)!=0) //保证其为偶数,使得食物能与蛇头(繁:頭)对其
{
food_1->x=rand()R 2
}
food_1->y=rand()$ 1
q=head
while(q->next==NULL)
{
if(q->x==food_1->x && q->y==food_1->y) //判断蛇身是否与食物(wù)重合
{
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,下{练:xià}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)//如【读:rú】果下一个有食物//
{
nexthead->next=head
head=nexthead
q=head
while(q!=NULL)
{
Pos(q->x,q->y)
printf("■")
q=q->next
}
score=score add
createfood()
}
else //如果没有食物(拼音:wù)//
{
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) //有(拼音:yǒu)食物
{
nexthead->next=head
head=nexthead
q=head
while(q!=NULL)
{
Pos(q->x,q->y)
printf("■")
q=q->next
}
score=score add
createfood()
}
else //没【méi】有食物
{
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)//有{拼音:yǒu}食物
{
nexthead->next=head
head=nexthead
q=head
while(q!=NULL)
{
Pos(q->x,q->y)
printf("■")
q=q->next
}
score=score add
createfood()
else //没有{练:yǒu}食物
{
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)//有食物(读:wù)
{
nexthead->next=head
q=head
while(q!=NULL)
{
Pos(q->x,q->y)
printf("■")
}
score=score add
createfood()
}
else //没有食(shí)物
{
nexthead
本文链接:http://21taiyang.com/Family/20417677.html
网站上{pinyin:shàng}传贪吃蛇网页版游戏代码转载请注明出处来源