zoukankan      html  css  js  c++  java
  • C++贪吃蛇——(2)

      贪吃蛇第二部分,上下左右方法就不全部实现了。仅供参考,因为我也是参考了别人的代码来的...

      

    //
    //  main.cpp
    //  SnakeGame
    //
    //  Created by MadMarical on 15/11/26.
    //  Copyright (c) 2015年 com. All rights reserved.
    //
    
    #include "Header.h"
    
    using namespace std;
    
    Game::Game()
    {
        for (int i = 0; i != n + 1; ++ i)
        {
            for (int j = 0; j != n + 1; ++ j)
            {
                map[i][j] = 0;
            }
        }
        for (int i = 1; i != n; ++ i)
        {
            squence[i].x = 0;
            squence[i].y = 0;
        }
        lenOfSnake = 1;
        squence[lenOfSnake].x = 1;
        squence[lenOfSnake].y = 1;
        
        map[1][1] = 4;
        direction = 4;
        
        socre = 0;
    }
    
    void Game::Play()
    {
        cout<<"请输入游戏速度"<<endl;
        cin>>snakeSpeed;
        
        srand((unsigned int)time(0));
        
        while (socre != 20)
        {
            eat = false;
            while (true)
            {
                food.x = rand() % 20 + 1; //地图大小是21 * 21 随机数不能超过地图边界,为1到21之间的数
                food.y = rand() % 20 + 1;
                if (map[food.x][food.y] == 0)
                {
                    break;
                }
            }
            map[food.x][food.y] = 2;
            system("cls");
            Image();
            while(!eat)
            {
                auto start = clock();
                isOver = true;
                while (!kbhit())
                {
                    if (clock() - start <= snakeSpeed)
                    {
                        isOver = true;
                    }
                    else
                    {
                        isOver = false;
                        break;
                    }
                }
                if (isOver)
                {
                    char c = getchar();
                    c = getchar();
                    switch (c)
                    {
                        case 'w':
                            direction = 1;
                            break;
                        case 's':
                            direction = 2;
                        case 'a':
                            direction = 3;
                        case 'd':
                            direction = 4;
                        default:
                            break;
                    }
                }
                Move(direction);
                system("cls");
                if (isOver)
                {
                    Image();
                    cout<<"GG"<<endl;
                }
                Image();
            }
            socre ++;
        }
        cout<<"win"<<endl;
    }
    
    void Game::Image()
    {
        for (int j = 0; j != n + 1; ++ j)
        {
            cout<<"#";
        }
        cout<<endl;
        for (int i = 1; i != n; ++ i)
        {
            for (int j = 0; j != n + 1; ++ j)
            {
                if (j == 0 || j == n + 1)
                {
                    if (map[i][j] == 3)
                    {
                        cout<<"!!!";
                    }
                    else
                    {
                        cout<<"#";
                    }
                }
                else
                {
                    if (map[i][j] == 1)
                    {
                        cout<<"*";
                    }
                    else if (map[i][j] == 2)
                    {
                        cout<<"@";
                    }
                    else if (map[i][j] == 3)
                    {
                        cout<<"!!!";
                    }
                    else if (map[i][j] == 4)
                    {
                        switch (direction)
                        {
                            case 1:
                                cout<<"30";
                                break;
                            case 2:
                                cout<<"31";
                                break;
                            case 3:
                                cout<<"17";
                                break;
                            default:
                                cout<<"16";
                                break;
                        }
                    }
                    else
                    {
                    cout<<" ";
                    }
                }
    
            }
            cout<<endl;
        }
        for (int j = 0; j != n + 1; ++ j)
        {
            cout<<"#";
        }
        cout<<endl;
    
    }
    void Game::Move(int direction)
    {
        switch (direction)
        {
            case 1:
                MoveUp();
                break;
            case 2:
                MoveDown();
                break;
            case 3:
                MoveLeft();
                break;
            default:
                MoveRight();
        }  
    }
    void Game::MoveUp()
    {
        int i;
        int x, y;
        if (lenOfSnake > 1 && squence[lenOfSnake].y == squence[lenOfSnake - 1].y && squence[lenOfSnake].x == squence[lenOfSnake - 1].x + 1)
        { // 不能移动,则按原来的移动.
            direction = 2; // 按原来的向下移动.
            Move(direction);
            return ;
        }
        // 开始移动.
        x = squence[lenOfSnake].x - 1;
        y = squence[lenOfSnake].y;
        if (x == 0 || map[x][y] == 1)
        { // 撞到边界或者自己撞到自己.
            map[x][y] = 3;
            isOver = 1;
        }
        if (map[x][y] == 2)
        { // 说明已经吃到事物.
            map[squence[lenOfSnake].x][squence[lenOfSnake].y] = 1;
            lenOfSnake++;
            squence[lenOfSnake].x = x;
            squence[lenOfSnake].y = y;
            map[x][y] = 4;
            eat = true;
        }
        else
        {
            map[squence[1].x][squence[1].y] = 0;
            for (i = 1; i <= lenOfSnake - 1; i++)
            {
                squence[i].x = squence[i + 1].x;
                squence[i].y = squence[i + 1].y;
                map[squence[i + 1].x][squence[i + 1].y] = 1;
            }
            squence[lenOfSnake].x = squence[lenOfSnake].x - 1;
            if (isOver)
            {
                map[squence[lenOfSnake].x][squence[lenOfSnake].y] = 3;
            }
            else
            {
                map[squence[lenOfSnake].x][squence[lenOfSnake].y] = 4;
            }
        }
    }
    void Game::MoveDown()
    {
    }
    void Game::MoveLeft()
    {
    }
    void Game::MoveRight()
    {
    }
    int main(int argc, const char * argv[])
    {
        Game A;
        A.Play();
        return 0;
    }
    View Code

    反思:

    1.conio.h并非标准库,所以如果在其他平台下运行,需要自己实现kbhit()的功能,这已经超出我的能力范围了。

    2.编程风格必须在早期就坚持,不能说今天一个风格,明天另外一个风格。

    3.看别人的源码再重编并不可耻,慢慢理解别人的代码,为什么要这么写然后自己在思考后重写就能提高能力。

  • 相关阅读:
    为什么MySQL数据库索引选择使用B+树?
    nginx负载均衡策略
    视频笔记
    mysql show full processlist 分析问题
    Git利用命令行提交代码步骤
    zend studio远程自动上传代码并执行
    CentOS7 通过YUM安装MySQL5.7 linux
    PHP的按位运算符是什么意思
    git 分支操作
    php 技术知识点汇总
  • 原文地址:https://www.cnblogs.com/thewaytomakemiracle/p/4999892.html
Copyright © 2011-2022 走看看