贪吃蛇(windows C语言)

#include <stdio.h>
#include <malloc.h>
#include <time.h>
#include <stdlib.h>
#include <windows.h>
#include <unistd.h>
#include <pthread.h>
#include <conio.h>
#include <process.h>
//蛇
typedef struct
{
    int x;
    int y;
    struct SNAKE *pNext;
} SNAKE;
//苹果
typedef struct
{
    int x;
    int y;
} Point;
//边框
typedef struct
{
    int w;
    int h;
} Border;
char key;
Border b;
Point p;
SNAKE *h;//头
SNAKE *end;
Point e;//尾
char way;
//定位
void gotoxy(int x, int y)
{
    COORD pos = {x,y};
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);// 获取标准输出设备句柄
    SetConsoleCursorPosition(hOut, pos);//两个参数分别是指定哪个窗体,具体位置
}
//隐藏光标
void HideCursor()
{
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO cci;
    GetConsoleCursorInfo(hOut,&cci);//获取光标信息
    cci.bVisible = FALSE;//隐藏光标
    SetConsoleCursorInfo(hOut,&cci);//设置光标信息
}
//显示边框
void showborder()
{
    //边框
    for (int i = 0; i < b.h; i++)
    {
        for (int n = 0; n < b.w; n++)
        {
            if (i == 0 || i == b.h - 1 || n == 0 || n == b.w - 1)
                printf(".");
            else
                printf(" ");
        }
        printf("\n");
    }
}
//显示苹果
void showpoint()
{
    srand((int)time(0));
    p.x=rand()%(b.w-2)+2;
    p.y=rand()%(b.h-2)+2;
    gotoxy(p.x,p.y);
    printf(".");
}
void printmove()
{
    SNAKE *temp=h;
    //吃苹果
    if(temp->x==p.x&&temp->y==p.y)
    {
        SNAKE *temp2=(SNAKE *) malloc(sizeof(SNAKE));
        temp2->x=e.x;
        temp2->y=e.y;
        temp2->pNext=NULL;
        end->pNext=temp2;
        end=temp2;
        showpoint();
    }
    else
    {
        gotoxy(e.x,e.y);
        printf(" ");
    }
    do
    {
        //撞墙
        if(temp->x==0||temp->x==b.w||temp->y==0||temp->y==b.h)
            _endthreadex( 0 );
        gotoxy(temp->x,temp->y);
        printf(".");
    }
    while(temp=temp->pNext);
}
// 蛇移动
unsigned __stdcall SnakThreadFunc( void* pArguments )
{
    while(1)
    {
        SNAKE *temp=h;
        SNAKE *temp2=temp->pNext;
        Point tp1,tp2;
        e.x=end->x;
        e.y=end->y;
        tp1.x=temp->x;
        tp1.y=temp->y;
        switch(way)
        {
        case 'U':
        {
            if(temp->y-1<temp2->y)
                temp->y-=1;
            else
                temp->y+=1;
            break;
        }

        case 'D':
        {
            if(temp->y+1>temp2->y)
                temp->y+=1;
            else
                temp->y-=1;
            break;
        }

        case 'L':
        {
            if(temp->x-1<temp2->x)
                temp->x-=1;
            else
                temp->x+=1;
            break;
        }
        case 'R':
        {
            if(temp->x+1>temp2->x)
                temp->x+=1;
            else
                temp->x-=1;
            break;
        }
        }
        while(temp=temp->pNext)
        {
            tp2.x=temp->x;
            tp2.y=temp->y;
            temp->x=tp1.x;
            temp->y=tp1.y;
            tp1.x=tp2.x;
            tp1.y=tp2.y;
        }
        Sleep(1000);
        printmove();
    }
    return 0;
}
//初始化
void init()
{
    HideCursor();
    way='R';
    //游戏宽高
    b.h = 20, b.w = 60;
    h=(SNAKE *) malloc(sizeof(SNAKE));
    h->x=b.w/2;
    h->y=b.h/2;
    SNAKE *temp=(SNAKE *) malloc(sizeof(SNAKE));
    temp->x=h->x-1;
    temp->y=h->y;
    temp->pNext=NULL;
    h->pNext=temp;
    end=temp;
}
int main(int argc, char const *argv[])
{
    /* code */
    init();
    showborder();
    showpoint();

    HANDLE hThread;
    unsigned threadID;
    // Create the second thread.
    hThread = (HANDLE)_beginthreadex( NULL, 0, &SnakThreadFunc, NULL, 0, &threadID );
    // Wait until second thread terminates. If you comment out the line
    // below, Counter will not be correct because the thread has not
    // terminated, and Counter most likely has not been incremented to
    // 1000000 yet.
    WaitForSingleObject( hThread, 0 );
    // Destroy the thread object.
    CloseHandle( hThread );
    while(key=getch())
    {
        switch(key)
        {
        case (int)'w':
            way='U';
            break;
        case (int)'s':
            way='D';
            break;
        case (int)'a':
            way='L';
            break;
        case (int)'d':
            way='R';
            break;
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
贪吃蛇的代码可以相对简单,但需要一些基本的C语言知识和对数据结构的理解。下面是一个简单的贪吃蛇代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <windows.h> #include <time.h> // 定义蛇身节点的结构体 typedef struct Node { int x; int y; struct Node* next; } node; // 全局变量 int width = 20; // 地图宽度 int height = 20; // 地图高度 int score = 0; // 得分 int gameover = 0; // 游戏结束标志 int direction = 0; // 蛇的移动方向 node* head; // 蛇头节点 node* food; // 食物节点 // 初始化游戏 void init() { gameover = 0; direction = 0; score = 0; // 创建蛇头节点 head = (node*)malloc(sizeof(node)); head->x = width / 2; head->y = height / 2; head->next = NULL; // 创建食物节点 food = (node*)malloc(sizeof(node)); srand((unsigned)time(NULL)); food->x = rand() % (width - 2) + 1; food->y = rand() % (height - 2) + 1; food->next = NULL; } // 销毁游戏 void destroy() { node* p = head; while (p) { node* temp = p; p = p->next; free(temp); } head = NULL; free(food); food = NULL; } // 绘制地图 void drawMap() { system("cls"); // 清屏 // 绘制上边界 for (int i = 0; i < width + 2; i++) { printf("#"); } printf("\n"); // 绘制中间部分 for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (j == 0 || j == width - 1) { printf("#"); } else if (i == head->y && j == head->x) { printf("O"); // 蛇头 } else if (i == food->y && j == food->x) { printf("*"); // 食物 } else { int isBody = 0; node* p = head->next; while (p) { if (p->x == j && p->y == i) { printf("o"); // 蛇身 isBody = 1; break; } p = p->next; } if (!isBody) { printf(" "); } } } printf("\n"); } // 绘制下边界 for (int i = 0; i < width + 2; i++) { printf("#"); } printf("\n"); // 显示得分 printf("Score: %d\n", score); } // 处理键盘输入 void input() { if (_kbhit()) { switch (_getch()) { case 'w': direction = 1; // 上 break; case 's': direction = 2; // 下 break; case 'a': direction = 3; // 左 break; case 'd': direction = 4; // 右 break; case 'q': gameover = 1; // 退出游戏 break; } } } // 更新蛇的位置 void update() { // 创建新的蛇头节点 node* newHead = (node*)malloc(sizeof(node)); newHead->x = head->x; newHead->y = head->y; newHead->next = NULL; // 根据移动方向更新蛇头位置 switch (direction) { case 1: // 上 newHead->y--; break; case 2: // 下 newHead->y++; break; case 3: // 左 newHead->x--; break; case 4: // 右 newHead->x++; break; } // 判断是否吃到食物 if (newHead->x == food->x && newHead->y == food->y) { score++; // 得分加一 // 创建新的食物节点 node* newFood = (node*)malloc(sizeof(node)); newFood->x = rand() % (width - 2) + 1; newFood->y = rand() % (height - 2) + 1; newFood->next = NULL; // 将新的食物节点插入到蛇头之后 node* p = head; while (p->next) { p = p->next; } p->next = newFood; // 更新食物节点 food = newFood; } else { // 删除蛇尾节点 node* p = head; while (p->next->next) { p = p->next; } free(p->next); p->next = NULL; } // 将新的蛇头节点插入到头部 newHead->next = head; head = newHead; // 判断游戏是否结束 if (head->x <= 0 || head->x >= width - 1 || head->y <= 0 || head->y >= height || inSnake(head->x, head->y)) { gameover = 1; } } // 判断坐标(x, y)是否在蛇身上 int inSnake(int x, int y) { node* p = head->next; while (p) { if (x == p->x && y == p->y) { return 1; } p = p->next; } return 0; } // 主循环 void gameLoop() { while (!gameover) { drawMap(); input(); update(); Sleep(100); // 控制游戏速度 } } int main() { init(); gameLoop(); destroy(); return 0; } ``` 这是一个简单的贪吃蛇游戏的C语言代码示例。它使用了结构体、指针和链表等基本的C语言知识,并使用了一些Windows.h、stdlib.h和time.h库中的函数。代码实现了贪吃蛇的基本逻辑,包括初始化游戏、绘制地图、处理键盘输入、更新蛇的位置等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值