How to move 2 images w/the arrow keys, 1 working only while the alt key is held in combin. with an arrow key, the oth...

The Partridge Family were neither partridges nor a family. Discuss.
TheCollector
Posts: 41
Joined: August 1st, 2019, 9:57 pm

How to move 2 images w/the arrow keys, 1 working only while the alt key is held in combin. with an arrow key, the oth...

Post by TheCollector » August 1st, 2019, 10:02 pm

Basically, I'm on lesson 4.3 of Chili's where we move a reticle with velocity. Well, I decided to be a smarta$$ and instead of doing what Chili did I made it a little bit harder.

Ideally, I'd like the reticle to move when just the arrow keys are pressed.

When the alt-key is held and then an arrow key is pressed in combination, I'd like a separate group of pixels to move with velocity.

How to achieve this?

I've coded separate int's for each grouping of pixels. Basically, I set up the second grouping of pixels the way I did the first, but with Chili's explanation of how to achieve velocity. But the code is interfering with each other. I tried to achieve the combination key movements by creating a bool, initializing that bool if alt is pressed and then including code for each pixel to move under "if (name of bool).

Maybe I'm just going about it the wrong way. I'm very new to this.

At the moment, my code for the second grouping of pixels is interfering with the moving of the first image. And also, the way I've tried to code to get the second object to move only when the alt-key is pressed in combination with an arrow key is not working.

Also, if someone could tell me how to use a letter-key when doing KeyIsPressed(VK_???) that would be helpful. MSDN just says 'W' and that doesn't work. I was planning on using the W, A, D and X keys to move the second grouping of pixels. Even though I decided to go with the latter (alt-key in combination with arrow key) I'd still like to know.

Help me make my game!!!

Here's what I've done (and I'm sure you'll probably laugh)

Code: Select all

void Game::UpdateModel()
{

	//Move reticle up
	if (wnd.kbd.KeyIsPressed(VK_UP))
	{
		chy = chy - 3;
	}

	//Move reticle down
	if (wnd.kbd.KeyIsPressed(VK_DOWN))
	{
		chy = chy + 3;
	}

	//Move reticle left
	if (wnd.kbd.KeyIsPressed(VK_LEFT))
	{
		chx = chx - 3;
	}

	//Move reticle right
	if (wnd.kbd.KeyIsPressed(VK_RIGHT))
	{
		chx = chx + 3;
	}
	//Color change for reticle
	if (wnd.kbd.KeyIsPressed(VK_CONTROL))
	{
		gb = 0;
	}

	ShapeIsChanged = wnd.kbd.KeyIsPressed(VK_SHIFT);

	BoatIsInitiated = wnd.kbd.KeyIsPressed(VK_MENU);

	
	if (BoatIsInitiated)
	{
		//Move boat up
		if (wnd.kbd.KeyIsPressed(VK_UP))
			{
				bvy = bvy - 1;
			}
		//Move boat down
		if (wnd.kbd.KeyIsPressed(VK_DOWN))
			{
				bvy = bvy + 1;
			}
		//Move boat left
		if (wnd.kbd.KeyIsPressed(VK_LEFT))
			{
				bvx = bvx - 1;
			}
		//Move boat right
		if (wnd.kbd.KeyIsPressed(VK_RIGHT))
			{
				bvx = bvx + 1;
			}
	}
	bx = bx + bvx;
	by = by + bvy;
	}


