导航:首页 > 编程语言 > 贪吃蛇编程思路

贪吃蛇编程思路

发布时间:2023-05-01 03:27:54

① c语言 贪吃蛇 程序

基本思路:

蛇每吃一个食物蛇身子就增加一格,用UP, DOWN, LEFT, RIGHT控制蛇头的运动,而蛇身子跟着蛇头走,每后一格蛇身子下一步走到上一格蛇身子的位置,以此类推。

#include <stdio.h>

#include <conio.h>

#include <windows.h>

#define BEG_X 2

#define BEG_Y 1

#define WID 20

#define HEI 20

HANDLE hout;

typedef enum {UP, DOWN, LEFT, RIGHT} DIR;

typedef struct Snake_body

{

COORD pos;//蛇身的位置

struct Snake_body *next;//下一个蛇身

struct Snake_body *prev;//前一个蛇身

}SNAKE, *PSNAKE;

PSNAKE head = NULL;//蛇头

PSNAKE tail = NULL;//蛇尾

//画游戏边框的函数

void DrawBorder()

{

int i, j;

COORD pos = {BEG_X, BEG_Y};

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

{

SetConsoleCursorPosition(hout, pos);

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

{

if(i == 0)//第一行

{

if(j == 0)

printf("┏");

else if(j == WID - 1)

printf("┓");

else

printf("━");

}

else if(i == HEI - 1)//最后一行

{

if(j == 0)

printf("┗");

else if(j == WID - 1)

printf("┛");

else

printf("━");

}

else if(j == 0 || j == WID - 1)//第一列或最后一列

printf("┃");

else

printf(" ");

}

++pos.Y;

}

}

//添加蛇身的函数

void AddBody(COORD pos)

{

PSNAKE pnew = (PSNAKE)calloc(1, sizeof(SNAKE));

pnew->pos = pos;

if(!head)

{

head = tail = pnew;

}

else

{

pnew->next = head;//新创建蛇身的next指向原先的蛇头

head->prev = pnew;//原先的蛇头的prev指向新创建的蛇身

head = pnew;//把新创建的蛇身作为新的蛇头

}

SetConsoleCursorPosition(hout, head->pos);

printf("◎");

}

//蛇身移动的函数

void MoveBody(DIR dir)

{

PSNAKE ptmp;

COORD pos = head->pos;

switch(dir)

{

case UP:

if(head->pos.Y > BEG_Y + 1)

--pos.Y;

else

return;

break;

case DOWN:

if(head->pos.Y < BEG_Y + HEI - 2)

++pos.Y;

else

return;

break;

case LEFT:

if(head->pos.X > BEG_X + 2)

pos.X -= 2;

else

return;

break;

case RIGHT:

if(head->pos.X < BEG_X + (WID - 2) * 2)

pos.X += 2;

else

return;

break;

}

AddBody(pos);//添加了一个新的蛇头

ptmp = tail;//保存当前的蛇尾

tail = tail->prev;

if(tail)

tail->next = NULL;

SetConsoleCursorPosition(hout, ptmp->pos);

printf(" ");

free(ptmp);

}

int main()

{

int ctrl;

DIR dir = RIGHT;//初始蛇的方向是向右的

COORD pos = {BEG_X + 2, BEG_Y + HEI / 2};

system("color 0E");

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

hout = GetStdHandle(STD_OUTPUT_HANDLE);

printf(" ------------贪吃蛇的移动------------");

DrawBorder();

//自定义几个蛇的身体

AddBody(pos);

pos.X += 2;

AddBody(pos);

pos.X += 2;

AddBody(pos);

pos.X += 2;

AddBody(pos);

pos.X += 2;

AddBody(pos);

pos.X += 2;

AddBody(pos);

pos.X += 2;

AddBody(pos);

//控制蛇的移动

while(ctrl = getch())

{

switch(ctrl)

{

case 'w':

if(dir == DOWN)

continue;

dir = UP;

break;

case 's':

if(dir == UP)

continue;

dir = DOWN;

break;

case 'a':

if(dir == RIGHT)

continue;

dir = LEFT;

break;

case 'd':

if(dir == LEFT)

continue;

dir = RIGHT;

break;

case 'q':

return 0;

}

MoveBody(dir);

}

return 0;

}

(1)贪吃蛇编程思路扩展阅读:

实现逻辑

1,可以设置光标,就能实现制定位置打印制定符号。

2,涉及一个结构体,包含两个元素坐标元素和一个结构体指针。

3,结构体串联形成链表,遍历获取成员坐标,打印符号得到蛇身。

4,不断的加头,去尾,重新遍历坐标,再打印形成蛇的移动。

5,食物产生的位置判定,不能越界,也不能与蛇身体重合。

6,蛇的转向判定,一条规则,不允许倒退。

7,转向的实现,跟行进方向决定新的关节坐标(当前头的上下左右)

8,死亡检测,是否头节点坐标是否与墙壁重合,是否与身体其他关节重合。

9,加速减速,设置刷新休眠时间实现。

② 贪吃蛇 C语言 简易程序设计

#include<graphics.h>
#include<stdlib.h>
#define N 200
#define up 0x4800
#define down 0x5000
#define left 0x4b00
#define right 0x4d00
#define esc 0x011b
#define Y 0x1579
#define n 0x316e
int gamespeed; /* 游戏速度 */
int i,key,color;
int score=0; /* 游戏分数 */
char cai48H[]={
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x04,0x00,0x18,0x00,0x00,0x00,0x0E,0x00,
0x1C,0x00,0x00,0x00,0x1C,0x00,0x1C,0x00,
0x00,0x00,0x20,0x00,0x38,0x00,0x00,0x00,
0x40,0x00,0x78,0x00,0x00,0x01,0x80,0x40,
0x70,0x00,0x00,0x03,0x80,0xC0,0xE0,0x00,
0x00,0x07,0x80,0x80,0xC0,0x00,0x00,0x0E,
0x11,0x81,0xC0,0x00,0x00,0x08,0x61,0x01,
0x80,0x00,0x00,0x00,0x23,0x03,0x04,0x00,
0x00,0x02,0x02,0x00,0x06,0x00,0x00,0x1E,
0x04,0x00,0x0F,0x00,0x00,0x1C,0x1F,0x80,
0x1E,0x00,0x00,0x08,0x3F,0x80,0x3C,0x00,
0x00,0x00,0xFF,0x80,0x38,0x00,0x00,0x03,
0xFF,0x80,0x78,0x00,0x00,0x0F,0xF8,0x00,
0xF0,0x00,0x00,0x7F,0xF0,0x00,0xE0,0x00,
0x03,0xFF,0xFC,0x01,0x80,0x00,0x03,0xC0,
0xFF,0x01,0x03,0x80,0x01,0x01,0xFF,0x00,
0x03,0x80,0x00,0x01,0x3F,0x00,0x07,0x80,
0x00,0x02,0x11,0x00,0x07,0x00,0x00,0x00,
0x10,0x00,0x07,0x00,0x00,0x00,0x10,0x00,
0x0E,0x00,0x00,0x08,0x10,0x00,0x1C,0x00,
0x00,0x30,0x10,0x00,0x18,0x00,0x00,0x70,
0x10,0x00,0x30,0x00,0x01,0xE0,0x10,0x00,
0x70,0x00,0x03,0x80,0x10,0x00,0x60,0x00,
0x00,0x00,0x30,0x00,0xE0,0x00,0x00,0x00,
0xF0,0x01,0xC0,0x00,0x00,0x00,0x70,0x03,
0xC0,0x00,0x00,0x00,0x10,0x07,0x80,0x00,
0x00,0x00,0x00,0x0F,0x00,0x00,0x00,0x00,
0x00,0x1E,0x00,0x00,0x00,0x00,0x00,0x3C,
0x00,0x00,0x00,0x00,0x00,0x70,0x00,0x00,
0x00,0x00,0x01,0xC0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};

