Code: Select all
void Game::ComposeFrame()
{
static int x, y;
if(kbd.LeftIsPressed()) {
x-=5;
}
if(kbd.RightIsPressed()) {
x+=5;
}
if(kbd.DownIsPressed()) {
y+=5;
}
if(kbd.UpIsPressed()) {
y-=5;
}
if(x >= 390) {
x = 390;
}
if(y >= 240) {
y = 240;
}
if(x <= -390) {
x = -390;
}
if(y <= -240) {
y = -240;
}
gfx.PutPixel(400 + x, 300 + y, 255, 255, 255);
gfx.PutPixel(401 + x, 300 + y, 255, 255, 255);
gfx.PutPixel(402 + x, 300 + y, 255, 255, 255);
gfx.PutPixel(403 + x, 300 + y, 255, 255, 255);
gfx.PutPixel(404 + x, 300 + y, 255, 255, 255);
gfx.PutPixel(405 + x, 300 + y, 255, 255, 255);
gfx.PutPixel(399 + x, 300 + y, 255, 255, 255);
gfx.PutPixel(398 + x, 300 + y, 255, 255, 255);
gfx.PutPixel(397 + x, 300 + y, 255, 255, 255);
gfx.PutPixel(396 + x, 300 + y, 255, 255, 255);
gfx.PutPixel(396 + x, 300 + y, 255, 255, 255);
gfx.PutPixel(400 + x, 301 + y, 255, 255, 255);
gfx.PutPixel(400 + x, 302 + y, 255, 255, 255);
gfx.PutPixel(400 + x, 303 + y, 255, 255, 255);
gfx.PutPixel(400 + x, 304 + y, 255, 255, 255);
gfx.PutPixel(400 + x, 305 + y, 255, 255, 255);
gfx.PutPixel(400 + x, 299 + y, 255, 255, 255);
gfx.PutPixel(400 + x, 298 + y, 255, 255, 255);
gfx.PutPixel(400 + x, 297 + y, 255, 255, 255);
gfx.PutPixel(400 + x, 296 + y, 255, 255, 255);
gfx.PutPixel(400 + x, 295 + y, 255, 255, 255);
}
As you can see, I still declare the local variables within the scope but I set them as static variables (Not losing their value even after the function has finished) and this seems to fix it. My question is, my fix is simpler but is it better? Is it better to set it once in the constructor or to use a static variable?