Why doesn't the function work correctly? C++



  • Created a simple gravity function that has to move the character downwards to 1 cell at the coordinates tied to the mass, causing a function in the eternal cycle, then triggers the gambling function and over again, but nothing happens, but when the characters are pressed, they leave very far.

    #include<iostream>
    #include<conio.h>
    #include<windows.h>
    

    using namespace std;

    const int size_x=15;
    const int size_y=30;
    char world[size_x][size_y];

    class Player
    {
    private:
    int g = 1;
    public:
    int jumpforce = 2;
    int point_x;
    int point_y;
    int HP;
    Player()
    {
    point_x = 0;
    point_y = 0;
    HP = 100;
    };
    Player(int x, int y, char skin)
    {

        this-&gt;point_x = x;
        this-&gt;point_y = y;
        this-&gt;HP = 100;
        for (int i = 0; i &lt; size_x; i++) 
        {
            for (int k = 0; k &lt; size_y; k++) 
            {
                if (i == this-&gt;point_x &amp;&amp; k == this-&gt;point_y) 
                {
                    world[i][k] = skin;
                    break;
                }
            }
        }
    };
    
    void ChangePositiont(char skin) 
    {
        for (int i = 0; i &lt; size_x; i++)
        {
            for (int k = 0; k &lt; size_y; k++)
            {
                if (i == this-&gt;point_x &amp;&amp; k == this-&gt;point_y)
                {
                    world[i][k] = skin;                  
                }
                if(world[i][k] == skin&amp;&amp;(i!=this-&gt;point_x||k!=this-&gt;point_y))
                {
                    world[i][k] = '.';
                }
            }
        }
    }
    

    void Gravity() 
    {
        this-&gt;point_x=this-&gt;point_x + g;
    }
    

    };

    class Enemy :public Player
    {
    public:
    Enemy(int x, int y, char skin)
    {

        this-&gt;point_x = x;
        this-&gt;point_y = y;
        this-&gt;HP = 100;
        for (int i = 0; i &lt; size_x; i++)
        {
            for (int k = 0; k &lt; size_y; k++)
            {
                if (i == this-&gt;point_x &amp;&amp; k == this-&gt;point_y)
                {
                    world[i][k] = skin;
                    break;
                }
            }
        }
    };
    

    };

    void ArrFill(char a)
    {
    for (int i = 0; i < size_x; i++)
    {

        for (int j = 0; j &lt; size_y; j++)
        {
            world[i][j] = a;
        }
    }
    

    };

    void ShowScene()
    {
    for (int i = 0; i < size_x; i++)
    {
    for (int k=0; k < size_y; k++)
    {
    cout << world[i][k];
    }
    cout << endl;
    }

    }

    void Move(Player &pl)
    {
    if (_kbhit())
    {
    char a = _getche();
    switch (a)
    {
    case 'a':
    pl.point_y = pl.point_y - 1;
    pl.ChangePositiont('$');
    break;
    case 'd':
    pl.point_y = pl.point_y + 1;
    pl.ChangePositiont('$');
    break;
    case 's':
    pl.point_x = pl.point_x + 1;
    pl.ChangePositiont('$');
    break;
    case 'w':
    pl.point_x = pl.point_x - 1;
    pl.ChangePositiont('$');
    break;
    }
    }
    }

    int main()
    {
    setlocale(LC_ALL, "ru");
    ArrFill('.');
    Player player(size_x/2, size_y/2, '$');
    Enemy DrowRanger(5, 5, '%');
    while (1)
    {
    cout << "Здоровье: " << player.HP << endl;
    player.Gravity();
    ShowScene();
    Move(player);
    system("cls");
    }
    return 0;
    }



  • The problem is, changing the character position on the field, which the program will paint every footage, you only call when the user presses the button. That's how the coordinate changes, but it's not gonna be painted until the user presses the button.

    There are many ways to use, for example, ChangePositiont() functions Gravity()
    Although the right thing is, it'll just be every human being to call this function. Gravity() Either way, it's gonna do that, but when traffic buttons are pressed, ChangePositiont() 2 times per person)



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2