char she48H[]={
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,
0x00,0x00,0x00,0x00,0x00,0x0C,0x00,0x00,
0x00,0x00,0x00,0x0E,0x00,0x00,0x00,0x00,
0x00,0x0E,0x00,0x00,0x00,0x03,0x00,0x07,
0x00,0x00,0x00,0x02,0x00,0x03,0x00,0x00,
0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x02,
0x00,0x00,0xF8,0x00,0x00,0x02,0x00,0x07,
0x86,0x00,0x00,0x02,0x00,0x18,0x03,0x00,
0x00,0x02,0x00,0x00,0x07,0x80,0x00,0x03,
0xF0,0x00,0x07,0x80,0x00,0x0F,0xFC,0x00,
0x0C,0x00,0x00,0x7E,0x3F,0x80,0x00,0x00,
0x01,0xFE,0x1F,0x80,0x00,0x00,0x01,0xE2,
0x39,0x8C,0x00,0x00,0x00,0xC2,0x30,0x08,
0x00,0x00,0x00,0xC2,0x60,0x08,0x00,0x00,
0x00,0xC3,0xE0,0x08,0x60,0x00,0x00,0x7F,
0xE0,0x01,0xE0,0x00,0x00,0x3F,0x80,0x1F,
0xE0,0x00,0x00,0x1E,0x00,0x1F,0x80,0x00,
0x00,0x1E,0x00,0x1F,0x00,0x00,0x00,0x02,
0x38,0x1E,0x00,0x00,0x00,0x07,0xFC,0x1C,
0x00,0x20,0x00,0x07,0xFC,0x18,0x00,0x20,
0x00,0x1F,0x0C,0x10,0x00,0x20,0x00,0x7C,
0x04,0x10,0x00,0x60,0x01,0xF0,0x00,0x10,
0x00,0x60,0x01,0xE0,0x00,0x08,0x00,0xF0,
0x00,0x80,0x00,0x08,0x03,0xF0,0x00,0x00,
0x00,0x07,0xFF,0xF0,0x00,0x00,0x00,0x07,
0xFF,0xF0,0x00,0x00,0x00,0x03,0xFF,0xE0,
0x00,0x00,0x00,0x01,0xFF,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
char tun48H[]={
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x0E,0x00,0x00,0x00,0x00,0x00,0x3E,
0x00,0x00,0x00,0x00,0x00,0x7F,0x00,0x00,
0x00,0x00,0x00,0xE0,0x00,0x00,0x00,0x00,
0x03,0xC0,0x00,0x00,0x00,0x00,0x1F,0x00,
0x00,0x00,0x00,0x00,0x7C,0x00,0x00,0x00,
0x00,0x01,0xF8,0x00,0x00,0x00,0x00,0x03,
0xF8,0x00,0x40,0x00,0x00,0x00,0x06,0x07,
0xC0,0x00,0x00,0x00,0x07,0xFF,0xE0,0x00,
0x00,0x00,0x07,0xFF,0xE0,0x00,0x00,0x00,
0x0F,0xFF,0x80,0x00,0x00,0x00,0x7F,0xF8,
0x00,0x00,0x00,0x1F,0xFF,0xF8,0x00,0x00,
0x00,0x1F,0xFF,0xF8,0x00,0x00,0x00,0x1F,
0xFC,0x3C,0x00,0x00,0x00,0x0F,0xF8,0x0E,
0x00,0x00,0x00,0x04,0x70,0x07,0x00,0x00,
0x00,0x00,0x60,0x03,0x80,0x00,0x00,0x00,
0xC0,0x00,0xC0,0x00,0x00,0x01,0x80,0x00,
0x30,0x00,0x00,0x01,0x00,0x3C,0x18,0x00,
0x00,0x02,0x03,0xFF,0x0C,0x00,0x00,0x0C,
0x7F,0xFF,0x8E,0x00,0x00,0x18,0xFF,0xFF,
0xC7,0x80,0x00,0x78,0xFE,0x07,0x87,0xE0,
0x01,0xF0,0x70,0x07,0x03,0xF8,0x07,0xE0,
0x70,0x0E,0x03,0xFE,0x00,0x00,0x38,0x1E,
0x01,0xFE,0x00,0x00,0x3F,0xFE,0x00,0x0C,
0x00,0x00,0x1F,0xFE,0x00,0x00,0x00,0x00,
0x1F,0xFE,0x00,0x00,0x00,0x00,0x0F,0xFE,
0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
char dan48H[]={
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0xFC,0x00,0x00,0x00,0x00,0x07,0xFF,
0x00,0x00,0x00,0x00,0x7F,0xC0,0x80,0x00,
0x00,0x03,0xFF,0x80,0x40,0x00,0x00,0x01,
0xF1,0x80,0x40,0x00,0x00,0x01,0x81,0x80,
0xE0,0x00,0x00,0x00,0x01,0x93,0xF0,0x00,
0x00,0x00,0x01,0xFF,0xF0,0x00,0x00,0x00,
0x21,0xFF,0xF0,0x00,0x00,0x00,0x21,0xF8,
0x00,0x00,0x00,0x00,0x61,0xC0,0x00,0x00,
0x00,0x00,0x61,0x80,0x00,0x00,0x00,0x00,
0xF3,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,
0x00,0x00,0x00,0x01,0xFF,0xC0,0x00,0x00,
0x00,0x03,0xFF,0xF8,0x00,0x00,0x00,0x02,
0x00,0xFC,0x00,0x00,0x00,0x04,0x02,0x1F,
0x00,0x00,0x00,0x08,0x03,0x01,0xC0,0x00,
0x00,0x38,0x03,0x00,0x7C,0x00,0x00,0xF8,
0x07,0xF8,0x3F,0xC0,0x01,0xF0,0x3F,0xFE,
0x3F,0xF8,0x03,0xC1,0xFF,0x0F,0x1F,0xF8,
0x00,0x01,0xE3,0x0F,0x0F,0xF0,0x00,0x01,
0xC3,0x0E,0x00,0x00,0x00,0x01,0x83,0xFC,
0x00,0x00,0x00,0x00,0xC7,0xF8,0x00,0x00,
0x00,0x00,0xFF,0xF8,0x00,0x00,0x00,0x00,
0x7F,0xF0,0x00,0x00,0x00,0x00,0x3F,0x03,
0x80,0x00,0x00,0x00,0x03,0x04,0x00,0x00,
0x00,0x00,0x03,0xF8,0x00,0x00,0x00,0x00,
0x1F,0xF8,0x20,0x00,0x00,0x00,0xFF,0xFF,
0xE0,0x00,0x00,0x07,0xFF,0x81,0xE0,0x00,
0x00,0x07,0xE0,0x00,0xE0,0x00,0x00,0x03,
0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};

char zuo16H[]={
0x18,0xC0,0x18,0xC0,0x19,0x80,0x31,0xFE,
0x33,0xFE,0x76,0xC0,0xF0,0xFC,0xB0,0xFC,
0x30,0xC0,0x30,0xC0,0x30,0xFE,0x30,0xFE,
0x30,0xC0,0x30,0xC0,0x30,0xC0,0x00,0x00,
};
char zhe16H[]={
0x03,0x00,0x03,0x0C,0x1F,0xCC,0x1F,0xD8,
0x03,0x30,0xFF,0xFE,0xFF,0xFE,0x03,0x00,
0x0F,0xF8,0x3F,0xF8,0xEC,0x18,0xCF,0xF8,
0x0C,0x18,0x0F,0xF8,0x0F,0xF8,0x00,0x00,
};

char tian16H[]={
0x00,0x00,0x3F,0xFC,0x3F,0xFC,0x31,0x8C,
0x31,0x8C,0x31,0x8C,0x3F,0xFC,0x3F,0xFC,
0x31,0x8C,0x31,0x8C,0x31,0x8C,0x3F,0xFC,
0x3F,0xFC,0x30,0x0C,0x00,0x00,0x00,0x00,
};
char xue16H[]={
0x33,0x18,0x19,0x98,0x08,0xB0,0x7F,0xFC,
0x7F,0xFC,0x60,0x0C,0x1F,0xF0,0x1F,0xF0,
0x00,0xC0,0x7F,0xFC,0x7F,0xFC,0x01,0x80,
0x01,0x80,0x07,0x80,0x03,0x00,0x00,0x00,
};
char ke16H[]={
0x00,0x00,0x0C,0x18,0xFD,0x98,0xF8,0xD8,
0x18,0x58,0xFE,0x18,0xFE,0x98,0x18,0xD8,
0x3C,0x58,0x7E,0x1E,0xDB,0xFE,0x9B,0xF8,
0x18,0x18,0x18,0x18,0x18,0x18,0x00,0x00,
};

struct Food/*定义结构体存储食物的属性*/
{
int x; /* 食物的坐标 */
int y;
int yes; /* 值为0表示屏幕上没有食物,值为1表示屏幕上有食物 */
int color; /* 食物颜色 */
}food;

struct Snake/*定义结构体存储蛇的属性*/
{
int x[N]; /* 每一节蛇的坐标 */
int y[N];
int color[N];/*存储每一节蛇的颜色*/
int node; /* 蛇的节数 */
int direction; /* 蛇移动的方向 */
int life; /* 蛇的生命,如果为1,蛇死,游戏结束 */
}snake;

void init(void)/*图形驱动*/
{
int driver=DETECT,mode=0;
registerbgidriver(EGAVGA_driver);
initgraph(&driver,&mode,"");
}

void drawmat(char *mat,int matsize,int x,int y,int color)/*汉字点阵*/
{
int i,j,k,m;
m=(matsize-1)/8+1;
for(j=0;j<matsize;j++)
for(i=0;i<m;i++)
for(k=0;k<8;k++)
if(mat[j*m+i]&(0x80>>k))
putpixel(x+i*8+k,y+j,color);
}

void showword(void)
{/* 调用汉字点阵输出程序,显示标题和作者信息 */
drawmat(cai48H,48,249,-4,7);
drawmat(she48H,48,329,-4,7);
drawmat(tun48H,48,409,-4,7);
drawmat(dan48H,48,489,-4,7);

drawmat(cai48H,48,250,-5,4);
drawmat(she48H,48,330,-5,4);
drawmat(tun48H,48,410,-5,4);
drawmat(dan48H,48,490,-5,4);
/*作者 田学科*/
drawmat(zuo16H,16,515,465,7);
drawmat(zhe16H,16,530,465,7);

drawmat(tian16H,16,550,465,7);
drawmat(xue16H,16,565,465,7);
drawmat(ke16H,16,580,465,7);
}

void draw(void)/*画出四周的墙*/
{
if(color==15)
color=0;
setcolor(++color);
setlinestyle(SOLID_LINE,0,1);

for(i=30;i<=600;i+=10)
{
rectangle(i,40,i+10,49);
rectangle(i,451,i+10,460);
}
for(i=40;i<450;i+=10)
{
rectangle(30,i,39,i+10);
rectangle(601,i,610,i+10);
}
}

void prscore(void)
{/* 打印游戏分数 */
char str[10];
setfillstyle(SOLID_FILL,YELLOW);
bar(50,10,200,30);
setcolor(6);
settextstyle(0,0,2);
sprintf(str,"score:%d",score);
outtextxy(55,15,str);
}

void gameover(void)
{
cleardevice(); /* 清屏函数 */
for(i=0;i<snake.node;i++)/* 画出蛇死时的位置 */
{
setcolor(snake.color[i]);
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]+10);
}
prscore(); /* 显示分数 */
draw();
showword();
settextstyle(0,0,6);
setcolor(7);
outtextxy(103,203,"GAME OVER");
setcolor(RED);
outtextxy(100,200,"GAME OVER");
}

