From 8eb0b39ef3d95f5571a51f48ca4349b7ae8c09db Mon Sep 17 00:00:00 2001 From: JZFamily Date: Sun, 17 Jun 2018 12:13:16 +0800 Subject: [PATCH] point code --- point.cpp | 27 +++++++++++++++++++++++++++ point.h | 20 ++++++++++++++++++++ tools.cpp | 33 +++++++++++++++++++++++++++++++++ tools.h | 10 ++++++++++ 4 files changed, 90 insertions(+) create mode 100644 point.cpp create mode 100644 point.h create mode 100644 tools.cpp create mode 100644 tools.h diff --git a/point.cpp b/point.cpp new file mode 100644 index 0000000..9e76289 --- /dev/null +++ b/point.cpp @@ -0,0 +1,27 @@ +#include "point.h" +#include "tools.h" +#include + +void Point::Print()//输出方块 +{ + SetCursorPosition(x, y); + std::cout << "■"; +} + +void Point::PrintCircular()//输出圆形 +{ + SetCursorPosition(x, y); + std::cout << "●"; +} + +void Point::Clear()//清除输出 +{ + SetCursorPosition(x, y); + std::cout << " "; +} + +void Point::ChangePosition(const int x, const int y)//改变坐标 +{ + this->x = x; + this->y = y; +} diff --git a/point.h b/point.h new file mode 100644 index 0000000..662d666 --- /dev/null +++ b/point.h @@ -0,0 +1,20 @@ +#pragma once +#ifndef POINT_H +#define POINT_H + +class Point +{ +public: + Point() {} + Point(const int x, const int y) : x(x), y(y) {} + void Print(); + void PrintCircular(); + void Clear(); + void ChangePosition(const int x, const int y); + bool operator== (const Point& point) { return (point.x == this->x) && (point.y == this->y); } + int GetX() { return this->x; } + int GetY() { return this->y; } +private: + int x, y; +}; +#endif // POINT_H \ No newline at end of file diff --git a/tools.cpp b/tools.cpp new file mode 100644 index 0000000..e043c67 --- /dev/null +++ b/tools.cpp @@ -0,0 +1,33 @@ +#include "tools.h" +#include +#include + +void SetWindowSize(int cols, int lines)//设置窗口大小 +{ + system("title 贪吃蛇 ");//设置窗口标题 + char cmd[30]; + sprintf(cmd, "mode con cols=%d lines=%d", cols * 2, lines);//一个■占两个字符故*2 + system(cmd);//system(mode con cols=88 lines=88) +} + +void SetCursorPosition(const int x, const int y)// +{ + COORD position; + position.X = x * 2; + position.Y = y; + SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), position); +} + +void SetColor(int colorID)// +{ + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colorID); +} + +void SetBackColor()// +{ + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), + FOREGROUND_BLUE | + BACKGROUND_BLUE | + BACKGROUND_GREEN | + BACKGROUND_RED); +} diff --git a/tools.h b/tools.h new file mode 100644 index 0000000..eddbab2 --- /dev/null +++ b/tools.h @@ -0,0 +1,10 @@ +#ifndef TOOLS_H +#define TOOLS_H + + +void SetWindowSize(int cols, int lines); +void SetCursorPosition(const int x, const int y); +void SetColor(int colorID); +void SetBackColor(); + +#endif // TOOLS_H