Homework Lesson 6 *SPOILERS*

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
WwortelHD
Posts: 34
Joined: January 6th, 2012, 7:01 pm
Location: Lochem
Contact:

Homework Lesson 6 *SPOILERS*

Post by WwortelHD » January 11th, 2012, 6:57 am

WARNING: THIS CONTAINS SPOILERS OF THE HOMEWORK, DO NOT READ FURTHER THEN THIS IF YOU DIDN't FINISH YOUR HOMEWORK.


okay. big enough warning? 8-)

So... here is my Game.cpp ( I think I don't need to do Game.h also, because its the same like everyone i think)

Code: Select all

#include "Game.h"

Game::Game( HWND hWnd,const KeyboardServer& kServer )
:	gfx ( hWnd ),
	kbd( kServer ),
	x( 400 ),
	y( 300 )
{}

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

void Game::ComposeFrame()
{
	int gb;
	int speed;
	gb = 255;
	speed = 3;

	if( kbd.EnterIsPressed() )
	{
		speed = 8;
	}

	if( kbd.SpaceIsPressed() )
	{
		speed = 1;
	}

	if( kbd.LeftIsPressed() )
	{
		x = x - speed;
	}

	if( kbd.RightIsPressed() )
	{
		x = x + speed;
	}

	if ( kbd.UpIsPressed() )
	{
		y = y - speed;
	}

	if( kbd.DownIsPressed() )
	{
		y = y + speed;
	}

	if( x < 5 )
	{
		x = 5;
	}

	if( x > 794 )
	{
		x = 794;
	}

	if( y < 5 )
	{
		y = 5;
	}

	if( y > 594 )
	{
		y = 594;
	}

	if( x < 300 )
	{
		gb = 5;
	}

	if( x > 500 )
	{
		gb = 5;
	}

	if( y < 300 )
	{
		gb = 5;
	}

	if( y > 500 )
	{
		gb = 5;
	}

	gfx.PutPixel(       x,	   y,255,gb,gb );
	gfx.PutPixel(  -5 + x,	   y,255,gb,gb );
	gfx.PutPixel(  -4 + x,	   y,255,gb,gb );
	gfx.PutPixel(  -3 + x,	   y,255,gb,gb );
	gfx.PutPixel(   3 + x,	   y,255,gb,gb );
	gfx.PutPixel(   4 + x,	   y,255,gb,gb );
	gfx.PutPixel(   5 + x,	   y,255,gb,gb );       <--- this might look messed up, but it aint in the file.
	gfx.PutPixel(     + x,-5 + y,255,gb,gb );
	gfx.PutPixel(     + x,-4 + y,255,gb,gb );
	gfx.PutPixel(     + x,-3 + y,255,gb,gb );
	gfx.PutPixel(     + x, 3 + y,255,gb,gb );
	gfx.PutPixel(     + x, 4 + y,255,gb,gb );
	gfx.PutPixel(     + x, 5 + y,255,gb,gb );
}
Here is my second one, Fixed some stuff, and now I think it's right. Chili can you check? :geek:

Code: Select all

#include "Game.h"

Game::Game( HWND hWnd,const KeyboardServer& kServer )
:	gfx ( hWnd ),
	kbd( kServer ),
	x( 400 ),
	y( 300 )
{}

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

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

	if( kbd.EnterIsPressed() )
	{
		speed = 8;
	}

	if( kbd.SpaceIsPressed() )
	{
		speed = 1;
	}

	if( kbd.LeftIsPressed() )
	{
		x = x - speed;
	}

	if( kbd.RightIsPressed() )
	{
		x = x + speed;
	}

	if ( kbd.UpIsPressed() )
	{
		y = y - speed;
	}

	if( kbd.DownIsPressed() )
	{
		y = y + speed;
	}

	if( x < 5 )
	{
		x = 5;
	}

	if( x > 794 )
	{
		x = 794;
	}

	if( y < 5 )
	{
		y = 5;
	}

	if( y > 594 )
	{
		y = 594;
	}

	if( x < 300 )
	{
		r = 255;
	}

	if( x > 500 )
	{
		r = 255;
	}

	if( y < 200 )
	{
		r = 255;
	}

	if( y > 400 )
	{
		r = 255;
	}

	gfx.PutPixel(       x,	   y,r,g,b );
	gfx.PutPixel(  -5 + x,	   y,r,g,b );
	gfx.PutPixel(  -4 + x,	   y,r,g,b );
	gfx.PutPixel(  -3 + x,	   y,r,g,b );
	gfx.PutPixel(   3 + x,	   y,r,g,b );
	gfx.PutPixel(   4 + x,	   y,r,g,b );
	gfx.PutPixel(   5 + x,	   y,r,g,b );
	gfx.PutPixel(     + x,-5 + y,r,g,b );
	gfx.PutPixel(     + x,-4 + y,r,g,b );
	gfx.PutPixel(     + x,-3 + y,r,g,b );
	gfx.PutPixel(     + x, 3 + y,r,g,b );
	gfx.PutPixel(     + x, 4 + y,r,g,b );
	gfx.PutPixel(     + x, 5 + y,r,g,b );
}
Last edited by WwortelHD on January 11th, 2012, 5:50 pm, edited 1 time in total.

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

