vc6.0控制台贪吃蛇

#include<iostream>
#include<windows.h>
#include<algorithm>
#include<vector>
#include<list>
#include<set>
#include<time.h>
#include<cstdlib>
#include <stdio.h>
#define _WIN32_WINNT 0x0400
#define MAPSIZE 24

#define WH_KEYBOARD_LL 13

typedef struct tagKBDLLHOOKSTRUCT {

DWORD vkCode;

DWORD scanCode;

DWORD flags;

DWORD time;

DWORD dwExtraInfo;

} KBDLLHOOKSTRUCT, FAR *LPKBDLLHOOKSTRUCT, *PKBDLLHOOKSTRUCT;

#pragma data_seg("Shared")

HHOOK mHook=NULL;

HINSTANCE hInstance=NULL;

#pragma data_seg()

using namespace std;

typedef struct position
{
int px;
int py;
int operator ==(struct position a)
{

return (this->px==a.px&&this->py==a.py);
}
} POS;

enum DIR{UP,DOWN,LEFT,RIGHT};

int printMap(int snakeMap[MAPSIZE][MAPSIZE])
{
for(int i=0;i<MAPSIZE;i++)
{

for(int j=0;j<MAPSIZE;j++)
{
if(snakeMap[i][j]!=0)
cout<<snakeMap[i][j];
else
cout<<" ";
}
cout<<endl;
}
return 0;
}

int refreshMap(int snakeMap[MAPSIZE][MAPSIZE],list<POS> snake,POS food)
{
//sanke die
list<POS>::iterator it;
for(int i=1;i<MAPSIZE-1;i++)
for(int j=1;j<MAPSIZE-1;j++)
snakeMap[i][j]=0;
for( i=0;i<MAPSIZE;i++)
{
snakeMap[0][i]=8;
snakeMap[i][0]=8;
snakeMap[MAPSIZE-1][i]=8;
snakeMap[i][MAPSIZE-1]=8;
} 
snakeMap[food.px][food.py]=2;
for(it=snake.begin();it!=snake.end();it++)
{
snakeMap[it->px][it->py]=1;
}
return 0;
}

POS generFood(list<POS> snake)
{
srand(time(0));
POS food;
while(1)
{
food.px=rand()%(MAPSIZE-2)+1;
food.py=rand()%(MAPSIZE-2)+1;
list<POS>::iterator it;
it=find(snake.begin(),snake.end(),food);
if(it ==snake.end())
return food;
}
}

POS food;
int tag=0;
int step( list<POS> &snake,DIR dir,int snakeMap[MAPSIZE][MAPSIZE])
{
POS newHead={0,0};
list<POS>::iterator it;
it=snake.begin();
switch (dir)
{
//
case UP:
newHead.px=it->px-1;
newHead.py=it->py;
break;
//
case DOWN:
newHead.px=it->px+1;
newHead.py=it->py;
break;
//
case LEFT:
newHead.px=it->px;
newHead.py=it->py-1;
break;
//
case RIGHT:
newHead.px=it->px;
newHead.py=it->py+1;
break;
}

if(tag==0)
{
food=generFood(snake);
tag=1;
}
//snake die
if(snakeMap[newHead.px][newHead.py]==1||snakeMap[newHead.px][newHead.py]==8)
{
system("cls");
cout<<newHead.px<<" "<<newHead.py;

cout<<" failed _______"<<snakeMap[newHead.px][newHead.py]<<endl;
cout<< " it:"<<it->px<<" "<<it->py<<endl;
system("pause");
printMap( snakeMap);
}
//eat food
else if(snakeMap[newHead.px][newHead.py]==2)
{

snake.push_front(newHead);
snakeMap[newHead.px][newHead.py]=0;
food=generFood(snake);
refreshMap(snakeMap,snake,food);//refresh map
printMap( snakeMap);
}
//move next step
else 
{
snake.push_front(newHead);
snake.pop_back();

refreshMap(snakeMap,snake,food);//refresh map
printMap( snakeMap);
}

return 0;
}

int printSnake(list<POS> snake)
{
list<POS>::iterator it;
int i=0;
for(it=snake.begin();it!=snake.end();it++)
{
cout<<"no."<<i<<":"<<it->px<<" "<<it->py<<endl;
}
return 0;
}

 

DIR myDir=RIGHT;
HANDLE m_hMutex ;
int flag=0;

LRESULT CALLBACK snakeMoveProc(int nCode, WPARAM wParam, LPARAM lParam)
{

PKBDLLHOOKSTRUCT pkl = (PKBDLLHOOKSTRUCT)lParam;
if(nCode >= HC_ACTION
&& !(pkl->flags & 0x80)) {

WaitForSingleObject(m_hMutex,INFINITE);
if(flag==1)
{ char key=pkl->vkCode;
if(key==37)
{ 
if(myDir==RIGHT)
myDir=RIGHT;
else
myDir=LEFT;
}
if(key==39)
{ 
if(myDir==LEFT)
myDir=LEFT;
else
myDir=RIGHT;

}
if(key==38)
{
if(myDir==DOWN)
myDir=DOWN;
else
myDir=UP;
}
if(key==40)
{
if( myDir==UP)
myDir=UP;
else
myDir=DOWN;

}
flag=0;
}
ReleaseMutex(m_hMutex);
// Sleep(1);
return CallNextHookEx(hHook, nCode, wParam, lParam);

}

return CallNextHookEx(hHook, nCode, wParam, lParam);

}

DWORD WINAPI start(LPVOID lpParam )
{
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, snakeMoveProc, GetModuleHandle(NULL), 0);
MSG msg;


while(GetMessage(&msg, NULL, 0, 0))
{    

DispatchMessage(&msg);

}
UnhookWindowsHookEx(hHook);
return 0;
}

int main()
{
m_hMutex = CreateMutex(NULL, FALSE, "snake");
CreateThread( NULL, 0, 
start,NULL, 0, NULL); 
list<POS>snake;
int snakeMap[MAPSIZE][MAPSIZE]={0};
POS cur[3]={2,2,2,3,2,4};
snake.push_front(cur[0]);
snake.push_front(cur[1]);
snake.push_front(cur[2]);
while(1)
{ 
WaitForSingleObject(m_hMutex,INFINITE);
flag=1;
system("cls");
step(snake,myDir,snakeMap);
ReleaseMutex(m_hMutex);
Sleep(200);
}
system("pause");
return 0;
}

 

posted @ 2013-08-23 10:23  lxdonge  阅读(235)  评论(0)    收藏  举报