void gameplay(void)/* 玩游戏的具体过程 */
{
int flag,flag1;
randomize();
prscore();
gamespeed=50000;
food.yes=0;/* food.yes=0表示屏幕上没有食物 */
snake.life=1;/* snake.life=1表示蛇是活着的 */
snake.direction=4;/* 表示蛇的初始方向为向右 */
snake.node=2; /* 蛇的初始化为两节 */
snake.color[0]=2; /*两节蛇头初始化为绿色*/
snake.color[1]=2;
snake.x[0]=100;snake.y[0]=100;
snake.x[1]=110;snake.y[1]=100;
food.color=random(15)+1;
while(1)
{
while(1)
{
if(food.yes==0) /* 如果蛇活着 */
{
while(1)
{
flag=1;
food.yes=1;
food.x=random(56)*10+40;
food.y=random(40)*10+50;
for(i=0;i<snake.node;i++)
{
if(food.x==snake.x[i]&&food.y==snake.y[i])
flag=0;
}
if(flag) break;
}
}
if(food.yes)
{
setcolor(food.color);
rectangle(food.x,food.y,food.x+10,food.y+10);
}

for(i=snake.node-1;i>0;i--)
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}

switch(snake.direction)
{
case 1: snake.y[0]-=10;break;
case 2: snake.y[0]+=10;break;
case 3: snake.x[0]-=10;break;
case 4: snake.x[0]+=10;break;
}

for(i=3;i<snake.node;i++)
{
if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0])
{
gameover();
snake.life=0;
break;
}
}

if(snake.x[0]<40||snake.x[0]>590||snake.y[0]<50||snake.y[0]>440)
{
gameover();
snake.life=0;
}

if(snake.life==0)
break;

if(snake.x[0]==food.x&&snake.y[0]==food.y)/*蛇吃掉食物*/
{
setcolor(0);
rectangle(food.x,food.y,food.x+10,food.y+10);
snake.x[snake.node]=-20;
snake.y[snake.node]=-20;
snake.color[snake.node]=food.color;
snake.node++;
food.yes=0;
food.color=random(15)+1;
score+=10;
prscore();
if(score%100==0&&score!=0)
{
for(i=0;i<snake.node;i++)/* 画出蛇 */
{
setcolor(snake.color[i]);
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]+10);
}
sound(200);
delay(50000);delay(50000);delay(50000);
delay(50000);delay(50000);delay(50000);
nosound();
gamespeed-=5000;
draw();
}
else
{
sound(500);
delay(500);
nosound();
}
}

for(i=0;i<snake.node;i++)/* 画出蛇 */
{
setcolor(snake.color[i]);
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]+10);
}

delay(gamespeed);
delay(gamespeed);
flag1=1;
setcolor(0);
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+10,snake.y[snake.node-1]+10);

if(kbhit()&&flag1==1)/*如果没按有效键就重新开始循环*/
{
flag1=0;
key=bioskey(0);
if(key==esc)
exit(0);
else if(key==up&&snake.direction!=2)
snake.direction=1;
else if(key==down&&snake.direction!=1)
snake.direction=2;
else if(key==left&&snake.direction!=4)
snake.direction=3;
else if(key==right&&snake.direction!=3)
snake.direction=4;
}
}
if(snake.life==0)/*如果蛇死了就退出循环*/
break;
}
}

void main(void)
{
while(1)
{
color=0;
init();
cleardevice();
showword();
draw();
gameplay();
setcolor(15);
settextstyle(0,0,2);
outtextxy(200,400,"CONTINUE(Y/N)?");
while(1)
{
key=bioskey(0);
if(key==Y||key==n||key==esc)
break;
}
if(key==n||key==esc)
break;
}
closegraph();
}

③ scratch编程贪吃蛇教程

scratch编程贪吃蛇教程:

1、首先,我们新建一个项目文件。

2、我们点击添加精灵按钮。

3、在弹出的对话框中,我们选绘制角色。

8、选中橡皮擦,代码编辑区写入以下代码。

贪食蛇是一款经典的休闲游戏。有PC和手机等多平台版本,既简单又耐玩。

④ 用C语言编写 贪吃蛇的思路什么怎么样的

把你的邮箱给我,我把源程序发给你,或者你自己从网上下载,这个snake程序芹亩是NetBSD操作系统源代码的一部分,要是你能发现Bug,我先恭喜你。首巧

/者首键* $NetBSD: snake.c,v 1.9 1997/10/12 01:49:28 lukem Exp $ */

/*
* Copyright (c) 1980, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above right
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproce the above right
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This proct includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote procts derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
The Regents of the University of California. All rights reserved.\n");
#endif /* not lint */

#ifndef lint
#if 0
static char sccsid[] = "@(#)snake.c 8.2 (Berkeley) 1/7/94";
#else
__RCSID("$NetBSD: snake.c,v 1.9 1997/10/12 01:49:28 lukem Exp $");
#endif
#endif /* not lint */

/*
* snake - crt hack game.
*
* You move around the screen with arrow keys trying to pick up money
* without getting eaten by the snake. hjkl work as in vi in place of
* arrow keys. You can leave at the exit any time.
*
* compile as follows:
* cc -O snake.c move.c -o snake -lm -ltermlib
*/

#include <sys/param.h>

#include <errno.h>
#include <fcntl.h>
#include <pwd.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#include "snake.h"
#include "pathnames.h"

#define PENALTY 10 /* % penalty for invoking spacewarp */

#define EOT '\004'
#define LF '\n'
#define DEL '\177'

#define ME 'I'
#define SNAKEHEAD 'S'
#define SNAKETAIL 's'
#define TREASURE '$'
#define GOAL '#'

#define BSIZE 80

struct point you;
struct point money;
struct point finish;
struct point snake[6];

int loot, penalty;
int long tl, tm = 0L;
int moves;
char stri[BSIZE];
char *p;
char ch, savec;
char *kl, *kr, *ku, *kd;
int fast = 1;
int repeat = 1;
time_t tv;
char *tn;

int main __P((int, char **));

