c++控制檯小程序——貪吃蛇(新手簡易版)

先上一下效果圖

歡迎界面

在這裏插入圖片描述

遊戲界面

在這裏插入圖片描述

結束界面

在這裏插入圖片描述

大致思路

1.遊戲分爲四個部分:場景,蛇,食物和交互

2.打印整個地圖以及提示(完成場景)

3.食物需要的函數:打印食物,改變食物座標

4.蛇需要的函數:初始位置,移動,改變方向(鍵盤監聽)

5.交互函數:蛇吃食物(加長,改變食物座標),遊戲結束(蛇撞牆,碰到自己)

移動通過刪除尾巴,改變頭座標實現。加長則不刪除尾巴。

完整代碼(有詳細註釋)

#include <iostream>
#include <windows.h>
#include <cstdlib>   //定義雜項函數及內存分配函數
#include <cstdio>   //定義輸入/輸出函數
#include <string>   //字符串處理
#include <conio.h>  //主要是一些用戶通過按鍵盤產生的對應操作
#include <ctime>   //定義關於時間的函數

using namespace std;

int snake[1000][2];  //蛇每一節x,y座標
int length = 3;
int food[2] = {20,15}; //食物x,y座標
int a = 1;  //x座標變化
int b = 0;  //y座標變化
int s = 1;  //主函數循環
int speed = 500;  //間隔時間(速度)
int score = 0;
int level = 1;

void start();
void hide();
void gotoxy(short x, short y);
void drawWall();
void drawSnake();
void changeSnake();
void drawFood();
void changeDirection();
void eatFood();
void clear();
void gameOver();

int main()
{
	start();
	hide();
	drawWall();
	drawSnake();
	drawFood();
	while(s)
	{
		changeDirection();
		changeSnake();
		eatFood();
		gameOver();
	}
	clear();
	return 0;
}

void hide()   //隱藏光標
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO CursorInfo;
	GetConsoleCursorInfo(handle, &CursorInfo);//獲取控制檯光標信息
	CursorInfo.bVisible = false; //隱藏控制檯光標
	SetConsoleCursorInfo(handle, &CursorInfo);
}

void gotoxy(short x, short y)  //變換座標
{
    COORD position = { x, y };
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hOut, position);
}

void start()   //歡迎界面
{
	gotoxy(20, 10);
    cout << "welcome to snake";
	gotoxy(20, 20);
    cout << "Press enter to start the game.";
	cin.get();
	system("cls");
}

void drawWall()  //初始化打印邊界和提示
{
    for (int i=1;i<=20;i++)
    {
        gotoxy(0, i);
        cout << "#";
        gotoxy(40, i);
        cout << "#";
    }
	for (int l=0;l<=40;l++)
    {
		gotoxy(l, 0);
        cout << "#";
        gotoxy(l, 20);
        cout << "#";
    }
    gotoxy(50, 10);
    cout << "press w,s,a,d to change direction.";
	gotoxy(50,14);
	cout << "score: " << score << endl;
	gotoxy(50,16);
	cout << "level: " << level;
	gotoxy(50, 18);
    cout << "press Space to stop ,press Enter to continue";
}

void drawSnake()  //初始化蛇
{
	snake[0][0] = 10;
    snake[0][1] = 10;
	snake[1][0] = 9;
    snake[1][1] = 10;
	snake[2][0] = 8;
    snake[2][1] = 10;
	gotoxy(snake[0][0],snake[0][1]);
	cout << "*";
    gotoxy(snake[1][0], snake[1][1]);
    cout << "+";
	gotoxy(snake[2][0], snake[2][1]);
    cout << "+";
}

void drawFood()  //打印食物
{
	gotoxy(food[0], food[1]);
	cout <<"@";
}
void changeFood()  //改變食物座標
{
	int f = 1;
	while(f)
	{
		srand((int)time(0));
		food[0] = rand() % 39 + 1;
		srand((int)time(0));
		food[1] = rand() % 19 + 1;
		for (int i = length-1; i > 0; i--)
		{
			if(snake[i][0] == food[0] && snake[i][1] == food[1])
			{
				f = 1;
				break;
			}
			else
				f = 0;
		}
	}
	drawFood();
}

void eatFood()  //蛇吃食物
{
	if(snake[0][0] == food[0] && snake[0][1] == food[1])
	{
		gotoxy(snake[length-1][0],snake[length-1][1]);  //加分
		cout << "+";
		length += 1;
		score += 1;
		gotoxy(50,14);
		cout << "score: " << score << endl;
		if(score!= 0 && score%5 == 0)  //加等級
		{
			level += 1;
			if(speed !=100)
				speed -= 100;
			gotoxy(50,16);
			cout << "level: " << level;
		}
		changeFood();
	}
	gotoxy(0,0);
	cout << "#";
}

void changeDirection()  //鍵盤控制改變行進方向
{
	if (kbhit()!=0)
    {
        char ch = getch();
        switch (ch)
        {
        case 'w':
		case 'W':
			if(b == 0)  //防止反方向改變,下同
			{
				a = 0;
				b = -1;
			}
            break;
        case 's':
		case 'S':
			if(b == 0)
			{
				a = 0;
				b = 1;
			}
            break;
        case 'a':
		case 'A':
			if(a == 0)
			{
				a = -1;
				b = 0;
			}
            break;
        case 'd':
		case 'D':
			if(a == 0)
			{
				a = 1;
				b = 0;
			}
            break;
        case ' ':
            cin.get();
            break;
        default:
            break;
        }
   }
}

void changeSnake()  //改變蛇位置
{
	gotoxy(snake[length-1][0],snake[length-1][1]);
	cout << " ";  //清除尾巴
	for (int i = length-1; i > 0; i--)  //改變身體座標
	{
		for (int j = 0; j < 2; j++)
		{
			snake[i][j] = snake[i - 1][j];
		}
	}
	snake[0][0] += a;
	snake[0][1] += b;  //改變蛇頭座標
	gotoxy(snake[0][0],snake[0][1]);
	cout << "*";
	gotoxy(snake[1][0],snake[1][1]);
	cout << "+";
	Sleep(speed);
}

void gameOver()  //遊戲結束
{
	if(snake[0][0] == 0 || snake[0][0] == 40 || snake[0][1] == 0 || snake[0][1] == 20)  //撞牆
	{
		s = 0;
		system("cls");
	}
	for (int i = length-1; i > 0; i--)  //碰到自己
	{
		if(snake[0][0] == snake[i][0] && snake[0][1] == snake[i][1])
		{
		s = 0;
		system("cls");
		}
	}
}

void clear()
{
	gotoxy(20, 10);
	cout << "Game over!!!" << endl;
	gotoxy(20, 12);
	cout << "your score: " << score << endl;
	gotoxy(20, 14);
	cout << "Welcome to play again, press Enter to continue";
	gotoxy(20, 20);
	cin.get();
}

歡迎指正

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章