PDA

View Full Version : حرفه ای: مشکل در کد پروژه مار ( حرکت دادن مار )



aali361
پنج شنبه 27 آذر 1393, 21:58 عصر
سلام من برای پروژه پایان ترم دارم رو بازی مار کار میکنم . تا یه جاهایی پیش رفتم اما مشکل ام جایی هست که مار سیبی رو میخوره هست .
با ارایه یک بعدی هم نوشتم . فرض کنید طول اولیه مارمون یک هست بعد وقتی سیب میخوره طولش دو میشه . حال وقتی کاربر میخواد تغییر مسیر بده مثلا شیفن به پایین بده سر مار باید به سمت پایین بره و دمش جایی که سرش قرار داشته بیاد .
در واقع تو حرکت کردن مار باید سرش شیف به یکی از جهت ها بده و دمش به جایی که سرش بوده بره .چطور این کارو رو کنم ؟

#include <iostream>#include <time.h>
#include <conio.h>
using namespace std;
const int height = 20, width = 20;
int plane[height*width] = {}, direction=0, lenght = 1, x, y;
bool game = true;
void setbackground()
{
for (int i = 0; i < width; i++)
{
plane[i] = 1;
plane[(height - 1)*width + i] = 1;
}
for (int i = 1; i < height - 1; i++)
{
plane[i*width] = 2;
plane[i*width + width - 1] = 2;
}
}
void show()
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (plane[i*width + j] == 0)
cout << ' ';
if (plane[i*width + j] == 1)
cout << '-';
if (plane[i*width + j] == 2)
cout << '|';
if (plane[i*width + j] == 3)
cout << '*';
if (plane[i*width + j] == 4)
cout << '$';
if (plane[i*width + j] == 5)
cout << 'o';
}
cout << endl;
}
}
void randompoint()
{
int m, n;
x = (width / 2);
y = (height / 2);
plane[x+y*height] = 3;
do
{
m = rand() % height;
n = rand() % width;
} while (!plane[m*n] == 0);
plane[m*n] = 4;
}
void move()
{
if (direction == 0)
{
int newx = x;
int newy = y + 1;
plane[x*width + y] = 0;
if (plane[newx*width + newy] == 1 || plane[newx*width + newy] == 2 || plane[newx*width + newy] == 3)
game = false;
if (plane[newx*width + newy] == 4)
{
lenght++;
}
plane[newx*width + newy] = 3;
x = newx;
y = newy;
}
if (direction == 1)
{
int newx = x + 1;
int newy = y;
plane[x*width + y] = 0;
if (plane[newx*width + newy] == 1 || plane[newx*width + newy] == 2 || plane[newx*width + newy] == 3)
game = false;
if (plane[newx*width + newy] == 4)
{
lenght++;
plane[x*width + y] = 3;
}
plane[newx*width + newy] = 3;
x = newx;
y = newy;
}
if (direction == 2)
{
int newx = x;
int newy = y - 1;
plane[x*width + y] = 0;
if (plane[newx*width + newy] == 1 || plane[newx*width + newy] == 2 || plane[newx*width + newy] == 3)
game = false;
if (plane[newx*width + newy] == 4)
{
lenght++;
plane[x*width + y] = 3;
}
plane[newx*width + newy] = 3;
x = newx;
y = newy;
}
if (direction == 3)
{
int newx = x - 1;
int newy = y;
plane[x*width + y] = 0;
if (plane[newx*width + newy] == 1 || plane[newx*width + newy] == 2 || plane[newx*width + newy] == 3)
game = false;
if (plane[newx*width + newy] == 4)
{
lenght++;
plane[x*width + y] = 3;
}
plane[newx*width + newy] = 3;
x = newx;
y = newy;
}
}
void main()
{
srand(time(0));// استفاده برای اینکه تابع رندوم در هر بار کامپایل مقدار یکسانی ندهد
setbackground();
randompoint();
while (game)
{
if (kbhit())
{
char c = getch();
switch (c)
{
case 'w':
direction = 3;
break;
case'd':
direction = 0;
break;
case's':
direction = 1;
break;
case'a':
direction = 2;
break;
}


}
move();
show();
_sleep(400);
system("cls");
}
system("cls");
}