mercredi 9 septembre 2020

I tried making game using ascii characters but its flashing too much

Well I made a game. It basically works like this. Every half second it enters my if condition makes necessary calculations after then it prints everything to the console. This is my Game loop.

//game loop
    while(!IsGameOver)
    {
        // Updates every half a second
        if ((int)(ElapsedTime / 500) > PreviousRoundQSec)
        {
            PreviousRoundQSec = (int)(ElapsedTime / 500);

            BulletCalculation(bullets);
            RemoveBulletsOutsideOfTheBoard(bullets, BoardLength);
            HealthCalculation(SpaceShips, bullets);
            RemoveDead(SpaceShips, bullets);
            DrawGame(BoardWidth, BoardLength, SpaceShips, bullets);
        }
        
        // Updates EverySecond
        if ((int)(ElapsedTime / 1000) > PreviousRoundSec)
        {
            PreviousRoundSec = (int)(ElapsedTime / 1000);


            PreDir = HordLogic(SpaceShips, bullets,BoardWidth, BoardLength, PreDir, PreNearBorder);
            
        }

        IsGameOver = GameOver(SpaceShips);

        // To keep time
        auto end = chrono::steady_clock::now();
        ElapsedTime = chrono::duration_cast<chrono::milliseconds>(end - start).count();
    }

At first I tried cout ing every character after calculating. But it printing was pretty bad and you could see printing every character individually. that did pretty bad. After some search on the internet I found printf func was faster but didn't really helped me either. My final soultion was adding evey character to a string and printing that string at once. This was the best solution but not enough because you can see the game flashing and this makes everything complicating.

this my draw function's latest version.

void DrawGame(int BoardWidth, int BoardLength, vector<Entity*> &SpaceShips, vector<Bullet> &bullets)
{
    system("cls");

    string screen = "";
    for (int x = 0; x < BoardWidth + 2; x++)
    {
        screen += (char)178;
    }
    screen += "\n";
    for (int y = 1; y < BoardLength + 1; y++)
    {
        for (int x = 0; x < BoardWidth + 2; x++)
        {
            if (x == 0 || x == BoardWidth + 1)
            {
                screen += (char)178;
            }
            else
            {
                bool enteredIf = false;
                for (int i = 0; i < SpaceShips.size(); i++)
                {

                    // Tough Enemy drawing
                    if (SpaceShips[i]->x == x && SpaceShips[i]->y == y && SpaceShips[i]->type == 't')
                    {
                        screen += "#";
                        enteredIf = true;
                    }
                    else if (SpaceShips[i]->x + 1 == x && SpaceShips[i]->y == y && SpaceShips[i]->type == 't')
                    {
                        screen += "#";
                        enteredIf = true;
                    }
                    else if (SpaceShips[i]->x + 2 == x && SpaceShips[i]->y == y && SpaceShips[i]->type == 't')
                    {
                        screen += "#";
                        enteredIf = true;
                    }
                    else if (SpaceShips[i]->x == x && SpaceShips[i]->y + 1 == y && SpaceShips[i]->type == 't')
                    {
                        screen += "#";
                        enteredIf = true;
                    }
                    else if (SpaceShips[i]->x + 2 == x && SpaceShips[i]->y + 1 == y && SpaceShips[i]->type == 't')
                    {
                        screen += "#";
                        enteredIf = true;
                    }

                    //Idle Enemy drawing
                    if (SpaceShips[i]->x == x && SpaceShips[i]->y == y && SpaceShips[i]->type == 'i')
                    {
                        screen += "#";
                        enteredIf = true;
                    }
                    else if (SpaceShips[i]->x + 1 == x && SpaceShips[i]->y == y && SpaceShips[i]->type == 'i')
                    {
                        screen += "#";
                        enteredIf = true;
                    }
                    else if (SpaceShips[i]->x + 2 == x && SpaceShips[i]->y == y && SpaceShips[i]->type == 'i')
                    {
                        screen += "#";
                        enteredIf = true;
                    }
                    else if (SpaceShips[i]->x + 1 == x && SpaceShips[i]->y + 1 == y && SpaceShips[i]->type == 'i')
                    {
                        screen += "U";
                        enteredIf = true;
                    }
                    
                    //Shooting Enemy Drawing
                    if (SpaceShips[i]->x == x && SpaceShips[i]->y == y && SpaceShips[i]->type == 's')
                    {
                        screen += "#";
                        enteredIf = true;
                    }
                    else if (SpaceShips[i]->x + 1 == x && SpaceShips[i]->y == y && SpaceShips[i]->type == 's')
                    {
                        screen += "#";
                        enteredIf = true;
                    }
                    else if (SpaceShips[i]->x + 2 == x && SpaceShips[i]->y == y && SpaceShips[i]->type == 's')
                    {
                        screen += "#";
                        enteredIf = true;
                    }
                    else if (SpaceShips[i]->x + 2 == x && SpaceShips[i]->y + 1 == y && SpaceShips[i]->type == 's')
                    {
                        screen += "/";
                        enteredIf = true;
                    }
                    else if (SpaceShips[i]->x == x && SpaceShips[i]->y + 1 == y && SpaceShips[i]->type == 's')
                    {
                        screen += "\\";
                        enteredIf = true;
                    }

                    //Player Drawing
                    if (SpaceShips[i]->x + 1 == x && SpaceShips[i]->y == y && SpaceShips[i]->type == 'p')
                    {
                        screen += "^";
                        enteredIf = true;
                    }
                    else if (SpaceShips[i]->x == x && SpaceShips[i]->y + 1 == y && SpaceShips[i]->type == 'p')
                    {
                        screen += "<";
                        enteredIf = true;
                    }
                    else if (SpaceShips[i]->x + 1 == x && SpaceShips[i]->y + 1 == y && SpaceShips[i]->type == 'p')
                    {
                        screen += "_";
                        enteredIf = true;
                    }
                    else if (SpaceShips[i]->x + 2 == x && SpaceShips[i]->y + 1 == y && SpaceShips[i]->type == 'p')
                    {
                        screen += ">";
                        enteredIf = true;
                    }
                }
                if (!enteredIf)
                {
                    screen += " ";
                }
            }
        }
        screen += "\n";
    }
    for (int x = 0; x < BoardWidth + 2; x++)
    {
        screen += (char)178;
    }

    // draw bullets
    for (int i = 0; i < bullets.size(); i++)
    {
        if (bullets[i].GetDir() == 's')
        {
            screen.at(((bullets[i].y * (BoardWidth + 3)) - 1) + (bullets[i].x + 1)) = (char)203;
        }
        else if (bullets[i].GetDir() == 'w')
        {
            screen.at(((bullets[i].y* (BoardWidth + 3)) - 1) + (bullets[i].x + 1)) = (char)202;
        }
    }

    screen += "\n Remaining lives: ";
    screen += itoa(SpaceShips[0]->hp);

    cout << screen;
}

This is how game looks (I didn't see any rules against sharing links. if so any warning is welcome): https://www.loom.com/share/1bcc97fa0cbe4ee3aec62e317b10fcd0

Aucun commentaire:

Enregistrer un commentaire