C++贪吃蛇实例化

初学C++的我这是第一次的C++的作业,老师写好的一条贪吃蛇,要求再实例化另一条蛇,用WASD来控制上下左右。做出来效果如下:


这么多得代码做出来个看起来很low的东西让我再次觉得程序员这条道路道阻且跻。。。

控制方向的代码:

case WM_KEYDOWN:
{
switch(wParam)
{
case VK_LEFT:
sn.SetDirction(SNAKE_LEFT);
break;
case VK_RIGHT:
sn.SetDirction(SNAKE_RIGHT);
break;
case VK_UP:
sn.SetDirction(SNAKE_UP);
break;
case VK_DOWN:
sn.SetDirction(SNAKE_DOWN);
break;
   case 'A':
sn2.SetDirction(SNAKE_LEFT);
break;
case 'D':
sn2.SetDirction(SNAKE_RIGHT);
break;
case 'W':
sn2.SetDirction(SNAKE_UP);
break;
case 'S':
sn2.SetDirction(SNAKE_DOWN);
break;
default:
break;
}

绘制第二条蛇的代码:

list <position>::const_iterator sn2_pos;
hbrush2 = CreateSolidBrush(RGB(40, 255, 0));


SelectObject(hdc, hbrush2);


for (sn2_pos = sn2.pos.begin(); sn2_pos != sn2.pos.end(); sn2_pos++)
{
position p2 = *sn2_pos;
Ellipse(hdc, p2.x - SNAKE_NODE_SIZE, p2.y - SNAKE_NODE_SIZE,
p2.x + SNAKE_NODE_SIZE, p2.y + SNAKE_NODE_SIZE);
}

game.cpp全部代码如下:

#include <windows.h> 
#include "snake.h"


// Global variable 


HINSTANCE hinst; 
extern position food;


// Function prototypes. 


int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int); 
BOOL  InitApplication(HINSTANCE); 
BOOL  InitInstance(HINSTANCE, int); 
LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM); 




#define TIMER_ID 10240


// Application entry point. 


int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, 
  LPSTR lpCmdLine, int nCmdShow) 

MSG msg; 


if (!InitApplication(hinstance)) 
return FALSE; 


if (!InitInstance(hinstance, nCmdShow)) 
return FALSE; 


BOOL fGotMessage;
while ((fGotMessage = GetMessage(&msg, (HWND) NULL, 0, 0)) != 0 && fGotMessage != -1) 

TranslateMessage(&msg); 
DispatchMessage(&msg); 

return msg.wParam; 
UNREFERENCED_PARAMETER(lpCmdLine); 



BOOL InitApplication(HINSTANCE hinstance) 

WNDCLASSEX wcx; 


// Fill in the window class structure with parameters 
// that describe the main window. 


wcx.cbSize = sizeof(wcx);          // size of structure 
wcx.style = CS_HREDRAW | 
CS_VREDRAW;                    // redraw if size changes 
wcx.lpfnWndProc = MainWndProc;     // points to window procedure 
wcx.cbClsExtra = 0;                // no extra class memory 
wcx.cbWndExtra = 0;                // no extra window memory 
wcx.hInstance = hinstance;         // handle to instance 
wcx.hIcon = LoadIcon(NULL, 
IDI_APPLICATION);              // predefined app. icon 
wcx.hCursor = LoadCursor(NULL, 
IDC_CROSS);                    // predefined arrow 
wcx.hbrBackground = (HBRUSH)GetStockObject( 
WHITE_BRUSH);                  // white background brush 
wcx.lpszMenuName =  "MainMenu";    // name of menu resource 
wcx.lpszClassName = "MainWClass";  // name of window class 
wcx.hIconSm = (HICON)LoadImage(hinstance, // small class icon 
MAKEINTRESOURCE(5),
IMAGE_ICON, 
GetSystemMetrics(SM_CXSMICON), 
GetSystemMetrics(SM_CYSMICON), 
LR_DEFAULTCOLOR); 


// Register the window class. 


return RegisterClassEx(&wcx); 



BOOL InitInstance(HINSTANCE hinstance, int nCmdShow) 

HWND hwnd; 


// Save the application-instance handle. 


hinst = hinstance; 


// Create the main window. 


hwnd = CreateWindow( 
"MainWClass",        // name of window class 
"GDI-Snake-Demo",    // title-bar string 
WS_OVERLAPPEDWINDOW, // top-level window 
CW_USEDEFAULT,       // default horizontal position 
CW_USEDEFAULT,       // default vertical position 
CW_USEDEFAULT,       // default width 
CW_USEDEFAULT,       // default height 
(HWND) NULL,         // no owner window 
(HMENU) NULL,        // use class menu 
hinstance,           // handle to application instance 
(LPVOID) NULL);      // no window-creation data 


if (!hwnd) 
return FALSE; 




MoveWindow(hwnd, 100, 100, MAX_X+20, MAX_Y+50, TRUE);


SetTimer(hwnd, TIMER_ID, 1000, NULL);


// Show the window and send a WM_PAINT message to the window 
// procedure. 


ShowWindow(hwnd, nCmdShow); 
UpdateWindow(hwnd); 
return TRUE; 


}


int left = 100;
int right = 200;
int top = 100;
int buttom = 200;


#define STEP_LEN 10




DWORD dwDirct = SNAKE_DOWN;


snake sn;
snake sn2;


