📄 snake.cpp
字号:
#include "Snake.h"
Snake::Snake()
{
SnakeInit();
FoodInit();
}
Snake::~Snake()
{
Node* node = head;
Node* next = node->next;
while (next != NULL)
{
delete node;
node = next;
next = node->next;
}
delete node;
}
void Snake::FoodInit(void)
{
int x, y;
x = (rand() % WIDTH) / 20;
y = (rand() % HEIGHT) / 20;
if (x == 0)
x += 2;
else if (x == GRIDX - 1)
x -= 2;
if (y == 0)
y += 2;
else if (y == GRIDY - 1)
y -= 2;
candy.x = x;
candy.y = y;
while (!IsFoodPositionOK())
{
x = (rand() % WIDTH) / 20;
y = (rand() % HEIGHT) / 20;
if (x == 0)
x += 2;
else if (x == GRIDX - 1)
x -= 2;
if (y == 0)
y += 2;
else if (y == GRIDY - 1)
y -= 2;
candy.x = x;
candy.y = y;
}
candy.exist = true;
}
void Snake::SnakeInit(void)
{
int x, y;
x = (rand() % WIDTH) / 20;
y = (rand() % HEIGHT) / 20;
if (x == 0)
x++;
else if (x == GRIDX - 1)
x--;
if (y == 0)
y++;
else if (y == GRIDY - 1)
y--;
head = tail = new Node;
head->x = x;
head->y = y;
head->prev = head->next = NULL;
}
void Snake::SnakeEat(void)
{
candy.exist = false;
int x = candy.x, y = candy.y;
Node* node = new Node;
switch (m_dir)
{
case DIR_UP:
y--;
break;
case DIR_RIGHT:
x++;
break;
case DIR_DOWN:
y++;
break;
case DIR_LEFT:
x--;
break;
default: break;
}
node->x = x;
node->y = y;
node->prev = NULL;
node->next = head;
head->prev = node;
head = node;
}
void Snake::SnakeMove(void)
{
clear_x = tail->x;
clear_y = tail->y;
Node* node = tail;
Node* prev = node->prev;
while (prev != NULL)
{
node->x = prev->x;
node->y = prev->y;
node = prev;
prev = node->prev;
}
switch (m_dir)
{
case DIR_UP:
head->y--;
break;
case DIR_RIGHT:
head->x++;
break;
case DIR_DOWN:
head->y++;
break;
case DIR_LEFT:
head->x--;
break;
default: break;
}
if (head->x == candy.x && head->y == candy.y && candy.exist)
{
SnakeEat();
}
}
void Snake::SnakeDraw(void)
{
HDC hDc;
RECT rect;
HBRUSH hBrush;
hDc = GetDC(m_hWnd);
if (candy.exist)
{
hBrush = CreateSolidBrush(RGB(0xff, 0x00, 0x00));
rect.left = candy.x * 20;
rect.top = candy.y * 20;
rect.right = rect.left + 20;
rect.bottom = rect.top + 20;
FillRect(hDc, &rect, hBrush);
}
hBrush = CreateSolidBrush(RGB(0xff, 0xff, 0xff));
rect.left = clear_x * 20;
rect.top = clear_y * 20;
rect.right = rect.left + 20;
rect.bottom = rect.top + 20;
FillRect(hDc, &rect, hBrush);
hBrush = CreateSolidBrush(RGB(0x00, 0x00, 0xff));
Node* node = head;
while (node != NULL)
{
rect.left = node->x * 20;
rect.top = node->y * 20;
rect.right = rect.left + 20;
rect.bottom = rect.top + 20;
FillRect(hDc, &rect, hBrush);
node = node->next;
}
}
void Snake::SetDirection(int dir)
{
int dir_diff = dir - m_dir;
if (dir_diff < 0)
dir_diff = -dir_diff;
if (200 == dir_diff)
{
if (head == tail)
m_dir = dir;
}
else
{
m_dir = dir;
}
}
void Snake::SetHWND(HWND hWnd)
{
m_hWnd = hWnd;
}
bool Snake::IsSnakeDead(void)
{
int x, y;
x = head->x;
y = head->y;
if (x < 0 || x >= GRIDX)
return true;
if (y < 0 || y >= GRIDY)
return true;
Node* node = head->next;
while (node != NULL)
{
if (node->x == head->x && node->y == head->y)
return true;
node = node->next;
}
return false;
}
bool Snake::IsFoodPositionOK(void)
{
if (NULL == head)
return true;
else
{
Node* node = head;
while (node != NULL)
{
if (node->x == candy.x && node->y == candy.y)
return false;
node = node->next;
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -