贪吃蛇的C语言实现的傻瓜教程(包含详细解释)

本文是一篇关于使用C语言实现贪吃蛇游戏的教程,涵盖程序设计思路、完整代码和部分优化策略。内容包括界面设计、贪吃蛇构成、移动逻辑、食物生成、碰撞检测、变长机制、反应速度控制以及排行榜设计。同时,讨论了在C语言中如何处理蛇的移动、食物生成、碰撞检测和游戏速度控制等问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考文章

https://blog.youkuaiyun.com/qq_40953281/article/details/79315254

关于贪吃蛇C语言的要求介绍

题目描述
设计一个类似贪吃蛇的游戏,并用C语言编写程序实现,该程序的功能参照贪吃蛇游戏,主要功能包括:
(1)蛇移动区域的设定,移动控制键的设置。
(2)食物的出现位置的随机性。
(3)蛇移动路径的自然性,蛇每遇到(吃)一个食物,蛇身会长长,直至布满整个移动区域。
(4)蛇头冲出设定的移动区域和蛇身体即为“失败”。
2.题目要求
(1)按照分析、设计、编码、调试和测试过程完成应用程序;
(2)学习并使用流程图等工具,并在撰写报告中使用;
(3)边框、食物、蛇头、蛇身的构成、蛇头的初始位置、蛇身的长度等信息可以自行安排。

程序预览

主界面
用户名
排行榜
游戏说明
游戏界面

完整代码(包含部分测试代码已注释)

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <Windows.h>
#include <time.h>
#include <conio.h>
#include <stdlib.h>
#include <algorithm>
#include <windows.h>
using namespace std;

#define up 'w'
#define down 's'
#define left 'a'
#define right 'd'
#define stop 'p'

char name[21]= {"user"}; //系统默认名称为user

typedef struct
{
    char num[21];
    char Name[21];
    char garde[21];
} guys;//排行榜内容

guys noob[11];//排行榜中前十名的数据和当前玩家的数据
guys nownoob;//当前用户的信息

typedef struct
{
    int num;
    char Name[21];
    int garde;
} midguy;//中间储存

typedef struct Snakes
{
    int x;
    int y;
    struct Snakes *next;
} snake;//蛇坐标

snake *head,*tail;

struct Food
{
    int x;
    int y;
} food;//食物坐标

int score = 0;//分数
char click = 1;//是否按下键
int speed;//控制蛇的速度

void gotoxy(int x, int y)
{
    COORD pos;
    HANDLE handle;
    pos.X = x;
    pos.Y = y;
    handle = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(handle, pos);
}//控制光标位置

void print(int x, int y)
{
    gotoxy(x, y);
    printf("■");
}//打印数据

void clearprint(int x, int y)
{
    gotoxy(x, y);
    printf("  ");
}//使用覆盖的方式删除

void creatfood()
{
    srand((unsigned)time(NULL));//设置随机数
loop:
    food.y = rand() % (25) + 1;
    food.x = rand() % (54 - 1) + 2;
    if (food.x % 2 != 0)
    {
        food.x++;
    }
    snake *bodys = head;
    while (1)  //遍历排除蛇身重复
    {
        if (bodys->next == NULL)
            break;
        if (food.x == bodys->x&&food.y == bodys->y)
        {
            goto loop;
        }//如果食物生成在蛇的身上就重新生成一个食物。
        bodys = bodys->next;
    }
    gotoxy(food.x, food.y);
    printf("★");
}

int welcome()
{
    printf("**************************************************************************************************************\n");
    printf("**************************************************************************************************************\n");
    printf("                                                     贪吃蛇\n");
    printf("**************************************************************************************************************\n");
    printf("**************************************************************************************************************\n");
    printf("**************************************************************************************************************\n");
    printf("**************************************************************************************************************\n");
    printf("                                              作者:计机国际张伟\n");
    printf("                                              学号: 201825110118\n");
    printf("**************************************************************************************************************\n");
    printf("**************************************************************************************************************\n");
    printf("**************************************
用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 } ...
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值