本篇文章给大家谈谈微信小程序开发代码c,以及微信小程序开发代码编写对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。 今天给各位分享微信小程序开发代码c的知识,其中也会对微信小程序开发代码编写进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

  1. c语言贪吃蛇代码及解析?

1、c语言贪吃蛇代码及解析?

#includelt;stdio.hgt;

#includelt;time.hgt;

#includelt;windows.hgt;

#includelt;stdlib.hgt;

#define U 1

#define D 2

#define L 3

#define R 4 //蛇的状态,U:上 ;D:下;L:左 R:右

typedef struct SNAKE //蛇身的一个节点

{

int x;

int y;

struct SNAKE *next;

}snake;

//全局变量//

int score=0,add=10;//总得分与每次吃食物得分。

int status,sleeptime=200;//每次运行的时间间隔

snake *head, *food;//蛇头指针,食物指针

snake *q;//遍历蛇的时候用到的指针

int endgamestatus=0; //游戏结束的情况,1:撞到墙;2:咬到自己;3:主动退出游戏。

//声明全部函数//

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()//创建地图

{

int i;

for(i=0;ilt;58;i =2)//打印上下边框

{

Pos(i,0);

printf("■");

Pos(i,26);

printf("■");

}

for(i=1;ilt;26;i )//打印左右边框

{

Pos(0,i);

printf("■");

Pos(56,i);

printf("■");

}

}

void initsnake()//初始化蛇身

{

snake *tail;

int i;

tail=(snake*)malloc(sizeof(snake));//从蛇尾开始,头插法,以x,y设定开始的位置//

tail-gt;x=24;

tail-gt;y=5;

tail-gt;next=NULL;

for(i=1;ilt;=4;i )

{

head=(snake*)malloc(sizeof(snake));

head-gt;next=tail;

head-gt;x=24 2*i;

head-gt;y=5;

tail=head;

}

while(tail!=NULL)//从头到为,输出蛇身

{

Pos(tail-gt;x,tail-gt;y);

printf("■");

tail=tail-gt;next;

}

}

int biteself()//判断是否咬到了自己

{

snake *self;

self=head-gt;next;

while(self!=NULL)

{

if(self-gt;x==head-gt;x amp;amp; self-gt;y==head-gt;y)

{

return 1;

}

self=self-gt;next;

}

return 0;

}

void createfood()//随机出现食物

{

snake *food_1;

srand((unsigned)time(NULL));

food_1=(snake*)malloc(sizeof(snake));

while((food_1-gt;x%2)!=0) //保证其为偶数,使得食物能与蛇头对其

{

food_1-gt;x=rand()%52 2;

}

food_1-gt;y=rand()%24 1;

q=head;

while(q-gt;next==NULL)

{

if(q-gt;x==food_1-gt;x amp;amp; q-gt;y==food_1-gt;y) //判断蛇身是否与食物重合

{

free(food_1);

createfood();

}

q=q-gt;next;

}

Pos(food_1-gt;x,food_1-gt;y);

food=food_1;

printf("■");

}

void cantcrosswall()//不能穿墙

{

if(head-gt;x==0 || head-gt;x==56 ||head-gt;y==0 || head-gt;y==26)

{

endgamestatus=1;

endgame();

}

}

void snakemove()//蛇前进,上U,下D,左L,右R

