C语⾔实现贪吃蛇游戏(命令⾏)这是⼀个纯C语⾔写的贪吃蛇游戏,供⼤家参考,具体内容如下
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
#include<conio.h>
#define SNAKE_LENGTH 100//定义蛇的最⼤长度
#define SCREEN_WIDETH 80
#define SCREEN_HEIGHT 30
//定义每⼀节蛇的坐标
struct coor{
int x;
int y;
};
//枚举⽅向
enum CH {
right = VK_RIGHT,
left = VK_LEFT,
up = VK_UP,
down = VK_DOWN
};
//定义蛇的属性
struct snake{
int len;//当前蛇的长度
struct coor coord[SNAKE_LENGTH];//每⼀节蛇的坐标
enum CH CH;//定义蛇的⽅向
int SPEED;
int flag;//定义蛇的状态  1表⽰存活  0表⽰死亡
}snake;
//光标移动函数
void gotoxy(int x, int y)
{
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
//初始化游戏界⾯
void init_sence()
{
//初始化上下墙
for (int i = 0; i < SCREEN_WIDETH; i += 2)
{
gotoxy(i,0);
printf("■");
gotoxy(i, SCREEN_HEIGHT);
printf("■");
}
//初始化左右墙
for (int i = 0; i <=SCREEN_HEIGHT; i++)
{
gotoxy(0, i);
printf("■");
gotoxy(SCREEN_WIDETH,i);
printf("■");
}
//打印提⽰信息
gotoxy(SCREEN_WIDETH + 5, 2);
printf("\t\t贪吃蛇");
gotoxy(SCREEN_WIDETH + 5, 6);
printf("2018//12//1");
gotoxy(SCREEN_WIDETH + 5, 8);
printf("作者:⼩⾖芽");
gotoxy(SCREEN_WIDETH + 5, 10);
printf("F1:加速\tF2:减速");
gotoxy(SCREEN_WIDETH + 5, 12);
printf("CTRL:继续\t空格:暂停");
gotoxy(SCREEN_WIDETH + 5, 14);
printf("ESC:退出游戏");
gotoxy(SCREEN_WIDETH + 5, 28);
printf("建议:QQ:2862841130:::");
}
struct foodcoord {
int x;
int y;
int flag;//定义⾷物的状态
}food;
/
/**这是c程序**
#include"snake.h"
//蛇的移动
void move_snake();
//画出蛇
void draw_snake();
//产⽣⾷物
void creatfood();
//判断蛇是否吃到⾷物
void eatfood();
//判断蛇是否死掉
void SnakeState();
int main()
{
//设置窗⼝⼤⼩
system("mode con cols=110 lines=31");
//设置标题
SetConsoleTitleA("贪吃蛇");
//初始化蛇
begin:
snake.CH = VK_RIGHT;//初始化⽅向
snake.len = 5; //初始化长度
snake.SPEED = 300;//初始化蛇的移动速度
//初始化⾷物状态
food.flag = 1;//1表⽰吃到⾷物  0表⽰没有吃到⾷物
//初始化⾷物状态
snake.flag = 1;//1活 0死
init_sence();//初始化游戏界⾯
while (1)
{
draw_snake();//画蛇
Sleep(snake.SPEED);//蛇的移动速度
move_snake();//移动蛇
if(food.flag)
creatfood();//产⽣⾷物
eatfood();//判断是否吃到⾷物
SnakeState();//判断蛇是否死亡
if (!snake.flag)break;
}
system("cls");
gotoxy(SCREEN_WIDETH/2, SCREEN_HEIGHT/2-4);
printf(" GAME OVER");
gotoxy(SCREEN_WIDETH / 2-6, SCREEN_HEIGHT / 2+2);
printf("你的得分是:\t\t\t%d ",snake.len-1);
gotoxy(SCREEN_WIDETH / 2-6, SCREEN_HEIGHT / 2+4);
printf("我不服再来:\t\t\tCTRL ");
gotoxy(SCREEN_WIDETH / 2-6, SCREEN_HEIGHT / 2+6);
printf("算了垃圾游戏毁我青春:\t\tESC");
while (1)
{
if (GetAsyncKeyState(VK_CONTROL))
{
system("cls");
goto begin;
}
else if (GetAsyncKeyState(VK_ESCAPE))
return 0;
}
}
//蛇的移动
void move_snake()
{
//判断是否有按键操作
if (GetAsyncKeyState(up))
{
if(snake.CH!=down)snake.CH = up;
}
else if (GetAsyncKeyState(down))
{
if (snake.CH != up)snake.CH = down;
}
else if (GetAsyncKeyState(right))
{
if (snake.CH != left)snake.CH = right;
}
else if (GetAsyncKeyState(left))
{
if (snake.CH != right)snake.CH = left;
}
else if (GetAsyncKeyState(VK_F1))
{
if(snake.SPEED>=100)snake.SPEED -= 50;
}
else if (GetAsyncKeyState(VK_F2))
{
if (snake.SPEED <= 3000)snake.SPEED += 100;
}
//根据检测到的⽅向改变蛇头的位置
switch (snake.CH)
{
case d[1].x += 2; break;
case d[1].x -= 2; break;
case d[1].y -= 1; break;
case d[1].y += 1; break;
}
}
//画出蛇
void draw_snake()
{
//画出蛇头
d[1].x, d[1].y);
printf("□");
//画出蛇⾝,直接⼀个for循环实现
for (int i = 2; i <= snake.len; i++)
{
d[i].x, d[i].y);
printf("□");
}
//擦掉尾巴
d[snake.len].x, d[snake.len].y); printf(" ");
//遍历每⼀节蛇
for (int i = snake.len; i >1; i--)
{
}
gotoxy(0, 0);
printf("■");
gotoxy(85, 25);
printf("得分:%d ", snake.len-1);
}
//产⽣⾷物
void creatfood()
{
//随机种⼦⽣成
srand((unsigned)time(NULL));
if(food.flag)
while (1)
{
food.x = rand() % 80;
food.y = rand() % 30;
if (food.x % 2 == 0 && food.x >= 2 && food.x <= 78 && food.y > 1 && food.y < 30)
{
int flag = 0;
//判断产⽣的⾷物可不可能在蛇的⾝体上
for (int i = 1; i <= snake.len; i++)
{
if (d[i].x == food.x&&d[i].y == food.y)
{
flag = 1;
break;
}
}
if (flag)continue;
//绘制⾷物
else
{
gotoxy(food.x, food.y);
printf("⊙");
food.flag = 0;
break;
}
}
}
food.flag = 0;
}
//判断蛇是否吃到⾷物
void eatfood()
{
//只需要判断蛇头是否与⾷物重合
if (food.x == d[1].x&&food.y == d[1].y)
{
snake.len+=1;
food.flag = 1;
}
}
c语言贪吃蛇编程代码//判断蛇是否死掉
void SnakeState()
{
if (d[1].x < 2 || d[1].x>78 || d[1].y < 1 || d[1].y>29) snake.flag = 0;
for (int i = 2; i <= snake.len; i++)
{
if (d[1].x == d[i].x&&d[1].y == d[i].y)
snake.flag = 0;
}
}
更多有趣的经典⼩游戏实现专题,分享给⼤家:
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。