用vc6.0新建一个Win32 Application工程,把附件代码拷贝进去即可。
上下左右键控制蛇的方向,空格键用于启动或者停止游戏。
上下左右空格键
#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]t=2;
    s_count[snk_1.h_index].direction=GO_RIGHT;
    s_count[snk_1.t_index]t=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(blog.ednchina/zook0k/)",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;
        p    = 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]t = 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]t = 1;
c语言编程小游戏
                    }
                }
                else if(39 == wParam)
                {
                    if((s_count[snk_1.h_index].direction == GO_DOWN)||(s_count[snk_1.h_index].direction == GO_UP))
                    {

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。