wall.h
 
#pragma once
#ifndef _WALL_HEAD
#define _WALL_HEAD
#include <iostream>
using namespace std
;
class Wall
{
public:
	enum {
		ROW 
= 26 , 
		COL 
= 26
	};
	
	
	void initWall();
	
	void drawWall();
	
	void setWall(int x
, int y
, char c
);
	
	char getWall(int x
, int y
);
private:
	char gameArray
[ROW
][COL
];
};
#endif 
 
wall.cpp
 
#include "wall.h"
void Wall
::initWall()
{
	for (int i 
= 0; i 
< ROW
; i
++)
	{
		for (int j 
= 0; j 
< COL
; j
++)
		{
			
			if (i 
== 0 || j 
== 0 || i 
== ROW 
- 1 || j 
== COL 
- 1)
			{
				gameArray
[i
][j
] = '*';
			}
			else
			{
				gameArray
[i
][j
] = ' ';
			}
		}
	}
}
void Wall
::drawWall()
{
	for (int i 
= 0; i 
< ROW
; i
++)
	{
		for (int j 
= 0; j 
< COL
; j
++)
		{
			cout 
<< gameArray
[i
][j
] << " ";
		}
		if (i 
== 5)
		{
			cout 
<< "create by xiaomeng";
		}
		if (i 
== 6)
		{
			cout 
<< "a:left";
		}
		if (i 
== 6)
		{
			cout 
<< "a:left";
		}
		if (i 
== 7)
		{
			cout 
<< "d:right";
		}
		if (i 
== 8)
		{
			cout 
<< "w:ip";
		}
		if (i 
== 9)
		{
			cout 
<< "s:down";
		}
		cout 
<< endl
;
		
	}
}
void Wall
::setWall(int x
, int y
, char c
)
{
	gameArray
[x
][y
] = c
;
}
char Wall
::getWall(int x
, int y
)
{
	return gameArray
[x
][y
];
}
 
snake.h
 
#pragma once
#include <iostream>
#include "wall.h"
#include "food.h"
#include <Windows.h>
using namespace std
;
class Snake
{
public:
	Snake(Wall 
& tmpWall
, Food 
&food
);
	enum {
		UP 
= 'w',
		DOWN 
= 's',
		LEFT 
= 'a',
		RIGHT 
= 'd'
	};
	
	struct Point
	
{
		
		int x
;
		int y
;
		
		Point 
* next
;
	};
	void initSnake();
	
	void destoryPonit();
	
	void addPonit(int x
, int y
);
	
	void delPoint();
	
	bool move(char key
);
	
	
	int getSleepTime();
	
	int countList();
	
	int getScore();
	Point 
* pHead
;	
	Wall 
&wall
;
	Food 
&food
;
	bool isRool
;  
};
 
snake.cpp
 
#include "snake.h"
void gotoxy1(HANDLE hOut1
, int x
, int y
)
{
	COORD pos
;
	pos
.X 
= x
;	
	pos
.Y 
= y
;	
	SetConsoleCursorPosition(hOut1
, pos
);
}
HANDLE hOut1 
= GetStdHandle(STD_OUTPUT_HANDLE
);	
Snake
::Snake(Wall 
& tmpWall
, Food 
&tmpFood
):wall(tmpWall
),food(tmpFood
)  
{
	pHead 
= NULL;
	isRool 
= false;
}
void Snake
::initSnake()
{
	destoryPonit();
	addPonit(5, 3);
	addPonit(5, 4);
	addPonit(5, 5);
}
void Snake
::destoryPonit()
{
	Point 
* pCur 
= pHead
;
	while (pHead 
!= NULL)
	{
		pCur 
= pHead
->next
;
		delete pHead
;
		pHead 
= pCur
;
	}
}
void Snake
::addPonit(int x
, int y
)
{
	
	Point 
* newPoint 
= new Point
;
	newPoint
->x 
= x
;
	newPoint
->y 
= y
;
	newPoint
->next 
= NULL;
	
	if (pHead 
!= NULL)
	{
		wall
.setWall(pHead
->x
, pHead
->y
, '=');
		gotoxy1(hOut1
, pHead
->y 
* 2, pHead
->x
);
		cout 
<< "=";
	}
	newPoint
->next 
= pHead
;
	pHead 
= newPoint
;	
	wall
.setWall(pHead
->x
, pHead
->y
, '@');
	gotoxy1(hOut1
, pHead
->y 
* 2, pHead
->x
);
	cout 
<< "@";
}
void Snake
::delPoint()
{
	
	if (pHead 
== NULL || pHead
->next 
== NULL)
	{
		return;
	}
	Point 
* pPre 
= pHead
;
	Point 
* pCur 
= pHead
->next
;
	
	while (pCur
->next 
!= NULL)
	{
		pPre 
= pPre
->next
;
		pCur 
= pCur
->next
;
	}
	
	wall
.setWall(pCur
->x
, pCur
->y
, ' ');
	gotoxy1(hOut1
, pCur
->y 
* 2, pCur
->x
);
	cout 
<< ' ';
	delete pCur
;
	pCur 
= NULL;
	pPre
->next 
= NULL;
}
bool Snake
::move(char key
)
{
	int x 
= pHead
->x
;
	int y 
= pHead
->y
;
	switch (key
)
	{
	case UP
:
		x
--;
		break;
	case DOWN
:
		x
++;
		break;
	case LEFT
:
		y
--;
		break;
	case RIGHT
:
		y
++;
		break;
	default:
		break;
	}
	
	Point 
* pCur 
= pHead
->next
;
	Point 
* pPre 
= pHead
;
	while (pCur
->next 
!= NULL)
	{
		pPre 
= pPre
->next
;
		pCur 
= pCur
->next
;
	}
	if (pCur
->x 
== x 
&& pCur
->y 
== y
)
	{
		
		isRool 
= true;
	}
	else
	{
		
		if (wall
.getWall(x
, y
) == '*' || wall
.getWall(x
, y
) == '=')
		{
			addPonit(x
, y
);
			system("cls");
			wall
.drawWall();
			cout 
<< "得分 : " << getScore() << "分" << endl
;
			cout 
<< "GAME OVER !!!" << endl
;
			return false;
		}
	}
	
	
	
	if (wall
.getWall(x
, y
) == '#')
	{
		addPonit(x
, y
);
		
		food
.setFood();
	}
	else
	{
		addPonit(x
, y
);
		delPoint();
		if (isRool 
== true)
		{
			
			wall
.setWall(x
, y
, '@');
			gotoxy1(hOut1
, y 
* 2, x
);
			cout 
<< "@";
		}
	}
	return true;
}
int Snake
::getSleepTime()
{
	int sleepTime 
= 0;
	int size 
= countList();
	if (size 
< 5)
	{
		sleepTime 
= 300;
	}
	else if (size 
>= 5 <= 9)
	{
		sleepTime 
= 200;
	}
	else
	{
		sleepTime 
= 100;
	}
	return sleepTime
;
}
int Snake
::countList()
{
	int size 
= 0;
	Point 
* curPoint 
= pHead
;
	while (curPoint 
!= NULL)
	{
		size
++;
		curPoint 
= curPoint
->next
;
	}
	return size
;
}
int Snake
::getScore()
{
	int size 
= countList();
	int score 
= (size 
- 3) * 100;
	return score
;
}
 
