## Homework Assignment Episode 7 (** READING == SPOILER **)

The Partridge Family were neither partridges nor a family. Discuss.
Natox
Posts: 51
Joined: January 10th, 2012, 2:51 pm
Location: the Netherlands
Contact:

### Homework Assignment Episode 7 (** READING == SPOILER **)

DO NOT READ IF YOU DID NOT FINISH YOUR HOMEWORK!
UNLESS YOU WANT TO CHEAT ON YOURSELF AND LEARN LESS

Alrighty, I'm already enjoying this stuff ALOT!
As you might be able to figure out from the code, my starting point or 'origin' for my moving crosshair = 100 from x nullpoint and 100 from y nullpoint. It's running around in a dimension of 600 x 400ish. Also added a targetspeed variable so it can be set to a higher difficulty (target's moving speed) if wanted.
Anyway, I think I have the answer, atleast it seems to be working!

Code: Select all

``````	// Moving Target Movement.

if( mty == 100 && mtx < 700 )
{
mtx = mtx + targetspeed;
}
if( mtx == 700 && mty < 500 )
{
mty = mty + targetspeed;
}
if( mty == 499 && mtx < 701 )
{
mtx = mtx - targetspeed;
}
if( mtx == 100 && mty > 100 )
{
mty = mty - targetspeed;
}``````
The code might be a little too much at some points, I was going nuts after figuring this out for 20 minutes or so. So I think the code can be tweaked even more, but did not really try to figure that out since it is 05.22 AM here right now, this coding has got me addicted!

I'm off to bed now though because I got this moving crosshair working!! Now I can sleep in peace!! Hope I get your oh-so-wise answer tomorrow my friend.
And thanks AGAIN for putting effort into teaching C++ to us noobs!

Thanks
My Weblog: http://www.gamer-bay.com/
Keep track of my learning progress on the C++ programming language.

chili
Posts: 3939
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

### Re: Homework Assignment Episode 7 (** READING == SPOILER **)

Good work bro! You've essentially solved the exercise. This is more or less the approach that I will take when I explain the solution in the next video. There are however a couple of caveats.

1. This line is strange:

if( mty == 499 && mtx < 701 )

mtx should always be below 701, so that comparison does absolutely nothing.

2. It's really hard to see when the program is running, but your routine actually skips some corner coordinates. For example, the cursor jumps from (699,100) to (700, 101) without first being drawn at (700,100). Can you see why, and how to fix it?

P.S. Your enthusiasm about programming is infectious. It reminds me of late nights spent long ago perfecting code with an almost obsession-like zeal. Glad to have you on the forum Natox.
Chili

Natox
Posts: 51
Joined: January 10th, 2012, 2:51 pm
Location: the Netherlands
Contact:

### Re: Homework Assignment Episode 7 (** READING == SPOILER **)

Thanks mate, did some debugging today to see what could be easier.
I simplified the code ALOT! And it is still working:

Code: Select all

``````	if( mty == 100 )
{
mtx = mtx + targetspeed;
}
if( mtx == 700 )
{
mty = mty + targetspeed;
}
if( mty == 500 )
{
mtx = mtx - targetspeed;
}
if( mtx == 100 && mty > 100 )
{
mty = mty - targetspeed;
}``````
Also does not seem to skip any corner coördinates anymore.
Hope you are pleased by this, I know I am!
My Weblog: http://www.gamer-bay.com/
Keep track of my learning progress on the C++ programming language.

chili
Posts: 3939
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

### Re: Homework Assignment Episode 7 (** READING == SPOILER **)

Good work cutting out some unnecessary comparisons.

Still skipping that one corner coordinate though (700,100).
Chili

Natox
Posts: 51
Joined: January 10th, 2012, 2:51 pm
Location: the Netherlands
Contact:

### Re: Homework Assignment Episode 7 (** READING == SPOILER **)

Lol, it's driving me crazy! Since the code says that the crosshair center, goes all the way to 700 aslong as it is on a 100 pixels on the y-height....

so it must be passing the 700 by 100 coordinate, right? :S