Re: Homework Lesson 6 *SPOILERS*

Post by MtDewdGM » January 11th, 2012, 2:37 pm

I'm on my phone so I can't Post my source code, but basically I made a variable for red, green, and blue and set them to 255. This controls changing colors when the object is in the box. Next I made several if statements inside each other. We also could have used the && operator several times to make all the conditions in one if statement, but I just decided to figure the homework out with what chili taught us. The first if statement I made asked if ( x > 350 ), thereby asking if the retical was on the right 450 pixels of the screen. Inside that if statement was another if statement asking if
( x < 450 ). These two made it so that the body of the second if statement only was called if the reticle was between 350 and 450 on the x axis. Inside of the second if statement was the same thing, but for the y axis. Since the y axis only goes 600 pixels, I made it between 250 and 350. After that, if all those conditions were met, it set red to 0.

All of this simply asks " if the x axis is between 350 and 450, AND the y axis is between 250 and 350, then take all the red out of the reticle, leaving only blue and greeen, making it cyan. "
8-)

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

Re: Homework Lesson 6 *SPOILERS*

Post by chili » January 11th, 2012, 3:56 pm

wortel:

Like I thought, you solved the problem using a different method than what I was looking for. It's still a valid solution though, it just needs a few tweaks. First of all, your color region is not centered because you're using the same range of values for y as you use for x. The window is wider than it is high, so the center is at (400,300) and your y region should stretch from 200 to 400, and not 300 to 500 (like it does for x). After that, all you have to do is fix it so that the default color of the reticle is red, and that it turns white inside of the if statements, and you'll be all set. :)

Dewd:

Good job! You nailed it Dewd. Nested if statements is exactly what I was looking for. I will be talking about the logical-and operator (&&) in the next lesson, and you're right, the preferred method is and-ing together a bunch of comparisons, but since I didn't teach that yet I was looking for nested ifs.
Chili

WwortelHD
Posts: 34
Joined: January 6th, 2012, 7:01 pm
Location: Lochem
Contact:

Re: Homework Lesson 6 *SPOILERS*

Post by WwortelHD » January 11th, 2012, 5:37 pm

Okay! thanks for telling me. 1 more question: how big is the screen? 800x400? 800x480? because I didn't knew that and so, I could find the middle so easily.

I will try to fix it right now. :roll:




Edit: YAY! Passed you in post Chili! haha. when is the next lesson comming out?
Edit: Edit: Fixed the stuff, I think it's alright now. I got my new code in a new code thingy. check it out. :shock: <-- wut?! I still don't think it's fully in the middle, but I made it white on the borders, and cyan at the center. :D

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

Re: Homework Lesson 6 *SPOILERS*

Post by MtDewdGM » January 12th, 2012, 4:45 am

Thanks chili!

Has anyone else figured it out?

WwortelHD
Posts: 34
Joined: January 6th, 2012, 7:01 pm
Location: Lochem
Contact:

Re: Homework Lesson 6 *SPOILERS*

Post by WwortelHD » January 12th, 2012, 6:44 am

MtDewdGM wrote:Thanks chili!

Has anyone else figured it out?
Me! (only the dimensions not 100%)

Post Reply