Object-oriented approach to compiling a simple console version of the Snake (i)

Source: Internet
Author: User
Tags map class

Today, we started writing a simple console version of the snake with an object-oriented approach. I have limited ability, if there is a mistake, please point out in time, many forgive.

Before we write the program, we have to have a clearer idea of how to make this game. I think that you can follow the following several to clear the following ideas.

1. Think: First of all, you need to know how to play the snake. After all, if the rules of the game do not know, it is not practical to make up.

2. Identify technical points

(1) What technology can show maps, snakes, etc.

(2) Snakes need to be controlled, so control snakes need to be implemented in whatever way. Is the process of constantly drawing and detecting state (capturing keys).

3. Build a relationship (function): The snake eats the food to grow longer, touches the wall to die.

(1) It can be thought that we should build a few classes (class)-food, map, snakes. At the same time, you need a loop loop, and if it doesn't meet the criteria (say, "The Snake is dead), jump out of the loop."

(2) What is the specific way to store food, maps and snakes? You can use vectors to store each block. The food, the map and the snakes are all pressed into the vector container, and the three different kinds of things can be labeled (TYPE_). It then traverses the entire container and draws different patterns with different markers, which can be displayed on the control console.

(3) Loop loop: Each cycle, you need to traverse the container to detect food, snakes and map coordinates. Why do this. Because you need this way to detect whether the snake head and two other things are coincident, the coordinates are the same, it coincides. If the coordinates of the head of the snake and the coordinates of the wall are the same, then the game over; if the coordinates of the snake head coincide with the coordinates of the food, the food is eaten, then the extra food is generated, while the snake body increases.

(4) How to control the movement of snakes. You can write this class into a snake, but it's a lot more work, and you can write a control class that specifically controls the movement of the snake.

4. Implement it one by one.

5. Associated Debugging.


We already have these ideas, so let's start with them. But the process of implementation also needs some specific logic, which is mainly embodied in: what to do first, what to do, how to solve these problems.

First, the framework must be built, at least on the console to show snakes, maps and food.

According to the above ideas, we need snakes (Snake), map classes (maps), food products. We take the idea that the vector container of C + + is used to store each "node", if the node stores the point of the map class, we use "*", if it is the point of the snake, we use "o", if it is a food class, we use "$" to express.

However, this "knot" is not one-dimensional, after all, the display on the console is two-dimensional, there are X-values and Y-values. Therefore, we also have to define a node class (Basenode), which is used specifically to define "nodes".

To facilitate display and other operations, we specifically define a control class (Controller), which acts as a "leveling" of the entire game control.

So, what is the first class you should implement when implementing code? It's obviously a node class, because if you don't have a specific node, you can't define whether it's a map, a snake, or food (after all, snakes, maps, and food are in two-dimensional space). Next is the control class, without the control class, you can not achieve the snake, map and other operations such as display. After these two classes are written, snakes, maps, and food classes will be easier to implement. The first implementation is the node class:

/*
	baseNode.h
*/

#pragma once

enum NodeType  //define the type of end point
{
	type_snake,   //Snake
	Type_map,	  //Map class
	Type_food     //food category
};

Class Basenode
{public
:
	Basenode ();
	Basenode (int x, int y, nodeType type);
	~basenode ();


	int x;
	int y;
	NodeType type;
};
Accordingly, the implementation of the specific function in BaseNode.h is as follows:

BaseNode.cpp
#include "baseNode.h"

Basenode::basenode ()
{
}

basenode::basenode (int x, int y, NodeType type)
{
	this->x = x;
	This->y = y;
	This->type = type;
}

Basenode::~basenode ()
{
}

