70 lines
973 B
C++
70 lines
973 B
C++
#include<iostream>
|
|
#include<string>
|
|
#include<ctime>
|
|
#include<random>
|
|
#include<graphics.h>
|
|
#include<conio.h>
|
|
#include<vector>
|
|
//创建方向的枚举类型
|
|
enum Direction{UP,DOWN,LEFT,RIGHT};
|
|
//贪吃蛇的抽象基类
|
|
class Base {
|
|
public:
|
|
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:
|
|
};
|
|
/*
|
|
定义蛇的类
|
|
蛇具有以下属性:蛇的坐标位置,蛇的长度
|
|
具有的方法:移动
|
|
*/
|
|
class Snake :public Base{
|
|
public:
|
|
int snake_lenth;
|
|
int speed = 100;
|
|
//利用vector来存储蛇的节点
|
|
std::vector<Base> nodes;
|
|
Direction direction;
|
|
Snake() {
|
|
//默认向右移动
|
|
direction = RIGHT;
|
|
//位置
|
|
nodes.push_back(Base());
|
|
speed = 100;
|
|
}
|
|
//显示蛇的身体
|
|
void Show() {
|
|
for(int i=0;i<nodes.size();i++){
|
|
nodes[i].show();
|
|
}
|
|
}
|
|
};
|
|
class Food : public Base {
|
|
public:
|
|
};
|
|
int main() {
|
|
while (true) {
|
|
|
|
}
|
|
|
|
} |