码迷,mamicode.com
首页 > 编程语言 > 详细

C++贪吃蛇:

时间:2017-11-12 18:32:42      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:text   style   names   nbsp   round   visible   sleep   foo   system   

源码 

 Snake.h

#pragma once

#include <iostream>
#include <Windows.h>
#include <time.h>
#include <list>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stack>

using namespace std;

class Snake_Position
{
public:
    int x_snake;
    int y_snake;
};

//系统函数
void setColor(int color);
void GOTO(int x, int y);
void HiddenCursor();

//自定义函数
void Init_Map();
void Show_Snake();
void Init();
void isDead();
void Show_Score();

 

 Snake.cpp

#include "Snake.h"
using namespace std;


void GOTO(int x, int y)
{
    COORD wall{ 2 * x, y };
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), wall);
}

void setColor(int color)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}


void HiddenCursor()
{
    CONSOLE_CURSOR_INFO info;
    info.dwSize = 1;
    info.bVisible = FALSE;
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
}

 

Main.cpp

#include "Snake.h"

int iScore = 0;
int iGrade = 1;

//蛇头蛇尾初始位置
int x_head = 1, y_head = 3;
int x_tail = 1, y_tail = 1;

//地图坐标
int i_Map = 1, j_Map = 1;

//第二节初始位置
int x_second = 1, y_second = 2;

//初始化移动方向
int towards1 = 2; //原来方向
int towards2; //按键按下后的方向

const int SIDE = 20;
int Snake_Map[SIDE][SIDE] = { 0 };

int Speed = 300;




list<Snake_Position> LIST;
Snake_Position snake;



int main()
{
    Init();
    srand((unsigned)time(NULL));
    char getKeyboard = d;        //从键盘读取的键值
    char getKeyboard_Vice = d;    //副本: 如果读取其他值,则保持原来键值
    while (1)
    {
        int isSendFood = 1; //1--不发送食物  0--发送食物  
        int iFood;
        int x = rand() % 18 + 1;
        int y = rand() % 18 + 1;
        setColor(6);
        HiddenCursor();
        GOTO(y, x);
        cout << "";
        if (Snake_Map[x][y] != 2)
            Snake_Map[x][y] = 3;
        else
            continue;

        while (isSendFood)
        {
            if (_kbhit())
                getKeyboard = _getch();
            if (getKeyboard != s && getKeyboard != S && getKeyboard != a && getKeyboard != A && getKeyboard != w && getKeyboard != W && getKeyboard != d && getKeyboard != D)
                getKeyboard = getKeyboard_Vice;
            switch (getKeyboard)
            {
            case W:
            case w:
                towards2 = 4;
                if ((towards1 + towards2) == 5)
                {
                    getKeyboard = getKeyboard_Vice;
                    break;
                }
                towards1 = towards2; //如果现在方向合理,则保存方向到towards1
                x_head -= 1;

                isDead();
                if (Snake_Map[x_head][y_head] == 3)  //吃到食物
                {
                    isSendFood = 0;
                    iScore += 1;
                    snake.x_snake = x_head;
                    snake.y_snake = y_head;
                    LIST.push_front(snake);
                    Snake_Map[x_head][y_head] = 2;

                    setColor(7);
                    HiddenCursor();
                    GOTO(y_head, x_head);
                    cout << "";

                }
                else
                {
                    snake.x_snake = x_head;
                    snake.y_snake = y_head;
                    LIST.push_front(snake);
                    Snake_Map[LIST.back().x_snake][LIST.back().y_snake] = 0;

                    setColor(7);
                    HiddenCursor();
                    GOTO(y_head, x_head);
                    cout << "";
                    setColor(7);
                    HiddenCursor();
                    GOTO(LIST.back().y_snake, LIST.back().x_snake);
                    cout << " ";

                    LIST.pop_back();
                }
                Snake_Map[x_head][y_head] = 2;
                Show_Score();
                Sleep(Speed);
                //Show_Snake();

                getKeyboard_Vice = w;
                break;

            case S:
            case s:
                towards2 = 1;
                if ((towards1 + towards2) == 5)
                {
                    getKeyboard = getKeyboard_Vice;
                    break;
                }
                towards1 = towards2;
                x_head += 1;

                isDead();
                if (Snake_Map[x_head][y_head] == 3)  //吃到食物
                {
                    isSendFood = 0;
                    iScore += 1;
                    snake.x_snake = x_head;
                    snake.y_snake = y_head;
                    LIST.push_front(snake);
                    Snake_Map[x_head][y_head] = 2;

                    
                    setColor(7);
                    HiddenCursor();
                    GOTO(y_head, x_head);
                    cout << "";


                }
                else
                {
                    snake.x_snake = x_head;
                    snake.y_snake = y_head;
                    LIST.push_front(snake);
                    Snake_Map[LIST.back().x_snake][LIST.back().y_snake] = 0;
                    setColor(7);
                    HiddenCursor();
                    GOTO(y_head, x_head);
                    cout << "";
                    setColor(7);
                    HiddenCursor();
                    GOTO(LIST.back().y_snake, LIST.back().x_snake);
                    cout << " ";
                    LIST.pop_back();

                }
                Snake_Map[x_head][y_head] = 2;
                Show_Score();
                Sleep(Speed);
                //Show_Snake();

                getKeyboard_Vice = s;
                break;

            case A:
            case a:
                towards2 = 3;
                if ((towards1 + towards2) == 5)
                {
                    getKeyboard = getKeyboard_Vice;
                    break;
                }
                towards1 = towards2;

                y_head -= 1;

                isDead();
                if (Snake_Map[x_head][y_head] == 3)  //吃到食物
                {
                    isSendFood = 0;
                    iScore += 1;
                    snake.x_snake = x_head;
                    snake.y_snake = y_head;
                    LIST.push_front(snake);
                    Snake_Map[x_head][y_head] = 2;
                    setColor(7);
                    HiddenCursor();
                    GOTO(y_head, x_head);
                    cout << "";

                }
                else
                {
                    snake.x_snake = x_head;
                    snake.y_snake = y_head;
                    LIST.push_front(snake);
                    Snake_Map[LIST.back().x_snake][LIST.back().y_snake] = 0;
                    setColor(7);
                    HiddenCursor();
                    GOTO(y_head, x_head);
                    cout << "";
                    setColor(7);
                    HiddenCursor();
                    GOTO(LIST.back().y_snake, LIST.back().x_snake);
                    cout << " ";
                    LIST.pop_back();

                    
                }
                Snake_Map[x_head][y_head] = 2;
                Show_Score();
                Sleep(Speed);
                //Show_Snake();

                getKeyboard_Vice = a;
                break;

            case D:
            case d:
                towards2 = 2;
                if ((towards1 + towards2) == 5)
                {
                    getKeyboard = getKeyboard_Vice;
                    break;
                }
                towards1 = towards2;
                y_head += 1;

                isDead();
                if (Snake_Map[x_head][y_head] == 3)  //吃到食物
                {
                    isSendFood = 0;
                    iScore += 1;
                    snake.x_snake = x_head;
                    snake.y_snake = y_head;
                    LIST.push_front(snake);
                    Snake_Map[x_head][y_head] = 2;
                    setColor(7);
                    HiddenCursor();
                    GOTO(y_head, x_head);
                    cout << "";

                }
                else
                {
                    snake.x_snake = x_head;
                    snake.y_snake = y_head;
                    LIST.push_front(snake);
                    Snake_Map[LIST.back().x_snake][LIST.back().y_snake] = 0;
                    setColor(7);
                    HiddenCursor();
                    GOTO(y_head, x_head);
                    cout << "";
                    setColor(7);
                    HiddenCursor();
                    GOTO(LIST.back().y_snake, LIST.back().x_snake);
                    cout << " ";
                    LIST.pop_back();

                    
                }
                Snake_Map[x_head][y_head] = 2;
                Show_Score();
                Sleep(Speed);
                //Show_Snake();

                getKeyboard_Vice = d;
                break;

            default:
                break;
            }

        }
    }
    
    
    system("pause");
    return 0;
}