/************************************************
* this code deals with WM_MOUSEWHEEL
*************************************************/
LONG APIENTRY MainWndProc(
HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM lParam)
{
switch (msg)
{
case WM_CREATE:
sn = snake();
sn2=snake();
break;


case WM_PAINT: //所有使用GDI在窗口上绘制图形的程序都写在这里。
{
PAINTSTRUCT ps;
HPEN hpen;
HBRUSH hbrush, hbrush2;
HDC hdc;
HPEN hOldPen ;
HBRUSH hOldBrush;
HBRUSH hbrushFood;


HPEN hPenWall;


hdc = BeginPaint(hwnd, &ps);


hbrushFood = CreateSolidBrush(RGB(0, 0,30));


hOldBrush = (HBRUSH)SelectObject(hdc, hbrushFood);


Ellipse(hdc, food.x-SNAKE_NODE_SIZE, food.y-SNAKE_NODE_SIZE,
food.x+SNAKE_NODE_SIZE, food.y+SNAKE_NODE_SIZE);


hpen = CreatePen(0, 1, RGB(25, 25,0));
hbrush = CreateSolidBrush(RGB(0,255, 0));
hpen = CreatePen(0, 1, RGB(0, 255, 0));
hbrush = CreateSolidBrush(RGB(255,0, 0));
hOldPen = (HPEN)SelectObject(hdc, hpen);
hOldBrush = (HBRUSH)SelectObject(hdc, hbrush);
//Rectangle(hdc, 10, 10, 50, 60);
list <position>::const_iterator sn_pos;
for(sn_pos = sn.pos.begin(); sn_pos != sn.pos.end(); sn_pos++)
{
position p = *sn_pos;
Ellipse(hdc, p.x-SNAKE_NODE_SIZE, p.y-SNAKE_NODE_SIZE,
p.x+SNAKE_NODE_SIZE, p.y+SNAKE_NODE_SIZE);
}
list <position>::const_iterator sn2_pos;
hbrush2 = CreateSolidBrush(RGB(40, 255, 0));


SelectObject(hdc, hbrush2);


for (sn2_pos = sn2.pos.begin(); sn2_pos != sn2.pos.end(); sn2_pos++)
{
position p2 = *sn2_pos;
Ellipse(hdc, p2.x - SNAKE_NODE_SIZE, p2.y - SNAKE_NODE_SIZE,
p2.x + SNAKE_NODE_SIZE, p2.y + SNAKE_NODE_SIZE);
}


hPenWall = CreatePen(0, 3, RGB(0,0,255));
SelectObject(hdc, hPenWall);
POINT pt;
pt.x = 0; pt.y =0 ;
MoveToEx(hdc, 0, 0, &pt);
LineTo(hdc, 0, MAX_Y);
LineTo(hdc, MAX_X, MAX_Y);
LineTo(hdc, MAX_X, 0);
LineTo(hdc, 0, 0);
DeleteObject(hPenWall);


/*
HFONT hFont, hOldFont; 
RECT rectOfClient;




hFont = CreateFont(48,0,0,0,FW_DONTCARE,FALSE,TRUE,FALSE,DEFAULT_CHARSET,OUT_OUTLINE_PRECIS,
CLIP_DEFAULT_PRECIS,CLEARTYPE_QUALITY, VARIABLE_PITCH,TEXT("Impact"));


SelectObject(hdc, hFont);


GetClientRect(hwnd, &rectOfClient);


// Select the variable stock font into the specified device context. 
if (hOldFont = (HFONT)SelectObject(hdc, hFont)) 
{
// Display the text string.  
TextOut(hdc, rectOfClient.right/2, rectOfClient.bottom/2, "Sample ANSI_VAR_FONT text", 25); 


// Restore the original font.        
SelectObject(hdc, hOldFont); 
}*/


DeleteObject(hbrushFood);
DeleteObject(hpen);
DeleteObject(hbrush);
EndPaint(hwnd,&ps);
}
break;
case WM_KEYDOWN:
{
switch(wParam)
{
case VK_LEFT:
sn.SetDirction(SNAKE_LEFT);
break;
case VK_RIGHT:
sn.SetDirction(SNAKE_RIGHT);
break;
case VK_UP:
sn.SetDirction(SNAKE_UP);
break;
case VK_DOWN:
sn.SetDirction(SNAKE_DOWN);
break;
   case 'A':
sn2.SetDirction(SNAKE_LEFT);
break;
case 'D':
sn2.SetDirction(SNAKE_RIGHT);
break;
case 'W':
sn2.SetDirction(SNAKE_UP);
break;
case 'S':
sn2.SetDirction(SNAKE_DOWN);
break;
default:
break;
}
// 设置窗口重绘制,更新窗口
InvalidateRect (hwnd, NULL, TRUE);
UpdateWindow (hwnd);
break;
}
case WM_TIMER:
{
if(!sn.isDead())
{
if(sn.Move() != 0)
{
MessageBox(0,0,"snDead!!!!",0);
//KillTimer(hwnd, TIMER_ID);
ExitProcess(0);
}

// 设置窗口重绘制,更新窗口
// If this parameter is NULL, the entire client area is added to 
// the update region.
InvalidateRect (hwnd, NULL, TRUE);
UpdateWindow (hwnd);
}
if (!sn2.isDead())
{
if (sn2.Move() != 0)
{
MessageBox(0, 0, "sn2Dead!!!!", 0);
//KillTimer(hwnd, TIMER_ID);
ExitProcess(0);
}


InvalidateRect(hwnd, NULL, TRUE);
UpdateWindow(hwnd);
}


break;
}


case WM_LBUTTONDOWN:
break;
case WM_SIZE:
break;
case WM_DESTROY:
ExitProcess(0);
break;


default:
return DefWindowProc(hwnd,
msg,
wParam,
lParam);
}


return 0L;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值