C语言贪吃蛇简单实现

C语言贪吃蛇简单实现#include<stdio.h>#include<windows.h>#include<conio.h>#include<stdlib.h>#include<time.h>//18*30//z=directioninnumber;up-1\right-2\down-3\left-4.staticintx=1,y=4…

#include<stdio.h>

#include<windows.h>

#include<conio.h>

#include<stdlib.h>

#include<time.h>

// 18*30

// z=direction in number;up-1\right-2\down-3\left-4.

static int x = 1, y = 4, z = 5, g, h;

static char dir;

int input(int i, float q)

{


float a;


for(a = q;a >0; a –)


{


Sleep(100);


//函数名:kbhit()


//功能及返回值: 检查当前是否有键盘输入,若有则返回一个非0值,否则返回0


if (_kbhit())


{


// 函数名: getch()


// 功能及返回值: 从键盘上读取到的字符


dir = _getch();


switch (dir)


{


case ‘w’: x = x – 1; break;


case ‘d’: y = y + 1; break;


case ‘s’: x = x + 1; break;


case ‘a’: y = y – 1; break;


default: dir = dir;


}


a = 0.5;


}


if (a==1)


{


switch (dir)


{


case ‘w’: x = x – 1; break;


case ‘d’: y = y + 1; break;


case ‘s’: x = x + 1; break;


case ‘a’: y = y – 1; break;


}


a = 0.5;


}






return i;

}

float difficult(float q)

{


q = 10;


return q;

}

void finish(int p)

{


if (p == 0)


    printf(”   GAME OVER\n  length: %d\n by Iris Studio\n”,z-1);


if (p == 2)


    printf(“大爷你心情不错啊?!\n你再来一遍啊?\n”);

}

main()

{


printf(“w,a,s,d操作. 按回车键开始\n\nby :鸢尾工作室\n\nps,显示不全请放大显示框”);


getchar();


int   p=1, f=1, i, j, k;


float  q = 1;


//x,y是定位,z是方向,f是食物,p是判断, q是难度;


int   m = 8, n = 8;//食物位置


dir = ‘d’;


int map[20][33];


char pri[40][97];


x = 1, y = 4, z = 5;





// 以0作为底面背景。


for (i = 0; i < 20; i++)


for (j = 0; j < 32; j++)


{


map[i][j] = 0;


}


for (i = 0; i < 20; i++)


{


map[i][0] = -8;


map[i][31] = -8;


}


for (i = 0; i < 32; i++)


{


map[0][i] = -8;


map[19][i] = -8;


}


map[1][1] = 2; map[1][2] = 3; map[1][3] = 4;//以上完成了底板和边框



q=difficult(q);//难度选择,目前不支持更换,默认为1


while (p == 1)


{


if (x == m && y == n)


f = 1, z++, map[m][n] = 0;





map[x][y] = z + map[x][y];


if (map[x][y] != z)


if (x != m || y != n)


p = 0;





f–;


if (f == 0)


{


k = 0;


for (i = 1; i < 19; i++)


for (j = 1; j < 31; j++)


{


if (map[i][j] == 0)


k++;


}


if (k == 0)


{


p = 2;


finish(p);


}


srand(time(0));//随机食物位置


k = rand() % k;//随机食物位置


for (i = 1; i < 19; i++)


for (j = 1; j < 31; j++)


{


if (map[i][j] == 0)


k–;


if (k == 0)


{


m = i, n = j, f = 1;


map[i][j] = 40;//食物出现时间


f = map[i][j];


}


}


}





for (i = 1; i < 19; i++)


for (j = 1; j < 31; j++)


{


if(map[i][j] != 0)


map[i][j] -= 1;


}


system(“cls”);//清屏


//printf(“x=%d  y=%d\n”, x, y);





/*//输出图像方式一


for (i = 0; i < 20; i++)


{


for (j = 0; j < 32; j++)


//map[i][j] += 1;


printf(“%d “, map[i][j]);


printf(“\n”);


}


*/


//输出图像方式二


for (i = 0; i < 20; i++)


{


for (j = 0; j < 32; j++)


{


if (map[i][j] == -8)


{


pri[2 * i][3 * j] = ‘8’, pri[2 * i][3 * j + 1] = ‘8’, pri[2 * i][3 * j + 2] = ‘8’;


pri[2 * i + 1][3 * j] = ‘8’, pri[2 * i + 1][3 * j + 1] = ‘8’, pri[2 * i + 1][3 * j + 2] = ‘8’;


}


if (map[i][j] == 0)


{


pri[2 * i][3 * j] = ‘\0’, pri[2 * i][3 * j + 1] = ‘\0’, pri[2 * i][3 * j + 2] = ‘\0’;


pri[2 * i + 1][3 * j] = ‘\0’, pri[2 * i + 1][3 * j + 1] = ‘\0’, pri[2 * i + 1][3 * j + 2] = ‘\0’;


}


if (map[i][j] > 0)


{


pri[2 * i][3 * j] = ‘\0’, pri[2 * i][3 * j + 1] = ‘>’, pri[2 * i][3 * j + 2] = ‘<‘;


pri[2 * i + 1][3 * j] = ‘>’, pri[2 * i + 1][3 * j + 1] = ‘<‘, pri[2 * i + 1][3 * j + 2] = ‘\0’;


}


if (i == x && j == y)


{


pri[2 * i][3 * j] = ‘$’, pri[2 * i][3 * j + 1] = ‘@’, pri[2 * i][3 * j + 2] = ‘#’;


pri[2 * i + 1][3 * j] = ‘&’, pri[2 * i + 1][3 * j + 1] = ‘*’, pri[2 * i + 1][3 * j + 2] = ‘%’;


}


}


}





for (i = 0; i < 40; i++)


{


for (j = 0; j < 97; j++)


{



printf(“%c”, pri[i][j]);



}


printf(“\n”);


}





if(p==1)


input(z,q);


}


system(“cls”);//清屏


finish(p);

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/37835.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注