吃到食物后蛇身o怎么跟着蛇移动
时间: 2024-03-21 13:43:09 浏览: 12
在贪吃蛇游戏中,当蛇吃到食物时,需要将蛇的长度加1,并在蛇尾添加一个新的节点。在代码中,我们可以在判断蛇头是否与食物位置重合时,增加蛇的长度。此外,需要在移动蛇身的逻辑中,将新添加的节点坐标也更新到tailX和tailY数组中。具体来说,可以在移动蛇身的循环中,将新增的代码插入到`prevX`和`prevY`的赋值语句前面,如下所示:
```
for (int i = 1; i < nTail; i++) { //蛇的移动
prev2X = tailX[i];//后续节点坐标依次更新为前一个节点坐标
prev2Y = tailY[i];
tailX[i] = prevX;
tailY[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
// 如果蛇头与食物位置重合,增加蛇的长度
if (tailX[0] == fruitX && tailY[0] == fruitY) {
nTail++;
tailX[nTail - 1] = prev2X;
tailY[nTail - 1] = prev2Y;
fruitX = rand() % width;
fruitY = rand() % height;
score += 10;
}
}
```
这样,当蛇头吃到食物时,就会增加蛇的长度,并且新的节点会跟随在蛇身后面移动。
相关问题
用C++写代码实现贪吃蛇算法 程序要求: 游戏时,一条蛇在密闭的围墙内, 围墙内会随机出现一个食物通过键盘上的4个光标键控制蛇向上下左右4个方向移动,蛇头接到食物,则表示食物被蛇吃掉,这时蛇的身体加长一节,同时计10分。接着又出现食物等待被蛇吃掉。如果蛇在移动过程中,撞到墙壁或身体交叉(蛇头撞到自己的身体),则游戏结束。
好的,下面是 C++ 实现贪吃蛇算法的代码:
```cpp
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
const int width = 20; // 地图宽度
const int height = 20; // 地图高度
int x, y; // 蛇头坐标
int fx, fy; // 食物坐标
int score; // 得分
int tailx[100], taily[100]; // 蛇身坐标
int nTail; // 蛇身长度
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN }; // 移动方向
eDirection dir;
void Setup() {
dir = STOP;
x = width / 2;
y = height / 2;
fx = rand() % width;
fy = rand() % height;
score = 0;
}
void Draw() {
system("cls"); // 清屏
for (int i = 0; i < width + 2; i++)
cout << "#";
cout << endl;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (j == 0)
cout << "#";
if (i == y && j == x)
cout << "O"; // 蛇头
else if (i == fy && j == fx)
cout << "F"; // 食物
else {
bool print = false;
for (int k = 0; k < nTail; k++) {
if (tailx[k] == j && taily[k] == i) {
cout << "o"; // 蛇身
print = true;
}
}
if (!print)
cout << " ";
}
if (j == width - 1)
cout << "#";
}
cout << endl;
}
for (int i = 0; i < width + 2; i++)
cout << "#";
cout << endl;
cout << "Score: " << score << endl;
}
void Input() {
if (_kbhit()) {
switch (_getch()) {
case 'a':
dir = LEFT;
break;
case 'd':
dir = RIGHT;
break;
case 'w':
dir = UP;
break;
case 's':
dir = DOWN;
break;
case 'x':
dir = STOP;
break;
}
}
}
void Logic() {
int prevx = tailx[0];
int prevy = taily[0];
int prev2x, prev2y;
tailx[0] = x;
taily[0] = y;
for (int i = 1; i < nTail; i++) {
prev2x = tailx[i];
prev2y = taily[i];
tailx[i] = prevx;
taily[i] = prevy;
prevx = prev2x;
prevy = prev2y;
}
switch (dir) {
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;
default:
break;
}
if (x >= width) x = 0;
else if (x < 0) x = width - 1;
if (y >= height) y = 0;
else if (y < 0) y = height - 1;
for (int i = 0; i < nTail; i++) {
if (tailx[i] == x && taily[i] == y)
dir = STOP;
}
if (x == fx && y == fy) {
score += 10;
fx = rand() % width;
fy = rand() % height;
nTail++;
}
}
int main() {
Setup();
while (dir != STOP) {
Draw();
Input();
Logic();
Sleep(50); // 控制移动速度
}
return 0;
}
```
以上就是 C++ 实现贪吃蛇算法的代码。
程序要求: 游戏时,一条蛇在密闭的围墙内, 围墙内会随机出现一个食物通过键盘上的4个光标键控制蛇向上下左右4个方向移动,蛇头接到食物,则表示食物被蛇吃掉,这时蛇的身体加长一节,同时计10分。接着又出现食物等待被蛇吃掉。如果蛇在移动过程中,撞到墙壁或身体交叉(蛇头撞到自己的身体),则游戏结束。c加加编写程序
以下是一个基本的C++实现:
```cpp
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <time.h>
using namespace std;
const int width = 20; // 游戏区域宽度
const int height = 20; // 游戏区域高度
int x, y; // 蛇头坐标
int foodX, foodY; // 食物坐标
int score; // 得分
int tailX[100], tailY[100]; // 蛇身坐标
int nTail; // 蛇身长度
enum direction { STOP = 0, LEFT, RIGHT, UP, DOWN }; // 方向
direction dir;
bool gameOver;
void Setup()
{
srand(time(NULL)); // 随机数种子
gameOver = false;
dir = STOP;
x = width / 2;
y = height / 2;
foodX = rand() % width;
foodY = rand() % height;
score = 0;
}
void Draw()
{
system("cls"); // 清屏
// 画上边框
for (int i = 0; i < width + 2; i++)
cout << "#";
cout << endl;
// 画游戏区域
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (j == 0)
cout << "#";
if (i == y && j == x)
cout << "O"; // 蛇头
else if (i == foodY && j == foodX)
cout << "F"; // 食物
else
{
bool print = false;
for (int k = 0; k < nTail; k++)
{
if (tailX[k] == j && tailY[k] == i)
{
cout << "o"; // 蛇身
print = true;
}
}
if (!print)
cout << " ";
}
if (j == width - 1)
cout << "#";
}
cout << endl;
}
// 画下边框
for (int i = 0; i < width + 2; i++)
cout << "#";
cout << endl;
// 显示得分
cout << "Score: " << score << endl;
}
void Input()
{
if (_kbhit()) // 检查是否有键盘输入
{
switch (_getch()) // 获取输入的键盘码
{
case 'a':
dir = LEFT;
break;
case 'd':
dir = RIGHT;
break;
case 'w':
dir = UP;
break;
case 's':
dir = DOWN;
break;
case 'x':
gameOver = true;
break;
}
}
}
void Logic()
{
// 更新蛇身坐标
int prevX = tailX[0];
int prevY = tailY[0];
int prev2X, prev2Y;
tailX[0] = x;
tailY[0] = y;
for (int i = 1; i < nTail; i++)
{
prev2X = tailX[i];
prev2Y = tailY[i];
tailX[i] = prevX;
tailY[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
}
// 根据方向移动蛇头
switch (dir)
{
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;
}
// 检查蛇头是否撞到墙或身体
if (x < 0 || x >= width || y < 0 || y >= height)
gameOver = true;
for (int i = 0; i < nTail; i++)
{
if (tailX[i] == x && tailY[i] == y)
gameOver = true;
}
// 检查蛇头是否吃到食物
if (x == foodX && y == foodY)
{
score += 10;
foodX = rand() % width;
foodY = rand() % height;
nTail++;
}
}
int main()
{
Setup();
while (!gameOver)
{
Draw();
Input();
Logic();
Sleep(100); // 延迟100毫秒
}
return 0;
}
```
注意,这只是一个基本的实现,还有很多可以改进的地方,例如添加难度级别、增加音效等。