| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1201 人关注过本帖, 1 人收藏
标题:贪吃蛇游戏+源码
只看楼主 加入收藏
zook0k
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2006-5-2
收藏(1)
 问题点数:0 回复次数:3 
贪吃蛇游戏+源码
程序代码:
//2010.06.09
//zook0k
//<span style="color: #008000; text-decoration: underline;">http://blog.[/color]
//<span style="color: #008000; text-decoration: underline;">http://shop59223037.[/color]
//<span style="color: #008000; text-decoration: underline;">http://strongbaby1983.[/color]
//qq:83310093

#include <windows.h>
#include <stdio.h>
#include<time.h>

#define C_W   516
#define C_H   548
//#define C_W   1024
//#define C_H   1024

#define GO_RIGHT 0x01
#define GO_DOWN  0x02
#define GO_LEFT  0x03
#define GO_UP    0x04


#define SNAKE_NUMBER 30
typedef struct node_struct
{
unsigned char direction;
unsigned char cnt;
}s_node,*s_node_handle;
s_node s_count[SNAKE_NUMBER ];

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 food_state;
    unsigned char score;
    unsigned char snake_state;
} Snake_Data,*Snake_Data_handle;
Snake_Data snk_1;

#define MAP_X 64
#define MAP_Y 64
unsigned char game_map[MAP_Y][MAP_X];




LRESULT CALLBACK Win_tetris_Proc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
);