{

snake * nexthead;

cantcrosswall();

nexthead=(snake*)malloc(sizeof(snake));

if(status==U)

{

nexthead-gt;x=head-gt;x;

nexthead-gt;y=head-gt;y-1;

if(nexthead-gt;x==food-gt;x amp;amp; nexthead-gt;y==food-gt;y)//如果下一个有食物//

{

nexthead-gt;next=head;

head=nexthead;

q=head;

while(q!=NULL)

{

Pos(q-gt;x,q-gt;y);

printf("■");

q=q-gt;next;

}

score=score add;

createfood();

}

else //如果没有食物//

{

nexthead-gt;next=head;

head=nexthead;

q=head;

while(q-gt;next-gt;next!=NULL)

{

Pos(q-gt;x,q-gt;y);

printf("■");

q=q-gt;next;

}

Pos(q-gt;next-gt;x,q-gt;next-gt;y);

printf(" ");

free(q-gt;next);

q-gt;next=NULL;

}

}

if(status==D)

{

nexthead-gt;x=head-gt;x;

nexthead-gt;y=head-gt;y 1;

if(nexthead-gt;x==food-gt;x amp;amp; nexthead-gt;y==food-gt;y) //有食物

{

nexthead-gt;next=head;

head=nexthead;

q=head;

while(q!=NULL)

{

Pos(q-gt;x,q-gt;y);

printf("■");

q=q-gt;next;

}

score=score add;

createfood();

}

else //没有食物

{

nexthead-gt;next=head;

head=nexthead;

q=head;

while(q-gt;next-gt;next!=NULL)

{

Pos(q-gt;x,q-gt;y);

printf("■");

q=q-gt;next;

}

Pos(q-gt;next-gt;x,q-gt;next-gt;y);

printf(" ");

free(q-gt;next);

q-gt;next=NULL;

}

}

if(status==L)

{

nexthead-gt;x=head-gt;x-2;

nexthead-gt;y=head-gt;y;

if(nexthead-gt;x==food-gt;x amp;amp; nexthead-gt;y==food-gt;y)//有食物

{

nexthead-gt;next=head;

head=nexthead;

q=head;

while(q!=NULL)

{

Pos(q-gt;x,q-gt;y);

printf("■");

q=q-gt;next;

}

score=score add;

createfood();

}

else //没有食物

{

nexthead-gt;next=head;

head=nexthead;

q=head;

while(q-gt;next-gt;next!=NULL)

{

Pos(q-gt;x,q-gt;y);

printf("■");

q=q-gt;next;

}

Pos(q-gt;next-gt;x,q-gt;next-gt;y);

printf(" ");

free(q-gt;next);

q-gt;next=NULL;

}

}

if(status==R)

{

nexthead-gt;x=head-gt;x 2;

nexthead-gt;y=head-gt;y;

if(nexthead-gt;x==food-gt;x amp;amp; nexthead-gt;y==food-gt;y)//有食物

{

nexthead-gt;next=head;

head=nexthead;

q=head;

while(q!=NULL)

{

Pos(q-gt;x,q-gt;y);

printf("■");

q=q-gt;next;

}

score=score add;

createfood();

}

else //没有食物

{

nexthead-gt;next=head;

head=nexthead;

q=head;

while(q-gt;next-gt;next!=NULL)

{

Pos(q-gt;x,q-gt;y);

printf("■");

q=q-gt;next;

}

Pos(q-gt;next-gt;x,q-gt;next-gt;y);

printf(" ");

free(q-gt;next);

q-gt;next=NULL;

}

}

if(biteself()==1) //判断是否会咬到自己

{

endgamestatus=2;

endgame();

}

}

void pause()//暂停

{

while(1)

{

Sleep(300);

if(GetAsyncKeyState(VK_SPACE))

{

break;

}

}

}

void gamecircle()//控制游戏

{

Pos(64,15);

printf("不能穿墙,不能咬到自己\n");

Pos(64,16);

printf("用↑.↓.←.→分别控制蛇的移动.");

Pos(64,17);

printf("F1 为加速,F2 为减速\n");

Pos(64,18);

printf("ESC :退出游戏.space:暂停游戏.");

Pos(64,20);

printf("c语言研究中心 www.dotcpp.com");

status=R;

while(1)

{

Pos(64,10);

printf("得分:%d ",score);

Pos(64,11);

printf("每个食物得分:%d分",add);

if(GetAsyncKeyState(VK_UP) amp;amp; status!=D)

{

status=U;

}

else if(GetAsyncKeyState(VK_DOWN) amp;amp; status!=U)

{

status=D;

}

else if(GetAsyncKeyState(VK_LEFT)amp;amp; status!=R)

{

status=L;

}

else if(GetAsyncKeyState(VK_RIGHT)amp;amp; status!=L)

{

status=R;

}

else if(GetAsyncKeyState(VK_SPACE))

{

pause();

}

else if(GetAsyncKeyState(VK_ESCAPE))

{

endgamestatus=3;

break;

}

else if(GetAsyncKeyState(VK_F1))

{

if(sleeptimegt;=50)

{

sleeptime=sleeptime-30;

add=add 2;

if(sleeptime==320)

{

add=2;//防止减到1之后再加回来有错

}

}

}

else if(GetAsyncKeyState(VK_F2))

{

if(sleeptimelt;350)

{

sleeptime=sleeptime 30;

add=add-2;

if(sleeptime==350)

{

add=1; //保证最低分为1

}

}

}

Sleep(sleeptime);

snakemove();

}

}

void welcometogame()//开始界面

{

Pos(40,12);

system("title c语言研究中心 www.dotcpp.com");

printf("欢迎来到贪食蛇游戏!");

Pos(40,25);

system("pause");

system("cls");

Pos(25,12);

printf("用↑.↓.←.→分别控制蛇的移动, F1 为加速,2 为减速\n");

Pos(25,13);

printf("加速将能得到更高的分数。\n");

system("pause");

system("cls");

}

void endgame()//结束游戏

