这是snake.h,具体是设定另一组a,b.控制另一条蛇,定义一个position1类 ,仿照所有和x,y有关的雷雨函数定义a,b。具体在代码中体现。还有snake.cpp函数,修改方法和snake.h类似。这是关于长短。
还有方向就是就是修改bool group就好,具体方法在代码中
速度是同学帮我的 修改game.cpp
#include
#include
using std::list;
#define SNAKE_NODE_SIZE 10
#define MAX_X 300
#define MAX_Y 300
#define MAX_A 300//为另一条蛇设定参数
#define MAX_B 300
class position
{
public:
unsigned short x;
unsigned short y;
bool operator== (position rhs) const {
if (x == rhs.x && y ==
rhs.y)
return
true;
return false;
}
};//position, * lp_position;
class position1//设定类position1
{
public:
unsigned short a;
unsigned short b;
bool operator== (position1 rhs) const {
if (a == rhs.a && b ==
rhs.b)
return
true;
return false;
}
};//position, * lp_position;
enum dirction
{
SNAKE_UP,
SNAKE_DOWN,
SNAKE_LEFT,
SNAKE_RIGHT,
};
//position food;
class snake
{
private:
bool bDead;
dirction snake_dir;
int len,len1;
int speed;
public:
list pos;
listpos1;
public:
snake();
~snake()
{
pos.clear();
pos1.clear();
}
//改变方向、向前移动、变长、改变速度、吃到食物之后的动作。
void SetDirction(dirction dir)
{
switch (snake_dir)
{
case SNAKE_UP:
if (dir ==
SNAKE_DOWN)
return;
break;
case SNAKE_DOWN:
if (dir ==
SNAKE_UP)
return;
break;
case SNAKE_LEFT:
if (dir ==
SNAKE_RIGHT)
return;
break;
case SNAKE_RIGHT:
if (dir ==
SNAKE_LEFT)
return;
break;
}
snake_dir = dir;
}
bool Gorwup()
{
//position* posNewHead =
new(position);
position posHead =
pos.front();
position1
posHead1=pos1.front();
//pos.push_front(*posNewHead);
switch (snake_dir)
{
case SNAKE_UP:
posHead.y -=
SNAKE_NODE_SIZE*2;
posHead1.b -=
SNAKE_NODE_SIZE*2;//设定蛇出来后走向不同的方向
break;
case SNAKE_DOWN:
posHead.y +=
SNAKE_NODE_SIZE*2;
posHead1.b +=
SNAKE_NODE_SIZE*2;
break;
case SNAKE_LEFT:
posHead.x -=
SNAKE_NODE_SIZE*2;
posHead1.a -=
SNAKE_NODE_SIZE*2;
break;
case SNAKE_RIGHT:
posHead.x +=
SNAKE_NODE_SIZE*2;
posHead1.a
+=SNAKE_NODE_SIZE*2;
break;
}
pos.push_front(posHead);
len++;
pos1.push_front(posHead1);
len1++;
return true;
}
int
Move();
void Dead()
{
bDead = true;
}
bool isDead()
{
return bDead;
}
void SetSpeed(int newSpeed)
{
speed = newSpeed+5;
}
};
#include "snake.h"
position food;
position1 food1;//设定position1 food1;
bool create_food(snake sn)
{
time_t now;
time(&now);
srand(now);
bool on_snake = false;
while(true)
{
food.x = (rand() %
(MAX_X/(SNAKE_NODE_SIZE*2)) * (SNAKE_NODE_SIZE*2) ) +
SNAKE_NODE_SIZE;
food.y = (rand() %
(MAX_Y/(SNAKE_NODE_SIZE*2)) * (SNAKE_NODE_SIZE*2) ) +
SNAKE_NODE_SIZE;
// 判断是否和蛇重叠了。
list ::const_iterator
posbody;
for(posbody = sn.pos.begin ();
posbody != sn.pos.end (); posbody++)
{
if(*posbody
== food)
{
on_snake
= true;
break;
}
}
if(!on_snake)
return
true;
}
}
bool create_food1(snake sn)//复制上一段写create_food1函数
{
time_t now;
time(&now);
srand(now);
bool on_snake = false;
while(true)
{
food1.a = (rand() %
(MAX_A/(SNAKE_NODE_SIZE*2)) * (SNAKE_NODE_SIZE*2) ) +
SNAKE_NODE_SIZE;
food1.b = (rand() %
(MAX_B/(SNAKE_NODE_SIZE*2)) * (SNAKE_NODE_SIZE*2) ) +
SNAKE_NODE_SIZE;
// 判断是否和蛇重叠了。
list ::const_iterator
posbody1;
for(posbody1 = sn.pos1.begin
(); posbody1 != sn.pos1.end (); posbody1++)
{
if(*posbody1
== food1)
{
on_snake
= true;
break;
}
}
if(!on_snake)
return
true;
}
}
snake::snake()
{
SetDirction(SNAKE_LEFT);
bDead = false;
len = 2;
len1=3;//设定蛇的初始长度
position p;
pos.clear();
p.x = MAX_X/2;
p.y = MAX_Y/2;
for(int i = 0; i
{
p.x+=SNAKE_NODE_SIZE*2;
pos.push_back(p);
}
create_food(*this);
position1 p1;
pos1.clear();
p1.a = MAX_A/2;
p1.b = MAX_B/2;
for(int i = 0; i
{
p1.a+=SNAKE_NODE_SIZE*2;
pos1.push_back(p1);
}
create_food1(*this);
}
int snake::Move()
{
// 完成移动的动作
position posHead;// = new position;
position1 posHead1;
posHead = pos.front();
posHead1=pos1.front();
switch (snake_dir)
{
case SNAKE_UP:
posHead.y -=
SNAKE_NODE_SIZE*2;
posHead1.a
-=SNAKE_NODE_SIZE*2;//设定蛇出来向不同方向移动
break;
case SNAKE_DOWN:
posHead.y +=
SNAKE_NODE_SIZE*2;
posHead1.a +=
SNAKE_NODE_SIZE*2;
break;
case SNAKE_LEFT:
posHead.x -=
SNAKE_NODE_SIZE*2;
posHead1.b
-=SNAKE_NODE_SIZE*2;
break;
case SNAKE_RIGHT:
posHead.x +=
SNAKE_NODE_SIZE*2;
posHead1.b +=
SNAKE_NODE_SIZE*2;
break;
}
pos.push_front(posHead);
pos.pop_back();
pos1.push_front(posHead1);
pos1.pop_back();
// 判断是否吃到了食物
if (posHead == food)
{
create_food(*this);
Gorwup();
return 0;
}
if (posHead1 == food1)
{
create_food1(*this);
Gorwup();
return 0;
}
// 判断是否死亡
// 判断是否碰到自己
list ::const_iterator posbody;
for(posbody = pos.begin(), posbody++; posbody !=
pos.end (); posbody++)
{
if(*posbody == posHead)
{
bDead =
true;
return
2;
}
}
list ::const_iterator posbody1;
for(posbody1 = pos1.begin(), posbody1++; posbody1
!= pos1.end (); posbody1++)
{
if(*posbody1 == posHead1)
{
bDead =
true;
return
2;
}
}
// 判断是否碰到墙
if(posHead.x <= 0 || posHead.x >= MAX_X
||
posHead.y <= 0 || posHead.y
>= MAX_Y)
{
bDead = true;
return 1;
}
if(posHead1.a <= 0 || posHead1.a >= MAX_A ||
posHead1.b <= 0 ||
posHead1.b >= MAX_B)
{
bDead = true;
return 1;
}
return 0;
}#include
#include "snake.h"
// Global variable
extern int i=0;
HINSTANCE hinst;
extern position food;
extern position1 food1;
// 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);
MoveWindow(hwnd, 200, 200, MAX_A+20, MAX_B+50,
TRUE);
i++;
int j=500;
if(i%5==0) j=j/5;
SetTimer(hwnd, TIMER_ID, j, 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 sn2,sn1;//修改了这里,定义两条蛇
LONG APIENTRY MainWndProc(
HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM lParam)
{
switch (msg)
{
case WM_CREATE:
sn2 = snake();
sn1= snake();
break;
case WM_PAINT:
//所有使用GDI在窗口上绘制图形的程序都写在这里。
{
PAINTSTRUCT
ps;
HPEN
hpen;
HBRUSH
hbrush;
HDC
hdc;
HPEN hOldPen
;
HBRUSH
hOldBrush;
HBRUSH
hbrushFood;
HPEN
hPenWall;
hdc =
BeginPaint(hwnd, &ps);
hbrushFood
= CreateSolidBrush(RGB(10, 0,150));
hOldBrush =
(HBRUSH)SelectObject(hdc, hbrushFood);
Rectangle(hdc,
food.x-SNAKE_NODE_SIZE, food.y-SNAKE_NODE_SIZE,
food.x+SNAKE_NODE_SIZE,
food.y+SNAKE_NODE_SIZE);//huayuan
hpen =
CreatePen(0, 2, RGB(220,22,234));
hbrush =
CreateSolidBrush(RGB(225,25, 25));
hOldPen =
(HPEN)SelectObject(hdc, hpen);
hOldBrush =
(HBRUSH)SelectObject(hdc, hbrush);
//Rectangle(hdc,
10, 10, 50, 60);
list
::const_iterator sn_pos;
for(sn_pos =
sn2.pos.begin(); sn_pos != sn2.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
::const_iterator sn_pos1;
hpen =
CreatePen(1, 2, RGB(22,2,23));
hbrush =
CreateSolidBrush(RGB(2,252, 25));
hOldPen =
(HPEN)SelectObject(hdc, hpen);
hOldBrush =
(HBRUSH)SelectObject(hdc, hbrush);
for(sn_pos1 =
sn1.pos1.begin(); sn_pos1 != sn1.pos1.end(); sn_pos1++)
{
position1
p1 = *sn_pos1;
Rectangle(hdc,
p1.a-SNAKE_NODE_SIZE, p1.b-SNAKE_NODE_SIZE,
p1.a+SNAKE_NODE_SIZE,
p1.b+SNAKE_NODE_SIZE);
}
hPenWall =
CreatePen(1, 3, RGB(0,25,25));
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);
DeleteObject(hbrushFood);
DeleteObject(hpen);
DeleteObject(hbrush);
EndPaint(hwnd,&ps);
}
break;
case WM_KEYDOWN:
{
switch(wParam)
{
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;
}//复制此段代码,为另一条蛇的移动设置操作键
switch(wParam)
{
case 'J'
:
sn1.SetDirction(SNAKE_LEFT);
break;
case
'L':
sn1.SetDirction(SNAKE_RIGHT);
break;
case
'I':
sn1.SetDirction(SNAKE_UP);
break;
case
'K':
sn1.SetDirction(SNAKE_DOWN);
break;
default:
break;
}
//
设置窗口重绘制,更新窗口
InvalidateRect
(hwnd, NULL, TRUE);
UpdateWindow
(hwnd);
break;
}
case WM_TIMER:
{
if(!sn2.isDead())
{
if(sn2.Move()
!= 0)
{
MessageBox(0,0,"WOW!!!!!!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);
}
if(!sn2.pos.size()%2==1)
{
SetTimer(hwnd,TIMER_ID,500-sn2.pos.size()*20,NULL);
}//每吃三个食物,就加速
if(!sn1.isDead())
{
if(sn1.Move()
!= 0)
{
MessageBox(0,0,"WOW!!!!!!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);
if(!sn1.pos1.size()%2==1)
{
SetTimer(hwnd,TIMER_ID,500-sn2.pos1.size()*20,NULL);
}//每吃三个食物,就加速
}
break;
}
case WM_LBUTTONDOWN:
break;
case WM_SIZE:
break;
case WM_DESTROY:
ExitProcess(0);
break;
default:
return
DefWindowProc(hwnd,
msg,
wParam,
lParam);
}
return 0L;
}