#include <iostream>
#include <conio.h>
#include <ctime>
#include <windows.h>
using namespace std;
const int ROW = 20;
const int line = 35;
class snake;
class Node
{
private:
	Node() :x(0), y(0), llink(NULL), next(NULL){}
	Node *llink, *next;
	int x, y;
	friend class snake;
};
class snake
{
public:
	snake();
	~snake();
	void move();
	void chioce_key(char);
	void output();
private:
	Node *head, *tail;
	int game[ROW][line];
	enum { UP, DOWN, RIGHT, LEFT }point;
	int food_x, food_y;
	void get_food();
	void gotoxy(int, int);
	void add_head(int, int);
	void delete_tail();
};
snake::snake() :food_x(0), food_y(0), head(NULL), tail(NULL), point(RIGHT)
{
	int i, j;
	for (i = 0; i<ROW; ++i)
	{
		for (j = 0; j<line; ++j)
		{
			game[i][0] = game[i][line - 1] = 1;
			game[0][j] = game[ROW - 1][j] = 1;
		}
	}
	head = tail;
	add_head(6, 4);
	add_head(6, 5);
	add_head(6, 6);
	get_food();
}
snake::~snake()
{
	while (head)
	{
		Node *temp = head;
		head = head->next;
		delete temp;
	}
}
void snake::add_head(int a, int b)
{
	Node *temp = new Node;
	temp->x = a;
	temp->y = b;
	if (head == NULL)
	{
		head = tail = temp;
	}
	else
	{
		head->llink = temp;
		temp->next = head;
		head = head->llink;
	}
	game[a][b] = 1;
}
void snake::delete_tail()
{
	Node *temp = tail;
	game[tail->x][tail->y] = 0;
	tail = tail->llink;
	tail->next = NULL;
	delete temp;
}
void snake::chioce_key(char key)
{
	switch (key)
	{
	case 'w':
	case 'W':point = UP; break;
	case 'a':
	case 'A':point = LEFT; break;
	case 'd':
	case 'D':point = RIGHT; break;
	case 's':
	case 'S':point = DOWN; break;
	}
}
void snake::output()
{
	int i, j;
	gotoxy(0, 0);
	for (i = 0; i<ROW; ++i)
	{
		for (j = 0; j<line; ++j)
		{
			if (game[i][j] == 1)
			{
				cout << "* ";
			}
			else
			{
				cout << "  ";
			}
		}
		cout << endl;
	}
}
void snake::get_food()
{
	srand(unsigned(time(NULL)));
	do
	{
		food_x = rand() % 18 + 1;
		food_y = rand() % 18 + 1;
	} while (game[food_x][food_y] == 1);
	game[food_x][food_y] = 1;
}
void snake::move()
{
	int a = head->x;
	int b = head->y;
	switch (point)
	{
	case UP:--a; break;
	case DOWN:++a; break;
	case LEFT:--b; break;
	case RIGHT:++b; break;
	}
	if (game[a][b] == 1 && food_x != a&&food_y != b)
	{
		output();
		cout << "失败了" << endl;
		this->~snake();
		exit(0);
	}
	if (food_x == a&&food_y == b)
	{
		add_head(a, b);
		get_food();
		return;
	}
	add_head(a, b);
	delete_tail();
}
void snake::gotoxy(int x, int y)
{
	HANDLE hOutput;
	COORD loc;
	loc.X = x;
	loc.Y = y;
	hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hOutput, loc);
}
int main()
{
	snake s;
	s.output();
	cout << "操作方法:W ↑ S ↓ A ← D →\n";
	while (true)
	{
		char keydown = _getch();
		s.chioce_key(keydown);
		while (!_kbhit())
		{
			s.move();
			s.output();
			Sleep(100);
		}
	}
	return 0;
}
  • 我喜欢论坛,它不仅让我学到了许多知识及传播知识,同时让我看到了许多不能自圆其说的帖子,让我开心 2019-05-18
  • 夏普收购东芝PC业务重回个人电脑市场 2019-05-14
  • 既能散寒止痛也可抗菌杀毒 艾叶竟还有这些妙用? 2019-05-14
  • 让山里娃感受智慧科技乐趣 2019-05-11
  • 基层公安民警开展无人机警务驾驶培训 2019-05-02
  • 我省首例侵犯公民个人信息案终审宣判 2019-05-01
  • 女性之声——全国妇联 2019-04-23
  • 瑞典民众庆祝世界杯胜利时发生枪击 致4人受伤 2019-04-19
  • 遭遇隐形歧视 就业权益谁来维护 2019-04-10
  • 杭州拱墅区:打造“4+X”巡逻防控体系 2019-04-10
  • 新华网评:流浪犬伤人事件频发,亟待多措并举 2019-04-08
  • 两队防守均是薄弱环节 进攻快VS球风猛谁占优? 2019-04-05
  • 安徽九旬教师守候留守儿童:“鞠躬尽瘁,死而不已” 2019-04-05
  • 中纪委:有干部不信马列信鬼神 触犯纪律信小圈子 2019-04-02
  • 小说文体审视、研究唐蕃关系的新突破 2019-03-31
  • 98| 46| 442| 772| 109| 464| 357| 148| 167| 523|