Page 1 of 1

if statem

Posted: May 13th, 2017, 11:35 am
by silverhair

Code: Select all

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

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

void Game::UpdateModel()
{
	boxType(100, 50);
	boxType(300, 400);
	boxType(200, 250);
	moveIng();

	x =x+ vx ;
}


void Game::ComposeFrame()
{
	gfx.PutPixel(x + 10, y, 0, 255, gb);
	gfx.PutPixel(x + 11, y, 0, 255, gb);
	gfx.PutPixel(x + 12, y, 0, 255, gb);
	gfx.PutPixel(x + 13, y, 0, 255, gb);
	gfx.PutPixel(x + 14, y, 0, 255, gb);
	gfx.PutPixel(x + 15, y, 0, 255, gb);
	gfx.PutPixel(x + 16, y, 0, 255, gb);
	gfx.PutPixel(x + 17, y, 0, 255, gb);
	gfx.PutPixel(x + 18, y, 0, 255, gb);
	gfx.PutPixel(x + 19, y, 0, 255, gb);
	gfx.PutPixel(x + 20, y, 0, 255, gb);

	gfx.PutPixel(x + 10, y + 10, 0, 255, gb);
	gfx.PutPixel(x + 11, y + 10, 0, 255, gb);
	gfx.PutPixel(x + 12, y + 10, 0, 255, gb);
	gfx.PutPixel(x + 13, y + 10, 0, 255, gb);
	gfx.PutPixel(x + 14, y + 10, 0, 255, gb);
	gfx.PutPixel(x + 15, y + 10, 0, 255, gb);
	gfx.PutPixel(x + 16, y + 10, 0, 255, gb);
	gfx.PutPixel(x + 17, y + 10, 0, 255, gb);
	gfx.PutPixel(x + 18, y + 10, 0, 255, gb);
	gfx.PutPixel(x + 19, y + 10, 0, 255, gb);
	gfx.PutPixel(x + 20, y + 10, 0, 255, gb);

	gfx.PutPixel(x + 10, y, 0, 255, gb);

	gfx.PutPixel(x + 10, y + 1, 0, 255, gb);
	gfx.PutPixel(x + 10, y + 2, 0, 255, gb);
	gfx.PutPixel(x + 10, y + 3, 0, 255, gb);
	gfx.PutPixel(x + 10, y + 4, 0, 255, gb);
	gfx.PutPixel(x + 10, y + 5, 0, 255, gb);
	gfx.PutPixel(x + 10, y + 6, 0, 255, gb);
	gfx.PutPixel(x + 10, y + 7, 0, 255, gb);
	gfx.PutPixel(x + 10, y + 8, 0, 255, gb);
	gfx.PutPixel(x + 10, y + 9, 0, 255, gb);
	gfx.PutPixel(x + 10, y + 10, 0, 255, gb);

	gfx.PutPixel(x + 20, y, 0, 255, gb);

	gfx.PutPixel(x + 20, y + 1, 0, 255, gb);
	gfx.PutPixel(x + 20, y + 2, 0, 255, gb);
	gfx.PutPixel(x + 20, y + 3, 0, 255, gb);
	gfx.PutPixel(x + 20, y + 4, 0, 255, gb);
	gfx.PutPixel(x + 20, y + 5, 0, 255, gb);
	gfx.PutPixel(x + 20, y + 6, 0, 255, gb);
	gfx.PutPixel(x + 20, y + 7, 0, 255, gb);
	gfx.PutPixel(x + 20, y + 8, 0, 255, gb);
	gfx.PutPixel(x + 20, y + 9, 0, 255, gb);
	gfx.PutPixel(x + 20, y + 10, 0, 255, gb);
}