int WINAPI WinMain(
  HINSTANCE hInstance,      // handle to current instance
  HINSTANCE hPrevInstance,  // handle to previous instance
  LPSTR lpCmdLine,          // command line
  int nCmdShow              // show state
)
{
    snk_1.Head_X = 0x01;//head x
    snk_1.Head_Y = 0x00;//head y

    snk_1.Tail_X = 0x00;//tail x
    snk_1.Tail_Y = 0x00;//tail y
    snk_1.h_index=0;
    snk_1.t_index=0;
    snk_1.food_state=0;
    snk_1.score=0;
    snk_1.snake_state = 1;
    s_count[snk_1.h_index].cnt=2;
    s_count[snk_1.h_index].direction=GO_RIGHT;
    s_count[snk_1.t_index].cnt=2;
    s_count[snk_1.t_index].direction=GO_RIGHT;

    WNDCLASS wndcls;
    wndcls.cbClsExtra=0;
    wndcls.cbWndExtra=0;
    wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
    wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
    wndcls.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    wndcls.hInstance=hInstance;
    wndcls.lpfnWndProc=Win_tetris_Proc;
    wndcls.lpszClassName="Game_tetris";
    wndcls.lpszMenuName=NULL;
    wndcls.style=CS_HREDRAW | CS_VREDRAW;
    RegisterClass(&wndcls);

    //Game_Tetris
    HWND hwnd;
    hwnd=CreateWindow("Game_tetris","Game_Snake(http://blog.)",WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & ~WS_MINIMIZEBOX & ~WS_THICKFRAME,
        0,0,C_W,C_H,NULL,NULL,hInstance,NULL);


    ShowWindow(hwnd,SW_SHOWNORMAL);

//initial snake
        HDC hdc;
        hdc=GetDC(hwnd);
        HBRUSH hbr;
        RECT rect;  
       
        rect.left   = 0;
        rect.top    = 0;
        rect.right  = 16;
        rect.bottom = 8;
        hbr= CreateSolidBrush(RGB(255,0,0));
        FillRect(hdc,&rect,hbr);
        ReleaseDC(hwnd,hdc);
        game_map[0][0]=1;
        game_map[0][1]=1;
//initial rand
        SetTimer(hwnd,1,100,NULL) ;
        srand((int)time(0));


    UpdateWindow(hwnd);

    MSG msg;
    while(GetMessage(&msg,NULL,0,0))
    {
    //    TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}

LRESULT CALLBACK Win_tetris_Proc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
)
{

char szChar[20] = "score:";
unsigned char xx,yy;

    switch(uMsg)
    {
    case WM_KEYDOWN:
        {
            if(32 == wParam)
            {
                if(1 == snk_1.snake_state)
                {
                    snk_1.snake_state = 0;
                }
                else
                {
                    snk_1.snake_state = 1;
                }
            }

        if(1 == snk_1.snake_state)
        {
            if((wParam > 36)&&(wParam < 41))
            {                             
                if(38 == wParam)
                {
                    if((s_count[snk_1.h_index].direction == GO_RIGHT)||(s_count[snk_1.h_index].direction == GO_LEFT))
                    {
                        snk_1.h_index = (snk_1.h_index+1)%SNAKE_NUMBER ;
                        s_count[snk_1.h_index].direction = GO_UP;
                        s_count[snk_1.h_index].cnt = 1;
                    }
                }
                else if(40 == wParam)
                {
                     if((s_count[snk_1.h_index].direction == GO_RIGHT)||(s_count[snk_1.h_index].direction == GO_LEFT))
                    {
                        snk_1.h_index = (snk_1.h_index+1)%SNAKE_NUMBER ;
                        s_count[snk_1.h_index].direction = GO_DOWN;
                        s_count[snk_1.h_index].cnt = 1;
                    }
                }
                else if(39 == wParam)
                {
                     if((s_count[snk_1.h_index].direction == GO_DOWN)||(s_count[snk_1.h_index].direction == GO_UP))
                    {
                        snk_1.h_index = (snk_1.h_index+1)%SNAKE_NUMBER ;
                        s_count[snk_1.h_index].direction = GO_RIGHT;
                        s_count[snk_1.h_index].cnt = 1;
                    }
                }
                else if(37 == wParam)
                {
                     if((s_count[snk_1.h_index].direction == GO_DOWN)||(s_count[snk_1.h_index].direction == GO_UP))
                    {
                        snk_1.h_index = (snk_1.h_index+1)%SNAKE_NUMBER ;
                        s_count[snk_1.h_index].direction = GO_LEFT;
                        s_count[snk_1.h_index].cnt = 1;
                    }
                }                

            }
        }
        break;
    }


    case WM_TIMER:
    //case WM_PAINT:
        time_t t;
        HDC hdc;
        hdc=GetDC(hwnd);
        HBRUSH hbr;
           RECT rect;
       
        if(1 == snk_1.snake_state)
        {
//head
CHECK:
        switch(s_count[snk_1.h_index].direction)
        {
            case GO_RIGHT:
            {
                            if(snk_1.Head_X < 63)snk_1.Head_X++;
                            else                 snk_1.Head_X = 0;
            break;
            }
            case GO_LEFT:
            {
                            if(snk_1.Head_X >0 )snk_1.Head_X--;
                            else                snk_1.Head_X = 63;
            break;
            }
            case GO_DOWN :
            {
                            if(snk_1.Head_Y < 63)snk_1.Head_Y++;
                            else                 snk_1.Head_Y = 0;
                break;
            }
            case GO_UP:
            {
                            if(snk_1.Head_Y > 0)snk_1.Head_Y--;
                            else                snk_1.Head_Y = 63;
                break;
            }
            default:
                {
                break;
                }
        }

        s_count[snk_1.h_index].cnt++;
        if(0 == game_map[snk_1.Head_Y][snk_1.Head_X])//no point
        {
        game_map[snk_1.Head_Y][snk_1.Head_X] = 1;
        }
        else if(1 == game_map[snk_1.Head_Y][snk_1.Head_X])//game over
        {
            KillTimer(hwnd,1);
            sprintf(szChar,"score:%d",snk_1.score);
            MessageBox(hwnd,szChar,"GAME OVER!",0);               
        }
        else if(2 == game_map[snk_1.Head_Y][snk_1.Head_X])//eat food
        {
              game_map[snk_1.Head_Y][snk_1.Head_X] = 1;
              snk_1.food_state = 0;
              snk_1.score++;
              goto CHECK;
        }
        rect.left   = snk_1.Head_X*8;
        rect.top    = snk_1.Head_Y*8;
        rect.right  = rect.left + 8;
        rect.bottom = rect.top + 8;
        hbr= CreateSolidBrush(RGB(255,0,0));
        FillRect(hdc,&rect,hbr);//show head point

//tail
                game_map[snk_1.Tail_Y][snk_1.Tail_X] = 0;
                rect.left   = snk_1.Tail_X*8;
                rect.top    = snk_1.Tail_Y*8;
                rect.right  = rect.left + 8;
                rect.bottom = rect.top + 8;               
                //hbr= CreateSolidBrush(RGB(0,100,0));
                hbr= CreateSolidBrush(RGB(0,0,0));//clear tail point
                FillRect(hdc,&rect,hbr);

                 switch(s_count[snk_1.t_index].direction)
                {
                    case GO_RIGHT:
                    {
                        if(snk_1.Tail_X < 63)snk_1.Tail_X++;
                        else                 snk_1.Tail_X = 0;
                        break;
                    }
                    case GO_LEFT:
                    {
                        if(snk_1.Tail_X >0 )snk_1.Tail_X--;
                        else                snk_1.Tail_X = 63;
                        break;
                    }
                    case GO_DOWN :
                    {
                        if(snk_1.Tail_Y < 63)snk_1.Tail_Y++;
                        else                 snk_1.Tail_Y = 0;
                        break;
                    }
                    case GO_UP:
                    {
                        if(snk_1.Tail_Y > 0)snk_1.Tail_Y--;
                        else                snk_1.Tail_Y = 63;
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }

        if(s_count[snk_1.t_index].cnt == 2)
        {        
            snk_1.t_index = (snk_1.t_index + 1)%SNAKE_NUMBER ;
        }
        else
        {
            s_count[snk_1.t_index].cnt--;
        }

//output food
        if(0 == snk_1.food_state)
        {
            snk_1.food_state = 1;   
                do{
                    xx = rand()%3970%63;
                    yy = rand()%3970/63;
                }while(1 == game_map[yy][xx]);
                game_map[yy][xx]=2;
                rect.left   = xx*8;
                rect.top    = yy*8;
                rect.right  = rect.left + 8;
                rect.bottom = rect.top + 8;
                hbr= CreateSolidBrush(RGB(155,110,10));         
                FillRect(hdc,&rect,hbr);//show food
                srand((unsigned) time(&t));
        }
        ReleaseDC(hwnd,hdc);
        }
        break;

    case WM_CLOSE:
        DestroyWindow(hwnd);
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;


    default:
        return DefWindowProc(hwnd,uMsg,wParam,lParam);
    }
    return 0;
}
vc6.0新建Win32 Console Application,新建一个c++source file,将代码拷贝进去执行即可
上下左右键控制方向
空格键用于暂停继续游戏
搜索更多相关主题的帖子: 贪吃蛇 color 游戏 
2011-02-13 01:21
永恒之紫色
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-1-24
收藏
得分:0 
1>c:\users\acer\documents\visual studio 2008\projects\greedysnake\greedysnake\main.cpp(89) : error C2440: “=”: 无法从“const char [12]”转换为“LPCWSTR”
1>        与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换
1>c:\users\acer\documents\visual studio 2008\projects\greedysnake\greedysnake\main.cpp(97) : error C2664: “CreateWindowExW”: 不能将参数 2 从“const char [12]”转换为“LPCWSTR”
1>        与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换
1>c:\users\acer\documents\visual studio 2008\projects\greedysnake\greedysnake\main.cpp(260) : error C2664: “MessageBoxW”: 不能将参数 2 从“char [20]”转换为“LPCWSTR”
1>        与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换

出现了三个错误,不知道是怎么回事
2011-02-18 09:54
secondsen
Rank: 1
等 级:新手上路
帖 子:7
专家分:7
注 册:2010-9-12
收藏
得分:0 
设置不用UNICODE就行了,至少我是这么干的

Project->Properties->Configuration Properties->General->Character Set->改成Not Set

[ 本帖最后由 secondsen 于 2011-2-20 09:06 编辑 ]
2011-02-20 09:01
waterstar
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:5
帖 子:984
专家分:2810
注 册:2010-2-12
收藏
得分:0 
链接的时候把要设置project->settings->link->project opinion
找到/subsystem:把后面的console改成windows

冰冻三尺,非一日之寒;士别三日,不足刮目相看!
2011-02-20 13:45
快速回复:贪吃蛇游戏+源码
数据加载中...
 
   



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

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