1
\$\begingroup\$

ve started programming just recently and I've learned the basic concepts of classes and objects and how to create them. So I decided to try and create a simple game of breakout with what I've learned so far. I've only created the main movable platform with a for cycle and make it move horizontally with the directional keys so far. I've also created a void so the platform doesn't go over the border of the screen but it doesn't work and I've tried all I could think of. Can someone please tell me what I'm doing wrong?

 Game::Game( MainWindow& wnd )
    :
    wnd( wnd ),
    gfx( wnd )
{
}

void Game::Go()
{
    gfx.BeginFrame();   
    UpdateModel();
    ComposeFrame();
    gfx.EndFrame();
}

void Game::UpdateModel()
{
    if (wnd.kbd.KeyIsPressed(VK_RIGHT))
    {
        platform.vx += 3;
    }

    if (wnd.kbd.KeyIsPressed(VK_LEFT))
    {
        platform.vx -= 3;
    }

    platform.ScreenLimit();
}

void Game::ComposeFrame()
{
    for (platform.x = 460; platform.x <= platform.w; platform.x++)
    {
        for (platform.y = 500; platform.y <= platform.h; platform.y++)
        {
            gfx.PutPixel(platform.x + platform.vx, platform.y, 255, 255, 255);
        }
    }
}

and here's the header file and source file of the class I created for the platform:

Platform.h

#pragma once
#include "Graphics.h"
class Platform
{
public:

    int x = 460;
    int y = 500;
    int vx = 0;
    int width = 60;
    int heigth = 10;
    int w = x + width;
    int h = y + heigth;

    void ScreenLimit();

    
private:

};

Platform.cpp

#include "Platform.h"

void Platform::ScreenLimit()
{
    const int left_base = x;
    const int right_base = w;

    if (right_base >= Graphics::ScreenWidth)
    {
        x = (Graphics::ScreenWidth - 6) - width;
    }
    else if (left_base <= 0)
    {
        x = 0;
    }
}
\$\endgroup\$
0

1 Answer 1

0
\$\begingroup\$

Based on your code, the variable x and w seem to never be updated. And apparently they represent the "start" position. Other variables' meanings are also unclear to me. However, it seems that you are not really looking at the bounds that you expect.

Considering your code, your limits should probably be:

right_base = x + vx + width
left_base = x + vx

Assuming that x is the start position, vx an offset and width the bar width.

However, I would suggest you rethink your variable usage/naming. Also, the rendering loop changing the state of the platform seems unnecessary.

You can probably get away with just 4 variables:

  • x: Current position x
  • y: Current position y
  • width: Bar width
  • height: Bar height

I believe you wanted the extra variables to avoid having to recalculate correct positions every time. Which is ok. But make sure your variables are used properly. In that case, I would use more meaningful names. Such as: -initial_x -initial_y -offset_x -offset_y -width -height

The platform rendering loop could then be simply:

int xend = platform.x + platform.width;
int yend = platform.y + platform.height;
for (int x = platform.x; x <= xend; x++)
{
    for (int y = platform.y; y <= yend; y++)
    {
        gfx.PutPixel(x, y, 255, 255, 255);
    }
}

Note that we never change the state of the platform, our objective is just to render it!

Another note: I'm not familiar with this library, however, I would check if there is a way to render a rectangle or line, which could be more efficient than plotting individual pixels. If not, then you can create your own function to clean up the code and make it easier to render more rectangles.

\$\endgroup\$
3
  • \$\begingroup\$ Thanks for answering. I'm currently following the basic c++ course of ChiliTomatoNoodle. The homework and any other instruction (for basic level course) that he gives are done in a file that already has classes and files, all made by him. So that probably the reason you don't recognize these. I'll watch the naming next time I post. I made w because after I was done with the platform I wanted to make so when it touch I certain object (that I still have to make) the platform would have become longer. \$\endgroup\$
    – Rob LB
    Commented Jul 18, 2020 at 15:08
  • \$\begingroup\$ Also, I've already tried to do the variable for the rendering loop that way and for some reason what this does is making platform so big that it goes offscreen. \$\endgroup\$
    – Rob LB
    Commented Jul 18, 2020 at 15:14
  • \$\begingroup\$ It works now by the way. Someone from another forum made me understand the mistake. Still thank, your answer helped me too. \$\endgroup\$
    – Rob LB
    Commented Jul 18, 2020 at 16:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .