用c++语言,面对对象的思想,写贪吃蛇代码,定义了class Snake { private: int x; int y; Snake* pre; Snake* next; public: Food food; }class Food { private: int food_x; int food_y; char c;//食物的字符表示 public: Food(int x, int y); char getfoodchar(); };,怎么画出场景;
时间: 2023-12-02 17:03:45 浏览: 37
你可以使用C++中的console输出来画出贪吃蛇的场景。具体步骤如下:
1. 首先需要包含windows.h头文件,使用其中的函数来控制控制台窗口。
2. 定义一个二维数组来表示整个游戏场景,比如可以定义一个16x16的数组。
3. 使用循环输出每个格子,根据数组的值来输出不同的字符表示不同的物体,比如空格表示空地,#表示墙壁,@表示食物,*表示蛇身等等。
4. 在Snake类中定义一个move函数,用来移动蛇身,移动时修改数组中的值。
5. 在Food类中定义一个generate函数,用来随机生成食物的位置,并修改数组中的值。
6. 在主函数中循环调用Snake的move函数,判断是否吃到食物,如果吃到了就调用Food的generate函数生成新的食物。
7. 每次循环结束后,使用system("cls")清空控制台,然后再重新输出整个场景,实现动态效果。
这样就可以画出一个简单的贪吃蛇场景了。当然,还有很多细节需要注意,比如蛇头的移动方向、撞墙判断、吃到食物后蛇身长度的增加等等。
相关问题
用c++语言,面对对象的思想,写贪吃蛇代码,定义了class Snake { private: int x; int y; Snake* pre; Snake* next; public: Food food; }class Food { private: int food_x; int food_y; char c;//食物的字符表示 public: Food(int x, int y); char getfoodchar(); };,搭配二维数组,画出边跨和蛇,给个示例代码
以下是一个简单的贪吃蛇代码示例,使用了二维数组和面向对象的思想:
```c++
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <ctime>
using namespace std;
const int WIDTH = 20;
const int HEIGHT = 20;
const int INTERVAL = 100;
class Snake {
private:
int x, y;
Snake* pre;
Snake* next;
public:
Snake(int x, int y, Snake* pre = nullptr, Snake* next = nullptr) : x(x), y(y), pre(pre), next(next) {}
int getX() { return x; }
int getY() { return y; }
void setX(int x) { this->x = x; }
void setY(int y) { this->y = y; }
Snake* getPre() { return pre; }
Snake* getNext() { return next; }
void setPre(Snake* pre) { this->pre = pre; }
void setNext(Snake* next) { this->next = next; }
};
class Food {
private:
int x, y;
char c;
public:
Food(int x, int y, char c = '@') : x(x), y(y), c(c) {}
int getX() { return x; }
int getY() { return y; }
char getC() { return c; }
void setX(int x) { this->x = x; }
void setY(int y) { this->y = y; }
void setC(char c) { this->c = c; }
};
class Game {
private:
int map[HEIGHT][WIDTH];
Snake* head;
Snake* tail;
Food* food;
bool gameover;
int score;
public:
Game() : gameover(false), score(0) {
//初始化地图
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (i == 0 || i == HEIGHT - 1 || j == 0 || j == WIDTH - 1) {
map[i][j] = 1;//边跨
}
else {
map[i][j] = 0;//空地
}
}
}
//初始化蛇
head = new Snake(WIDTH / 2, HEIGHT / 2);
tail = head;
map[head->getY()][head->getX()] = 2;//蛇身
for (int i = 1; i < 3; i++) {
Snake* body = new Snake(head->getX() + i, head->getY(), tail, nullptr);
tail->setNext(body);
tail = body;
map[body->getY()][body->getX()] = 2;//蛇身
}
//初始化食物
srand((unsigned)time(nullptr));
food = new Food(rand() % (WIDTH - 2) + 1, rand() % (HEIGHT - 2) + 1);
map[food->getY()][food->getX()] = 3;//食物
}
void draw() {
system("cls");
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (map[i][j] == 0) {
cout << " ";//空地
}
else if (map[i][j] == 1) {
cout << "##";//边跨
}
else if (map[i][j] == 2) {
cout << "**";//蛇身
}
else if (map[i][j] == 3) {
cout << food->getC() << " ";//食物
}
}
cout << endl;
}
cout << "Score: " << score << endl;
}
void update() {
//移动蛇
int x = head->getX();
int y = head->getY();
switch (_getch()) {
case 'w':
y--;
break;
case 'a':
x--;
break;
case 's':
y++;
break;
case 'd':
x++;
break;
}
if (map[y][x] == 1 || map[y][x] == 2) {
gameover = true;//撞墙或撞身体
return;
}
if (map[y][x] == 3) {
score += 10;//吃到食物
food->setX(rand() % (WIDTH - 2) + 1);
food->setY(rand() % (HEIGHT - 2) + 1);
map[food->getY()][food->getX()] = 3;//生成新的食物
}
else {
Snake* p = tail;
map[p->getY()][p->getX()] = 0;//清空尾巴
tail = p->getPre();
tail->setNext(nullptr);
delete p;//删除尾巴
}
head->setPre(new Snake(x, y, nullptr, head));
head = head->getPre();
map[y][x] = 2;//更新蛇头位置
}
bool isGameover() { return gameover; }
};
int main() {
Game game;
while (!game.isGameover()) {
game.draw();
game.update();
Sleep(INTERVAL);
}
cout << "Game over! Score: " << game.score << endl;
system("pause");
return 0;
}
```
这个示例代码使用了二维数组来表示游戏场景,用数字表示不同的物体,使用Snake和Food类来表示蛇和食物,并且使用了面向对象的思想。在主函数中不断循环调用Game类的draw和update函数来画出场景和更新游戏状态。
用c++写面对对象思想贪吃蛇
好的,我可以帮你回答这个问题。下面是一个简单的面向对象思想的贪吃蛇游戏的C++实现:
```c++
#include <iostream>
#include <vector>
#include <conio.h>
#include <windows.h>
using namespace std;
const int width = 20; // 地图宽度
const int height = 20; // 地图高度
int score = 0; // 得分
class Point {
private:
int x;
int y;
public:
Point(int _x, int _y) :x(_x), y(_y) {}
int getX() const { return x; }
int getY() const { return y; }
void setX(int _x) { x = _x; }
void setY(int _y) { y = _y; }
};
class Snake {
private:
vector<Point> body; // 蛇身
int direction; // 移动方向
public:
Snake(int x, int y) {
body.push_back(Point(x, y));
direction = 1;
}
void move() {
Point head = getHead();
switch (direction) {
case 1: // 上
body.insert(body.begin(), Point(head.getX(), head.getY() - 1));
break;
case 2: // 下
body.insert(body.begin(), Point(head.getX(), head.getY() + 1));
break;
case 3: // 左
body.insert(body.begin(), Point(head.getX() - 1, head.getY()));
break;
case 4: // 右
body.insert(body.begin(), Point(head.getX() + 1, head.getY()));
break;
}
body.pop_back();
}
void changeDirection(int d) {
if (abs(direction - d) != 2) {
direction = d;
}
}
bool checkCollision() const {
Point head = getHead();
if (head.getX() < 0 || head.getX() >= width || head.getY() < 0 || head.getY() >= height) {
return true;
}
for (int i = 1; i < body.size(); i++) {
if (head.getX() == body[i].getX() && head.getY() == body[i].getY()) {
return true;
}
}
return false;
}
void eat() {
Point head = getHead();
body.insert(body.begin(), Point(head.getX(), head.getY()));
score += 10;
}
Point getHead() const {
return body.front();
}
vector<Point> getBody() const {
return body;
}
};
class Food {
private:
Point position; // 食物位置
public:
Food() {
srand((unsigned)time(NULL));
int x = rand() % width;
int y = rand() % height;
position = Point(x, y);
}
Point getPosition() const {
return position;
}
void regenerate() {
srand((unsigned)time(NULL));
int x = rand() % width;
int y = rand() % height;
position.setX(x);
position.setY(y);
}
};
class Game {
private:
Snake snake;
Food food;
public:
Game(int x, int y) :snake(x, y) {}
void run() {
while (true) {
system("cls");
drawMap();
drawSnake();
drawFood();
moveSnake();
if (snake.checkCollision()) {
gameOver();
break;
}
if (eatFood()) {
snake.eat();
food.regenerate();
}
Sleep(150);
}
}
void drawMap() const {
for (int i = 0; i < width + 2; i++) {
cout << "#";
}
cout << endl;
for (int i = 0; i < height; i++) {
cout << "#";
for (int j = 0; j < width; j++) {
cout << " ";
}
cout << "#" << endl;
}
for (int i = 0; i < width + 2; i++) {
cout << "#";
}
cout << endl;
cout << "score: " << score << endl;
}
void drawSnake() const {
vector<Point> body = snake.getBody();
for (int i = 0; i < body.size(); i++) {
int x = body[i].getX();
int y = body[i].getY();
gotoxy(x + 1, y + 1);
if (i == 0) {
cout << "@";
}
else {
cout << "*";
}
}
}
void drawFood() const {
Point pos = food.getPosition();
gotoxy(pos.getX() + 1, pos.getY() + 1);
cout << "$";
}
void moveSnake() {
if (_kbhit()) {
int c = _getch();
switch (c) {
case 'w':
case 'W':
snake.changeDirection(1);
break;
case 's':
case 'S':
snake.changeDirection(2);
break;
case 'a':
case 'A':
snake.changeDirection(3);
break;
case 'd':
case 'D':
snake.changeDirection(4);
break;
}
}
snake.move();
}
bool eatFood() const {
Point head = snake.getHead();
Point foodPos = food.getPosition();
if (head.getX() == foodPos.getX() && head.getY() == foodPos.getY()) {
return true;
}
return false;
}
void gameOver() const {
system("cls");
cout << "Game Over!" << endl;
cout << "Your Score: " << score << endl;
system("pause");
}
void gotoxy(int x, int y) const {
COORD pos = { x, y };
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(console, pos);
}
};
int main() {
Game game(width / 2, height / 2);
game.run();
return 0;
}
```
这个程序使用了三个类:`Point`、`Snake`和`Food`。其中`Point`类表示地图上的一个点,包含`x`和`y`两个坐标;`Snake`类表示蛇,包含蛇身、移动方向等属性和移动、改变方向、检查碰撞等方法;`Food`类表示食物,包含食物的位置和重新生成食物的方法。`Game`类是游戏主类,包含游戏的主循环、绘制地图、绘制蛇、绘制食物、移动蛇、检测碰撞、吃食物、游戏结束等方法。
在`main`函数中创建`Game`对象并调用`run`方法即可启动游戏。这个程序使用了Windows API的一些函数来实现控制台的光标移动和清屏等操作。