【基础语法】C语言编写贪吃蛇游戏

本次为大家提供一个c语言编写的贪吃蛇代码

目录

一、实现效果

二、部分代码解释

三、完整代码下载链接

一、实现效果

键位:使用wasd四个键位来控制方向,按q键退出(注意在终用英文输入法实现键控)

规则:蛇每吃一个豆会得10分,同时身体边长、移速加快

当蛇碰到墙壁或咬到自身时游戏结束,同时会输出游戏得分

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA6bi_6JKZ5LmL5aeL,size_20,color_FFFFFF,t_70,g_se,x_16

 watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA6bi_6JKZ5LmL5aeL,size_20,color_FFFFFF,t_70,g_se,x_16

 

二、部分代码解释

(1)用结构体定义蛇和豆

typedef struct Snakes{int x;int y;struct Snakes *next;}snake; snake *head,*tail; struct Food{int x;int y;}food;

(2)打印墙壁

void creatgraph()             {int i;for (i = 0; i<58; i += 2)//打印上下边框{gotoprint(i, 0);gotoprint(i, 26);}for (i = 1; i < 26; i++){gotoprint(0, i);gotoprint(56, i);}head = (snake*)malloc(sizeof(snake));head->x = 16;head->y = 15;//gotoprint(head->x, head->y);tail = (snake*)malloc(sizeof(snake));snake *p = (snake*)malloc(sizeof(snake));snake *q = (snake*)malloc(sizeof(snake));p->x = 16;p->y = 16;q->x = 16;q->y = 17;head->next = p;p->next = q;q->next = tail;//gotoprint(p->x, p->y);//gotoprint(q->x, q->y);tail->next = NULL;tail->x = 4;tail->y = 2;}void gotoxy(int x, int y){COORD pos;HANDLE hOutput;pos.X = x;pos.Y = y;hOutput = GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorPosition(hOutput, pos);}void gotoprint(int x, int y){gotoxy(x, y);printf("■");}void gotodelete(int x, int y){gotoxy(x, y);printf("  ");}

(3)生成豆

void creatfood(){srand((int)time(NULL));lable:food.y = rand() % (25 - 1 + 1) + 1;food.x = rand() % (54 - 2 + 1) + 2;if (food.x % 2 != 0){food.x = food.x+1;}snake *judge = head;while (1)  {if (judge->next == NULL) break;if (food.x == judge->x&&food.y == judge->y){goto lable;}judge = judge->next;}gotoxy(food.x, food.y);printf("⊙");}

(4)点击控制函数

int ClickControl(){char c;while (1){if (Judge()==0) return 0;if (_kbhit()){click = _getch();}MovingBody();Eating();}return 1;}

(5)移动控制

void MovingBody(){int count = 0;int a = head->x, b = head->y;snake *p = head;while (1){if (p->next == NULL) break;gotodelete(p->x, p->y);count++;p = p->next;}switch (click){case up:head->y -= 1;ChangeBody(a,b);break;case down:head->y += 1;ChangeBody(a,b);break;case left:head->x -= 2;ChangeBody(a,b);break;case right:head->x += 2;ChangeBody(a,b);break;case stop:_getch();break;}p = head;while (1){if (p->next == NULL) break;gotoprint(p->x, p->y);p = p->next;}p = head;gotoxy(0, 28);if (count <= 10) speed = 150;else if (count > 10 && count <= 20) speed = 100;else if (count > 20 && count <= 40) speed = 50;else speed = 10;Sleep(speed);}

(6)更改蛇身

void ChangeBody(int a,int b){snake *p = head->next;int mid1, mid2,_mid1,_mid2;mid1 = p->x;mid2 = p->y;while (1){if (p->next->next == NULL) break;_mid1 = p->next->x;_mid2 = p->next->y;p->next->x = mid1;p->next->y = mid2;mid1 = _mid1;mid2 = _mid2; p = p->next;}p = head->next;{p->x = a;p->y = b;}}

三、完整代码下载链接

链接:https://pan.baidu.com/s/1XxCY2XBjKeoKL9q3XKgUpg 
提取码:6666

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
用windows api 做的贪吃蛇 #include #include"resource.h" #include"Node.h" #include #include TCHAR szAppname[] = TEXT("Snack_eat"); #define SIDE (x_Client/80) #define x_Client 800 #define y_Client 800 #define X_MAX 800-20-SIDE //点x的范围 #define Y_MAX 800-60-SIDE //点y的范围 #define TIME_ID 1 #define SECOND 100 #define NUM_POINT 10 //点的总个数 #define ADD_SCORE 10 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { HWND hwnd; //窗口句柄 MSG msg; //消息 WNDCLASS wndclass; //窗口类 HACCEL hAccel;//加速键句柄 wndclass.style = CS_HREDRAW | CS_VREDRAW; //窗口的水平和垂直尺寸被改变时,窗口被重绘 wndclass.lpfnWndProc = WndProc; //窗口过程为WndProc函数 wndclass.cbClsExtra = 0; //预留额外空间 wndclass.cbWndExtra = 0; //预留额外空间 wndclass.hInstance = hInstance; //应用程序的实例句柄,WinMain的第一个参数 wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); //设置图标 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); //载入预定义的鼠标指针 wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); //设置画刷 wndclass.lpszMenuName = szAppname; //设置菜单 wndclass.lpszClassName = szAppname; //设置窗口类的名字 if (!RegisterClass(&wndclass))//注册窗口类 { MessageBox(NULL, TEXT("这个程序需要windows NT!"), szAppname, MB_ICONERROR); return 0; } hwnd = CreateWindow(szAppname, TEXT("Snack_eat"),//CreateWindow函数调用时,WndProc将受到WM_CREATE WS_OVERLAPPEDWINDOW&~WS_THICKFRAME& ~WS_MAXIMIZEBOX,//普通的层叠窗口&禁止改变大小&禁止最大化 CW_USEDEFAULT, //初始x坐标(默认) CW_USEDEFAULT, //初始y坐标 x_Client, //初始x方向尺寸 770 y_Client, //初始y方向尺寸 750 NULL, //父窗口句柄 NULL, //窗口菜单句柄 hInstance, //程序实例句柄 WinMain函数中第二个参数 NULL); //创建参数 ShowWindow(hwnd, iCmdShow);//显示窗口,iCmdShow是WinMain的第四个参数,决定窗口在屏幕中的初始化显示形式,例:SW_SHOWNORMAL表示正常显示 UpdateWindow(hwnd);//使窗口客户区重绘,通过向WndProc发送一条WM_PAINT消息而完成的 hAccel = LoadAccelerators(hInstance, szAppname);//加载加速键 while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(hwnd, hAccel, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } }/* while (GetMessage(&msg, NULL, 0, 0))//GetMessage函数从消息队列中得到消息,填充msg。如果msg.message等于WM_QUIT,返回0,否则返回非0 { TranslateMessage(&msg);//将msg返回给windows已进行某些键盘消息的转换 DispatchMessage(&msg);//将msg再次返回给windows }*/ return msg.wParam;//msg.wParam是PostQuitMessage函数的参数值,通常是0 } ...

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鸿蒙之始

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值