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