| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 356 人关注过本帖
标题:贪吃蛇问题,求大侠帮忙!(蛇不能定时走)
取消只看楼主 加入收藏
一个人的孤独
Rank: 2
等 级:论坛游民
帖 子:45
专家分:56
注 册:2012-10-15
结帖率:87.5%
收藏
已结贴  问题点数:40 回复次数:0 
贪吃蛇问题,求大侠帮忙!(蛇不能定时走)
#include<windows.h>
#include<time.h>
#include<stdio.h>

#define S_W        500   
#define S_H        500
#define LEFT    0x01
#define RIGHT    0x02
#define UP        0X03
#define DOWN    0x04

typedef struct SNAKE
{
    unsigned char Head_x;
    unsigned char Head_y;
    unsigned char Tail_x;
    unsigned char Tail_y;
    unsigned char h_index;
    unsigned char t_index;
    unsigned char state;
    unsigned char food_state;
    unsigned char score;
}Snake;
Snake snake;
unsigned char map[50][50]={0x00};
typedef struct CNT
{
    unsigned char direction;
    unsigned char count;
}CNT;
CNT s_count[50];

//窗口过程
LRESULT CALLBACK Tcs(HWND    hwnd,
                     UINT    message,
                     WPARAM wparam,
                     LPARAM lparam);
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpCmdLine,
                     int nCmdShow
                     )
{
//    map[0][1]=1;
//    map[0][0]=0;
    snake.Head_x    =0x01;
    snake.Head_y    =0x00;
    snake.Tail_x    =0x00;
    snake.Tail_y    =0x00;
    snake.h_index    =0x00;
    snake.t_index    =0x00;
    snake.state        =0x01;
    snake.food_state=0x00;
    snake.score        =0x00;
    s_count[snake.h_index].count=2;
    s_count[snake.h_index].direction=RIGHT;
    s_count[snake.t_index].count=2;
    s_count[snake.t_index].direction=RIGHT;
    //窗口
    HWND     hwnd;
    MSG         msg;
    WNDCLASS wndclass;
    wndclass.cbClsExtra=0;
    wndclass.cbWndExtra=0;
    wndclass.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
    wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
    wndclass.hIcon=LoadIcon(NULL,IDC_ARROW);
    wndclass.hInstance=hInstance;
    wndclass.lpfnWndProc=Tcs;
    wndclass.lpszClassName="tcs";
    wndclass.lpszMenuName="贪吃蛇";
    wndclass.style=CS_HREDRAW|CS_VREDRAW;
    RegisterClass(&wndclass);
    hwnd=CreateWindow(    "tcs",
                        TEXT("贪吃蛇"),
                        WS_OVERLAPPEDWINDOW,
                        0X00,
                        0X00,
                        S_W,
                        S_H,
                        NULL,
                        NULL,
                        hInstance,
                        NULL);
    ShowWindow(hwnd,nCmdShow);
    RECT rect;
    HDC     hdc;
    hdc=GetDC(hwnd);
    HBRUSH hbrush;
    hbrush=CreateSolidBrush(RGB(255,0,0));
    rect.left    =0x00;
    rect.top    =0x00;
    rect.right    =0x20;
    rect.bottom    =0x10;
    FillRect(hdc,&rect,hbrush);
    ReleaseDC(hwnd,hdc);
    SetTimer(hwnd,WM_TIMER,100,NULL);
    srand((int)time(0));
    UpdateWindow(hwnd);
    while(GetMessage(&msg,NULL,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}
LRESULT CALLBACK Tcs(HWND hwnd,
                     UINT message,
                     WPARAM wParam,
                     LPARAM lParam)
{   
    unsigned char swX=0,swY=0;
    char szScore[10]={0};
    HDC hdc;
    RECT rect;
    HBRUSH hbrush;   
    switch(message)   
    {
    case WM_KEYDOWN:
        {   
            if(wParam!=VK_LEFT&&wParam!=VK_RIGHT&&wParam!=VK_UP&&wParam!=VK_DOWN)   
            {   
                if(1==snake.state)
            {
                snake.state=0;
            }   
                else snake.state=1;                           
            }
            if(wParam==VK_LEFT)
            {
                if(wParam==VK_UP||wParam==VK_DOWN)
                {
                    snake.h_index=(snake.h_index+1)%25;
                    s_count[snake.h_index].direction=VK_LEFT;
                    s_count[snake.h_index].count=1;
                }                    
            }
            if(wParam==VK_RIGHT)
            {
                if(wParam==VK_UP||wParam==VK_DOWN)
                {
                    snake.h_index=(snake.h_index+1)%25;
                    s_count[snake.h_index].direction=VK_RIGHT;
                    s_count[snake.h_index].count=1;
                }                    
            }
            if(wParam==VK_UP)
            {
                if(wParam==VK_LEFT||wParam==VK_RIGHT)
                {
                    snake.h_index=(snake.h_index+1)%25;
                    s_count[snake.h_index].direction=VK_UP;
                    s_count[snake.h_index].count=1;
                }                    
            }
            if(wParam==VK_DOWN)
            {
                if(wParam==VK_LEFT||wParam==VK_RIGHT)
                {
                    snake.h_index=(snake.h_index+1)%25;
                    s_count[snake.h_index].direction=VK_DOWN;
                    s_count[snake.h_index].count=1;
                }                    
            }
    case WM_TIMER:
        time_t t;
        HDC hdc;
        hdc=GetDC(hwnd);
        HBRUSH hbrush;
        RECT rect;
        
        if(snake.state==1)
        {
GO:                switch(s_count[snake.h_index].direction)   
                {               
                    case VK_LEFT:               
                        {               
                            if(snake.Head_x>0)snake.Head_x--;        
                            else        
                            {        
                                    sprintf(szScore,TEXT("%d"),snake.score);        
                                    MessageBox(hwnd,szScore,"游戏失败",MB_OK);        
                            }        
                            break;        
                        }   
                    case VK_RIGHT:        
                        {   
                            if(snake.Head_x<50)snake.Head_x++;        
                            else        
                            {   
                                    sprintf(szScore,TEXT("%d"),snake.score);
                                                MessageBox(NULL,szScore,"游戏失败",MB_OK);        
                            }        
                            break;        
                        }   
                    case VK_UP:   
                        {        
                            if(snake.Head_y>0)snake.Head_y--;        
                            else        
                            {        
                                    sprintf(szScore,TEXT("%d"),snake.score);
                                    MessageBox(NULL,szScore,"游戏失败",MB_OK);        
                            }        
                            break;        
                        }        
                    case VK_DOWN:        
                        {   
                            if(snake.Head_y<50)snake.Head_y++;        
                            else        
                            {        
                                    sprintf(szScore,TEXT("%d"),snake.score);            
                                    MessageBox(NULL,szScore,"游戏失败",MB_OK);            
                            }            
                            break;            
                        }        
                }
        }

//判断是否吃的食物   
        s_count[snake.h_index].count++;
        if(snake.state==1)
        {
            if(map[snake.Head_x][snake.Head_y]==0)
            {
                map[snake.Head_x][snake.Head_y]=1;
            }
            else if(map[snake.Head_x][snake.Head_y]==1)
            {
                sprintf(szScore,TEXT("%d"),snake.score);
                MessageBox(NULL,szScore,"游戏失败",MB_OK);
            }
            else if(map[snake.Head_x][snake.Head_y]==2)//吃到食物
            {
                snake.score++;
                snake.food_state=0;
                map[snake.Head_x][snake.Head_y]=1;
                goto CHECK;
            }
        break;
        }
//蛇头   
        rect.left    =snake.h_index*10;
        rect.top    =snake.Head_y *10;
        rect.right    =rect.left      +10;
        rect.bottom    =rect.bottom  +10;
        hbrush=CreateSolidBrush(RGB(255,0,0));
        FillRect(hdc,&rect,hbrush);

//蛇尾
        s_count[snake.t_index].count--;
        map[snake.Tail_y][snake.Tail_x]=0;
        rect.left    =snake.Tail_x *10;
        rect.top    =snake.Tail_y *10;
        rect.right    =rect.left      +10;
        rect.bottom    =rect.top      +10;
        hbrush=CreateSolidBrush(RGB(0,0,0));
        FillRect(hdc,&rect,hbrush);
//蛇尾坐标
        switch(s_count[snake.t_index].direction)
        {
        case VK_LEFT:
            {
            if(snake.Tail_x>0)snake.Tail_x--;
            else
            {
                sprintf(szScore,TEXT("%d"),snake.score);
                MessageBox(hwnd,szScore,"游戏失败",MB_OK);
            }
            break;
            }
        case VK_RIGHT:
            {
            if(snake.Tail_x<50)snake.Tail_x++;
            else
            {
                sprintf(szScore,TEXT("%d"),snake.score);
                MessageBox(NULL,szScore,"游戏失败",MB_OK);
            }
            break;
            }
        case VK_UP:
            {
            if(snake.Tail_y>0)snake.Tail_y--;
            else
            {
                sprintf(szScore,TEXT("%d"),snake.score);
                MessageBox(NULL,szScore,"游戏失败",MB_OK);
            }
            break;
            }   
        case VK_DOWN:
            {
            if(snake.Tail_y<50)snake.Tail_y++;
            else
            {
                sprintf(szScore,TEXT("%d"),snake.score);
                MessageBox(NULL,szScore,"游戏失败",MB_OK);
            }
            break;
            }
        }
        if(s_count[snake.t_index].count=2)
        {
            snake.t_index=(snake.t_index+1)%25;
        }
        else
        {
            s_count[snake.t_index].count--;
        }

//食物显示
        if(snake.food_state==0)
        {
            snake.food_state=1;
            while(map[swX][swY]!=1)
            {
                swX=rand()%2500/50;
                swY=rand()%2500/50;
            }
            map[swX][swY]=2;
            rect.left    =swX*10;
            rect.top    =swY*10;
            rect.right    =rect.left+10;
            rect.bottom    =rect.top +10;
            hbrush=CreateSolidBrush(RGB(155,155,0));
            FillRect(hdc,&rect,hbrush);
            srand((unsigned)time(&t));
        }
        ReleaseDC(hwnd,hdc);
    }break;
    case WM_CLOSE:
        DestroyWindow(hwnd);
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    default:
        return DefWindowProc(hwnd,message,wParam,lParam);

    }
    return 0;
}


搜索更多相关主题的帖子: include 贪吃蛇 
2012-12-23 11:53
快速回复:贪吃蛇问题,求大侠帮忙!(蛇不能定时走)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018217 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved