贪吃蛇程序,开始可正常操作,按下控制按钮后按键操作不能用

我写了个贪吃蛇程序,用c++ + windows编程写的,左边是游戏区,右边有暂停,加减速等按钮,刚开始可正常操作贪吃蛇,但是在按下按钮后再用上下左右操作就没反应了,不知道为什么?求大神解答

#include
#include
#include
#include

using namespace std;

//定义 游戏区 和 控制区 大小
#define BOUND_SIZE 10 //边框大小
#define SNAKE_SIZE 10 //贪吃蛇方块大小
#define GAME_WIDTH 80 //游戏区宽
#define GAME_HEIGHT 60 //长
#define INFO_WIDTH 30 //游戏信息区宽
#define INFO_HEIGHT GAME_HEIGHT //长

#define MAX_NODE 20 //蛇的最大长度
#define MY_TIMER 1 //定时器ID
#define DE_INTERVAL 100 //定义贪吃蛇的默认移动速度--500毫秒/节 // 间隔

#define PAUSE_ID 1//暂停按钮
#define SPDUP_ID 2//加减速
#define SPDDOWN_ID 3
#define GAME_AGAIN_ID 4//游戏重新开始

#define BT_SIZE_WIDTH 10//按钮大小
#define BT_SIZE_HEIGHT 5

// 蛇
vector vSnake;
int headPos, tailPos; // 头尾的位置
int len; // 长度
int interval; // 速度
POINT direct; // 方向 - 横移
bool needsFood = true; // 是否需要食物
POINT ptFood; // 食物坐标
bool bPause = false; // 暂停状态

enum Status {CONTINUE, WIN, LOST};
Status g_status; //游戏状态

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPreInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("HelloWin");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;

wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;

if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"),
szAppName, MB_ICONERROR);
return 0;
}

hwnd = CreateWindow(szAppName,
TEXT("The Hello Program"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);

ShowWindow (hwnd, iCmdShow);
UpdateWindow(hwnd);

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

return msg.wParam;
}

// 初始化蛇
void initSnake() {
// 初始两个结点位置
vSnake.clear();
vSnake.resize(MAX_NODE);
// 初始长度
len = 2;
for (int i = 0; i < len; i++)
{
vSnake[i].x=i;
vSnake[i].y=1;
}

// 初始尾巴在蛇身的位置
tailPos = 0;
headPos = 1;
interval = DE_INTERVAL; // 初始速度
direct = {1, 0}; // 初始方向

g_status = CONTINUE; // 初始化游戏状态

needsFood = true; // 需要食物
bPause = false; // 不暂停
}

// 获取蛇的结点位置
POINT &getSnakeNode(int index)
{
int i = tailPos + index; // 尽量在更局部的地方修改变量以增加可维护性-tailPos

if(i >= MAX_NODE)
{
i -= MAX_NODE;
}

return vSnake[i];
}

// 画蛇
void drawSnake(HDC hdc)
{
int i;
POINT ptNode;
HBRUSH hBrush = (HBRUSH) GetStockObject (WHITE_BRUSH); //贪吃蛇块上色
SelectObject(hdc, hBrush);
/*
也可以for(i = tail; i < head; i++)
但是需要判断head大于界值后从0续起的情况
*/
for(i = 0; i < len; i++)
{
//从蛇尾开始画
ptNode = getSnakeNode(i);

Rectangle(hdc, ptNode.x * SNAKE_SIZE + BOUND_SIZE,
ptNode.y * SNAKE_SIZE + BOUND_SIZE,
(ptNode.x + 1) * SNAKE_SIZE + BOUND_SIZE,
(ptNode.y + 1) * SNAKE_SIZE + BOUND_SIZE);
}
}

// 画食物
void drawFood(HDC hdc) {
POINT ptNode;
int x, y;
int i = 0;
HBRUSH hBrush=(HBRUSH)GetStockObject(BLACK_BRUSH);

if (needsFood)
{
srand(time(0)); //随机数种子
//获取随机坐标,不为蛇身
while(i != len)
{
x=rand() % (GAME_WIDTH - 1);
y=rand() % (GAME_HEIGHT - 1);

for(i = 0; i < len; i++) // 食物是否在蛇身,若在则直接退出
{
ptNode = getSnakeNode(i);

if(ptNode.x == x && ptNode.y == y)
break;
}
}

ptFood.x = x;
ptFood.y = y;
needsFood = false;
}

SelectObject(hdc,hBrush); // 选取画刷
Ellipse(hdc, BOUND_SIZE + ptFood.x * SNAKE_SIZE, // 画食物
BOUND_SIZE + ptFood.y * SNAKE_SIZE,
BOUND_SIZE + (ptFood.x + 1) * SNAKE_SIZE,
BOUND_SIZE + (ptFood.y + 1) * SNAKE_SIZE);
}

