if statem

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
silverhair
Posts: 2
Joined: May 13th, 2017, 11:04 am

if statem

Post by silverhair » May 13th, 2017, 11:35 am

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.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: if statem

Post by albinopapa » May 13th, 2017, 11:55 am

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.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

silverhair
Posts: 2
Joined: May 13th, 2017, 11:04 am

Re: if statem

Post by silverhair » May 13th, 2017, 12:55 pm

Thank you, I understand now. And I'm sorry I'm new to this, so I don't know what to post.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: if statem

Post by albinopapa » May 13th, 2017, 1:04 pm

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.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply