俄罗斯方块形状下降C ++

时间:2016-05-08 03:52:18

标签: c++

我正在用C ++制作一个俄罗斯方块游戏。我是语言新手,需要帮助在我的桶中显示我的俄罗斯方块。此外,如果有人想指出我的任何改进,将不胜感激!

"包括"代码已被修改,以便显示出来。到目前为止,我的代码是以“'大纲”开头的。我正在一次添加一个函数。

代码:

    #include "stdafx.h"
    #include <iostream>
    #include <cstdlib>
    #include <string>
    #include <windows.h>
    #include <ctime>

    using namespace std;


    //declare variables used in functions.
    string name;
    bool gameOver;
    int shapeArray[4][4];
    int tetrisShape;

    //This function gets the player's name.
   string getPlayerName()
    {
        cout << "Enter your name: ";
        cin >> name;
        return name;
        cout << endl << endl;
    }


    //This will initialize the bucket for the game.

     void initBucket(int x, int y)
    {
        HANDLE handle;
        COORD position;
        handle = GetStdHandle(STD_OUTPUT_HANDLE);
        position.X = x;
        position.Y = y;
        SetConsoleCursorPosition(handle, position);
    }



         int initPieces()
       {

        srand(static_cast<unsigned int>(time(0)));
        int randomNumber = rand();
        int shapeType = (randomNumber % 7);

        switch (shapeType)
       {
        case 0:
            shapeArray[0][0] = ' '; shapeArray[1][0] = 'X'; shapeArray[2][0]            = ' '; shapeArray[3][0] = ' ';
    shapeArray[0][1] = ' '; shapeArray[1][1] = 'X'; shapeArray[2][1] = ' '; shapeArray[3][1] = ' ';
    shapeArray[0][2] = ' '; shapeArray[1][2] = 'X'; shapeArray[2][2] = 'X'; shapeArray[3][2] = ' ';
    shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = ' '; 

case 1:
    shapeArray[0][0] = ' '; shapeArray[1][0] = 'X'; shapeArray[2][0] = ' '; shapeArray[3][0] = ' ';
    shapeArray[0][1] = ' '; shapeArray[1][1] = 'X'; shapeArray[2][1] = ' '; shapeArray[3][1] = ' ';
    shapeArray[0][2] = 'X'; shapeArray[1][2] = 'X'; shapeArray[2][2] = ' '; shapeArray[3][2] = ' ';
    shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';
case 2:
    shapeArray[0][0] = ' '; shapeArray[1][0] = 'X'; shapeArray[2][0] = ' '; shapeArray[3][0] = ' ';
    shapeArray[0][1] = 'X'; shapeArray[1][1] = 'X'; shapeArray[2][1] = 'X'; shapeArray[3][1] = ' ';
    shapeArray[0][2] = ' '; shapeArray[1][2] = ' '; shapeArray[2][2] = ' '; shapeArray[3][2] = ' ';
    shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';
case 3:
    shapeArray[0][0] = ' '; shapeArray[1][0] = 'X'; shapeArray[2][0] = ' '; shapeArray[3][0] = ' ';
    shapeArray[0][1] = ' '; shapeArray[1][1] = 'X'; shapeArray[2][1] = ' '; shapeArray[3][1] = ' ';
    shapeArray[0][2] = ' '; shapeArray[1][2] = 'X'; shapeArray[2][2] = ' '; shapeArray[3][2] = ' ';
    shapeArray[0][3] = ' '; shapeArray[1][3] = 'X'; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';
case 4:
    shapeArray[0][0] = ' '; shapeArray[1][0] = 'X'; shapeArray[2][0] = 'X'; shapeArray[3][0] = ' ';
    shapeArray[0][1] = ' '; shapeArray[1][1] = 'X'; shapeArray[2][1] = 'X'; shapeArray[3][1] = ' ';
    shapeArray[0][2] = ' '; shapeArray[1][2] = ' '; shapeArray[2][2] = ' '; shapeArray[3][2] = ' ';
    shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';
case 5:
    shapeArray[0][0] = ' '; shapeArray[1][0] = 'X'; shapeArray[2][0] = 'X'; shapeArray[3][0] = ' ';
    shapeArray[0][1] = ' '; shapeArray[1][1] = ' '; shapeArray[2][1] = 'X'; shapeArray[3][1] = 'X';
    shapeArray[0][2] = ' '; shapeArray[1][2] = ' '; shapeArray[2][2] = ' '; shapeArray[3][2] = ' ';
    shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';
case 6:
    shapeArray[0][0] = ' '; shapeArray[1][0] = 'X'; shapeArray[2][0] = 'X'; shapeArray[3][0] = ' ';
    shapeArray[0][1] = 'X'; shapeArray[1][1] = 'X'; shapeArray[2][1] = ' '; shapeArray[3][1] = ' ';
    shapeArray[0][2] = ' '; shapeArray[1][2] = ' '; shapeArray[2][2] = ' '; shapeArray[3][2] = ' ';
    shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';
default:
    break;
}
return tetrisShape;

}

    //This function drops the game piece. It will do this without question the first time.
    //Each time after it whether or not a piece is dropped will depend on  the output of the gameOver function.
    void dropPiece(int tetrisShape)
    {
        cout << tetrisShape << endl;
    }

    //This function will allow the player to rotate the game pieces.
    void rotatePiece()
    {
        cout << "This function will rotate the piece" << endl;
    }

    //This function will determine if a line needs to be deleted.
   void deleteLine()
    {
        cout << "This function deletes lines." << endl;
    }

    //This function will add points as pieces are dropped and rows are deleted.
    void KeepScore()
    {
        cout << "This function will award x points for each piece that is dropped and x+y points for each row deleted." << endl;

    }


    bool isGameOver()
    {
        cout << "Before the next piece is dropped this function will check if the game is over." << endl;
        gameOver = true;
        return gameOver;
    }


    //This function will run after game over and it displays the player's name and their final score.
    void finalScore(string name)
   {
        cout << "This function will display the player's name and the final score" << endl;
        cout << name << " 'score'" << endl;
    }


    int _tmain(int argc, _TCHAR* argv[])
    {
        string endGame = "no";


        //This loop is for the application
        while (endGame == "no")
        {
            name = getPlayerName();
            initBucket(0, 1);
            cout << "#" << endl;
            initBucket(0, 2);
            cout << "#" << endl;
            initBucket(0, 3);
            cout << "#" << endl;
            initBucket(0, 4);
            cout << "#" << endl;
            initBucket(0, 5);
            cout << "#" << endl;
            initBucket(0, 6);
            cout << "#" << endl;
            initBucket(0, 7);
            cout << "#" << endl;
            initBucket(0, 8);
            cout << "#" << endl;
            initBucket(0, 9);
            cout << "#" << endl;
            initBucket(0, 10);
            cout << "#" << endl;
            initBucket(0, 11);
            cout << "#" << endl;
            initBucket(0, 12);
            cout << "#" << endl;
            initBucket(0, 13);
            cout << "#" << endl;
            initBucket(0, 14);
            cout << "#" << endl;
            initBucket(0, 15);
            cout << "#" << endl;
            initBucket(0, 16);
            cout << "#" << endl;
            initBucket(0, 17);
            cout << "#" << endl;
            initBucket(0, 18);
            cout << "#" << endl;
            initBucket(0, 19);
            cout << "#" << endl;
            initBucket(0, 20);
            cout << "#" << endl;
            initBucket(0, 21);
            cout << "#" << endl;
            initBucket(0, 22);
            cout << "#" << endl;
            initBucket(0, 23);
            cout << "#" << endl;
            initBucket(0, 24);
            cout << "#" << endl;
            initBucket(0, 25);
            cout << "#" << endl;



            initBucket(12, 1);
            cout << "#" << endl;
            initBucket(12, 2);
            cout << "#" << endl;
            initBucket(12, 3);
            cout << "#" << endl;
            initBucket(12, 4);
            cout << "#" << endl;
            initBucket(12, 5);
            cout << "#" << endl;
            initBucket(12, 6);
            cout << "#" << endl;
            initBucket(12, 7);
            cout << "#" << endl;
            initBucket(12, 8);
            cout << "#" << endl;
            initBucket(12, 9);
            cout << "#" << endl;
            initBucket(12, 10);
            cout << "#" << endl;
            initBucket(12, 11);
            cout << "#" << endl;
            initBucket(12, 12);
            cout << "#" << endl;
            initBucket(12, 13);
            cout << "#" << endl;
            initBucket(12, 14);
            cout << "#" << endl;
            initBucket(12, 15);
            cout << "#" << endl;
            initBucket(12, 16);
            cout << "#" << endl;
            initBucket(12, 17);
            cout << "#" << endl;
            initBucket(12, 18);
            cout << "#" << endl;
            initBucket(12, 19);
            cout << "#" << endl;
            initBucket(12, 20);
            cout << "#" << endl;
            initBucket(12, 21);
            cout << "#" << endl;
            initBucket(12, 22);
            cout << "#" << endl;
            initBucket(12, 23);
            cout << "#" << endl;
            initBucket(12, 24);
            cout << "#" << endl;
            initBucket(12, 25);
            cout << "#" << endl;

            initBucket(0, 26);
            cout << "#" << endl;
            initBucket(1, 26);
            cout << "#" << endl;
            initBucket(2, 26);
            cout << "#" << endl;
            initBucket(3, 26);
            cout << "#" << endl;
            initBucket(4, 26);
            cout << "#" << endl;
            initBucket(5, 26);
            cout << "#" << endl;
            initBucket(6, 26);
            cout << "#" << endl;
            initBucket(7, 26);
            cout << "#" << endl;
            initBucket(8, 26);
            cout << "#" << endl;
            initBucket(9, 26);
            cout << "#" << endl;
            initBucket(10, 26);
            cout << "#" << endl;
            initBucket(11, 26);
            cout << "#" << endl;
            initBucket(12, 26);
            cout << "#" << endl;

            tetrisShape = initPieces();

            //This loop is for the game play.
            while (gameOver == false)
            {
                dropPiece(tetrisShape);
                rotatePiece();
                deleteLine();
                KeepScore();
                gameOver = isGameOver();



            }
            finalScore(name);
            cout << "Do you want to end the game: ";
            cin >> endGame;
        }



        return 0;
    }

