C++ヘビ食いゲームを実現(無料付ソースコード)


クラシックヘビ遊び
アップソースコード
#include 
#include 
#include 
#pragma comment(lib,"User32.lib")

using namespace std;

//       
void Pos(int x, int y);
void creatMap();
void initsnake();
int biteself();
void createfood();
void cantcrosswall();
void snakemove();
void pause();
void gamecircle();
void welcometogame();
void endgame();
void gamestart();

#define U 1
#define D 2
#define L 3 
#define R 4 //      U:  D:  L:  R: 

//        
struct snake
{
    int x;
    int y;
    struct snake *next;
};

int score = 0, add = 10; //            。
int status, sleeptime = 200; //          
snake *head, *food; //     ,    
snake *q; //            
int endgamestatus = 0; //         1:    2:     3:      

//       
void Pos(int x, int y)
{
    COORD pos;
    pos.X = x;
    pos.Y = y;
    HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hOutput, pos);
}

//     
void creatMap()
{
    int i;
    for (i = 0; i < 58; i += 2) //       
    {
	Pos(i, 0);
	cout << "■";
	Pos(i, 26);
	cout << "■";
    }
    for (i = 1; i < 26; i++) //       
    {
	Pos(0, i);
	cout << "■";
	Pos(56, i);
	cout << "■";
    }
}

//      
void initsnake()
{
    snake *tail;
    int i;
    tail = (snake*)malloc(sizeof(snake)); //      ,   , x,y       
    tail->x = 24;
    tail->y = 5;
    tail->next = NULL;
    for (i = 1; i <= 4; i++)
    {
        head = (snake*)malloc(sizeof(snake));
	head->next = tail;
	head->x = 24 + 2 * i;
	head->y = 5;
	tail = head;
    }
    while (tail != NULL) //     ,    
    {
	Pos(tail->x, tail->y);
	cout << "■";
	tail = tail->next;
    }
}

//          
int biteself()
{
    snake *self;
    self = head->next;
    while (self != NULL)
    {
	if (self->x == head->x && self->y == head->y)
	{
	    return 1;
	}
	self = self->next;
    }
    return 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 + 3;
    }
    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 = q->next;
    }
    Pos(food_1->x, food_1->y);
    food = food_1;
    cout << "■";
}

//     
void cantcrosswall()
{
    if (head->x == 0 || head->x == 56 || head->y == 0 || head->y == 26)
    {
        endgamestatus = 1;
	endgame();
    }
}

//    : U  D  L  R
void snakemove()
{
    cantcrosswall();
    snake *nexthead;
    nexthead = (snake*)malloc(sizeof(snake));
    if (status == U)
    {
	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 != NULL)
	    {
		Pos(q->x, q->y);
		cout << "■";
		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);
		cout << "■";
		q = q->next;
	    }
	    Pos(q->next->x, q->next->y);
	    cout << "  ";
	    free(q->next);
	    q->next = NULL;
	}
    }
    if (status == D)
    {
	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 != NULL)
	    {
		Pos(q->x, q->y);
		cout << "■";
		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);
		cout << "■";
		q = q->next;
	    }
	    Pos(q->next->x, q->next->y);
	    cout << "  ";
	    free(q->next);
	    q->next = NULL;
	}
    }
    if (status == L)
    {
        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 != NULL)
	    {
		Pos(q->x, q->y);
		cout << "■";
		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);
		cout << "■";
		q = q->next;
	    }
	    Pos(q->next->x, q->next->y);
	    cout << "  ";
	    free(q->next);
	    q->next = NULL;
        }
    }
    if (status == R)
    {
	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 != NULL)
	    {
		Pos(q->x, q->y);
		cout << "■";
		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);
		cout << "■";
		q = q->next;
	    }
	    Pos(q->next->x, q->next->y);
	    cout << "  ";
	    free(q->next);
	    q->next = NULL;
	}
    }
    if (biteself() == 1) //          
    {
	endgamestatus = 2;
	endgame();
    }
}

//   
void pause()
{
    while (1)
    {
	Sleep(300);
	if (GetAsyncKeyState(VK_SPACE))
	{
	    break;
	}
    }
}

//      
void gamecircle()       
{
    Pos(64, 15);
    cout << "    ,      " << endl;
    Pos(64, 16);
    cout << " ↑.↓.←.→        !";
    Pos(64, 17);
    cout << "F1    ,F2    " << endl;
    Pos(64, 18);
    cout << "ESC :    .space:    !";
    status = R;
    while (1)
    {
	Pos(64, 10);
	cout  << "  :" << endl << score;
	Pos(64, 11);
	cout << "      :" << endl << add;
	if (GetAsyncKeyState(VK_UP) && status != D)
	{
	    status = U;
	}
	else if (GetAsyncKeyState(VK_DOWN) && status != U)
	{
	    status = D;
	}
	else if (GetAsyncKeyState(VK_LEFT) && status != R)
	{
	    status = L;
	}
	else if (GetAsyncKeyState(VK_RIGHT) && status != L)
	{
            status = R;
	}
	else if (GetAsyncKeyState(VK_SPACE))
	{
	    pause();
	}
	else if (GetAsyncKeyState(VK_ESCAPE))
	{
	    endgamestatus = 3;
	    break;
	}
	else if (GetAsyncKeyState(VK_F1))
	{
	    if (sleeptime >= 50)
	    {
		sleeptime = sleeptime - 30;
		add = add + 2;
		if (sleeptime == 320)
		{
		    add = 2; //     1        
		}
	    }
	}
	else if (GetAsyncKeyState(VK_F2))
	{
	    if (sleeptime < 350)
	    {
		sleeptime = sleeptime + 30;
		add = add - 2;
		if (sleeptime == 350)
		{
		    add = 1; //       1
		}
	    }
	}
	Sleep(sleeptime);
	snakemove();
    }
}

//     
void welcometogame()
{
    Pos(40, 12);
    cout << "         !";
    Pos(40, 25);
    cout << "Programed By HSF!" << endl;
    system("pause");
    system("cls");
    Pos(25, 12);
    cout << " ↑.↓.←.→        , F1    ,2    " << endl;;
    Pos(25, 13);
    cout << "           。";
    system("pause");
    system("cls");
}

//     
void endgame()
{
    system("cls");
    Pos(24, 12);
    if (endgamestatus == 1)
    {
	cout << "   ,     。    !" << endl;
    }
    else if (endgamestatus == 2)
    {
	cout << "   ,      。    !" << endl;
    }
    else if (endgamestatus == 3)
    {
	cout << "        ";
    }
    Pos(24, 13);
    cout << "     :" << endl << score;
    exit(0);
}

//      
void gamestart()
{
    system("mode con cols=100 lines=30");
    welcometogame();
    creatMap();
    initsnake();
    createfood();
}

void main()
{
    gamestart();
    gamecircle();
    endgame();
}