food.h
 
#pragma once
#include <iostream>
#include "wall.h"
#include <Windows.h>
using namespace std
;
class Food
{
public:
	Food(Wall 
&tmpWall
);
	
	void setFood();
	int foodX
;
	int foodY
;
	Wall 
& wall
;
};
 
food.cpp
 
#include "food.h"
void gotoxy2(HANDLE hOut2
, int x
, int y
)
{
	COORD pos
;
	pos
.X 
= x
;	
	pos
.Y 
= y
;	
	SetConsoleCursorPosition(hOut2
, pos
);
}
HANDLE hOut2 
= GetStdHandle(STD_OUTPUT_HANDLE
);	
Food
::Food(Wall 
&tmpWall
) :wall(tmpWall
)
{
}
void Food
::setFood()
{
	while (true)
	{
		
		foodX 
= rand() % (Wall
::ROW 
- 2) + 1;
		foodY 
= rand() % (Wall
::COL 
- 2) + 1;
		
		if (wall
.getWall(foodX
, foodY
) == ' ')
		{
			wall
.setWall(foodX
, foodY
, '#');
			gotoxy2(hOut2
, foodY 
* 2, foodX
);
			cout 
<< "#";
			break;
		}
	}
}
 
game.cpp
 
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std
;
#include "wall.h"
#include "snake.h"
#include "food.h"
#include "ctime"
#include "conio.h"
#include <Windows.h>
void gotoxy(HANDLE hOut
, int x
, int y
)
{
	COORD pos
;
	pos
.X 
= x
;	
	pos
.Y 
= y
;	
	SetConsoleCursorPosition(hOut
, pos
);
}
HANDLE hOut 
= GetStdHandle(STD_OUTPUT_HANDLE
);	
int main()
{
	
	srand((unsigned int)time(NULL));
	
	bool isDead 
= false;
	char preKey 
= NULL;
	Wall wall
;
	wall
.initWall();
	wall
.drawWall();  
	
	
	
	Food 
food(wall
);
	food
.setFood();
	Snake 
snake(wall
, food
);
	snake
.initSnake();
	
	gotoxy(hOut
, 0, Wall
::ROW
);
	cout 
<< "得分 : " << snake
.getScore() << "分" << endl
;
	gotoxy(hOut
, 10, 5);
	
	while (!isDead
)
	{
		char key 
= _getch();
		
		
		if (preKey 
== NULL && key 
== snake
.LEFT
)
		{
			continue;
		}
		do
		{
			if (key 
== snake
.UP 
|| key 
== snake
.DOWN 
|| key 
== snake
.LEFT 
|| key 
== snake
.RIGHT
)
			{
				
				if (
					(key 
== snake
.LEFT 
&& preKey 
== snake
.RIGHT
) || (key 
== snake
.RIGHT 
&& preKey 
== snake
.LEFT
) ||
					(key 
== snake
.UP 
&& preKey 
== snake
.DOWN
) || (key 
== snake
.DOWN 
&& preKey 
== snake
.UP
)
					)
				{
					key 
= preKey
;
				}
				else
				{
					preKey 
= key
;  
				}
				if (snake
.move(key
) == true)
				{
					
					
					
					gotoxy(hOut
, 0, Wall
::ROW
);
					cout 
<< "得分 : " << snake
.getScore() << "分" << endl
;
					Sleep(snake
.getSleepTime());
				}
				else
				{
					isDead 
= true;
					break;
				}
			}
			else
			{
				key 
= preKey
;  
			}
		} while (!_kbhit());  
	}
	
	system("pause");
	return EXIT_SUCCESS
;
}
                
                
                
        
    
                    转载请注明原文地址: https://mac.8miu.com/read-4151.html