2 个答案:

答案 0 :(得分:1)

你面前有很长的路,但即使是目前编写的代码也有很多问题。

让我们看一下int initPieces()例如。

  • 它应该返回一个随机对应的数字 选择的一块,但它返回tetrisShape一个永远不变的变量 甚至初始化。
  • 每次都使用rand()并调用rands(),这是一个坏主意,为随机数生成器播种更好(还有 更好和现代的替代品,如功能) 程序中的一次(或几次)。
  • 初始化全局变量shapeArray的方式非常难以理解。

考虑这个片段,其中实现了不同的方法:

#include <array>

using std::array;

using shape_t = std::array<std::array<char, 4>, 4>;

shape_t initPieces() 
{
    static array<shape_t, 7> pieces{{  
        {{  { ' ', ' ', ' ', ' ' },
            { 'X', 'X', 'X', ' ' },
            { ' ', ' ', 'X', ' ' },
            { ' ', ' ', ' ', ' ' }
        }},
        {{  { ' ', ' ', 'X', ' ' },
            { 'X', 'X', 'X', ' ' },
            { ' ', ' ', ' ', ' ' },
            { ' ', ' ', ' ', ' ' }
        }},
        {{  { ' ', 'X', ' ', ' ' },
            { 'X', 'X', ' ', ' ' },
            { ' ', 'X', ' ', ' ' },
            { ' ', ' ', ' ', ' ' }
        }},
        {{  { ' ', ' ', ' ', ' ' },
            { 'X', 'X', 'X', 'X' },
            { ' ', ' ', ' ', ' ' },
            { ' ', ' ', ' ', ' ' }
        }},
        {{  { ' ', ' ', ' ', ' ' },
            { 'X', 'X', ' ', ' ' },
            { 'X', 'X', ' ', ' ' },
            { ' ', ' ', ' ', ' ' }
        }},
        {{  { 'X', ' ', ' ', ' ' },
            { 'X', 'X', ' ', ' ' },
            { ' ', 'X', ' ', ' ' },
            { ' ', ' ', ' ', ' ' }
        }},
        {{  { ' ', 'X', ' ', ' ' },
            { 'X', 'X', ' ', ' ' },
            { 'X', ' ', ' ', ' ' },
            { ' ', ' ', ' ', ' ' }
        }}
    }};

    return pieces[rand() % pieces.size()];
}

除了对程序的全局设计以及特别是主要功能(它的工作进度)的任何进一步考虑之外,initBucket()main的所有重复调用都是可替换的有几个循环和对该函数本身的一点修改(如果你真的需要这样做)。

void initBucket( int x, int y, char c )
{
    HANDLE handle;
    COORD position;
    handle = GetStdHandle(STD_OUTPUT_HANDLE);
    position.X = x;
    position.Y = y;
    SetConsoleCursorPosition(handle, position);
    std::cout << c;
}

然后在main

srand(time(NULL));

for ( int i = 1; i < 26; ++ i ) {
    initBucket(0, i, '#');
    initBucket(12, i, '#');
}

for ( int i = 0; i < 13; ++ i ) {
    initBucket(i, 26, '#');
}

// ... other stuff ...

// initialize the falling piece:
shape_t shape = initPieces();

答案 1 :(得分:0)

首先,一些提示: 制作多个文件并将它们包含在你的主文件中以获得一些订单进入这个功能丛林(我想即使你不再经历这些混乱?) 在GetPlayerName()中,功能在

之前结束/返回
相关问题