// 刷新蛇的状态(移动)
void refreshSnake()
{
POINT newNode;
newNode.x = vSnake[headPos].x + direct.x;
newNode.y = vSnake[headPos].y + direct.y;
// 吃到食物 - 尾不变,头 + 1
if (!needsFood && newNode.x == ptFood.x && newNode.y == ptFood.y)
{
len++;
needsFood = true;
if (len == MAX_NODE)
{
g_status = WIN;
}
headPos = headPos + 1 >= MAX_NODE ? 0 : headPos + 1;
vSnake[headPos] = newNode;
return;
}

// 蛇撞墙
if (newNode.x < 0 || newNode.x >= GAME_WIDTH
|| newNode.y < 0 || newNode.y >= GAME_HEIGHT)
{
g_status = LOST;
return;
}
POINT ptNode;
// 蛇碰到自己的身体
for (int i = 0; i < len; i++)
{
ptNode = getSnakeNode(i);
if (ptNode.x == newNode.x && ptNode.y == newNode.y)
{
g_status = LOST;
return;
}
}

headPos = headPos + 1 >= MAX_NODE ? 0 : headPos + 1;
tailPos = tailPos + 1 >= MAX_NODE ? 0 : tailPos + 1;

vSnake[headPos] = newNode;
}

LRESULT CALLBACK WndProc (HWND hwnd,
UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
HBRUSH hBrush;

int nWinW, nWinH, nClientW, nClientH; // 窗口和客户区的长宽
static HWND hPause; //暂停
static HWND hSpdUp; //加速
static HWND hSpdDown; //减速
static HWND hStartAgain; //重新开始

switch (message)
{
case WM_CREATE:
GetWindowRect(hwnd,&rect); // 绘制窗口大小,获取外窗口大小
nWinW = rect.right-rect.left;
nWinH = rect.bottom-rect.top;

GetClientRect(hwnd,&rect); // 客户区大小,内窗口大小
nClientW = rect.right-rect.left;
nClientH = rect.bottom-rect.top;

//修改窗口大小,客户区大小+边框大小(nWinX-nClientX外边框)
MoveWindow(hwnd, 0, 0,
(GAME_WIDTH + INFO_WIDTH) * SNAKE_SIZE

  • BOUND_SIZE * 3 + (nWinW - nClientW), (GAME_HEIGHT * SNAKE_SIZE + BOUND_SIZE * 2
  • (nWinH-nClientH)), TRUE); hStartAgain = CreateWindow(TEXT("BUTTON"), TEXT("重新开始"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 2 * BOUND_SIZE + GAME_WIDTH * SNAKE_SIZE + (INFO_WIDTH - BT_SIZE_WIDTH) * SNAKE_SIZE / 2, (INFO_HEIGHT - BT_SIZE_HEIGHT * 5) * SNAKE_SIZE / 2 - BOUND_SIZE, BT_SIZE_WIDTH * SNAKE_SIZE, BT_SIZE_HEIGHT * SNAKE_SIZE, hwnd, (HMENU) GAME_AGAIN_ID, ((LPCREATESTRUCT) lParam) -> hInstance, NULL); // 重新开始按钮,位置(横,纵,长宽)

hPause = CreateWindow(TEXT("BUTTON"), TEXT("暂停"),
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
2 * BOUND_SIZE + GAME_WIDTH * SNAKE_SIZE +
(INFO_WIDTH - BT_SIZE_WIDTH) * SNAKE_SIZE / 2,
(INFO_HEIGHT - BT_SIZE_HEIGHT * 3) * SNAKE_SIZE / 2,
BT_SIZE_WIDTH * SNAKE_SIZE,
BT_SIZE_HEIGHT * SNAKE_SIZE,
hwnd, (HMENU) PAUSE_ID,
((LPCREATESTRUCT) lParam) -> hInstance, NULL); // 暂停按钮,位置(横,纵,长宽)

hSpdUp = CreateWindow(TEXT("BUTTON"), TEXT("速度加快"),
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
2 * BOUND_SIZE + GAME_WIDTH * SNAKE_SIZE +
(INFO_WIDTH - BT_SIZE_WIDTH) * SNAKE_SIZE / 2,
BOUND_SIZE + (INFO_HEIGHT - BT_SIZE_HEIGHT * 3)

  • SNAKE_SIZE / 2 + BT_SIZE_HEIGHT * SNAKE_SIZE, BT_SIZE_WIDTH * SNAKE_SIZE, BT_SIZE_HEIGHT * SNAKE_SIZE, hwnd, (HMENU) SPDUP_ID, ((LPCREATESTRUCT) lParam) -> hInstance, NULL); //加速按钮,位置(横,纵,长宽)

hSpdDown = CreateWindow(TEXT("BUTTON"), TEXT("速度减慢"),
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
2 * BOUND_SIZE + GAME_WIDTH * SNAKE_SIZE

  • (INFO_WIDTH - BT_SIZE_WIDTH) * SNAKE_SIZE / 2, BOUND_SIZE + (INFO_HEIGHT - BT_SIZE_HEIGHT * 3)
  • SNAKE_SIZE / 2 + BOUND_SIZE + BT_SIZE_HEIGHT * SNAKE_SIZE * 2, BT_SIZE_WIDTH * SNAKE_SIZE, BT_SIZE_HEIGHT * SNAKE_SIZE, hwnd, (HMENU) SPDDOWN_ID, ((LPCREATESTRUCT) lParam) -> hInstance, NULL); //减速按钮,位置(横,纵,长宽)

initSnake();
SetTimer(hwnd, MY_TIMER, interval, NULL); //设置定时器

return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
hBrush = (HBRUSH) GetStockObject(GRAY_BRUSH); // 给游戏区和控制区上色
SelectObject(hdc, hBrush);

Rectangle(hdc, BOUND_SIZE, BOUND_SIZE, // 游戏区
BOUND_SIZE + GAME_WIDTH * SNAKE_SIZE,
BOUND_SIZE + GAME_HEIGHT * SNAKE_SIZE);

Rectangle(hdc, // 控制区
BOUND_SIZE * 2 + GAME_WIDTH * SNAKE_SIZE, BOUND_SIZE,
BOUND_SIZE + (GAME_WIDTH + INFO_WIDTH) * SNAKE_SIZE,
BOUND_SIZE + INFO_HEIGHT * SNAKE_SIZE);

drawSnake(hdc);
drawFood(hdc);

EndPaint (hwnd, &ps);
return 0;
case WM_TIMER:
//移动蛇
refreshSnake();
if (g_status != CONTINUE)
{
KillTimer(hwnd, MY_TIMER);

(g_status == WIN) ?
MessageBox(NULL, TEXT("你赢了"), TEXT("Win"), MB_OK)
: MessageBox(NULL,TEXT("你输了"), TEXT("Fail"), MB_OK);

initSnake();
}

InvalidateRect(hwnd,NULL,TRUE);

return 0;
case WM_KEYDOWN:

switch(wParam)
{
case VK_UP: //调节方向(向原来方向或相反方向时不反应)
if(direct.x!=0)
{
direct.x=0;
direct.y=-1;
}
break;
case VK_DOWN:
if(direct.x!=0)
{
direct.x=0;
direct.y=1;
}
break;
case VK_LEFT:
if(direct.y!=0)
{
direct.x=-1;
direct.y=0;
}
break;
case VK_RIGHT:
if(direct.y!=0)
{
direct.x=1;
direct.y=0;
}
break;
}
return 0;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case GAME_AGAIN_ID:
KillTimer(hwnd, MY_TIMER);
initSnake();
SetTimer(hwnd, MY_TIMER, interval, NULL);
break;

case PAUSE_ID:
if(bPause)
{
bPause = false;
SetWindowText(hPause, TEXT("暂停"));
SetTimer(hwnd, MY_TIMER, interval, NULL);
} else {
bPause = true;
SetWindowText(hPause, TEXT("继续"));
KillTimer(hwnd, MY_TIMER);
}
break;

case SPDUP_ID:
if(interval >= 100)
{
interval -= 50;
KillTimer(hwnd, MY_TIMER);
SetTimer(hwnd, MY_TIMER, interval, NULL);
}
break;

case SPDDOWN_ID:
if(interval <= 500)
{
interval += 50;
KillTimer(hwnd, MY_TIMER);
SetTimer(hwnd, MY_TIMER, interval, NULL);
}
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
case WM_DESTROY:
KillTimer(hwnd,MY_TIMER);
PostQuitMessage(0);
return 0;
}

return DefWindowProc (hwnd, message, wParam, lParam);
}

查看全部
tom9238
Routz
2016/07/17 09:14
  • 点赞
  • 收藏
  • 回答
    私信

1个回复