void Init_Map()
{
    for (int i = 0; i < SIDE; i++)
    {
        for (int j = 0; j < SIDE; j++)
        {
            if (i == 0 || i == SIDE - 1 || j == 0 || j == SIDE - 1)
            {
                GOTO(i, j);
                cout << "";
            }
        }
    }
}

void Show_Snake()
{
    for (int i = 1; i < SIDE - 1; i++)
    {
        for (int j = 1; j < SIDE - 1; j++)
        {
            if (Snake_Map[i][j] == 3)
            {
                GOTO(j, i);
                cout << "";
            }
            if (Snake_Map[i][j] == 2)
            {
                GOTO(j, i);
                cout << "";
            }
            if (Snake_Map[i][j] == 0)
            {
                GOTO(j, i);
                cout << " ";
            }
        }
    }
}

void Init()
{
    Init_Map();    //初始化显示地图
    for (int i = 0; i < SIDE; i++)
    {
        for (int j = 0; j < SIDE; j++)
        {
            if (i == 0 || i == SIDE - 1 || j == 0 || j == SIDE - 1)
                Snake_Map[i][j] = 9;
        }
    }
    //将蛇的初始三节坐标依次保存到LIST中
    Snake_Map[1][1] = 2;
    snake.x_snake = 1;
    snake.y_snake = 1;
    LIST.push_front(snake);

    Snake_Map[1][2] = 2;
    snake.x_snake = 1;
    snake.y_snake = 2;
    LIST.push_front(snake);

    Snake_Map[1][3] = 2;
    snake.x_snake = 1;
    snake.y_snake = 3;
    LIST.push_front(snake);

    Show_Snake();
}

void isDead()
{
    if (Snake_Map[x_head][y_head] == 9 || Snake_Map[x_head][y_head] == 2)  //死亡条件
    {
        system("cls");
        cout << "你已经挂了, 游戏结束!" << endl;
        Sleep(2000);
        exit(-1);
    }
}

void Show_Score()
{
    if (iScore == 5)
    {
        iGrade = 2;
        Speed = 250;
        //Sleep(2000);
    }
    if (iScore == 10)
    {

        iGrade = 3;
        Speed = 200;
        //Sleep(2000);
    }
    if (iScore == 15)
    {
        setColor(4);
        HiddenCursor();
        GOTO(5, 5);
        cout << "您已达到王者级别";
        Sleep(5000);
        exit(0);
    }
    setColor(4);
    HiddenCursor();
    GOTO(30, 8);
    cout << "级别: " << iGrade;
    GOTO(30, 12);
    cout << "得分: " << iScore;
}

 

C++贪吃蛇:

标签:text   style   names   nbsp   round   visible   sleep   foo   system   

原文地址:http://www.cnblogs.com/yan1314/p/7822273.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!