You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

282 lines
7.0 KiB

#include<iostream>
using namespace std;
#include<easyx.h> //<2F><><EFBFBD><EFBFBD>ͼ<EFBFBD>ο<EFBFBD>ͷ<EFBFBD>ļ<EFBFBD>
#include<stdlib.h>
#include<time.h>
#define WIDTH 1040 //<2F><><EFBFBD><EFBFBD><E5B3A1><EFBFBD><EFBFBD>С
#define HEIGHT 640
#define SIZE 20 //<2F><><EFBFBD><EFBFBD>ʳ<EFBFBD><CAB3><EFBFBD>Լ<EFBFBD><D4BC>ߵĴ<DFB5>С һ<><D2BB><EFBFBD><EFBFBD><EFBFBD>ӵĴ<D3B5>С
#define UP 1 //<2F><><EFBFBD><EFBFBD><EFBFBD>ߵij<DFB5><C4B3><EFBFBD>
#define DOWN -1
#define LEFT 2
#define RIGHT -2
#define MAX 2000 //<2F>ߵ<EFBFBD><DFB5><EFBFBD><EFBFBD>󳤶<EFBFBD>
typedef struct
{
int x;
int y;
}SnakeNode;
SnakeNode temp[MAX]; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E6B4A2>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
int speed = 150; //<2F>ߵ<EFBFBD><DFB5>ٶ<EFBFBD>
//<2F><><EFBFBD><EFBFBD><EFBFBD>ߵ<EFBFBD><DFB5><EFBFBD>
class Snake
{
friend class Food;
public:
Snake(); //<2F><>ʼ<EFBFBD><CABC>
void Move(); //<2F>ƶ<EFBFBD>
void Draw(); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
bool Eat(Food food); //<2F><>ʳ<EFBFBD><CAB3>
bool Defeat(); //ʧ<><CAA7><EFBFBD>ж<EFBFBD>
private:
int dirt; //<2F><><EFBFBD><EFBFBD>
int length; //<2F><><EFBFBD><EFBFBD>
SnakeNode node[MAX]; //<2F>ߵ<EFBFBD><DFB5><EFBFBD><EFBFBD><EFBFBD>
};
//<2F><><EFBFBD><EFBFBD>ʳ<EFBFBD><CAB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
class Food
{
friend class Snake;
public:
Food(Snake snake); //ʳ<><CAB3><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
void Draw(); //<2F><><EFBFBD><EFBFBD>ʳ<EFBFBD><CAB3>
private:
int x, y; //<2F><><EFBFBD><EFBFBD>
};
//<2F>ߵij<DFB5>ʼ<EFBFBD><CABC>
Snake::Snake()
{
dirt = RIGHT;
length = 3;
//<2F>±<EFBFBD><C2B1><EFBFBD>0<EFBFBD><30>λ<EFBFBD><CEBB>Ϊ<EFBFBD>ߵ<EFBFBD>ͷ<EFBFBD><CDB7>
for (int i = 0; i < length; i++)
{
node[i].x = 60 - ((i + 1) * SIZE);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>20
node[i].y = 0;
} //<2F>̶<EFBFBD><CCB6>տ<EFBFBD>ʼ<EFBFBD>ߵ<EFBFBD>λ<EFBFBD><CEBB>
//<2F><>һ<EFBFBD><D2BB>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD><EFBFBD>Ͻdz<CFBD><C7B3><EFBFBD>
}
//<2F>ƶ<EFBFBD>
void Snake::Move()
{
for (int i = 0; i < length; i++) //<2F><>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD><EFBFBD>߽<EFBFBD><DFBD><EFBFBD><E3BFBD>һ<EFBFBD><D2BB>
{
temp[i].x = node[i].x;
temp[i].y = node[i].y;
}
int status = 0; //<2F><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD>򰴼<EFBFBD>
if (dirt == RIGHT) //<2F>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵķ<DAB5><C4B7>򣬷ֱ<F2A3ACB7><D6B1>ж<EFBFBD><D0B6>Ƿ<EFBFBD>ת<EFBFBD>򣬲<EFBFBD><F2A3ACB2>ܷ<EFBFBD><DCB7><EFBFBD><EFBFBD><EFBFBD>
{
//<2F>ж<EFBFBD><D0B6>Ƿ<EFBFBD>ת<EFBFBD><D7AA>
if (GetAsyncKeyState('W') && status == 0) //<2F><>׽<EFBFBD><D7BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊw<CEAA><77>easyx <20>ж<EFBFBD>ת<EFBFBD><D7AA>
{
dirt = UP; //ȥ<><C8A5>if(dirt==up)<29><>else
status = 1;
}
else if (GetAsyncKeyState('S') && status == 0)
{
dirt = DOWN;
status = 1;
}
else
{
node[0].x = node[0].x + SIZE;
}
}
if (dirt == DOWN)
{
//<2F>ж<EFBFBD><D0B6>Ƿ<EFBFBD>ת<EFBFBD><D7AA>
if (GetAsyncKeyState('A') && status == 0)
{
dirt = LEFT;
status = 1;
}
else if (GetAsyncKeyState('D') && status == 0)
{
node[0].x = node[0].x + SIZE;
dirt = RIGHT;
status = 1;
}
else
{
node[0].y = node[0].y + SIZE;
}
}
if (dirt == LEFT)
{
//<2F>ж<EFBFBD><D0B6>Ƿ<EFBFBD>ת<EFBFBD><D7AA>
if (GetAsyncKeyState('W') && status == 0)
{
dirt = UP;
status = 1;
}
else if (GetAsyncKeyState('S') && status == 0)
{
node[0].y = node[0].y + SIZE;
dirt = DOWN;
status = 1;
}
else
{
node[0].x -= SIZE;
}
}
if (dirt == UP)
{
//<2F>ж<EFBFBD><D0B6>Ƿ<EFBFBD>ת<EFBFBD><D7AA>
if (GetAsyncKeyState('A') && status == 0)
{
node[0].x = node[0].x - SIZE;
dirt = LEFT;
status = 1;
}
else if (GetAsyncKeyState('D') && status == 0)
{
node[0].x = node[0].x + SIZE;
dirt = RIGHT;
status = 1;
}
else
{
node[0].y = node[0].y - SIZE;
//<2F>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>
}
}
//<2F>ƶ<EFBFBD>
for (int i = 1; i < length; i++)
{
node[i].x = temp[i - 1].x;
node[i].y = temp[i - 1].y;
}
Sleep(speed); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><C3B4>ʱ<EFBFBD>䣬ͣ<E4A3AC><CDA3>һ<EFBFBD><D2BB>
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void Snake::Draw()
{
cleardevice(); //<2F><><EFBFBD><EFBFBD>ԭ<EFBFBD>ȵĻ<C8B5>ͼ
srand((unsigned)time(NULL)); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>setfillcolor)ʱ<><CAB1>
for (int i = 0; i < length; i++)
{
setfillcolor(RGB(rand() % 256, rand() % 256, rand() % 256)); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>
fillrectangle(node[i].x, node[i].y, node[i].x + SIZE, node[i].y + SIZE);
}
}
//<2F><>ʳ<EFBFBD><CAB3>
bool Snake::Eat(Food food)
{
if (food.x == node[0].x && food.y == node[0].y) //ͷ<><CDB7>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʳ<EFBFBD><CAB3>
{
if (node[length - 1].x - node[length - 2].x == 0 && node[length - 1].y - node[length - 2].y == -20)
{
//<2F>ж<EFBFBD><D0B6><EFBFBD>β<EFBFBD>͵ķ<CDB5><C4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD>ù<EFBFBD>ϵ<EFBFBD><CFB5><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD>ͺ<EFBFBD><CDBA><EFBFBD>
length++;
node[length - 1].x = node[length - 2].x;
node[length - 1].y = node[length - 2].y - SIZE;
}
else if (node[length - 1].x - node[length - 2].x == 0 && node[length - 1].y - node[length - 2].y == 20)
{
length++;
node[length - 1].x = node[length - 2].x;
node[length - 1].y = node[length - 2].y + SIZE;
}
else if (node[length - 1].x - node[length - 2].x == 20 && node[length - 1].y - node[length - 2].y == 0)
{
length++;
node[length - 1].x = node[length - 2].x + SIZE;
node[length - 1].y = node[length - 2].y;
}
else if (node[length - 1].x - node[length - 2].x == -20 && node[length - 1].y - node[length - 2].y == 0)
{
length++;
node[length - 1].x = node[length - 2].x - SIZE;
node[length - 1].y = node[length - 2].y;
}
return true;
}
return false;
}
//ʧ<><CAA7><EFBFBD>ж<EFBFBD>
bool Snake::Defeat()
{
//<2F><><EFBFBD><EFBFBD><EFBFBD>߽<EFBFBD>
if (node[0].x < 0 || node[0].x >= WIDTH || node[0].y < 0 || node[0].y >= HEIGHT)
return true;
//<2F><><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (int i = 1; i < length; i++)
{
if (node[0].x == node[i].x && node[0].y == node[i].y)
return true;
}
return false;
}
//ʳ<><CAB3><EFBFBD>ij<EFBFBD>ʼ<EFBFBD><CABC>
Food::Food(Snake snake)
{
table:
srand((unsigned)time(NULL)); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD><D3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
x = (rand() % (WIDTH / SIZE)) * SIZE;
y = (rand() % (HEIGHT / SIZE)) * SIZE;
for (int i = 0; i < snake.length; i++)
{
if (snake.node[i].x == x && snake.node[i].y == y)
goto table; //<2F><><EFBFBD><EFBFBD>ʳ<EFBFBD><CAB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD><CFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>³<EFBFBD>ʼ<EFBFBD><CABC>
}
}
//<2F><><EFBFBD><EFBFBD>ʳ<EFBFBD><CAB3>
void Food::Draw()
{
setfillcolor(GREEN); //ʳ<><CAB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ
fillrectangle(x, y, x + SIZE, y + SIZE); //<2F><><EFBFBD><EFBFBD>ʳ<EFBFBD><CAB3>Ϊ<EFBFBD><CEAA><EFBFBD>Σ<EFBFBD><CEA3>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
int main()
{
initgraph(WIDTH, HEIGHT);
Snake snake;
table:
Food food(snake);
while (1)
{
BeginBatchDraw();
FlushBatchDraw(); //˫<><CBAB><EFBFBD><EFBFBD><E5A3AC>ֹ<EFBFBD><D6B9>Ļһ<C4BB><D2BB>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>
snake.Draw();
food.Draw();
FlushBatchDraw(); //˫<><CBAB><EFBFBD><EFBFBD><E5A3AC>ֹ<EFBFBD><D6B9>Ļһ<C4BB><D2BB>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>
EndBatchDraw();
/*˫<><CBAB><EFBFBD><EFBFBD><E5A3AC>ֹ<EFBFBD><D6B9>Ļһ<C4BB><D2BB>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>*/
if (snake.Eat(food))
{
goto table; //<2F><><EFBFBD><EFBFBD><EFBFBD>Ե<EFBFBD>ʳ<EFBFBD><EFBFBD><EFA3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ʳ<EFBFBD><CAB3>
}
if (snake.Defeat())
{
break;
}
snake.Move();
}
HWND window = GetHWnd(); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE>Ϸʧ<CFB7><CAA7>
SetWindowText(window, L"<EFBFBD><EFBFBD>ʾ"); //<2F>޸Ĵ<DEB8><C4B4>ڱ<EFBFBD><DAB1><EFBFBD><E2A3AC><EFBFBD>ó<EFBFBD><C3B3><EFBFBD>ʾ
MessageBox(window, L"<EFBFBD><EFBFBD>Ϸʧ<EFBFBD><EFBFBD>", L"<EFBFBD><EFBFBD>ʾ", MB_OKCANCEL);
//<2F><>ʾʧ<CABE><CAA7><EFBFBD><EFBFBD>Ϣ MB_OKCANCEL<45><4C><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1>
return 0;
}