TransWikia.com

Why does the platform created with for cycle go over the border limit?

Game Development Asked by Rob LB on November 2, 2021

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;
    }
}

One Answer

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.

Answered by MateusMP on November 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP