• Windows程序设计 贪吃蛇c


    看Windows程序有段时间了,终于动手写东西。贪吃蛇算是一个开始吧,下面的贪吃蛇很简单,也有很多地方需要修改,还有情况没有考虑QAQ 但这不是我的目的了。。。

    思路很简单:建个链表储存蛇身节点即可。

    #include <windows.h>
    #include <time.h>
    
    #define ID_TIMER    1   
    #define TIMERSET    600 
    char    score,temp=0;  
    int     flag,tempx,tempy,
            foodx,foody,key;//运动方向,食物坐标,标记
    bool    havebody[50][50]; 
    bool    over = false;
    
    struct Snake
    {
        Snake *next;
        int x;
        int y;
    }; 
    struct Snake *head;
    struct Snake *creat()//链表初始化
    {
        struct Snake *p1,*p2,*p3;
        head=(struct Snake*)malloc(sizeof(Snake));
        p1=(struct Snake*)malloc(sizeof(Snake));
        p2=(struct Snake*)malloc(sizeof(Snake));
        p3=(struct Snake*)malloc(sizeof(Snake));
        
        head->x=23;head->y=23;
        head->next=p1;
        p1->x=24;p1->y=23;
        p1->next=p2; 
        p2->x=25;p2->y=23;
        p2->next=p3; 
        p3->x=26;p3->y=23;
        p3->next=NULL; 
        return head;
    };
            
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
        TCHAR        szAppName[]    = TEXT("dy");
        WNDCLASS    wndcls;
        wndcls.cbClsExtra        = 0;
        wndcls.cbWndExtra        = 0;
        wndcls.hbrBackground    = (HBRUSH) GetStockObject(WHITE_BRUSH);
        wndcls.hCursor            = LoadCursor(hInstance, IDC_ARROW);
        wndcls.hIcon            = LoadIcon(hInstance, IDI_APPLICATION);
        wndcls.hInstance        = hInstance;
        wndcls.lpfnWndProc        = WndProc;
        wndcls.lpszClassName    = szAppName;
        wndcls.lpszMenuName        = NULL;
        wndcls.style            = CS_HREDRAW | CS_VREDRAW;
        RegisterClass(&wndcls);
    
        HWND hwnd = CreateWindow (szAppName, TEXT ("dyww"),
                              WS_OVERLAPPEDWINDOW,
                               300, 100,
                              390, 400,
                              NULL, NULL, hInstance, NULL) ;
         
        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);
    
        MSG msg;
        while(GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        return msg.wParam;
    }
    
    void food(HWND hwnd)//食物
    {
        foodx = rand() % 37, foody = rand() %36;
        while(havebody[foodx][foody])
        {
            foodx = rand() % 37, foody = rand() % 36;
        }
        havebody[foodx][foody] = true;
        InvalidateRect(hwnd, NULL, TRUE);
    }
    
    void  checkdeath()//检查是否死亡
    {
        Snake *p= head,*end;
        do
        {    
            p=p->next;
        }while(p->next!=NULL);
        end=p;
        if(p->x <0||p->x >36 ||    p->y <0 ||    p->y >35)
        over=true;
        p=head->next;
        do
        {
            if(p->x==end->x&&p->y==end->y){over=true;}
            p=p->next;
        }while(p->next!=NULL);
    }        
    bool havefood(Snake *p)
    {
        if(foodx==p->x&&foody==p->y)
            return true;
        else
            return false;
    }
    
    struct Snake *insert(int x,int y)//插入节点
    {
        struct Snake *p1,*p;
        p=(struct Snake*)malloc(sizeof(Snake));
        p1=head;
        p->x=head->x;p->y=head->y;
        p->next=head->next;
        head->x=tempx;head->y=tempy;
        head->next=p;
        havebody[head->x][head->y]=true;
        return head;
    };
    
    Snake  *mainrun(HWND hwnd)
    {
        struct Snake *p;
        p=head;
        int temp=0,deadfood=0;
        tempx=p->x;tempy=p->y;//储存头节点的值
            do
            {        
                if(p->next!=NULL){
                    if(temp==0){havebody[p->x][p->y]=false; p->x=p->next->x;p->y=p->next->y;havebody[p->x][p->y]=true;temp++;}
                else 
                    {p->x=p->next->x;p->y=p->next->y;havebody[p->x][p->y]=true;}}        
                p=p->next;    
            }while(p->next!=NULL);
            //havebody[p->x][p->y]=false;
            if(flag==1) p->x--;
            if(flag==2) p->y--;
            if(flag==3) p->x++;
            if(flag==4) p->y++;
            if(havefood(p))  {insert(tempx,tempy);deadfood=1;score+=1;}
            checkdeath();    
            havebody[p->x][p->y]=true;
            InvalidateRect(hwnd, NULL, TRUE);
        if(deadfood)  food(hwnd);
        return head;    
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)//窗口过程
    {
        HDC                hdc;
        PAINTSTRUCT        ps;
        TCHAR            szScore[]=TEXT("GAME OVER 得分:"),
                        szBuffer[20];
                    
        int                x, y;
    
        switch(message)
        {
        case WM_CREATE://各种初始化
            flag=3;key=1;score=0;
            srand(time(NULL));
            creat();
            food(hwnd);
            havebody[foodx][foody]=true;
            SetTimer(hwnd, ID_TIMER, TIMERSET, NULL);
            return 0;
    
        case WM_TIMER:    
            mainrun(hwnd);
            if(over)
            {
                KillTimer(hwnd, ID_TIMER);
                InvalidateRect(hwnd, NULL, TRUE);
            }
            else InvalidateRect(hwnd, NULL, TRUE);
            return 0;
    
        case WM_KEYDOWN:
            if(over) return 0;
            switch(wParam)
            {
            case VK_UP:
                if(flag != 4)
                {
                    flag = 2;
                    mainrun(hwnd);
                }
                break;
            case VK_DOWN:
                if(flag != 2)
                {
                    flag = 4;
                    mainrun(hwnd);
                }
                break;
            case VK_LEFT:
                if(flag != 3)
                {
                flag = 1;
                    mainrun(hwnd);
                }
                break;
            case VK_RIGHT:
                if(flag != 1)
                {
                    flag = 3;
                    mainrun(hwnd);
                }
                break;
            default:
                break;
            }
            return 0;
    
        case WM_PAINT:
            hdc = BeginPaint(hwnd, &ps);
            Rectangle(hdc,0, 0,    370, 360);
            if(over)
                {SelectObject(hdc, GetStockObject(RGB(100,255,0)));Rectangle(hdc,0, 0,    370, 360);
                TextOut(hdc, 140, 130, szScore, lstrlen(szScore));
                TextOut(hdc, 178, 160,    szBuffer, wsprintf(szBuffer, TEXT("%4d"), score));
                //TextOut(hdc, 198, 190,    szBuffer, wsprintf(szBuffer, TEXT("%4d"), temp));
                }
             SelectObject(hdc, GetStockObject(BLACK_BRUSH));
            for(x = 0; x <37; x++)
                for(y = 0; y <36; y++)
                {    if(havebody[x][y])
                        Rectangle(hdc,x*10,y*10,(x+1)*10,(y+1)*10);
                }
            EndPaint (hwnd, &ps) ;
            return 0;
    
        case WM_DESTROY:
            KillTimer(hwnd, ID_TIMER);
            PostQuitMessage(0);
            return 0;
        }
        return DefWindowProc(hwnd, message, wParam, lParam);
    }
    View Code
  • 相关阅读:
    Django入门
    RCNN 研究相关
    [Android UI]View滑动方式总结
    [Android UI]View基础知识
    [Android]Android开发艺术探索第1章笔记
    [Leetcode]017. Letter Combinations of a Phone Number
    java之this关键字
    POJ 1000 A+B
    [Leetcode]016. 3Sum Closest
    [Leetcode]015. 3Sum
  • 原文地址:https://www.cnblogs.com/duanyuanzhi/p/3980043.html
Copyright © 2020-2023  润新知