void MainWindow::moveOne(QPoint p)
{
    if(is_in(p) == false){
        gameover();
        return;
    }
    bool flag = false;
    if(map[p.x()][p.y()] == food) flag = true;
    map[p.x()][p.y()] = snake_body;
    que.push_back(QPoint(p.x(), p.y()));
    if(flag == false){
        p = que.front();
        map[p.x()][p.y()] = none;
        que.pop_front();
    }else{
        createfood();
    }
    auto iter = que.begin() + 1;
    for(; iter != que.end(); ++iter)
    {
        map[iter -> x()][iter -> y()] = snake_body;
    }
    iter = que.end() - 1;
    map[iter -> x()][iter -> y()] = snake_head;
    iter = que.begin();
    map[iter -> x()][iter -> y()] = snake_foot;
}
Exemple #2
0
void createfood()

{

	snake *food_1;

	srand((unsigned)time(NULL));

	food_1 = (snake*)malloc(sizeof(snake));

	while ((food_1->x % 2) != 0)    //保证其为偶数,使得食物能与蛇头对其

	{

		food_1->x = rand() % 52 + 2;

	}

	food_1->y = rand() % 24 + 1;

	q = head;

	while (q->next !=NULL)

	{

		if (q->x == food_1->x && q->y == food_1->y) //判断蛇身是否与食物重合

		{

			free(food_1);

			createfood();
			q = head;

		}

		q = q->next;

	}

	Pos(food_1->x, food_1->y);

	food = food_1;
	SetConsoleTextAttribute(Handle, FOREGROUND_INTENSITY | FOREGROUND_RED |FOREGROUND_BLUE);
	printf("■");
	SetConsoleTextAttribute(Handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);

}
Exemple #3
0
void gamestart()//游戏初始化

{

	system("mode con cols=100 lines=30");

	welcometogame();

	creatMap();

	initsnake();

	createfood();

}
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),timer(new QTimer(this))
{
    srand(unsigned (time(nullptr)));
    resize(640, 480);
    memset(map, none, sizeof(map));
    connect(timer, SIGNAL(timeout()), this, SLOT(move()));

    map[2][0] = snake_head;
    map[1][0] = snake_body;
    map[0][0] = snake_foot;
    createfood();

    timer->setInterval(150);
    timer->start();

    que.push_back(QPoint(0, 0));
    que.push_back(QPoint(1, 0));
    que.push_back(QPoint(2, 0));
}
Exemple #5
0
void snakemove()
{
	snake *nexthead;
	cantcrosswall();
	nexthead = (snake*)malloc(sizeof(snake));
	if (status == U)
	{
		SetConsoleTextAttribute(Handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
		nexthead->x = head->x;
		nexthead->y = head->y - 1; 
		if (nexthead->x == food->x&&nexthead->y == food->y)
		{
			nexthead->next = head;
			head = nexthead;
			q = head;
			Pos(q->x, q->y);
			printf("■");
			score = score + add;
			createfood();
		}
		else
		{
			nexthead->next = head;
			head = nexthead;
			q = head;
			while (q->next->next != NULL)
			{
				Pos(q->x, q->y);
				printf("■");
				q = q->next;
			}
			Pos(q->next->x, q->next->y);
			SetConsoleTextAttribute(Handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN);
			printf("■");
			SetConsoleTextAttribute(Handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
			free(q->next);
			q->next = NULL;
		}
	}
	if (status== D)
	{
		SetConsoleTextAttribute(Handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
		nexthead->x = head->x;
		nexthead->y = head->y + 1;
		if (nexthead->x == food->x&&nexthead->y == food->y)
		{
			nexthead->next = head;
			head = nexthead;
			q = head;
			while (q != head)
			{
				Pos(q->x, q->y);
				//SetConsoleTextAttribute(Handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE );
				printf("■");
				q = q->next;
			}
			score = score + add;
			createfood();
		}
		else
		{
			nexthead->next = head;
			head = nexthead;
			q = head;
			while (q->next->next != NULL)
			{
				Pos(q->x, q->y);
				printf("■");
				q = q->next;
			}
			Pos(q->next->x, q->next->y);
			SetConsoleTextAttribute(Handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN);
			printf("■");
			SetConsoleTextAttribute(Handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
			free(q->next);
			q->next = NULL;
		}
	}
	if (status == L)
	{
		SetConsoleTextAttribute(Handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE );
		nexthead->x = head->x-2;
		nexthead->y = head->y;
		if (nexthead->x == food->x&&nexthead->y == food->y)
		{
			nexthead->next = head;
			head = nexthead;
			q = head;
			while (q != head)
			{
				Pos(q->x, q->y);
				printf("■");
				q = q->next;
			}
			score = score + add;
			createfood();
		}
		else
		{
			nexthead->next = head;
			head = nexthead;
			q = head;
			while (q->next->next != NULL)
			{
				Pos(q->x, q->y);
				printf("■");
				q = q->next;
			}
			Pos(q->next->x, q->next->y);
			SetConsoleTextAttribute(Handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN);
			printf("■");
			SetConsoleTextAttribute(Handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
			free(q->next);
			q->next = NULL;
		}
	}
	if (status == R)
	{
		SetConsoleTextAttribute(Handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
		nexthead->x = head->x+2;
		nexthead->y = head->y;
		if (nexthead->x == food->x&&nexthead->y == food->y)
		{
			nexthead->next = head;
			head = nexthead;
			q = head;
			while (q != head)
			{
				Pos(q->x, q->y);
				printf("■");
				q = q->next;
			}
			score = score + add;
			createfood();
		}
		else
		{
			nexthead->next = head;
			head = nexthead;
			q = head;
			while (q->next->next != NULL)
			{
				Pos(q->x, q->y);
				printf("■");
				q = q->next;
			}
			Pos(q->next->x, q->next->y);
			SetConsoleTextAttribute(Handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN);
			printf("■");
			SetConsoleTextAttribute(Handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
			free(q->next);
			q->next = NULL;
		}
	}
	if (biteself() == 1)
	{
		endgamestatus = 1;
		endgame();
	}
}