贪吃蛇双人对战c++代码
时间: 2023-12-09 14:02:35 浏览: 320
MFC贪吃蛇 人机对战 人人对战
4星 · 用户满意度95%
以下是基于C++语言开发的贪吃蛇游戏的双人对战版源码的部分代码,仅供参考:
```c++
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <ctime>
#include <cstdlib>
using namespace std;
bool gameOver;
const int width = 20;
const int height = 20;
int x1, y1, x2, y2, fruitX, fruitY, score1, score2;
int tail1X[100], tail1Y[100], tail2X[100], tail2Y[100];
int nTail1, nTail2;
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
eDirection dir1, dir2;
void Setup()
{
gameOver = false;
dir1 = STOP;
dir2 = STOP;
x1 = width / 4;
y1 = height / 2;
x2 = 3 * width / 4;
y2 = height / 2;
fruitX = rand() % width;
fruitY = rand() % height;
score1 = 0;
score2 = 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 == y1 && j == x1)
cout << "1";
else if (i == y2 && j == x2)
cout << "2";
else if (i == fruitY && j == fruitX)
cout << "F";
else
{
bool print = false;
for (int k = 0; k < nTail1; k++)
{
if (tail1X[k] == j && tail1Y[k] == i)
{
cout << "o";
print = true;
}
}
for (int k = 0; k < nTail2; k++)
{
if (tail2X[k] == j && tail2Y[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 << "Player 1: " << score1 << endl;
cout << "Player 2: " << score2 << endl;
}
void Input()
{
if (_kbhit())
{
switch (_getch())
{
case 'a':
dir1 = LEFT;
break;
case 'd':
dir1 = RIGHT;
break;
case 'w':
dir1 = UP;
break;
case 's':
dir1 = DOWN;
break;
case 'j':
dir2 = LEFT;
break;
case 'l':
dir2 = RIGHT;
break;
case 'i':
dir2 = UP;
break;
case 'k':
dir2 = DOWN;
break;
case 'x':
gameOver = true;
break;
}
}
}
void Logic()
{
int prevX1 = tail1X[0];
int prevY1 = tail1Y[0];
int prevX2 = tail2X[0];
int prevY2 = tail2Y[0];
int prev2X1, prev2Y1, prev2X2, prev2Y2;
tail1X[0] = x1;
tail1Y[0] = y1;
tail2X[0] = x2;
tail2Y[0] = y2;
for (int i = 1; i < nTail1; i++)
{
prev2X1 = tail1X[i];
prev2Y1 = tail1Y[i];
tail1X[i] = prevX1;
tail1Y[i] = prevY1;
prevX1 = prev2X1;
prevY1 = prev2Y1;
}
for (int i = 1; i < nTail2; i++)
{
prev2X2 = tail2X[i];
prev2Y2 = tail2Y[i];
tail2X[i] = prevX2;
tail2Y[i] = prevY2;
prevX2 = prev2X2;
prevY2 = prev2Y2;
}
switch (dir1)
{
case LEFT:
x1--;
break;
case RIGHT:
x1++;
break;
case UP:
y1--;
break;
case DOWN:
y1++;
break;
default:
break;
}
switch (dir2)
{
case LEFT:
x2--;
break;
case RIGHT:
x2++;
break;
case UP:
y2--;
break;
case DOWN:
y2++;
break;
default:
break;
}
if (x1 >= width) x1 = 0; else if (x1 < 0) x1 = width - 1;
if (y1 >= height) y1 = 0; else if (y1 < 0) y1 = height - 1;
if (x2 >= width) x2 = 0; else if (x2 < 0) x2 = width - 1;
if (y2 >= height) y2 = 0; else if (y2 < 0) y2 = height - 1;
for (int i = 0; i < nTail1; i++)
if (tail1X[i] == x1 && tail1Y[i] == y1)
gameOver = true;
for (int i = 0; i < nTail2; i++)
if (tail2X[i] == x2 && tail2Y[i] == y2)
gameOver = true;
if (x1 == fruitX && y1 == fruitY)
{
score1 += 10;
fruitX = rand() % width;
fruitY = rand() % height;
nTail1++;
}
if (x2 == fruitX && y2 == fruitY)
{
score2 += 10;
fruitX = rand() % width;
fruitY = rand() % height;
nTail2++;
}
}
int main()
{
Setup();
while (!gameOver)
{
Draw();
Input();
Logic();
Sleep(50);
}
return 0;
}
```
阅读全文