C-控制台-贪吃蛇

C-控制台-贪吃蛇

  • 效果图
  • core.h
  • core.cpp
  • main.cpp

效果图

C-控制台-贪吃蛇_第1张图片

core.h

#pragma once
#include 
#include 
#include 

void initMap();
void showMap();
void clearMap();
int createSnake();
int creatFood();
bool eat(int,int);
void move(int xOffset,int yOffset);
void moveUp();
void moveDown();
void moveLeft();
void moveRight();

core.cpp

#include "Core.h"

const int row = 20;
const int col = 20;

wchar_t** wall = NULL;
int** snake = NULL;
int** food = NULL;
int snakeLength = 0;

void initMap()
{
    srand(time(NULL));
    wall = new wchar_t* [row];
    for (int i = 0; i < row; i++) {
        wall[i] = new wchar_t[col];
    }

    createSnake();
    creatFood();

    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            if (i == 0 || j == 0 || i == row - 1 || j == col - 1)
            {
                wall[i][j] = L'墙';
            }
            else {
                wall[i][j] = L'白';
            }
        }
    }
}


void showMap()
{
    clearMap();
    // 输出地图
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            bool isSnake = false;
            for (int x = 0; x < snakeLength; x++)
            {
                if (i == snake[x][0] && j == snake[x][1]) {
                    wprintf(L"蛇");
                    isSnake = true;
                    break;
                }
            }
            if (isSnake)
            {
                continue;
            }
            if (i == food[0][0] && j == food[0][1])
            {
                wprintf(L"食");
            }
            else if (wall[i][j] == L'白') {
                wprintf(L"  ");
            }
            else {
                wprintf(L"%lc", wall[i][j]);
            }
        }
        wprintf(L"\r\n");
    }
}

void clearMap()
{
    system("cls");
}

int createSnake()
{
    int length = (row - 2) * (row - 2);
    snake = new int* [length];
    for (int i = 0; i < length; i++) {
        snake[i] = new int[2];
        snake[i][0] = 0xffffffff;
        snake[i][1] = 0xffffffff;
    }


    int randomNum = 0;

    int coordinate = rand() % (row - 2) + 1;

    snake[0][0] = coordinate;
    snake[0][1] = coordinate;
    snakeLength++;

    return coordinate;
}

int creatFood()
{
    food = new int* [1];
    food[0] = new int[2];

    int coordinate =  rand() % (row - 2)+1;
    food[0][0] = coordinate;
    food[0][1] = coordinate;

    return coordinate;
}

bool eat(int xOffset, int yOffset)
{
    int xTemp = snake[0][0] + xOffset;
    int yTemp = snake[0][1] + yOffset;


    if (xTemp == food[0][0] && yTemp == food[0][1])
    {

        snakeLength++;
        for (int i = snakeLength - 1; i > 0; i--)
        {
            snake[i][0] = snake[i - 1][0];
            snake[i][1] = snake[i - 1][1];
            printf("x->%d,y->%d\n", snake[i][0], snake[i][1]);
        }

        snake[0][0] = food[0][0];
        snake[0][1] = food[0][1];
        printf("x->%d,y->%d\n", snake[0][0], snake[0][1]);
        creatFood();
        return true;
    }
    return false;
}

void move(int xOffset,int yOffset)
{
    bool result = eat(xOffset,yOffset);
    if (result)
    {
        return;
    }

    int xCurrnet = snake[0][0];
    int yCurrnet = snake[0][1];
    int xTemp = xCurrnet;
    int yTemp = yCurrnet;

    snake[0][0] = snake[0][0] + xOffset;
    snake[0][1] = snake[0][1] + yOffset;


    for (int i = 1; i < snakeLength; i++)
    {
        xCurrnet = snake[i][0];
        yCurrnet = snake[i][1];
        snake[i][0] = xTemp;
        snake[i][1] = yTemp;
        xTemp = xCurrnet;
        yTemp = yCurrnet;
    }

}

void moveUp()
{
    move(-1,0);
}

void moveDown()
{
    move(1,0);
}

void moveLeft()
{
    move(0,-1);
}

void moveRight()
{
    move(0,1);
}

main.cpp


#include "Core.h"
#include 
#include 


int main()
{
	setlocale(LC_ALL, "zh_CN");

	initMap();
	char key = NULL;
	do
	{
		showMap();
		key = _getch();
		if (key == 119)
		{
			moveUp();
			printf("你输入的字符是: w\n");
		}
		else if (key == 97)
		{
			moveLeft();
			printf("你输入的字符是: a\n");
		}
		else if (key == 115)
		{
			moveDown();
			printf("你输入的字符是: s\n");
		}
		else if (key == 100)
		{
			moveRight();
			printf("你输入的字符是: d\n");
		}
		else if (key == 113) {
			break;
		}
	} while (true);

	return 0;
}

你可能感兴趣的:(我的学习,c语言,开发语言)