int
main(argc, argv)
int argc;
char **argv;
{
extern char *optarg;
extern int optind;
int ch, i;

(void) time(&tv);
srandom((int) tv);

while ((ch = getopt(argc, argv, "l:w:")) != -1)
switch ((char) ch) {
#ifdef notdef
case 'd':
tv = atol(optarg);
break;
#endif
case 'w': /* width */
ccnt = atoi(optarg);
break;
case 'l': /* length */
lcnt = atoi(optarg);
break;
case '?':
default:
fputs("usage: snake [-d seed] [-w width] [-l length]\n", stderr);
exit(1);
}

penalty = loot = 0;
getcap();

i = MIN(lcnt, ccnt);
if (i < 4) {
cook();
pr("snake: screen too small for a fair game.\n");
exit(1);
}
/*
* chunk is the amount of money the user gets for each $.
* The formula below tries to be fair for various screen sizes.
* We only pay attention to the smaller of the 2 edges, since
* that seems to be the bottleneck.
* This formula is a hyperbola which includes the following points:
* (24, $25) (original scoring algorithm)
* (12, $40) (experimentally derived by the "feel")
* (48, $15) (a guess)
* This will give a 4x4 screen $99/shot. We don't allow anything
* smaller than 4x4 because there is a 3x3 game where you can win
* an infinite amount of money.
*/
if (i < 12)
i = 12; /* otherwise it isn't fair */
/*
* Compensate for border. This really changes the game since
* the screen is two squares smaller but we want the default
* to be $25, and the high scores on small screens were a bit
* much anyway.
*/
i += 2;
chunk = (675.0 / (i + 6)) + 2.5; /* min screen edge */

signal(SIGINT, stop);
putpad(TI); /* String to begin programs that use cm */
putpad(KS); /* Put terminal in keypad transmit mode */

snrand(&finish);
snrand(&you);
snrand(&money);
snrand(&snake[0]);

if (ospeed < 9600 || ((!CM) && (!TA)))
fast = 0;
for (i = 1; i < 6; i++)
chase(&snake[i], &snake[i - 1]);
setup();
mainloop();
/* NOTREACHED */
return (0);
}

/* Main command loop */
void
mainloop()
{
int j, k;

for (;;) {
int c, lastc, match;

lastc = 0;
move(&you);
fflush(stdout);
if (((c = getchar() & 0177) <= '9') && (c >= '0')) {
ungetc(c, stdin);
j = scanf("%d", &repeat);
c = getchar() & 0177;
} else {
if (c != '.')
repeat = 1;
}
if (c == '.') {
c = lastc;
}
if ((Klength > 0) &&
(c == *KL || c == *KR || c == *KU || c == *KD)) {
savec = c;
match = 0;
kl = KL;
kr = KR;
ku = KU;
kd = KD;
for (j = Klength; j > 0; j--) {
if (match != 1) {
match = 0;
if (*kl++ == c) {
ch = 'h';
match++;
}
if (*kr++ == c) {
ch = 'l';
match++;
}
if (*ku++ == c) {
ch = 'k';
match++;
}
if (*kd++ == c) {
ch = 'j';
match++;
}
if (match == 0) {
ungetc(c, stdin);
ch = savec;
/* Oops! This works if we
* figure it out on second
* character. */
break;
}
}
savec = c;
if (j != 1)
c = getchar() & 0177;
}
c = ch;
}
if (!fast)
flushi();
lastc = c;
switch (c) {
case CTRL('z'):
suspend();
continue;
case EOT:
case 'x':
case 0177: /* del or end of file */
ll();
length(moves);
logit("quit");
done();
case CTRL('l'):
setup();
winnings(cashvalue);
continue;
case 'p':
case 'd':
snap();
continue;
case 'w':
spacewarp(0);
continue;
case 'A':
repeat = you.col;
c = 'h';
break;
case 'H':
case 'S':
repeat = you.col - money.col;
c = 'h';
break;
case 'T':
repeat = you.line;
c = 'k';
break;
case 'K':
case 'E':
repeat = you.line - money.line;
c = 'k';
break;
case 'P':
repeat = ccnt - 1 - you.col;
c = 'l';
break;
case 'L':
case 'F':
repeat = money.col - you.col;
c = 'l';
break;
case 'B':
repeat = lcnt - 1 - you.line;
c = 'j';
break;
case 'J':
case 'C':
repeat = money.line - you.line;
c = 'j';
break;
}
for (k = 1; k <= repeat; k++) {
moves++;
switch (c) {
case 's':
case 'h':
case '\b':
if (you.col > 0) {
if ((fast) || (k == 1))
pchar(&you, ' ');
you.col--;
if ((fast) || (k == repeat) ||
(you.col == 0))
pchar(&you, ME);
}
break;
case 'f':
case 'l':
case ' ':
if (you.col < ccnt - 1) {
if ((fast) || (k == 1))
pchar(&you, ' ');
you.col++;
if ((fast) || (k == repeat) ||
(you.col == ccnt - 1))
pchar(&you, ME);
}
break;
case CTRL('p'):
case 'e':
case 'k':
case 'i':
if (you.line > 0) {
if ((fast) || (k == 1))
pchar(&you, ' ');
you.line--;
if ((fast) || (k == repeat) ||
(you.line == 0))
pchar(&you, ME);
}
break;
case CTRL('n'):
case 'c':
case 'j':
case LF:
case 'm':
if (you.line + 1 < lcnt) {
if ((fast) || (k == 1))
pchar(&you, ' ');
you.line++;
if ((fast) || (k == repeat) ||
(you.line == lcnt - 1))
pchar(&you, ME);
}
break;
}

if (same(&you, &money)) {
loot += 25;
if (k < repeat)
pchar(&you, ' ');
do {
snrand(&money);
} while ((money.col == finish.col &&
money.line == finish.line) ||
(money.col < 5 && money.line == 0) ||
(money.col == you.col &&
money.line == you.line));
pchar(&money, TREASURE);
winnings(cashvalue);
continue;
}
if (same(&you, &finish)) {
win(&finish);
ll();
cook();
pr("You have won with $%d.\n", cashvalue);
fflush(stdout);
logit("won");
post(cashvalue, 1);
length(moves);
done();
}
if (pushsnake())
break;
}
fflush(stdout);
}
}

/*
* setup the board
*/
void
setup()
{
int i;

clear();
pchar(&you, ME);
pchar(&finish, GOAL);
pchar(&money, TREASURE);
for (i = 1; i < 6; i++) {
pchar(&snake[i], SNAKETAIL);
}
pchar(&snake[0], SNAKEHEAD);
drawbox();
fflush(stdout);
}

void
drawbox()
{
int i;
struct point p;

p.line = -1;
for (i = 0; i < ccnt; i++) {
p.col = i;
pchar(&p, '-');
}
p.col = ccnt;
for (i = -1; i <= lcnt; i++) {
p.line = i;
pchar(&p, '|');
}
p.col = -1;
for (i = -1; i <= lcnt; i++) {
p.line = i;
pchar(&p, '|');
}
p.line = lcnt;
for (i = 0; i < ccnt; i++) {
p.col = i;
pchar(&p, '-');
}
}

void
snrand(sp)
struct point *sp;
{
struct point p;
int i;

for (;;) {
p.col = random() % ccnt;
p.line = random() % lcnt;

/* make sure it's not on top of something else */
if (p.line == 0 && p.col < 5)
continue;
if (same(&p, &you))
continue;
if (same(&p, &money))
continue;
if (same(&p, &finish))
continue;
for (i = 0; i < 5; i++)
if (same(&p, &snake[i]))
break;
if (i < 5)
continue;
break;
}
*sp = p;
}

int
post(iscore, flag)
int iscore, flag;
{
short score = iscore;
int rawscores;
short uid;
short oldbest = 0;
short allbwho = 0, allbscore = 0;
struct passwd *p;

/*
* Neg uid, 0, and 1 cannot have scores recorded.
*/
if ((uid = getuid()) <= 1) {
pr("No saved scores for uid %d.\n", uid);
return (1);
}
if ((rawscores = open(_PATH_RAWSCORES, O_RDWR | O_CREAT, 0644)) < 0) {
pr("No score file %s: %s.\n", _PATH_RAWSCORES,
strerror(errno));
return (1);
}
/* Figure out what happened in the past */
read(rawscores, &allbscore, sizeof(short));
read(rawscores, &allbwho, sizeof(short));
lseek(rawscores, uid * sizeof(short), 0);
read(rawscores, &oldbest, sizeof(short));
if (!flag)
return (score > oldbest ? 1 : 0);

/* Update this jokers best */
if (score > oldbest) {
lseek(rawscores, uid * sizeof(short), 0);
write(rawscores, &score, sizeof(short));
pr("You bettered your previous best of $%d\n", oldbest);
} else
pr("Your best to date is $%d\n", oldbest);

/* See if we have a new champ */
p = getpwuid(allbwho);
if (p == NULL || score > allbscore) {
lseek(rawscores, 0, 0);
write(rawscores, &score, sizeof(short));
write(rawscores, &uid, sizeof(short));
if (allbwho)
pr("You beat %s's old record of $%d!\n",
p->pw_name, allbscore);
else
pr("You set a new record!\n");
} else
pr("The highest is %s with $%d\n", p->pw_name, allbscore);
close(rawscores);
return (1);
}