kinda weird... Could u spill it? :p
My Weblog: http://www.gamer-bay.com/
Keep track of my learning progress on the C++ programming language.

MtDewdGM
Posts: 11
Joined: January 7th, 2012, 12:24 am

### Re: Homework Assignment Episode 7 (** READING == SPOILER **)

Code: Select all

``````void Game::ComposeFrame()
{
int speed = 3;
int r = 0;
int g = 255;
int b = 255;
int q = 6;

int dr = 0;
int dg = 255;
int db = 0;

dx += dxspeed;
dy += dyspeed;

if ( kbd.SpaceIsPressed() )
{
speed = 1;
}
if ( kbd.EnterIsPressed() )
{
speed = 8;
}
if ( kbd.RightIsPressed() )
{
x += speed;
}
if ( kbd.LeftIsPressed() )
{
x -= speed;
}
if ( kbd.UpIsPressed() )
{
y -= speed;
}
if ( kbd.DownIsPressed() )
{
y += speed;
}
if ( x < 5 )
{
x = 5;
}
if ( y < 5 )
{
y = 5;
}
if ( x > 795 )
{
x = 795;
}
if ( y > 595 )
{
y = 595;
}

//next

if ( dx < 5 )
{
dxspeed = -dxspeed;
}
if ( dy < 5 )
{
dyspeed = -dyspeed;
}
if ( dx > 795 )
{
dxspeed = -dxspeed;
}
if ( dy > 595 )
{
dyspeed = -dyspeed;
}

if ( x > dx - q)
{
if ( x < dx + q)
{
if ( y > dy - q)
{
if ( y < dy + q)
{
dx = 300;
dy = 400;
}
}
}
}
gfx.PutPixel ( -1 + x, 3 + y,r,g,b );
gfx.PutPixel (      x, 3 + y,r,g,b );
gfx.PutPixel (  1 + x, 3 + y,r,g,b );
gfx.PutPixel ( -1 + x,-3 + y,r,g,b );
gfx.PutPixel (      x,-3 + y,r,g,b );
gfx.PutPixel (  1 + x,-3 + y,r,g,b );

gfx.PutPixel (  3 + x, -1 + y,r,g,b );
gfx.PutPixel (  3 + x,      y,r,g,b );
gfx.PutPixel (  3 + x,  1 + y,r,g,b );
gfx.PutPixel ( -3 + x, -1 + y,r,g,b );
gfx.PutPixel ( -3 + x,      y,r,g,b );
gfx.PutPixel ( -3 + x,  1 + y,r,g,b );

gfx.PutPixel ( -1 + dx, 3 + dy,dr,dg,db );
gfx.PutPixel (      dx, 3 + dy,dr,dg,db );
gfx.PutPixel (  1 + dx, 3 + dy,dr,dg,db );
gfx.PutPixel ( -1 + dx,-3 + dy,dr,dg,db );
gfx.PutPixel (      dx,-3 + dy,dr,dg,db );
gfx.PutPixel (  1 + dx,-3 + dy,dr,dg,db );

gfx.PutPixel (  3 + dx, -1 + dy,dr,dg,db );
gfx.PutPixel (  3 + dx,      dy,dr,dg,db );
gfx.PutPixel (  3 + dx,  1 + dy,dr,dg,db );
gfx.PutPixel ( -3 + dx, -1 + dy,dr,dg,db );
gfx.PutPixel ( -3 + dx,      dy,dr,dg,db );
gfx.PutPixel ( -3 + dx,  1 + dy,dr,dg,db );``````
That is my code in the Compose Frame function.

Code: Select all

``````Game::Game( HWND hWnd,const KeyboardServer& kServer )
:	gfx( hWnd ),
kbd( kServer ),
x(400),
y(300),
dx(300),
dy(200),
dxspeed(3),
dyspeed(3)``````
That is the code for the Game constructor.

Code: Select all

``````private:
D3DGraphics gfx;
KeyboardClient kbd;
int x;
int y;
int dx;
int dy;
int dxspeed;
int dyspeed;
};``````
And this is the code in my Game.h file at the bottom.