void Game::ComposeFrame()
{
	if (ShapeIsChanged)
	{

		gfx.PutPixel(3 + chx, 8 + chy, 255, gb, gb);
		gfx.PutPixel(4 + chx, 9 + chy, 255, gb, gb);
		gfx.PutPixel(5 + chx, 10 + chy, 255, gb, gb);
		gfx.PutPixel(-3 + chx, -1 + chy, 255, gb, gb);
		gfx.PutPixel(-4 + chx, -2 + chy, 255, gb, gb);
		gfx.PutPixel(-5 + chx, -3 + chy, 255, gb, gb);

		gfx.PutPixel(3 + chx, -1 + chy, 255, gb, gb);
		gfx.PutPixel(4 + chx, -2 + chy, 255, gb, gb);
		gfx.PutPixel(5 + chx, -3 + chy, 255, gb, gb);
		gfx.PutPixel(-3 + chx, 8 + chy, 255, gb, gb);
		gfx.PutPixel(-4 + chx, 9 + chy, 255, gb, gb);
		gfx.PutPixel(-5 + chx, 10 + chy, 255, gb, gb);


	}
	else
	{
//Crosshair gfx (first image/grouping of pixels)
		gfx.PutPixel(-3 + bx, by, 255, gb, gb);
		gfx.PutPixel(-4 + bx, by, 255, gb, gb);
		gfx.PutPixel(-5 + bx, by, 255, gb, gb);
		gfx.PutPixel(2 + bx, by, 255, gb, gb);
		gfx.PutPixel(3 + bx, by, 255, gb, gb);
		gfx.PutPixel(4 + bx, by, 255, gb, gb);
		gfx.PutPixel(bx, -3 + by, 255, gb, gb);
		gfx.PutPixel(bx, -4 + by, 255, gb, gb);
		gfx.PutPixel(bx, -5 + by, 255, gb, gb);
		gfx.PutPixel(bx, 2 + by, 255, gb, gb);
		gfx.PutPixel(bx, 3 + by, 255, gb, gb);
		gfx.PutPixel(bx, 4 + by, 255, gb, gb);
	}

//Boat gfx (second image/grouping of pixels)
	gfx.PutPixel(30 + bx, 20 + by, 0, 255, 0);
	gfx.PutPixel(31 + bx, 20 + by, 0, 255, 0);
	gfx.PutPixel(32 + bx, 20 + by, 0, 255, 0);
	gfx.PutPixel(33 + bx, 20 + by, 0, 255, 0);
	gfx.PutPixel(34 + bx, 20 + by, 0, 255, 0);
	gfx.PutPixel(35 + bx, 20 + by, 0, 255, 0);

	gfx.PutPixel(31 + bx, 21 + by, 0, 255, 0);
	gfx.PutPixel(32 + bx, 21 + by, 0, 255, 0);
	gfx.PutPixel(33 + bx, 21 + by, 0, 255, 0);
	gfx.PutPixel(34 + bx, 21 + by, 0, 255, 0);


}
Last edited by albinopapa on August 2nd, 2019, 9:11 am, edited 1 time in total.
Reason: Added code tags for readability

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

Re: How to move 2 images w/the arrow keys, 1 working only while the alt key is held in combin. with an arrow key, the ot

Post by albinopapa » August 2nd, 2019, 9:25 am

//Crosshair gfx (first image/grouping of pixels)
gfx.PutPixel(-3 + bx, by, 255, gb, gb);

//Boat gfx (second image/grouping of pixels)
gfx.PutPixel(30 + bx, 20 + by, 0, 255, 0);

Hopefully isolating the two sections like this will give you some clue ( I'm hoping I'm right here since I don't have all your code in front of me to test in real time ). If I press ALT+UP I change bvy and update the variable 'by'. If I press ALT+LEFT I change 'bvx' and update 'bx'. As I see it, no matter what bx and by are going to change both sets of pixels because you use the same variables for both sets of pixels. I'm guessing if you want two separate sets of pixels moving, you're going to need to have a bx/by for one set and a cx/cy ( var names are up to you ) for the second set.

To answer your question about using W,A,S,etc... the documentation from MSDN is correct. You must use the capital letter surrounded by single quotes 'W', 'A', 'S', 'D', 'X' and so on. There are no VK virtual key codes for the printable characters. I suppose though you could, I'm not suggesting you do, make your own.
#define VK_A 'A'
#define VK_B 'B'
and so on.
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

TheCollector
Posts: 41
Joined: August 1st, 2019, 9:57 pm

Re: How to move 2 images w/the arrow keys, 1 working only while the alt key is held in combin. with an arrow key, the ot

Post by TheCollector » August 2nd, 2019, 4:12 pm

I see what you're talking about. I did not catch that! Thank you =]

TheCollector
Posts: 41
Joined: August 1st, 2019, 9:57 pm

Re: How to move 2 images w/the arrow keys, 1 working only while the alt key is held in combin. with an arrow key, the ot

Post by TheCollector » August 2nd, 2019, 4:23 pm

Fixed the error and now the cross-hair at least works again. Only problem is I can't make secondary use of the arrow keys by holding down the alt-key with the code I have in there. Any idea how to go about this? I thought this would take care of it:

BoatIsInitiated = wnd.kbd.KeyIsPressed(VK_MENU);


if (BoatIsInitiated)
{
//Move boat up
if (wnd.kbd.KeyIsPressed(VK_UP))
{
bvy = bvy - 1;
}
//Move boat down
if (wnd.kbd.KeyIsPressed(VK_DOWN))
{
bvy = bvy + 1;
}
//Move boat left
if (wnd.kbd.KeyIsPressed(VK_LEFT))
{
bvx = bvx - 1;
}
//Move boat right
if (wnd.kbd.KeyIsPressed(VK_RIGHT))
{
bvx = bvx + 1;
}
}
bx = bx + bvx;
by = by + bvy;
}