After the Basenode class is implemented, the write control class is started, and in the present case we only need to implement two functions: the ability to display nodes on the console and capture coordinates on the console.

	/* Control class: In some ways, this class is "leveling" the entire game/

#pragma once
#include <vector>
#include "baseNode.h"
using namespace std;
Class Controller
{public
:
	static void movexy (int x, int y);  Move the cursor to the position of
	static void Showbasenode (Vector<basenode*> v);//Map, snake, food, etc. on the console display
};
Description: In the Controller.cpp file I define: If it is a MAP type of node (TYPE_MAP), Draw "*", if it is a snake node (type_snake), Draw "O", if it is food node (type_food), Draw "$".
Controller.cpp

#include "Controller.h"
#include "baseNode.h"
#include <iostream>
#include <Windows.h>
#include <conio.h>
using namespace std;
	/* movexy This function can navigate to (x, Y) position on the console by setconsolecursorposition This function
	, I call this function in the Showbasenode function, You can output the style I want on the console in the appropriate location.
*

/void controller::movexy (int x, int y)
{
	COORD COORD;
	Coord. x = x;
	Coord. y = y;
	SetConsoleCursorPosition (GetStdHandle (std_output_handle), coord);
}

void Controller::showbasenode (vector<basenode*> v)
{for
	(int i = 0; i < v.size (); i++)
	{
		basenode* node = v.at (i);
		Controller::movexy (Node->x, node->y);  The role of the Movexy method is manifested here, if the statement is removed
		                                       //The image displayed on the console is output in turn, regardless of the control line.
		if (Node->type = = Type_snake)
		{
			cout << "O";
		}
		else if (Node->type = = Type_map)
		{
			cout << "*";
		}
		else if (Node->type = = Type_food)
		{
			cout << "#";}}
}
After these two classes have been implemented, we have implemented snakes, maps and food classes in turn. It's easier.

Snake Type:

Snake.h

#pragma once
#include <vector>
#include "Controller.h"
#include "baseNode.h
" using namespace std;

Class Snake
{public
:
	Snake (int x, int y);  A constructor is a ~snake () used to initialize the position of a snake
	;

	void Showsnake ();

Private:
	vector<basenode*> m_snake;
};
Snake.cpp
#include "Snake.h"
#include "Controller.h"

snake::snake (int x, int y)
{
	m_ Snake.push_back (New Basenode (x, Y, Type_snake));//Create a snake by pressing the corresponding node into the vector, and the map and food in the same vein.
}

Snake::~snake ()
{
	while (m_snake.size ())
	{
		delete m_snake.back ();
		M_snake.pop_back ();
	}
}

void Snake::showsnake ()
{
	controller::showbasenode (m_snake);
}



Food class, the realization of the truth and snakes basically the same:

Food.h
#pragma once
#include "Controller.h"
#include "baseNode.h"
class food
{public
: Food
	(int x,int y);
	~food ();

	void Showfood ();
Private:
	vector<basenode*> m_food;
};

Food.cpp
#include "Food.h"

food::food (int x,int y)
{
	m_food.push_back (new Basenode (x, Y, Type_ food));
}

Food::~food ()
{
	while (m_food.size ())
	{
		delete m_food.back ();
		M_food.pop_back ();
	}
}

void Food::showfood ()
{
	controller::showbasenode (m_food);
}

Finally, the map class:

Map.h
#pragma once
#include "baseNode.h"
#include "Controller.h"
using namespace std;
Class map
{public
:
	map ();
	~map ();

	void Showmap ();

Private: 
	vector<basenode*> m_map;
};

Map.cpp
#include "Map.h"

map::map ()  //The first time the map was initialized, apparently surrounded by four walls
{for
	(int i = 0; i <; i++)
	{
		M_map.push_back (new Basenode (i, 0, Type_map));
		M_map.push_back (New Basenode (i, N, Type_map));
	}

	for (int i = 0; i < i++)
	{
		m_map.push_back (new Basenode (0, I, Type_map));
		M_map.push_back (New Basenode (I, Type_map));}
}

Map::~map ()
{
	while (m_map.size ())
	{
		delete m_map.back ();
		M_map.pop_back ();
	}
}

void Map::showmap ()
{
	controller::showbasenode (m_map);
}

Finally we write a main function test, at this stage, we temporarily regardless of the random generation of food and other functions. So let's write the coordinates in the fixed value first. Just see if you can achieve the goal of the first phase--a map, snake, and food can be displayed in the console.

Main.cpp

#include <iostream>
#include "Controller.h"
#include "baseNode.h"
#include "Map.h";
#include "Snake.h"
#include "Food.h"

void Main ()
{
	{
		map* map = new map ();
		snake* Snake = new Snake (5, 6);
		food* food = new food (+);

		Snake->showsnake ();
		Food->showfood ();
		Map->showmap ();
	}
	System ("pause");
}

Operation Result:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.