c语言实现一个简易贪吃蛇还是比较容易的,强一点的同学在学完数组可能就可以写出来,差一点的同学像我一样学完结构体琢磨一下写出来也比较容易。
下面是写贪吃蛇是查阅的一些函数
1.隐藏光标函数
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor;
cursor.bVisible = FALSE;
cursor.dwSize = sizeof(cursor);
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorInfo(handle, &cursor);
}
使用此函数可以隐藏后面一闪一闪的光标,也就是这个小竖杠。
2.光标设置函数
void set(int x, int y)
{
HANDLE hd;
COORD pos;
pos.X = x;
pos.Y = y;
hd = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hd, pos);
}
这个函数可以实现光标设置,也就是能在任意位置输出想要的数据,传入的x,y是行数和列数。
即在x行y列输出。
3.随机值输出
用rand函数来输出随机值,需要用srand函数来给rand函数传入一个种子
srand函数设置种子的参数可以用时间函数(返回值是不断变化的时间戳)来进行设置。
需要进行一下类型的转换
srand((unsigned int)time(NULL));//设置种子
int x=rand()%y//产生0~(y-1)的随机数
需要加上头文件<time.h>,<stdlib.h>
4.暂停函数
可以让程序暂停一段时间
sleep()//参数为要暂停的时间,单位为毫秒。
需要加上头文件<windows.h>。
5.清屏函数
可以清空控制台的输出
system("cls")
需要加上头文件<windoes.h>。
学会以上函数加上学到数组的知识就可以写出一个简易贪吃蛇啦!有兴趣的小伙伴快动手试试吧。
下面是本人滴贪吃蛇实现
#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#define shang 119
#define xia 115
#define zuo 97
#define you 100
#define hang 20
#define lie 20
struct sneakhead {
int HFangXiang;
int x; int y;
int num;
}s1,shi;
sneakhead ss[100];
void set(int, int);//设置光标位置
void HideCursor();//隐藏光标
void myprintf(sneakhead);//打印蛇头
void printbian();//打印边框
int panduanzq(sneakhead);//判断是否撞墙
void yidong(sneakhead &s1);//移动
int panduanchishi(sneakhead&, sneakhead);//吃食物
void GaiFanagXiang(int , sneakhead& s1);//更改蛇方向
void sheshenfuzhi(sneakhead s1, sneakhead ss[]);
void printfsheshen(sneakhead ss[]);
int panduanzzz(sneakhead s1, sneakhead ss[]);
int main()
{
sneakhead ss [100]={};
srand((unsigned int)time(NULL));
HideCursor();
int ch; int sh;
s1.x = 1, s1.y = 1; s1.num = 0;
ss[0].x = 0; ss[0].y = 0;
s1.HFangXiang = xia;
shi.x = (rand() %( hang-2)) + 1;
shi.y = (rand() % (lie-2)) + 1;
system("title 贪吃蛇");
while (1) {
printbian();
if (panduanchishi(shi, s1) == 1)
{
s1.num++;
}
sheshenfuzhi(s1, ss);
if (_kbhit()) {
ch = _getch();
sh = ch;
GaiFanagXiang(sh, s1);
}
yidong(s1);
myprintf(s1);
printfsheshen(ss);
myprintf(shi);
Sleep(100);
system("cls");
if(panduanzq(s1)==0)
break;
if (panduanzzz(s1,ss) == 0)
break;
}
printf("得分%d", s1.num);
Sleep(500);
}
int panduanzzz(sneakhead s1, sneakhead ss[])
{
int i = s1.num - 1;
while (i >= 0)
{
if (ss[i].x == s1.x && ss[i].y == s1.y)
return 0;
i--;
}
return 1;
}
void printfsheshen(sneakhead ss[])
{
int i = 0;
while (ss[i].x)
{
myprintf(ss[i]);
i++;
}
}
void sheshenfuzhi(sneakhead s1,sneakhead ss[]) {
int i = s1.num-1;
while (i > 0)
{
ss[i].x = ss[i - 1].x;
ss[i].y = ss[i - 1].y;
i--;
}
if(s1.num!=0)
ss[0].x = s1.x; ss[0].y = s1.y;
}
int panduanchishi(sneakhead& shi, sneakhead s1)
{
if (shi.x == s1.x && shi.y == s1.y)
{
shi.x =(rand() % (hang-2)) + 1; shi.y = (rand() % (lie-2))+1;
return 1;
}
return 0;
}
int panduanzq(sneakhead s1) {
if (s1.x < 1 || s1.x>(hang - 2) || s1.y < 1 || s1.y>(lie - 2))
{
system("cls");
printf("你输了");
return 0;
}
return 1;
}
void printbian() {
int y = 0;
for (int x = 0; x < hang; x++)
{
set(x, y);
printf("■");
}
y = lie-1;
for (int x = 0; x < hang; x++)
{
set(x, y);
printf("■");
}
int x = 0;
for (int y = 0; y < lie; y++)
{
set(x, y);
printf("■");
}
x = hang-1;
for (int y = 0; y < lie; y++)
{
set(x, y);
printf("■");
}
}
void GaiFanagXiang(int ch, sneakhead& s1)
{
switch (ch)
{
case shang:if (s1.HFangXiang == xia) break;
else s1.HFangXiang = ch; break;
case xia:if (s1.HFangXiang == shang) break;
else s1.HFangXiang = ch; break;
case zuo:if (s1.HFangXiang == you) break;
else s1.HFangXiang = ch; break;
case you:if (s1.HFangXiang == zuo) break;
else s1.HFangXiang = ch; break;
}
}
void yidong(sneakhead &s1)
{
switch (s1.HFangXiang)
{
case shang:s1.y -= 1; break;
case xia:s1.y += 1;break;
case zuo:s1.x -= 1;break;
case you:s1.x += 1;break;
}
}
void myprintf (sneakhead s1)
{
set(s1.x, s1.y), printf("■");
}
void set(int x, int y)
{
HANDLE hd;
COORD pos;
pos.X = x;
pos.Y = y;
hd = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hd, pos);
}
//定义隐藏光标函数
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor;
cursor.bVisible = FALSE;
cursor.dwSize = sizeof(cursor);
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorInfo(handle, &cursor);
}