Skip to main content
Typo
Link
DMGregory
  • 141k
  • 23
  • 258
  • 401

Some objects only rendering half of the screen in sneakesnake game

edited body
Source Link

i'mI'm trying to make a basic snake game with OpenGL. I hold all the game object locations in a matrix called MAP. And when i render them, i operate the location info from MAP (that comes as x and y [0 < x < 40 and 0 < y < 20]) so that i can use it in the glTranslatef().

i'm trying to make a basic snake game with OpenGL. I hold all the game object locations in a matrix called MAP. And when i render them, i operate the location info from MAP (that comes as x and y [0 < x < 40 and 0 < y < 20]) so that i can use it in the glTranslatef().

I'm trying to make a basic snake game with OpenGL. I hold all the game object locations in a matrix called MAP. And when i render them, i operate the location info from MAP (that comes as x and y [0 < x < 40 and 0 < y < 20]) so that i can use it in the glTranslatef().

Source Link

Some objects only rendering half of the screen in sneake game

i'm trying to make a basic snake game with OpenGL. I hold all the game object locations in a matrix called MAP. And when i render them, i operate the location info from MAP (that comes as x and y [0 < x < 40 and 0 < y < 20]) so that i can use it in the glTranslatef().

I use the same equation for all objects. Snake's head is rendering correctly and can be move to anywhere in the screen but tails and bait are only rendering to half of the screen. Here's the code:

void SetLocationE()
{
    glLoadIdentity();
    glTranslatef(-11.5f, -8.125f, .0f);
}

void ScreenBuffer::RenderToScreen(GLvoid)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    /* DRAW */
    for (int i = 0; i < 20; i++)
    {
        for (int j = 0; j < 40; j++)
        {
            
            if (MAP[i][j] == 'x')
            {
                SetLocationE();
                bx = j;
                by = i;
                /* BAIT */
                bxx = bx * 23 / 40;
                byy = by * 16.25 / 40;
                glTranslatef(bxx, byy, zet);
                glBegin(GL_QUADS);
                //painting
                glColor3f(0.0f, 0.0f, 0.0f);
                //drawing  TOP RIGHT -> TOP LEFT
                //         -> BOTTOM LEFT -> BOTTOM RIGHT
                glVertex3f(0.15f, 0.15f, .0f);  //tr
                glVertex3f(-0.15f, 0.15f, .0f);  //tl
                glVertex3f(-0.15f, -0.15f, .0f);  //bl
                glVertex3f(0.15f, -0.15f, .0f);  //br
                glEnd();

            }

            else if (MAP[i][j] == '0')
            {
                SetLocationE();
                hx = j;
                hy = i;
                /* HEAD */
                hxx = hx * 23 / 40;
                hyy = hy * 16.25 / 20;
                glTranslatef(hxx, hyy, zet);
                glBegin(GL_QUADS);
                //painting
                glColor3f(0.0f, 0.3f, 0.1f);
                //drawing  TOP RIGHT -> TOP LEFT
                //         -> BOTTOM LEFT -> BOTTOM RIGHT
                glVertex3f(0.15f, 0.15f, .0f);  //tr
                glVertex3f(-0.15f, 0.15f, .0f);  //tl
                glVertex3f(-0.15f, -0.15f, .0f);  //bl
                glVertex3f(0.15f, -0.15f, .0f);  //br
                glEnd();
            }

            else if (MAP[i][j] == 'o')
            {
                SetLocationE();
                /* TAIL */
                txx = j * 23 / 40;
                tyy = i * 16.25 / 40;
                glTranslatef(txx, tyy, zet);
                glBegin(GL_QUADS);
                //painting
                glColor3f(0.0f, 0.7f, 0.2f);
                //drawing  TOP RIGHT -> TOP LEFT
                //         -> BOTTOM LEFT -> BOTTOM RIGHT
                glVertex3f(0.15f, 0.15f, .0f);  //tr
                glVertex3f(-0.15f, 0.15f, .0f);  //tl
                glVertex3f(-0.15f, -0.15f, .0f);  //bl
                glVertex3f(0.15f, -0.15f, .0f);  //br
                glEnd();

            }
        }
    }
}

Moving of the head is correct. When out of borders, it reappears again. Same thing is available for the tails but they reappear if they are at the horizontally half line. Tails and bait is rendering between red lines:

enter image description here

It was a console game at first. Now i'm trying to implement OpenGL to the old codes. I don't know what causes this. Thanks in advance.