{

system("cls");

Pos(24,12);

if(endgamestatus==1)

{

printf("对不起,您撞到墙了。游戏结束.");

}

else if(endgamestatus==2)

{

printf("对不起,您咬到自己了。游戏结束.");

}

else if(endgamestatus==3)

{

printf("您的已经结束了游戏。");

}

Pos(24,13);

printf("您的得分是%d\n",score);

exit(0);

}

void gamestart()//游戏初始化

{

system("mode con cols=100 lines=30");

welcometogame();

creatMap();

initsnake();

createfood();

}

int main()

{

gamestart();

gamecircle();

endgame();

return 0;

}

C语言贪吃蛇代码及解析:

代码:intscore=0;

函数:initgraph(amp;gd,amp;gm,#34; #34;);

参数:gd、gm

返回值:0

其中,initgraph()函数用于初始化围墙,shardevice()函数用于执行围墙的shardevice()函数,close()函数用于结束游戏。

以下是一个使用C语言编写的简单贪吃蛇游戏,包括初始化游戏界面、绘制蛇和食物、移动蛇和检测碰撞等功能。

```c

#include lt;stdio.hgt;

#include lt;conio.hgt;

#include lt;windows.hgt;

// 定义常量

const int width = 20;

const int height = 20;

const int max_length = 5;

const int block_size = 20;

const char direction[] = #34;RDLU#34;;

const int food_x = 10;

const int food_y = 10;

const int snake_speed = 100;

// 定义结构体,存储蛇的身体坐标和方向

struct Snake {

nbsp; nbsp; int x, y;

nbsp; nbsp; int length;

nbsp; nbsp; char direction;

};

// 定义结构体,存储食物的位置和状态(是否被吃掉)

struct Food {

nbsp; nbsp; int x, y;

};

// 定义全局变量,存储蛇和食物的信息

struct Snake snake;

struct Food food;

int score;

// 初始化游戏界面和蛇的状态(位置和长度)

void init() {

nbsp; nbsp; // 初始化窗口大小和标题栏

nbsp; nbsp; SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), width * block_size, height * block_size);

nbsp; nbsp; GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), amp;buffer_info);

nbsp; nbsp; SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), TRUE, NULL, NULL, buffer_info.dwMaximumWindowSize);

nbsp; nbsp; printf(#34;Snake Game!

#34;);

nbsp; nbsp; fflush(stdout);

nbsp; nbsp;nbsp;

nbsp; nbsp; // 初始化蛇的位置和长度为3个方块,方向为左移符(#39;L#39;)

nbsp; nbsp; snake.x = height / 2;

nbsp; nbsp; snake.y = height / 2;

nbsp; nbsp; snake.length = 3;

nbsp; nbsp; snake.direction = #39;L#39;;

nbsp; nbsp;nbsp;

nbsp; nbsp; // 随机生成一个食物的位置和状态(是否被吃掉)

nbsp; nbsp; srand((unsigned)time(NULL));

nbsp; nbsp; food.x = (rand() % (width * block_size)) food_x;

nbsp; nbsp; food.y = (rand() % (height * block_size)) food_y;

}

// 在屏幕上绘制蛇和食物的图像

void draw() {

nbsp; nbsp; RECT rect;

nbsp; nbsp; int i;

nbsp; nbsp;nbsp;

nbsp; nbsp; // 根据蛇的位置和方向计算出每个方块的坐标和颜色值(RGB)

nbsp; nbsp; i = snake.length;

nbsp; nbsp; int colorR = (snake.direction amp; #39;R#39;) == #39;R#39;?155:155-(snake.length-i)*20;

nbsp; nbsp; int colorG = (snake.direction amp; #39;G#39;) == #39;G#39;?180:180-(snake.length-i)*20;

nbsp; nbsp; int colorB = (snake.direction amp; #39;B#39;) == #39;B#39;?25:25-(snake.length-i)*20;

nbsp; nbsp; int colorD = (snake.direction amp; #39;D#39;) == #39;D#39;?0:0-(snake.length-i)*20;

nbsp; nbsp; int colorE = (snake.direction amp; #39;E#39;) == #39;E#39;?7:7-(snake.length-i)*20;

nbsp; nbsp; int colorF = (snake.direction amp; #39;F#39;) == #39;F#39;?145:145-(snake.length-i)*20;

nbsp; nbsp; int colorY = (snake.direction amp; #39;Y#39;) == #39;Y#39;?11:11-(snake.length-i)*20;

nbsp; nbsp; int colorX = (snake.direction amp; #39;X#39;) == #39;X#39;?191:191-(snake.length-i)*20;

nbsp; nbsp; int colorN = (snake.direction amp; #39;N#39;) == #39;N#39;?165:165-(snake.length-i)*20;

nbsp; nbsp; int colorM = (snake.direction amp; #39;M#39;) == #39;M#39;?135:135-(snake.length-i)*20;

以下是一个简单的C语言贪吃蛇游戏代码,包含了蛇的移动、食物的生成和消除等功能。

```c

#include lt;stdio.hgt;

#include lt;stdlib.hgt;

#include lt;conio.hgt;

#include lt;time.hgt;

#define WIDTH 20

#define HEIGHT 15

#define MAX_SIZE 100

int x[MAX_SIZE], y[MAX_SIZE]; // 蛇的位置

int tailX, tailY; // 蛇尾的位置

int nTail; // 蛇的长度

int direction = 1; // 蛇的运动方向

int score = 0; // 得分

int foodX, foodY; // 食物的位置

int gameover = 0; // 是否游戏结束

// 初始化游戏

void init() {

srand(time(NULL));

x[0] = WIDTH / 2; y[0] = HEIGHT / 2;

nTail = 3;

tailX = x[0]; tailY = y[0];

score = 0;

}

// 在屏幕上打印文字

void print(char *str) {

int i;

clrscr();

for (i = 0; i lt; strlen(str); i ) putchar(str[i]);

}

// 在指定位置打印文字

void printXY(int x, int y, char *str) {

int i;

clrscr();

for (i = y-y%8; i lt; y y%8 amp;amp; i gt;=0 amp;amp; i lt; HEIGHT; i = 8) putchar(#39;\b#39;); // 每行输出8个字符,向上移动一个光标位置再输出下一个字符,以避免超出屏幕范围

for (i = x; i lt; x strlen(str); i ) putchar(str[i]);

}

// 将蛇的身体从当前位置移动到目标位置,并更新蛇头的位置和长度

void moveSnake() {

int i, j;

int dx = direction == #39;d#39;?1:-1; // 根据运动方向计算蛇头移动的距离和方向

int dy = direction == #39;l#39;?0:1; // 根据运动方向计算蛇头向上或向下移动的距离和方向

int headX = tailX dx*nTail; // 根据蛇头的位置和长度计算新的蛇头位置

int headY = tailY dy*nTail; // 根据蛇头的位置和长度计算新的蛇头位置

int length = nTail-1; // 根据当前蛇的长度计算新的蛇的长度

x[length] = headX; // 将新的蛇头位置赋值给最后一个元素作为新的蛇尾位置,同时更新蛇的长度

y[length] = headY; // 将新的蛇头位置赋值给最后一个元素作为新的蛇尾位置,同时更新蛇的长度

nTail ; // 将蛇的长度加一,表示蛇向右移动了一步

}

// 在指定位置生成新的食物,如果食物与蛇的身体相交则将食物删除并重新生成食物,否则增加得分并将食物放置在指定位置上。如果食物与蛇的身体相交则需要判断是否游戏结束并返回相应的结果。如果食物超出屏幕范围或者没有足够的空间放置食物则也需要返回相应的结果。最后更新分数和状态。

void generateFood() {

int i, j;

srand(time(NULL)); // 每隔一段时间重新生成食物以保持游戏的新鲜感。这里使用时间作为种子来随机生成食物的位置。

int foodX = (tailX (rand() % (WIDTH-1))) % (WIDTH-1) (rand() % (HEIGHT-1)); // 在屏幕上随机生成新的食物位置。注意要保证食物不超出屏幕范围。

int foodY = (tailY (rand() % (HEIGHT-1))) % (HEIGHT-1); // 在屏幕上随机生成新的食物位置。注意要保证食物不超出屏幕范围。

int dx = tailX gt; foodX?1:-1; // 如果蛇头在食物的左边则向右移动,否则向左移动。这样才能保证蛇头吃到食物后继续移动而不是停在原地。注意这里需要减去1是因为坐标是从0开始计数的。因为每次移动都是在蛇尾的基础上进行的,所以需要减去1。这样才能保证每次移动都沿着原来的方向前进。注意这里需要使用绝对值符号来判断是否越界。因为有可能会出现蛇头刚好在食物的正中间的情况。这样就需要根据具体的情况来选择是向左移动还是向右移动。这种方法称为“剪枝”。如果直接写if语句的话可能会出现越界的情况导致程序崩溃。因为每次移动的方向不同,所以需要分别判断是否越界。这样才能保证程序正确执行。这种方法称为“优化”。

关于微信小程序开发代码c和微信小程序开发代码编写的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。 微信小程序开发代码c的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于微信小程序开发代码编写、微信小程序开发代码c的信息别忘了在本站进行查找喔。