贪吃蛇源码(C语⾔版)贪吃蛇其实就是实现以下⼏步——
1:蛇的运动(通过“画头擦尾”来达到蛇移动的视觉效果)
2:⽣成⾷物
3:蛇吃⾷物(实现“画头不擦尾”)
4:游戏结束判断(也就是蛇除了⾷物,其余东西都不能碰)
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
#include<time.h>
#define width 60
#define hight 25
#define SNAKESIZE 200//蛇⾝的最长长度
int key=72;//初始化蛇的运动⽅向,向上
int changeflag=1;//⽤来标识是否⽣成⾷物,1表⽰蛇还没吃到⾷物,0表⽰吃到⾷物
int speed=0;//时间延迟
struct{
int len;
//⽤来记录蛇⾝每个⽅块的坐标
int x[SNAKESIZE];
int y[SNAKESIZE];
int speed;
}snake;
struct
{
int x;int y;
}food;
void gotoxy(int x,int y)//调⽤Windows的API函数,可以在控制台的指定位置直接操作,这⾥可暂时不⽤深究{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
//■○
void drawmap()
{
//打印图框
for(int _y =0; _y < hight; _y++)
{
for(int x =0; x < width; x+=2)
{
if(x ==0|| _y ==0|| _y == hight -1|| x == width -2)
{
gotoxy(x, _y);
c语言游戏贪吃蛇源码
printf("■");
}
}
}
//打印蛇头
snake.len=3;
snake.x[0]=width/2;
snake.y[0]=hight/2;
gotoxy(snake.x[0],snake.y[0]);
printf("■");
printf("■");
/
/打印蛇⾝
for(int i=1;i<snake.len;i++)
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1]+1;
gotoxy(snake.x[i],snake.y[i]);
printf("■");
}
//初始化⾷物的位置
food.x=20;
food.y=20;
gotoxy(food.x,food.y);
printf("○");
}
/*
*控制台按键所代表的数字
*“↑”:72
*“↓”:80
*“←”:75
*“→”:77
*/
void snake_move()//按键处理函数
{
int history_key=key;
if(_kbhit())
{
fflush(stdin);
key =_getch();
key =_getch();
}
if(changeflag==1)//还没吃到⾷物,把尾巴擦掉
{
gotoxy(snake.x[snake.len-1],snake.y[snake.len-1]);
printf(" ");
}
for(int i=snake.len-1;i>0;i--)
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
if(history_key==72&&key==80) key=72;
if(history_key==80&&key==72) key=80;
if(history_key==75&&key==77) key=75;
if(history_key==77&&key==75) key=77;
switch(key)
{
case72:snake.y[0]--;break;
case75:snake.x[0]-=2;break;
case77:snake.x[0]+=2;break;
case80:snake.y[0]++;break;
}
gotoxy(snake.x[0],snake.y[0]);
printf("■");
gotoxy(0,0);
changeflag=1;
}
void creatfood()
{
if(snake.x[0]== food.x && snake.y[0]== food.y)//只有蛇吃到⾷物,才能⽣成新⾷物{
changeflag=0;
snake.len++;
if(speed<=100)speed+=10;
while(1)
{
srand((unsigned int)time(NULL));
food.x=rand()%(width-6)+2;//限定⾷物的x范围不超出围墙,但不能保证food.x为偶数 food.y=rand()%(hight-2)+1;
for(int i=0;i<snake.len;i++)
{
if(food.x==snake.x[i]&&food.y==snake.y[i])//如果产⽣的⾷物与蛇⾝重合则退出break;
}
if(food.x%2==0)break;//符合要求,退出循环
}
gotoxy(food.x,food.y);printf("○");
}
}
bool Gameover()
{
/
/碰到围墙,OVER
if(snake.x[0]==0||snake.x[0]==width-2)return false;
if(snake.y[0]==0||snake.y[0]==hight-1)return false;
//蛇⾝达到最长,被迫OVER
if(snake.len==SNAKESIZE)return false;
//头碰到蛇⾝,OVER
for(int i=1;i<snake.len;i++)
{
if(snake.x[0]==snake.x[i]&&snake.y[0]==snake.y[i])
return false;
}
return true;
}
int main()
{
system("mode con cols=60 lines=27");
drawmap();
while(Gameover())
{
snake_move();
creatfood();
Sleep(350-speed);//蛇的移动速度
}
return0;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论