TheCollector
Posts: 41
Joined: August 1st, 2019, 9:57 pm

Re: How to move 2 images w/the arrow keys, 1 working only while the alt key is held in combin. with an arrow key, the ot

Post by TheCollector » August 2nd, 2019, 4:50 pm

So, I tried changing it from being the alt-key to the enter key to make the bool become true. For some reason the alt key would bring me to the window menu instead of doing what I programmed it to do. Only problem now is that both the cross-hair and the boat move when you hold the enter key and press up, down, left or right and I want to be able to switch between the two.

TheCollector
Posts: 41
Joined: August 1st, 2019, 9:57 pm

Re: How to move 2 images w/the arrow keys, 1 working only while the alt key is held in combin. with an arrow key, the ot

Post by TheCollector » August 2nd, 2019, 4:54 pm

I suppose I just turn the cross-hair into a bool and have it be false when BoatIsInitialized and vice versa?

TheCollector
Posts: 41
Joined: August 1st, 2019, 9:57 pm

Re: How to move 2 images w/the arrow keys, 1 working only while the alt key is held in combin. with an arrow key, the ot

Post by TheCollector » August 2nd, 2019, 5:07 pm

Here's the updated code (I've set CrossHairIsInitiated to true in the header file so you start with being able to move the cross-hair):

void Game::UpdateModel()
{
if (CrossHairIsInitiated)
{
BoatIsInitiated = false;
//Move reticle up
if (wnd.kbd.KeyIsPressed(VK_UP))
{
chy = chy - 3;
}

//Move reticle down
if (wnd.kbd.KeyIsPressed(VK_DOWN))
{
chy = chy + 3;
}

//Move reticle left
if (wnd.kbd.KeyIsPressed(VK_LEFT))
{
chx = chx - 3;
}

//Move reticle right
if (wnd.kbd.KeyIsPressed(VK_RIGHT))
{
chx = chx + 3;
}
//Color change for reticle
if (wnd.kbd.KeyIsPressed(VK_CONTROL))
{
chgb = 0;
}
}
ChangeWeapon = wnd.kbd.KeyIsPressed(VK_SHIFT);
BoatIsInitiated = wnd.kbd.KeyIsPressed(VK_RETURN);

if (BoatIsInitiated)
{
CrossHairIsInitiated = false;
//Initiate cross-hair
if (wnd.kbd.KeyIsPressed(VK_RETURN))
{
CrossHairIsInitiated = true;
BoatIsInitiated = false;
}
//Move boat up
if (wnd.kbd.KeyIsPressed(VK_UP))
{
bvy = bvy - 1;
}
//Move boat down
if (wnd.kbd.KeyIsPressed(VK_DOWN))
{
bvy = bvy + 1;
}
//Move boat left
if (wnd.kbd.KeyIsPressed(VK_LEFT))
{
bvx = bvx - 1;
}
//Move boat right
if (wnd.kbd.KeyIsPressed(VK_RIGHT))
{
bvx = bvx + 1;
}
}
bx = bx + bvx;
by = by + bvy;
}


