贪吃蛇(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
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值