用c++语言写贪吃蛇游戏,已经用vector创建2维数组,则对象蛇,含有哪些属性,如何构造蛇身,
时间: 2024-04-05 18:34:19 浏览: 13
蛇的属性可以包括:
1. 蛇身的长度
2. 蛇的方向(向上、向下、向左、向右)
3. 蛇头的位置
4. 蛇身的位置
构造蛇身可以使用一个 vector 来存储蛇的身体,其中每个元素表示一个蛇节的位置,这个位置可以使用一个二维数组中的坐标来表示。一开始,蛇身可以只包含一节,即蛇头,然后在游戏开始后,每当蛇吃到食物后,就可以在蛇尾添加一个蛇节,使得蛇身长度加一。添加蛇节的方法可以是在 vector 的末尾添加一个新元素,表示新的蛇节的位置。新蛇节的位置可以根据蛇身的方向和蛇尾的位置来计算得出。
相关问题
用c++语言面对对象思想,再用vector构建二维数组,写出一个贪吃蛇代码。其中食物类构成,代码
好的,我会尽力回答你的问题。以下是基于C++语言和面向对象思想的贪吃蛇代码,其中使用了vector构建二维数组,同时实现了食物类。
```c++
#include <iostream>
#include <vector>
#include <conio.h>
#include <windows.h>
using namespace std;
// 坐标类
class Coordinate {
public:
int x;
int y;
Coordinate(int _x = 0, int _y = 0) {
x = _x;
y = _y;
}
};
// 食物类
class Food {
public:
Coordinate coordinate;
Food() {
srand((unsigned)time(NULL));
coordinate.x = rand() % 20;
coordinate.y = rand() % 20;
}
void generateFood() {
srand((unsigned)time(NULL));
coordinate.x = rand() % 20;
coordinate.y = rand() % 20;
}
};
// 贪吃蛇类
class Snake {
public:
vector<Coordinate> body;
Coordinate direction;
int length;
Snake() {
length = 3;
direction.x = 1;
direction.y = 0;
body.push_back(Coordinate(0, 0));
body.push_back(Coordinate(1, 0));
body.push_back(Coordinate(2, 0));
}
void move() {
// 计算蛇头新的位置
Coordinate newHead = body[length - 1];
newHead.x += direction.x;
newHead.y += direction.y;
// 判断蛇头是否与食物重合
bool isEatFood = (newHead.x == food.coordinate.x && newHead.y == food.coordinate.y);
// 将蛇头插入到身体最后面
body.push_back(newHead);
// 判断是否吃到了食物
if (!isEatFood) {
// 如果没有吃到食物,将蛇尾移除
body.erase(body.begin());
}
else {
// 如果吃到了食物,重新生成一个食物
food.generateFood();
length++;
}
}
bool isDead() {
// 判断蛇头是否超过边界
if (body[length - 1].x < 0 || body[length - 1].x >= 20 || body[length - 1].y < 0 || body[length - 1].y >= 20) {
return true;
}
// 判断蛇头是否与身体重合
for (int i = 0; i < length - 1; i++) {
if (body[i].x == body[length - 1].x && body[i].y == body[length - 1].y) {
return true;
}
}
return false;
}
private:
Food food;
};
// 游戏类
class Game {
public:
void start() {
while (true) {
// 清空屏幕
system("cls");
// 输出地图
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
bool isSnake = false;
// 判断该位置是否为蛇身
for (int k = 0; k < snake.length; k++) {
if (snake.body[k].x == j && snake.body[k].y == i) {
isSnake = true;
break;
}
}
// 输出地图
if (isSnake) {
cout << "■";
}
else if (food.coordinate.x == j && food.coordinate.y == i) {
cout << "★";
}
else {
cout << "□";
}
}
cout << endl;
}
// 判断是否死亡
if (snake.isDead()) {
cout << "Game Over" << endl;
break;
}
// 接收用户输入
if (_kbhit()) {
switch (_getch()) {
case 'w':
if (snake.direction.y != 1) {
snake.direction.x = 0;
snake.direction.y = -1;
}
break;
case 's':
if (snake.direction.y != -1) {
snake.direction.x = 0;
snake.direction.y = 1;
}
break;
case 'a':
if (snake.direction.x != 1) {
snake.direction.x = -1;
snake.direction.y = 0;
}
break;
case 'd':
if (snake.direction.x != -1) {
snake.direction.x = 1;
snake.direction.y = 0;
}
break;
}
}
// 移动蛇
snake.move();
// 延时
Sleep(100);
}
}
private:
Snake snake;
Food food;
};
int main() {
Game game;
game.start();
return 0;
}
```
其中,贪吃蛇类包含了蛇的身体、移动方向和长度等信息,食物类包含了食物的位置信息,游戏类包含了游戏的开始、结束和循环等逻辑。整个游戏的流程如下:
1. 初始化贪吃蛇和食物,输出地图;
2. 接收用户输入,移动蛇;
3. 判断是否死亡,如果死亡则结束游戏,否则继续循环;
4. 延时一段时间,再次循环。
希望这个代码能够帮助到你!
c++ vector创建二维数组
回答: 在C++中,可以使用vector来创建二维数组。下面是几种不同的方法:
方法一:使用vector嵌套vector的方式创建二维数组。可以先定义一个vector,然后在内层vector中定义大小。例如:
```
#include <iostream>
#include <vector>
using namespace std;
int main() {
int m = 2; // 行数
int n = 3; // 列数
vector<vector<int>> vec(m); // 创建m行的vector
for (int i = 0; i < m; i++) {
vec[i].resize(n); // 给内层vector定义大小
}
// 赋值
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
vec[i][j] = j + 1;
}
}
// 输出
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cout << vec[i][j] << " ";
}
cout << endl;
}
return 0;
}
```
方法二:使用resize函数来创建二维数组。可以先定义一个vector,然后使用resize函数来指定行数和列数。例如:
```
#include <iostream>
#include <vector>
using namespace std;
int main() {
int m = 2; // 行数
int n = 3; // 列数
vector<vector<int>> vec;
vec.resize(m, vector<int>(n)); // 创建m行n列的二维vector
// 赋值
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
vec[i][j] = j + 1;
}
}
// 输出
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cout << vec[i][j] << " ";
}
cout << endl;
}
return 0;
}
```
这些方法都可以创建一个二维数组,并进行赋值和输出。希望对你有帮助![2]