void Game::ComposeFrame()
{
gfx.PutPixel(30 + bx, 20 + by, 0, 255, 0);
gfx.PutPixel(31 + bx, 20 + by, 0, 255, 0);
gfx.PutPixel(32 + bx, 20 + by, 0, 255, 0);
gfx.PutPixel(33 + bx, 20 + by, 0, 255, 0);
gfx.PutPixel(34 + bx, 20 + by, 0, 255, 0);
gfx.PutPixel(35 + bx, 20 + by, 0, 255, 0);

gfx.PutPixel(31 + bx, 21 + by, 0, 255, 0);
gfx.PutPixel(32 + bx, 21 + by, 0, 255, 0);
gfx.PutPixel(33 + bx, 21 + by, 0, 255, 0);
gfx.PutPixel(34 + bx, 21 + by, 0, 255, 0);

if (ChangeWeapon)
{

gfx.PutPixel(3 + chx, 8 + chy, 255, chgb, chgb);
gfx.PutPixel(4 + chx, 9 + chy, 255, chgb, chgb);
gfx.PutPixel(5 + chx, 10 + chy, 255, chgb, chgb);
gfx.PutPixel(-3 + chx, -1 + chy, 255, chgb, chgb);
gfx.PutPixel(-4 + chx, -2 + chy, 255, chgb, chgb);
gfx.PutPixel(-5 + chx, -3 + chy, 255, chgb, chgb);

gfx.PutPixel(3 + chx, -1 + chy, 255, chgb, chgb);
gfx.PutPixel(4 + chx, -2 + chy, 255, chgb, chgb);
gfx.PutPixel(5 + chx, -3 + chy, 255, chgb, chgb);
gfx.PutPixel(-3 + chx, 8 + chy, 255, chgb, chgb);
gfx.PutPixel(-4 + chx, 9 + chy, 255, chgb, chgb);
gfx.PutPixel(-5 + chx, 10 + chy, 255, chgb, chgb);


}
else
{
gfx.PutPixel(-3 + chx, chy, 255, chgb, chgb);
gfx.PutPixel(-4 + chx, chy, 255, chgb, chgb);
gfx.PutPixel(-5 + chx, chy, 255, chgb, chgb);
gfx.PutPixel(2 + chx, chy, 255, chgb, chgb);
gfx.PutPixel(3 + chx, chy, 255, chgb, chgb);
gfx.PutPixel(4 + chx, chy, 255, chgb, chgb);
gfx.PutPixel(chx, -3 + chy, 255, chgb, chgb);
gfx.PutPixel(chx, -4 + chy, 255, chgb, chgb);
gfx.PutPixel(chx, -5 + chy, 255, chgb, chgb);
gfx.PutPixel(chx, 2 + chy, 255, chgb, chgb);
gfx.PutPixel(chx, 3 + chy, 255, chgb, chgb);
gfx.PutPixel(chx, 4 + chy, 255, chgb, chgb);
}




}

TheCollector
Posts: 41
Joined: August 1st, 2019, 9:57 pm

Re: How to move 2 images w/the arrow keys, 1 working only while the alt key is held in combin. with an arrow key, the ot

Post by TheCollector » August 2nd, 2019, 5:42 pm

I figured out that if I set the cross-hair to only move when the escape key is held and the boat only when the enter key is held they won't move each other. But that's not the way I wanted to do it. Ideally, I'd like the cross-hair to be true when the game starts and toggle between the two when the return key is pressed.

TheCollector
Posts: 41
Joined: August 1st, 2019, 9:57 pm

Re: How to move 2 images w/the arrow keys, 1 working only while the alt key is held in combin. with an arrow key, the ot

Post by TheCollector » August 2nd, 2019, 5:56 pm

Okay, I figured it out with some trial and error. Cannot use the return key for both. Set it so that when you hit escape while the boat is initiated it initiates the cross-hair. That works.

TheCollector
Posts: 41
Joined: August 1st, 2019, 9:57 pm

Re: How to move 2 images w/the arrow keys, 1 working only while the alt key is held in combin. with an arrow key, the ot

Post by TheCollector » August 2nd, 2019, 6:33 pm

Finishing with tutorial 4.3 and the velocity fix (with the else statements) messed it up a bit. For some reason now, when you press escape while boatisinitiated it doesn't put the focus back on the cross-hair. It was doing it before I changed the code so that the velocity doesn't increase with each frame. I don't know. I'm missing something simple here.

Code: Select all

