From 6d87594d11137790cee35b4a0a8aa9c3553ba8b4 Mon Sep 17 00:00:00 2001 From: kemna Date: Thu, 10 Oct 2024 20:52:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A1=86=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- practise/practise.cpp | 51 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/practise/practise.cpp b/practise/practise.cpp index 694a42d..dabe1a9 100644 --- a/practise/practise.cpp +++ b/practise/practise.cpp @@ -4,11 +4,33 @@ #include #include #include +#include +//创建方向的枚举类型 +enum Direction{UP,DOWN,LEFT,RIGHT}; +//贪吃蛇的抽象基类 class Base { public: - int positionX; - + Base() { + m_x = 50; + m_y = 50; + } + Base(int x,int y) { + m_x = x; + m_y = y; + } + //绘图 + virtual void show() { + setlinecolor(BLUE); + fillcircle(m_x, m_y, 6); + } + //移动 + void move(int x,int y) { + m_x += x; + m_x += y; + } + int m_x; + int m_y; private: }; /* @@ -17,17 +39,32 @@ private: 具有的方法:移动 */ class Snake :public Base{ - +public: + int snake_lenth; + int speed = 100; + //利用vector来存储蛇的节点 + std::vector nodes; + Direction direction; + Snake() { + //默认向右移动 + direction = RIGHT; + //位置 + nodes.push_back(Base()); + speed = 100; + } + //显示蛇的身体 + void Show() { + for(int i=0;i