C语言贪吃蛇两条蛇,贪吃蛇游戏--两条蛇改编

首先,创造一条蛇需要笔和刷子,因此要创造两条蛇所有的paint工具都需要两个。

然后再改变相应的颜色或边线对应的参数,让两条蛇的颜色不一样。

因为是双人游戏,所以还需要另外设置四个控制方向键,在keydown中添加。

所有的绘制函数都已经在代码中备注。代码改编成功!赞一个!

以下是game.cpp的代码:

#include

#include "snake.h"

// Global variable

HINSTANCE hinst;

extern position food;

// Function prototypes.

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);

//winmain是入口函数

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; //windows特有的数据

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, 200, 200, MAX_X+50, 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 = 200;

int right = 200;

int top = 200;

int buttom = 200;

int count = 0;

#define STEP_LEN 3

DWORD dwDirct = SNAKE_DOWN;

snake sn1;

snake sn2;

LONG APIENTRY MainWndProc(

HWND hwnd,

UINT msg,

WPARAM wParam,

LPARAM lParam)

{

switch (msg)

{

case WM_CREATE:

sn1 = snake();

sn2 = snake();

break;

case WM_PAINT:

//所有使用GDI在窗口上绘制图形的程序都写在这里。

{

PAINTSTRUCT

ps;

HPEN

hPen1,hPen2;

HBRUSH

hBrush1,hBrush2;

HDC

hdc;

HPEN

hOldPen1,hOldPen2 ;

HBRUSH

hOldBrush1,hOldBrush2,hOldBrush;

HBRUSH

hbrushFood;

HPEN

hPenWall;

hdc =

BeginPaint(hwnd, &ps);

hbrushFood

= CreateSolidBrush(RGB(100, 300,500));//食物颜色

hOldBrush =

(HBRUSH)SelectObject(hdc, hbrushFood);//创造食物,创造5个就调用timer函数改变速度

Ellipse(hdc,

food.x-SNAKE_NODE_SIZE, food.y-SNAKE_NODE_SIZE,

food.x+SNAKE_NODE_SIZE,

food.y+SNAKE_NODE_SIZE);//改变食物形状

hPen1 =

CreatePen(4, 4, RGB(255, 100,100));//蛇的边界线

hBrush1 =

CreateSolidBrush(RGB(500,5000, 1000));//蛇的颜色

hOldPen1 =

(HPEN)SelectObject(hdc, hPen1);

hOldBrush1 =

(HBRUSH)SelectObject(hdc, hBrush1);

//Rectangle(hdc,

10, 10, 50, 60);

list

::const_iterator sn1_pos,sn2_pos;

for(sn1_pos =

sn1.pos.begin(); sn1_pos != sn1.pos.end(); sn1_pos++)

{

position

p = *sn1_pos;

//position

q = *sn2_pos;

Ellipse(hdc,

p.x-SNAKE_NODE_SIZE, p.y-SNAKE_NODE_SIZE,

p.x+SNAKE_NODE_SIZE,

p.y+SNAKE_NODE_SIZE);

}

hPen2 =

CreatePen(1, 1, RGB(10, 10, 100));//蛇的边界线

hBrush2 =

CreateSolidBrush(RGB(500, 500, 100));//蛇的颜色

hOldPen2 =

(HPEN)SelectObject(hdc, hPen2);

hOldBrush2 =

(HBRUSH)SelectObject(hdc, hBrush2);

for (sn2_pos

= sn2.pos.begin(); sn2_pos != sn2.pos.end(); sn2_pos++)

{

position

p = *sn2_pos;

//position

q = *sn2_pos;

Ellipse(hdc,

p.x - SNAKE_NODE_SIZE, p.y - SNAKE_NODE_SIZE,

p.x

+ SNAKE_NODE_SIZE, p.y + SNAKE_NODE_SIZE);

}

hPenWall =

CreatePen(10, 3, RGB(10,10,255));

SelectObject(hdc,

hPenWall);

POINT

pt;

pt.x = 0;

pt.y =0 ;

//pt.a = 1;

pt.b = 1;

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

DeleteObject(hbrushFood);

DeleteObject(hPen1);

DeleteObject(hBrush1);

DeleteObject(hPen2);

DeleteObject(hBrush2);

EndPaint(hwnd,&ps);

}

break;

case WM_KEYDOWN:

{

switch(wParam)

{

case

VK_LEFT://改变控制方向的键

sn1.SetDirction(SNAKE_LEFT);

break;

case

VK_RIGHT:

sn1.SetDirction(SNAKE_RIGHT);

break;

case

VK_UP:

sn1.SetDirction(SNAKE_UP);

break;

case

VK_DOWN:

sn1.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

'Z':

sn2.SetDirction(SNAKE_DOWN);

break;

default:

break;

}

//

设置窗口重绘制,更新窗口

InvalidateRect

(hwnd, NULL, TRUE);

UpdateWindow

(hwnd);

break;

}

case WM_TIMER:

{

if(!sn1.isDead()||!sn2.isDead())

{

if(sn1.Move()

!= 0||sn2.Move()!=0)

{

MessageBox(0,0,"Dead!!!!",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);

}

break;

}

case WM_LBUTTONDOWN:

break;

case WM_SIZE:

break;

case WM_DESTROY:

ExitProcess(0);

break;

default:

return

DefWindowProc(hwnd,

msg,

wParam,

lParam);

}

return 0L;

}a4c26d1e5885305701be709a3d33442f.png

a4c26d1e5885305701be709a3d33442f.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值