void Game::UpdateModel()
{
	if (CrossHairIsInitiated)
	{
		BoatIsInitiated = false;

		//Move reticle up
		if (wnd.kbd.KeyIsPressed(VK_UP))
		{
			chy = chy - 3;
		}

		//Move reticle down
		if (wnd.kbd.KeyIsPressed(VK_DOWN))
		{
			chy = chy + 3;
		}

		//Move reticle left
		if (wnd.kbd.KeyIsPressed(VK_LEFT))
		{
			chx = chx - 3;
		}

		//Move reticle right
		if (wnd.kbd.KeyIsPressed(VK_RIGHT))
		{
			chx = chx + 3;
		}
		//Color change for reticle
		if (wnd.kbd.KeyIsPressed(VK_CONTROL))
		{
			chgb = 0;
		}
	}
	BoatIsInitiated = wnd.kbd.KeyIsPressed(VK_RETURN);
	ChangeWeapon = wnd.kbd.KeyIsPressed(VK_SHIFT);

	if (BoatIsInitiated)
	{
		CrossHairIsInitiated = false;
		
		
		//Move boat up
		if (wnd.kbd.KeyIsPressed(VK_UP))
			if (InhibitUp)
			{

			}
			else
			{
				bvy = bvy - 1;
				InhibitUp = true;
			}
		else
		{
			InhibitUp = false;
		}

		//Move boat down
		if (wnd.kbd.KeyIsPressed(VK_DOWN))
			if (InhibitDown)
			{

			}
			else
            {
				bvy = bvy + 1;
				InhibitDown = true;
			}
		else
		{
			InhibitDown = false;
		}

		//Move boat left
		if (wnd.kbd.KeyIsPressed(VK_LEFT))
			if (InhibitLeft)
			{

			}
			else
			{
				bvx = bvx - 1;
				InhibitLeft = true;
			}
		else
		{
			InhibitLeft = false;
		}

		//Move boat right
		if (wnd.kbd.KeyIsPressed(VK_RIGHT))
			if (InhibitRight)
			{

			}
			else
			{
				bvx = bvx + 1;
				InhibitRight = true;
			}
		else
		{
			InhibitRight = false;
		}

	}

	bx = bx + bvx;
	by = by + bvy;

	}


void Game::ComposeFrame()
{
	gfx.PutPixel(30 + bx, 20 + by, 0, 255, 0);
	gfx.PutPixel(31 + bx, 20 + by, 0, 255, 0);
	gfx.PutPixel(32 + bx, 20 + by, 0, 255, 0);
	gfx.PutPixel(33 + bx, 20 + by, 0, 255, 0);
	gfx.PutPixel(34 + bx, 20 + by, 0, 255, 0);
	gfx.PutPixel(35 + bx, 20 + by, 0, 255, 0);

	gfx.PutPixel(31 + bx, 21 + by, 0, 255, 0);
	gfx.PutPixel(32 + bx, 21 + by, 0, 255, 0);
	gfx.PutPixel(33 + bx, 21 + by, 0, 255, 0);
	gfx.PutPixel(34 + bx, 21 + by, 0, 255, 0);

	if (ChangeWeapon)
	{

		gfx.PutPixel(3 + chx, 8 + chy, 255, chgb, chgb);
		gfx.PutPixel(4 + chx, 9 + chy, 255, chgb, chgb);
		gfx.PutPixel(5 + chx, 10 + chy, 255, chgb, chgb);
		gfx.PutPixel(-3 + chx, -1 + chy, 255, chgb, chgb);
		gfx.PutPixel(-4 + chx, -2 + chy, 255, chgb, chgb);
		gfx.PutPixel(-5 + chx, -3 + chy, 255, chgb, chgb);

		gfx.PutPixel(3 + chx, -1 + chy, 255, chgb, chgb);
		gfx.PutPixel(4 + chx, -2 + chy, 255, chgb, chgb);
		gfx.PutPixel(5 + chx, -3 + chy, 255, chgb, chgb);
		gfx.PutPixel(-3 + chx, 8 + chy, 255, chgb, chgb);
		gfx.PutPixel(-4 + chx, 9 + chy, 255, chgb, chgb);
		gfx.PutPixel(-5 + chx, 10 + chy, 255, chgb, chgb);


	}
	else
	{
		gfx.PutPixel(-3 + chx, chy, 255, chgb, chgb);
		gfx.PutPixel(-4 + chx, chy, 255, chgb, chgb);
		gfx.PutPixel(-5 + chx, chy, 255, chgb, chgb);
		gfx.PutPixel(2 + chx, chy, 255, chgb, chgb);
		gfx.PutPixel(3 + chx, chy, 255, chgb, chgb);
		gfx.PutPixel(4 + chx, chy, 255, chgb, chgb);
		gfx.PutPixel(chx, -3 + chy, 255, chgb, chgb);
		gfx.PutPixel(chx, -4 + chy, 255, chgb, chgb);
		gfx.PutPixel(chx, -5 + chy, 255, chgb, chgb);
		gfx.PutPixel(chx, 2 + chy, 255, chgb, chgb);
		gfx.PutPixel(chx, 3 + chy, 255, chgb, chgb);
		gfx.PutPixel(chx, 4 + chy, 255, chgb, chgb);
	}




}

Post Reply