/*
* Flush typeahead to keep from buffering a bunch of chars and then
* overshooting. This loses horribly at 9600 baud, but works nicely
* if the terminal gets behind.
*/
void
flushi()
{
tcflush(0, TCIFLUSH);
}

int mx[8] = {
0, 1, 1, 1, 0, -1, -1, -1
};
int my[8] = {
-1, -1, 0, 1, 1, 1, 0, -1
};
float absv[8] = {
1, 1.4, 1, 1.4, 1, 1.4, 1, 1.4
};
int oldw = 0;

void
chase(np, sp)
struct point *sp, *np;
{
/* this algorithm has bugs; otherwise the snake would get too good */
struct point d;
int w, i, wt[8];
double v1, v2, vp, max;
point(&d, you.col - sp->col, you.line - sp->line);
v1 = sqrt((double) (d.col * d.col + d.line * d.line));
w = 0;
max = 0;
for (i = 0; i < 8; i++) {
vp = d.col * mx[i] + d.line * my[i];
v2 = absv[i];
if (v1 > 0)
vp = ((double) vp) / (v1 * v2);
else
vp = 1.0;
if (vp > max) {
max = vp;
w = i;
}
}
for (i = 0; i < 8; i++) {
point(&d, sp->col + mx[i], sp->line + my[i]);
wt[i] = 0;
if (d.col < 0 || d.col >= ccnt || d.line < 0 || d.line >= lcnt)
continue;
/*
* Change to allow snake to eat you if you're on the money,
* otherwise, you can just crouch there until the snake goes
* away. Not positive it's right.
*
* if (d.line == 0 && d.col < 5) continue;
*/
if (same(&d, &money))
continue;
if (same(&d, &finish))
continue;
wt[i] = i == w ? loot / 10 : 1;
if (i == oldw)
wt[i] += loot / 20;
}
for (w = i = 0; i < 8; i++)
w += wt[i];
vp = ((rand() >> 6) & 01777) % w;
for (i = 0; i < 8; i++)
if (vp < wt[i])
break;
else
vp -= wt[i];
if (i == 8) {
pr("failure\n");
i = 0;
while (wt[i] == 0)
i++;
}
oldw = w = i;
point(np, sp->col + mx[w], sp->line + my[w]);
}

void
spacewarp(w)
int w;
{
struct point p;
int j;
char *str;

snrand(&you);
point(&p, COLUMNS / 2 - 8, LINES / 2 - 1);
if (p.col < 0)
p.col = 0;
if (p.line < 0)
p.line = 0;
if (w) {
str = "BONUS!!!";
loot = loot - penalty;
penalty = 0;
} else {
str = "SPACE WARP!!!";
penalty += loot / PENALTY;
}
for (j = 0; j < 3; j++) {
clear();
delay(5);
apr(&p, str);
delay(10);
}
setup();
winnings(cashvalue);
}

void
snap()
{
struct point p;

if (you.line < 3) {
pchar(point(&p, you.col, 0), '-');
}
if (you.line > lcnt - 4) {
pchar(point(&p, you.col, lcnt - 1), '_');
}
if (you.col < 10) {
pchar(point(&p, 0, you.line), '(');
}
if (you.col > ccnt - 10) {
pchar(point(&p, ccnt - 1, you.line), ')');
}
if (!stretch(&money))
if (!stretch(&finish))
delay(10);
if (you.line < 3) {
point(&p, you.col, 0);
chk(&p);
}
if (you.line > lcnt - 4) {
point(&p, you.col, lcnt - 1);
chk(&p);
}
if (you.col < 10) {
point(&p, 0, you.line);
chk(&p);
}
if (you.col > ccnt - 10) {
point(&p, ccnt - 1, you.line);
chk(&p);
}
fflush(stdout);
}

int
stretch(ps)
struct point *ps;
{
struct point p;

point(&p, you.col, you.line);
if (abs(ps->col - you.col) < 6) {
if (you.line < ps->line) {
for (p.line = you.line + 1; p.line <= ps->line;
p.line++)
pchar(&p, 'v');
delay(10);
for (; p.line > you.line; p.line--)
chk(&p);
} else {
for (p.line = you.line - 1; p.line >= ps->line;
p.line--)
pchar(&p, '^');
delay(10);
for (; p.line < you.line; p.line++)
chk(&p);
}
return (1);
} else
if (abs(ps->line - you.line) < 3) {
p.line = you.line;
if (you.col < ps->col) {
for (p.col = you.col + 1; p.col <= ps->col;
p.col++)
pchar(&p, '>');
delay(10);
for (; p.col > you.col; p.col--)
chk(&p);
} else {
for (p.col = you.col - 1; p.col >= ps->col;
p.col--)
pchar(&p, '<');
delay(10);
for (; p.col < you.col; p.col++)
chk(&p);
}
return (1);
}
return (0);
}

void
surround(ps)
struct point *ps;
{
struct point x;
int j;

if (ps->col == 0)
ps->col++;
if (ps->line == 0)
ps->line++;
if (ps->line == LINES - 1)
ps->line--;
if (ps->col == COLUMNS - 1)
ps->col--;
apr(point(&x, ps->col - 1, ps->line - 1), "/*\\\r* *\r\\*/");
for (j = 0; j < 20; j++) {
pchar(ps, '@');
delay(1);
pchar(ps, ' ');
delay(1);
}
if (post(cashvalue, 0)) {
apr(point(&x, ps->col - 1, ps->line - 1), " \ro.o\r\\_/");
delay(6);
apr(point(&x, ps->col - 1, ps->line - 1), " \ro.-\r\\_/");
delay(6);
}
apr(point(&x, ps->col - 1, ps->line - 1), " \ro.o\r\\_/");
}

void
win(ps)
struct point *ps;
{
struct point x;
int j, k;
int boxsize; /* actually diameter of box, not radius */

boxsize = fast ? 10 : 4;
point(&x, ps->col, ps->line);
for (j = 1; j < boxsize; j++) {
for (k = 0; k < j; k++) {
pchar(&x, '#');
x.line--;
}
for (k = 0; k < j; k++) {
pchar(&x, '#');
x.col++;
}
j++;
for (k = 0; k < j; k++) {
pchar(&x, '#');
x.line++;
}
for (k = 0; k < j; k++) {
pchar(&x, '#');
x.col--;
}
}
fflush(stdout);
}

int
pushsnake()
{
int i, bonus;
int issame = 0;

/*
* My manual says times doesn't return a value. Furthermore, the
* snake should get his turn every time no matter if the user is
* on a fast terminal with typematic keys or not.
* So I have taken the call to times out.
*/
for (i = 4; i >= 0; i--)
if (same(&snake[i], &snake[5]))
issame++;
if (!issame)
pchar(&snake[5], ' ');
for (i = 4; i >= 0; i--)
snake[i + 1] = snake[i];
chase(&snake[0], &snake[1]);
pchar(&snake[1], SNAKETAIL);
pchar(&snake[0], SNAKEHEAD);
for (i = 0; i < 6; i++) {
if (same(&snake[i], &you)) {
surround(&you);
i = (cashvalue) % 10;
bonus = ((rand() >> 8) & 0377) % 10;
ll();
pr("%d\n", bonus);
delay(30);
if (bonus == i) {
spacewarp(1);
logit("bonus");
flushi();
return (1);
}
if (loot >= penalty) {
pr("You and your $%d have been eaten\n",
cashvalue);
} else {
pr("The snake ate you. You owe $%d.\n",
-cashvalue);
}
logit("eaten");
length(moves);
done();
}
}
return (0);
}