void Game::boxType(int x , int y)
{
	gfx.PutPixel(x + 10, y, 0, 255, 0);
	gfx.PutPixel(x + 11, y, 0, 255, 0);
	gfx.PutPixel(x + 12, y, 0, 255, 0);
	gfx.PutPixel(x + 13, y, 0, 255, 0);
	gfx.PutPixel(x + 14, y, 0, 255, 0);
	gfx.PutPixel(x + 15, y, 0, 255, 0);
	gfx.PutPixel(x + 16, y, 0, 255, 0);
	gfx.PutPixel(x + 17, y, 0, 255, 0);
	gfx.PutPixel(x + 18, y, 0, 255, 0);
	gfx.PutPixel(x + 19, y, 0, 255, 0);
	gfx.PutPixel(x + 20, y, 0, 255, 0);

	gfx.PutPixel(x + 10, y + 10, 0, 255, 0);
	gfx.PutPixel(x + 11, y + 10, 0, 255, 0);
	gfx.PutPixel(x + 12, y + 10, 0, 255, 0);
	gfx.PutPixel(x + 13, y + 10, 0, 255, 0);
	gfx.PutPixel(x + 14, y + 10, 0, 255, 0);
	gfx.PutPixel(x + 15, y + 10, 0, 255, 0);
	gfx.PutPixel(x + 16, y + 10, 0, 255, 0);
	gfx.PutPixel(x + 17, y + 10, 0, 255, 0);
	gfx.PutPixel(x + 18, y + 10, 0, 255, 0);
	gfx.PutPixel(x + 19, y + 10, 0, 255, 0);
	gfx.PutPixel(x + 20, y + 10, 0, 255, 0);

	gfx.PutPixel(x + 10, y, 0, 255, 0);

	gfx.PutPixel(x + 10, y + 1, 0, 255, 0);
	gfx.PutPixel(x + 10, y + 2, 0, 255, 0);
	gfx.PutPixel(x + 10, y + 3, 0, 255, 0);
	gfx.PutPixel(x + 10, y + 4, 0, 255, 0);
	gfx.PutPixel(x + 10, y + 5, 0, 255, 0);
	gfx.PutPixel(x + 10, y + 6, 0, 255, 0);
	gfx.PutPixel(x + 10, y + 7, 0, 255, 0);
	gfx.PutPixel(x + 10, y + 8, 0, 255, 0);
	gfx.PutPixel(x + 10, y + 9, 0, 255, 0);
	gfx.PutPixel(x + 10, y + 10, 0, 255, 0);

	gfx.PutPixel(x + 20, y, 0, 255, 0);

	gfx.PutPixel(x + 20, y + 1, 0, 255, 0);
	gfx.PutPixel(x + 20, y + 2, 0, 255, 0);
	gfx.PutPixel(x + 20, y + 3, 0, 255, 0);
	gfx.PutPixel(x + 20, y + 4, 0, 255, 0);
	gfx.PutPixel(x + 20, y + 5, 0, 255, 0);
	gfx.PutPixel(x + 20, y + 6, 0, 255, 0);
	gfx.PutPixel(x + 20, y + 7, 0, 255, 0);
	gfx.PutPixel(x + 20, y + 8, 0, 255, 0);
	gfx.PutPixel(x + 20, y + 9, 0, 255, 0);
	gfx.PutPixel(x + 20, y + 10, 0, 255, 0);
}


May I know why "else { stop = false;}" is needed for the box to keep increasing in moving speed when press UP?

Code: Select all

void Game::moveIng()
{
	boxType(400, 500);

	if (wnd.kbd.KeyIsPressed(VK_RIGHT))
	{
		if (stop)
		{
		}
		else
		{ vx = vx + 1; stop = true; 
		}
	}
	else 
	{ 
		stop = false;
	}
}
Edited by albinopapa to make formatting easier to read.

Re: if statem

Posted: May 13th, 2017, 11:55 am
by albinopapa
Let's follow the logic of the code:

First let's assume stop = true to start with ( I don't see what or where it is initialized ).

Frame0 -> stop = true;
If you don't press the RIGHT button, stop is set to false
Frame1 -> stop = false;
Now you press the RIGHT button, and check if stop is true, it's not so we go to the else
Frame2 -> increment vx and set stop to true;
Since the program is running at 60 frames per second, the next 10+ frames or so you'll still be holding down the RIGHT button and stop is set to true.
Frame3 - 13 -> check if stop is true, it is so don't do anything.
Now you release the RIGHT button and again stop is set to false.

Hopefully things are a little clearer and you can figure out why it's setup that way.

Re: if statem

Posted: May 13th, 2017, 12:55 pm
by silverhair
Thank you, I understand now. And I'm sorry I'm new to this, so I don't know what to post.

Re: if statem

Posted: May 13th, 2017, 1:04 pm
by albinopapa
Nah, you're fine. I just moved some tags around. The stuff inside the code tags don't get formatted, so the questions wasn't being highlighted.