As you can see, I did the homework, but my ball bounced around the screen. I also tested for collision by testing if the reticle that was movable by the arrow keys (cyan colored) was inside and invisible box surrounding the bouncing reticle (green colored). If it was, then the bouncing ball was set back to its original position.

Is there anything that I should change?

-MtDewd

chili
Posts: 3939
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

### Re: Homework Assignment Episode 7 (** READING == SPOILER **)

Natox:

Sure, I'll spill the beans... in the next video.

For the moment, I will give you a hint. To be sure, your reticle does pass through the point (700,100) in the sense that x and y are given these values. However, when they are set to these values, they remain at those values only instantaneously, and they are never drawn with those values, so the user will never actually see the reticle at (700,100).

The key is the order of your if statements.

Happy hunting! Don't rack your brains too hard over this one though. Tenacity is a virtue in coding, but sometimes you just have to walk away from a problem for a while and do some other things. You can often gain new insights if you forget about it for a spell and then come back at it fresh.
Chili

chili
Posts: 3939
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

### Re: Homework Assignment Episode 7 (** READING == SPOILER **)

Dewd:

Interesting variation. Your code seems to work fine, however there are a couple of minor issues I see.

Code: Select all

``````if ( dx < 5 )
{
dxspeed = -dxspeed;
}
if ( dy < 5 )
{
dyspeed = -dyspeed;
}
if ( dx > 795 )
{
dxspeed = -dxspeed;
}
if ( dy > 595 )
{
dyspeed = -dyspeed;
}``````
Your new sprite only extends 3 pixels from its center but this collision test is based on a sprite extending 5 pixels in every direction (the original reticle). Therefore I doubt that the sprite is going to the actual edge of the screen.

The other thing is that because the dx/dy variables are not being clamped to the screen, there is a chance that they might exceed the edge and cause a memory access exception if the speed is too high.

All in all, good work MtDewd.
Chili

Natox
Posts: 51
Joined: January 10th, 2012, 2:51 pm
Location: the Netherlands
Contact:

### Re: Homework Assignment Episode 7 (** READING == SPOILER **)

You are so mean I'm yet again (2.35 am) trying to figure this thing out.
Been busy with other C++ tutorials all evening and loving it aswell.
But to be truely honest, I was waiting for an answer from you.

Now I'm struggling with my code again.

Thanks alot!

PS; when is the new video coming out? :)
My Weblog: http://www.gamer-bay.com/
Keep track of my learning progress on the C++ programming language.

Natox
Posts: 51
Joined: January 10th, 2012, 2:51 pm
Location: the Netherlands
Contact:

### Re: Homework Assignment Episode 7 (** READING == SPOILER **)

I'm doubting about my judgement right now, but I switched 2 ifstatements.... let me show you.

Code: Select all

``````	if( mtx == 700 )
{
mty = mty + targetspeed;
}
if( mty == 100 )
{
mtx = mtx + targetspeed;
}
if( mty == 500 )
{
mtx = mtx - targetspeed;
}
if( mtx == 100 && mty > 100 )
{
mty = mty - targetspeed;
}``````
I switched the first and the second statement with eachother.... Why you're asking?
Well I'm not really sure myself anymore... haha!

The reason that I swapped the top 2 if-statements, is because since the program reads the complete composedframe code over and over again, it actually moves the target to the right, untill it hits the 700pixel distance, then it reads all over the rest of the code before it starts over again and finally gets to the 'moving down code'. I thought this would give it just a mili-mili-mili second more time to display the target on the actual 700x100 coordinates.

Now again, this might be total bull-crap since I'm tired and all, but it might just be true!
So, this is my last shot, the only thing I can come up with. If this is false, I will kill myself!

Just kidding ofcourse, if this is not the right solution I will stick with the 'faulty' programming until your next video comes out!

cout << "C H E E R S" << endl;
My Weblog: http://www.gamer-bay.com/
Keep track of my learning progress on the C++ programming language.