int
chk(sp)
struct point *sp;
{
int j;

if (same(sp, &money)) {
pchar(sp, TREASURE);
return (2);
}
if (same(sp, &finish)) {
pchar(sp, GOAL);
return (3);
}
if (same(sp, &snake[0])) {
pchar(sp, SNAKEHEAD);
return (4);
}
for (j = 1; j < 6; j++) {
if (same(sp, &snake[j])) {
pchar(sp, SNAKETAIL);
return (4);
}
}
if ((sp->col < 4) && (sp->line == 0)) {
winnings(cashvalue);
if ((you.line == 0) && (you.col < 4))
pchar(&you, ME);
return (5);
}
if (same(sp, &you)) {
pchar(sp, ME);
return (1);
}
pchar(sp, ' ');
return (0);
}

void
winnings(won)
int won;
{
struct point p;

p.line = p.col = 1;
if (won > 0) {
move(&p);
pr("$%d", won);
}
}

void
stop(mmy)
int mmy;
{
signal(SIGINT, SIG_IGN);
ll();
length(moves);
done();
}

void
suspend()
{
ll();
cook();
kill(getpid(), SIGTSTP);
raw();
setup();
winnings(cashvalue);
}

void
length(num)
int num;
{
pr("You made %d moves.\n", num);
}

void
logit(msg)
const char *msg;
{
FILE *logfile;
time_t t;

if ((logfile = fopen(_PATH_LOGFILE, "a")) != NULL) {
time(&t);
fprintf(logfile, "%s $%d %dx%d %s %s",
getlogin(), cashvalue, lcnt, ccnt, msg, ctime(&t));
fclose(logfile);
}
}

⑤ scratch贪吃蛇制作教程

scratch贪吃蛇制作教程如下:

1、游戏工亮轮作过程。键盘主要控制贪吃蛇的头部移动,尾巴是沿着轨迹移动就行。贪吃蛇的身体,是用程序画笔模块敬差信画出来的。如果头部吃到食物,就给游戏分数加分。

6、最终游戏效果:点击“绿色旗子”开始游戏,按下键盘的方向键,控制贪吃蛇移动。

吃到食物时,变量“游戏分数”就会加1分。

⑥ 利用解释型语言编写的贪吃蛇程序什么意思

用C语言来实现也是一个好玩的事情。这个游戏我写完后放在知乎,竟然点赞的人数超级多。我觉得大家喜欢,一个方面是因为写得简单,大家都能看得懂,一个可扩展性还是非常强的。

我试了说一下这个代码 核心的三个函数

menu();

setup();

draw();

menu用来设置菜单,也就是我们一运行看到的那个。setup用来设置参数,我们需要设置高度和宽度,还有分搜清橘数,食物的位置。draw也就是画,也就是画整个画面。

还有一个枚举类型 这个结构体用来设置蛇的几个状态,我觉得这个也是面向对象编程的一个思想,把蛇的状态都封装成一个枚举类型。

typedef enum

{
STOP = 0,

LEFT,

RIGHT,

UP,

DOWN

}Direction;

还有

/*判断贪吃蛇的长度*/

void logic()

这个函数,这个函数应该是整个贪吃蛇的精髓了,要理解代码怎么把蛇给连接起来。用了点巧妙的东西。

来看这里面的关键代码

/*把上一个位置记下*/

int lastX = tailX[0];

int lastY = tailY[0];

int last2X, last2Y;

/*重新获取当前的位置*/

tailX[0]=x;

tailY[0]=y;

int i=0;

/*遍历整条蛇的长度 把 0 的位置空出来正闷,其余蛇的位置往后面的空间移动*/

for(i=1; i ntail;i++)

{
last2X = tailX[i];

last2Y = tailY[i];

tailX[i]=lastX;

tailY[i]=lastY;

lastX = last2X;

lastY = last2Y;

}

lastX lastY 用来存上一次的蛇头的位置。后面的 for 循环,通过tail 蛇的长度,把蛇上个状态给保存到数组tailX tailY里面。

完整代码

#include stdio.h

#include stdlib.h

#include stdbool.h

#include Windows.h

#include time.h

#include conio.h /*键盘输入获取*/

bool gameOver;

bool stop = false;

bool hit = false;

/*游戏的边框大小*/

const int width = 50;

const int height = 20;

/*蛇的坐标,食物的坐标还有分数*/

int x,y,fruitX,fruitY,score;

/*蛇每个点的坐标*/

int tailX[200],tailY[200];

/*蛇的默认长度*/

int ntail=3;

typedef enum

{
STOP = 0,

LEFT,

RIGHT,

UP,

DOWN

}Direction;

Direction Dir;

/*开始菜单*/

void menu()

