admin 管理员组

文章数量: 887021


2024年3月1日发(作者:web前端培训学校开发)

用vc6.0新建一个Win32 Application工程,把附件代码拷贝进去即可。

上下左右键控制蛇的方向,空格键用于启动或者停止游戏。

上下左右空格键

#include

#include

#include

#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__X = 0x01;//head x

snk__Y = 0x00;//head y

snk__X = 0x00;//tail x

snk__Y = 0x00;//tail y

snk_1.h_index=0;

snk_1.t_index=0;

snk__state=0;

snk_=0;

snk__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;

xtra=0;

xtra=0;

kground=(HBRUSH)GetStockObject(BLACK_BRUSH);

r=LoadCursor(NULL,IDC_CROSS);

=LoadIcon(NULL,IDI_APPLICATION);

nce=hInstance;

dProc=Win_tetris_Proc;

assName="Game_tetris";

nuName=NULL;

=CS_HREDRAW | CS_VREDRAW;

RegisterClass(&wndcls);

//Game_Tetris

HWND hwnd;

hwnd=CreateWindow("Game_tetris","Game_Snake(/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;

= 0;

= 0;

= 16;

= 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__state)

{

snk__state = 0;

}

else

{

snk__state = 1;

}

}

if(1 == snk__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__state)

{

//head

CHECK:

switch(s_count[snk_1.h_index].direction)

{

case GO_RIGHT:

{

if(snk__X < 63)snk__X++;

else snk__X = 0;

break;

}

case GO_LEFT:

{

if(snk__X >0 )snk__X--;

else snk__X = 63;

break;

}

case GO_DOWN :

{

if(snk__Y < 63)snk__Y++;

else snk__Y = 0;

break;

}

case GO_UP:

{

if(snk__Y > 0)snk__Y--;

else snk__Y = 63;

break;

}

default:

{

break;

}

}

s_count[snk_1.h_index].cnt++;

if(0 == game_map[snk__Y][snk__X])//no point

{

game_map[snk__Y][snk__X] = 1;

}

else if(1 == game_map[snk__Y][snk__X])//game over

{

KillTimer(hwnd,1);

sprintf(szChar,"score:%d",snk_);

MessageBox(hwnd,szChar,"GAME OVER!",0);

}

else if(2 == game_map[snk__Y][snk__X])//eat food

{

game_map[snk__Y][snk__X] = 1;

snk__state = 0;

snk_++;

goto CHECK;

}

= snk__X*8;

= snk__Y*8;

= + 8;

= + 8;

hbr= CreateSolidBrush(RGB(255,0,0));

FillRect(hdc,&rect,hbr);//show head point

//tail

game_map[snk__Y][snk__X] = 0;

= snk__X*8;

= snk__Y*8;

= + 8;

= + 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__X < 63)snk__X++;

else snk__X = 0;

break;

}

case GO_LEFT:

{

if(snk__X >0 )snk__X--;

else snk__X = 63;

break;

}

case GO_DOWN :

{

if(snk__Y < 63)snk__Y++;

else snk__Y = 0;

break;

}

case GO_UP:

{

if(snk__Y > 0)snk__Y--;

else snk__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__state)

{

snk__state = 1;

do{

xx = rand()%3970%63;

yy = rand()%3970/63;

}while(1 == game_map[yy][xx]);

game_map[yy][xx]=2;

= xx*8;

= yy*8;

= + 8;

= + 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;


本文标签: 培训 用于 启动 附件 代码