{
int a;

printf( ------------------------------------------------------------------\n

printf( | 贪吃蛇游戏 |\n

printf( | 1) 新游戏 |\n

printf( | 2) 开始边界 |\n

printf( | 3) 退出游戏 |\n

printf( ------------------------------------------------------------------\n

printf( ---- 请输入你的选择:

scanf( %d ,

}

/*初始化状态*/

void setup()

{
gameOver = false;

/*根据当前时间设置“随机数种子”*/

srand(time(NULL));

Dir = STOP;

/*贪吃蛇的位置,固定在中间*/

x= width/2;

y= height/2;

/*食物的位置,位置是随机的*/

fruitX = rand()%width;

fruitY = rand()%height;

score = 0;

}

/*绘制界面世团*/

void draw()

{
if(stop == true)

{
return;

}

system( cls /*清除屏幕*/

printf( 分数:%d ,score);

printf( \n

/*第一行*/

int i;

for(i= 0 ;i width+1;i++)

{
printf( -

}

printf( \n

/*画中间的画面*/

int p;

for(p= 0 ;p height;p++)/*高度*/

{
int q;

for(q= 0 ;q width;q++)/*宽度*/

{
/*第一行最后已给字符*/

if(q==0 || q==width-1)

{
printf( |

}

if(p == fruitY q == fruitX)/*食物的随机坐标*/

{
printf( O

}

else

{
int k=0;

bool print = false;

/*贪吃蛇的长度 默认长度是 3*/

for(k=0;k ntail;k++)

{
if(tailX[k]==q tailY[k]==p)

{
printf( *

print = true;

}

}

/*如果这个位置打印了 * 就不要打印空格了*/

if(!print)

{
printf(

}

}

}

printf( \n

}

/*最后一行*/

int j;

for(j= 0 ;j width+1;j++)

{
printf( -

}

}

/*按键输入控制*/

void input()

{
if(_kbhit())

{
/*获取键盘的输入字符*/

switch(_getch())

{
case 4 :

case 75:/*左键*/

Dir = LEFT;

hit= true;

break;

case 8 :

case 72:/*上键*/

Dir = UP;

hit= true;

break;

case 6 :

case 77:/*右键*/

Dir = RIGHT;

hit= true;

break;

case 2 :

case 80:/*向下键盘键 */

Dir = DOWN;

hit= true;

break;

case x :

case 27:/*ESE*/

gameOver = true;

break;

case 32:/*空格 暂停键*/

stop = !stop;

break;

}

}

else if(!hit stop == false)/*如果没有改变方向*/

{
x++;

}

}

/*判断贪吃蛇的长度*/

void logic()

{
if(stop == true)

{
return;

}

/*把上一个位置记下*/

int lastX = tailX[0];

int lastY = tailY[0];

int last2X, last2Y;

/*重新获取当前的位置*/

tailX[0]=x;

tailY[0]=y;

int i=0;

/*遍历整条蛇的长度 把 0 的位置空出来,其余蛇的位置往后面的空间移动*/

for(i=1; i ntail;i++)

{
last2X = tailX[i];

last2Y = tailY[i];

tailX[i]=lastX;

tailY[i]=lastY;

lastX = last2X;

lastY = last2Y;

}

/*根据方向来改变x y 的值*/

switch(Dir)

{
case UP:

y--;

break;

case DOWN:

y++;

break;

case LEFT:

x--;

break;

case RIGHT:

x++;

break;

}

if(x 0 || width x || y 0 || height y)

{
gameOver = true;

/*清除屏幕*/

system( cls

printf( ------------------------------------------------------------------\n

printf( | |\n

printf( | |\n

printf( | 游戏结束 |\n

printf( | |\n

printf( | |\n

printf( ------------------------------------------------------------------\n

}

if(x==fruitX y==fruitY)

{
/*吃了一个食物,蛇的长度增加1*/

ntail++;

score+=10;

/*更新下一个食物的位置*/

fruitX = rand()%width;

fruitY = rand()%height;

}

}

int main()

{
#if 0

while(1)

{
printf( %d\n ,_getch());

}

#endif

menu();

setup();

draw();

/*循环画贪吃蛇的界面*/

while(!gameOver)

{
draw();

input();

logic();

Sleep(70);

}

return 0;

}

上面这段代码直接在Dev C++上面应该是可以运行的,很多人在知乎上私信问我,为什么我的贪吃蛇执行不了呢,可能就是平台不同,少了这个头文件,少了那个头文件,但是你为什么不能跟我一样,用Dev C++呢,轻量级,简单。代码的精髓是什么?我认为精髓一定是思想,不是你写了多少行代码,用了什么高端的IDE。

我自认为我的注释已经写得不错了,所以就没有什么好说明的了吧,有不明白的把代码过一下,至于屏幕刷新这个东西,如果只是用时间刷新就会闪屏,所以出现了一个双缓存,把要显示的东西送到一个buff里面去,另一个buff用来显示,这样就可以保证不会出现闪屏。除了写贪吃蛇,可以用这个方法写其他小程序,挺有意思的。

在知乎上,发起了一个C语言 100 行代码之内实现贪吃蛇的问题。我觉得很不错,里面很多同学的回复都非常赞,特别是叶大神的回复。

0142235ea7197f4e1d7ClTovj.png

学习C/C++编程知识,想要成为一个更加优秀的程序员,或者你学习C/C++的时候有难度,可以来UP主页的C++编程学习圈,里面不仅有学习视频和文件资料,还有更多志同道合的朋友,欢迎初学者和想转行的朋友,和大家一起交流成长会比自己琢磨更快哦! UP也上传了一些C/C++学习的视频教程和C语言基础教程,有兴趣的小伙伴可以看看~ 谢谢阅读!

文章知识点与官方知识档案匹配
C技能树首页概览
115488 人正在系统学习中
点击阅读全文
打开CSDN APP,看更多技术内容

C语言之出圈游戏(详解)
PTA7-5 出圈游戏 用指针实现以下功能:有n个人围成一个圈,顺序排号。从第1个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来的第几号。 (1)编程提示 每三个人离开,置为0;当数到最后一个人时,将指针重新指向第一个人;m表示离开的人数,当m=n-1时,说明只剩下一个人,循环结束。 输入样例: 10 输出样例: 4 上面是题目的要求。 本小白的思路是让n个人形成一个一维数组,每次判断该人是不是要离开, 如果离开,这就不添加到这个一维数组里,并记录下来离开的人数,否则,就在数组里加上这
继续访问
9718 整数因子分解(优先做)
9718 整数因子分解(优先做)Description输入格式输出格式输入样例输出样例 时间限制:1000MS 代码长度限制:10KB 提交次数:0 通过次数:0 题型: 编程题 语言: G++;GCC;VC Description 大于1的正整数 n 都可以分解为 n = x1 * x2 * … * xm, 每个xi为大于1的因子,即1<xi<=n 。 例如:当n=12时,共有8种不同的分解式: 12 = 12 12 = 62 12 = 43 12 = 34 12 = 322 12 =
继续访问
【C语言】链表——圈中游戏问题(数到3退出)
问题描述: 有n个人围成一圈,从第1个人开始报数1、2、3,每报到3的人退出圈子。使用链表找出最后留下的人。
继续访问
套圈游戏c语言程序设计教程课后答案,概率统计习题带答案
概率论与数理统计习题及题解沈志军 盛子宁第一章 概率论的基本概念1.设事件B A ,及B A 的概率分别为q p ,及r ,试求)(),(),(B A P B A P AB P 及)(AB P2.若C B A ,,相互独立,试证明:C B A ,,亦必相互独立。3.试验E 为掷2颗骰子观察出现的点数。每种结果以),(21x x 记之,其中21,x x 分别表示第一颗、第二颗骰子的点数。设事件}10...
继续访问
c语言贪吃蛇设计意义,C语言贪吃蛇设计理念.pdf
基于C语言的 “贪吃蛇”游戏的设计与实现摘3.功能描述 本游戏主要实现以下几种功能:“贪吃蛇”游戏贪 游 游吃 戏 戏蛇 显 分的 ...
继续访问
热门推荐 一个好玩的小游戏(纯C语言编写)
最近在看知乎是发现了一个这一个专栏 https://zhuanlan.hu.com/c2game 从中获取的许多知识,本文中的游戏也是从里面学到的,不过本人又自己加了一些功能。 这是一个类似于飞机大战的游戏,不过目前代码量比较小,所以看起来非常简陋游戏界面如下 更新日志,本人将原来的原来的代码有进一步的优化了一下,之前是只有一个非常小的战机现在更新后可以产生一个非常大的战机(看起来也更
继续访问
如何用C语言实现圈叉游戏(-)
今天情人节,还是在学习C语言 自己写了一遍发现自己写的没有书上的代码更简练 就把书上的代码稍微修改了一下 下面看游戏界面 和昨天的米字棋差不多,有时间会结合米字旗的代码做些修改
继续访问
C语言:围圈报数游戏
游戏规则:有N个人围成一圈,顺序排号,从第一个人开始1到D报数,,凡报到D的人退出圈子(下场),问最后留下来的是原来的第几号? 逻辑思想:用布尔数组记下每个人的上场状态,1为上场,0为下场,开始游戏后每D个状态为1的人将状态改为0(即下场),重复下场动作N-1次后可知剩下一人,遍历数组找出剩下的状态为1的人即可。 代码如下: #include<stdio.h> #define N 1000 //参与的总人数 #define D 3 //每D个人报数下场 int main() { /
继续访问

数圈圈
26个大写字母里面,有一部分字母是带有圈的,比如A有1个圈,B有2个圈,C没有圈, 给你一个带有n个大写字母组成的字符串,请问一共有多少个圈圈。 你可以将字母中完全封闭的一个区域当作一个圈 输入描述: 第一行输入一个整数t,代表有t组测试数据, 对于每组测试数据, 第一行输入一个整数n代表字符串的长度, 第二行输入一个长度为n的字符串S,保证只由大写字母组成。 1<=t<=10 1<=n<=1*10^5 输出描述: 对于每组测试数据,输出一个整数代表这个字符串共有多少个圈圈。 并且对
继续访问
C语言围圈游戏
玩游戏,一共 N( 1≤N≤1000 )个人围成一圈,从某个人起顺时针顺序编号为 1 ~ N 号。 游戏只能有一个人赢,船长让大家数数,从编号为 1 的人开始顺时针报数,每轮从 1 报到 M 号( 1≤ M ),凡报到 M 的人视为出局,接着又从紧邻的下一个人开始同样的报数(紧邻的下一个人又报 1 )。 依次输入人数 N ,和报数规则的末数 M ,中间隔一个空格。输出能赢的相应编号R。 #include<stdio.h> int main() {...
继续访问
圈中游戏
有n个人围成一圈,从第1个人开始报数1、2、3,每报到3的人退出圈子。编程使用链表找出最后留下的人。 bug版本 #include <stdio.h> #include <stdlib.h> struct player { long num; struct player *next; }; typedef struct player NODE; NODE *create(int n) { NODE *head, *tail, *p; int i =
继续访问
c语言圈中的游戏,C语言实现扫雷游戏
本文将介绍如何用C语言多文件编程实现扫雷该示例扫雷程序可实现以下几个功能:自定义雷数踩雷后会将所有雷显示出来地图大小易修改Mine_clearance.h#pragma once#define _CRT_SECURE_NO_WARNINGS#include#include#include#define ROW 11#define COL 11#define 踩雷 0#define 玩家胜利 1in...
继续访问

c语言程序设计求各位数之和,C语言for回圈设计输入一个正整数,求它的各位数字之和及位数 例如234的各位数之和为9 位数是3...
C语言for回圈设计输入一个正整数,求它的各位数字之和及位数 例如234的各位数之和为9 位数是3以下文字资料是由(历史新知网www.lishixin.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!C语言for回圈设计输入一个正整数,求它的各位数字之和及位数 例如234的各位数之和为9 位数是3同意二楼,但得改一下#includeint main(int argc, ch...
继续访问

c语言draw函数使用实例,使用函数实现两个数的交换(C语言)
<>题目:使用函数实现两个数的代码<>常规思路:定义函数,调用函数,完成交换。你的代码是否和下面一样呢?#include #include void Swap(int a,int b) { int tmp = a; a =b; b = tmp; } int main() { int x = 10; int y = 20; Swap(x,y); printf("%d %d\n...
继续访问
圈中的游戏 c语言,圈叉棋小游戏的简单实现代码
该楼层疑似违规已被系统折叠隐藏此楼查看此楼#include int game[3][3]={0};void Show(int turn,int x,int y){int i=0,j=0;if(x>0 && y>0){if(turn%2){game[x-1][y-1]=1;}else{game[x-1][y-1]=-1;}}for(i=0;i<3;++i){for...
继续访问
c语言圈中的游戏,圈叉棋小游戏的简单实现代码
该楼层疑似违规已被系统折叠隐藏此楼查看此楼#include int game[3][3]={0};void Show(int turn,int x,int y){int i=0,j=0;if(x>0 && y>0){if(turn%2){game[x-1][y-1]=1;}else{game[x-1][y-1]=-1;}}for(i=0;i<3;++i){for...

java 编写贪吃蛇游戏的大体思路是什么

楼主没有看到蛇移动的本质,蛇虽然是分成很多块,但他们还是一个整体,每一块的移动都和上一块有关,所以不需要对每一块都进行判断。x0dx0a原理:x0dx0a把蛇身体的每一块看成一个对象(对象存储该块的坐标和相关信息),作为节点存储在线性链表中,再设置一个变量标志蛇的方向(通过按键可以改变)。一般人都是让每一个节点等于他指向的下一个节点,并让头节点改变位置来实差局现转弯和移动,这个算法复杂度太高(O(n)),实际上只要做两步操作,插入一个头节点,删除一个尾节点就可以了,新插入的头节点位置根据蛇当前的方向决定 用一个数组将蛇头的行径记录下来,然后第二段的下一个方格设置为蛇头走过的方格,这样子蛇走过的路径都是前一段走过的,最后将跟着蛇头走了,比如x0dx0a蛇身的路径x0dx0a for(int i=snakeLength-1;i>0;i--){x0dx0arows[i]=rows[i-1];//依次将蛇前面一段走过行的路段赋值给蛇的下一段x0dx0acols[i]=cols[i-1];//依次将蛇前面一段走过列的路段赋值给蛇的下一段x0dx0a}x0dx0afor(int i=1;i

⑧ 如何用慧编程做贪吃蛇代码

用慧编程做贪吃蛇代码过程如下:
1、我们需要建立四个头文件,然后分别设置蛇的状态,上下左右,这是蛇能够有方向可走的前提,然后我们再设置蛇身的节点,定义一个简单的函数,这样蛇的全身以及他的行走方向就弄完了。
2、贪吃蛇不能穿墙代码。
3、第二步,一个函数这个函数的目的是贪吃蛇不能穿墙,很简单的代码分别设置长宽的最大位移,在内部范围内设置为一即可通过,否则不能穿墙。贪吃蛇随机生成一个食物。
4、设置一个随机函数。这样贪吃蛇代码就做好了。
慧编程是一款面向STEAM教育领域的积木式和代码编程软件,基于图形化编程开发。

⑨ c语言 贪吃蛇 程序

基本思路:

蛇每吃一个食物蛇身子就增加一格,用UP, DOWN, LEFT, RIGHT控制蛇头的运动,而蛇身子跟着蛇头走,每后一格蛇身子悉薯桥下一步走到上一格蛇身子的位置睁猛,以此类推。

#include <stdio.h>

#include <conio.h>

#include <windows.h>

#define BEG_X2

#define BEG_Y1

#define WID20

#define HEI20

HANDLE hout;

typedef enum {UP, DOWN, LEFT, RIGHT} DIR;

typedef struct Snake_body

{

COORD pos;//蛇身的位置

struct Snake_body *next;//下一个蛇身

struct Snake_body *prev;//前一个蛇身

}SNAKE, *PSNAKE;

PSNAKE head = NULL;//蛇头

PSNAKE tail = NULL;//蛇尾

//画游戏边框的函数

void DrawBorder()

{

int i, j;

COORD pos = {BEG_X, BEG_Y};

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

{

SetConsoleCursorPosition(hout, pos);

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

{

if(i == 0)//第一行

{

if(j == 0)

printf("┏");

else if(j == WID - 1)

printf("┓");

else

printf("━");

}

else if(i == HEI - 1)//最后一行

{

if(j == 0)

printf("┗");

else if(j == WID - 1)

printf("┛");

else

printf("━");

}

else if(j == 0 || j == WID - 1)//第一列或最后一列

printf("┃");

else

printf(" ");

}

++pos.Y;

}

}

//添加蛇身的函数

void AddBody(COORD pos)

{

PSNAKE pnew = (PSNAKE)calloc(1, sizeof(SNAKE));

pnew->pos = pos;

if(!head)

{

head = tail = pnew;

}

else

{

pnew->next = head;//新创建蛇身的next指向原先的蛇头

head->prev = pnew;//原先的蛇头的prev指向新创建的蛇身

head = pnew;//把新创建的蛇身作为新的手和蛇头

}

SetConsoleCursorPosition(hout, head->pos);

printf("◎");

}

//蛇身移动的函数

void MoveBody(DIR dir)

{

PSNAKE ptmp;

COORD pos = head->pos;

switch(dir)

{

case UP:

if(head->pos.Y > BEG_Y + 1)

--pos.Y;

else

return;

break;

case DOWN:

if(head->pos.Y < BEG_Y + HEI - 2)

++pos.Y;

else

return;

break;

case LEFT:

if(head->pos.X > BEG_X + 2)

pos.X -= 2;

else

return;

break;

case RIGHT:

if(head->pos.X < BEG_X + (WID - 2) * 2)

pos.X += 2;

else

return;

break;

}

AddBody(pos);//添加了一个新的蛇头

ptmp = tail;//保存当前的蛇尾

tail = tail->prev;

if(tail)

tail->next = NULL;

SetConsoleCursorPosition(hout, ptmp->pos);

printf(" ");

free(ptmp);

}

int main()

{

int ctrl;

DIR dir = RIGHT;//初始蛇的方向是向右的

COORD pos = {BEG_X + 2, BEG_Y + HEI / 2};

system("color 0E");

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

hout = GetStdHandle(STD_OUTPUT_HANDLE);

printf(" ------------贪吃蛇的移动------------");

DrawBorder();

//自定义几个蛇的身体

AddBody(pos);

pos.X += 2;

AddBody(pos);

pos.X += 2;

AddBody(pos);

pos.X += 2;

AddBody(pos);

pos.X += 2;

AddBody(pos);

pos.X += 2;

AddBody(pos);

pos.X += 2;

AddBody(pos);

//控制蛇的移动

while(ctrl = getch())

{

switch(ctrl)

{

case 'w':

if(dir == DOWN)

continue;

dir = UP;

break;

case 's':

if(dir == UP)

continue;

dir = DOWN;

break;

case 'a':

if(dir == RIGHT)

continue;

dir = LEFT;

break;

case 'd':

if(dir == LEFT)

continue;

dir = RIGHT;

break;

case 'q':

return 0;

}

MoveBody(dir);

}

return 0;

}

(9)贪吃蛇编程思路扩展阅读:

实现逻辑

1,可以设置光标,就能实现制定位置打印制定符号。

2,涉及一个结构体,包含两个元素坐标元素和一个结构体指针。

3,结构体串联形成链表,遍历获取成员坐标,打印符号得到蛇身。

4,不断的加头,去尾,重新遍历坐标,再打印形成蛇的移动。

5,食物产生的位置判定,不能越界,也不能与蛇身体重合。

6,蛇的转向判定,一条规则,不允许倒退。

7,转向的实现,跟行进方向决定新的关节坐标(当前头的上下左右)

8,死亡检测,是否头节点坐标是否与墙壁重合,是否与身体其他关节重合。

9,加速减速,设置刷新休眠时间实现。

⑩ java贪吃蛇技术选型怎么写的

Java贪吃蛇技术选型一般需要考虑以下几点:

阅读全文

与贪吃蛇编程思路相关的资料

热点内容
微信聊天界面源码 浏览:22
seo竞价推广点击价格算法公式 浏览:315
框架结构可以加密吗 浏览:218
python编译器怎么清除 浏览:73
linux全局socks代理 浏览:611
php微信抽奖 浏览:771
压缩算法嵌入式移植 浏览:531
php新手小例子 浏览:233
按照医生的算法一周是几天 浏览:805
三次b样条曲线算法 浏览:924
java7特性 浏览:555
爱山东app小学报名怎么知道报没报上 浏览:458
android获取wifi信号 浏览:133
娜拉美妆app怎么使用 浏览:760
有了源码要买服务器吗 浏览:365
app怎么查看自己的存款利息 浏览:515
碧蓝安卓与b站有什么区别 浏览:342
php静态块 浏览:719
ftpmget命令 浏览:475
源码时代怎样 浏览:415