My Tutorial 4 Homework

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Andoxico
Posts: 3
Joined: February 24th, 2017, 9:05 am

My Tutorial 4 Homework

Post by Andoxico » February 24th, 2017, 10:41 am

in Game.h:
private:
MainWindow& wnd;
Graphics gfx;
/********************************/
/* User Variables */
/********************************/

// for shape changer
bool shapeChange = false;

// for standard shape
short int standX = 400;
short int standY = 300;

// for alternate shape
short int altX = 400;
short int altY = 300;

// for RGB settings
bool isRed = false;
short int redVal = 255;
short int greenVal = 255;
short int blueVal = 255;

// for movement
short int verSpeed = 0;
short int horSpeed = 0;
};

in Game.cpp:
void Game::UpdateModel()
{
// color changer
if (wnd.kbd.KeyIsPressed('C'))
{
if (isRed)
{
isRed = false;
}
else
{
isRed = true;
}
}

// shape changer
if (wnd.kbd.KeyIsPressed('S'))
{
if (shapeChange)
{
shapeChange = false;
}

else
{
shapeChange = true;
}
}

// position changer
// vertical movements
if (wnd.kbd.KeyIsPressed(VK_UP))
{
verSpeed--;
}
else if (wnd.kbd.KeyIsPressed(VK_DOWN))
{
verSpeed++;
}

// horizontal movements
if (wnd.kbd.KeyIsPressed(VK_LEFT))
{
horSpeed--;
}
else if (wnd.kbd.KeyIsPressed(VK_RIGHT))
{
horSpeed++;
}

// return reticle to center
if(wnd.kbd.KeyIsPressed(VK_F5))
{
standX = 400;
standY = 300;
altX = 400;
altY = 300;
}
}

void Game::ComposeFrame()
{
// position movement
standX += horSpeed;
altX += horSpeed;
standY += verSpeed;
altY += verSpeed;

// color changer
if (isRed == false)
{
greenVal = 255;
blueVal = 255;
}

else
{
greenVal = 0;
blueVal = 0;
}
// shape changer
if(shapeChange)
{
// alternate shape
gfx.PutPixel(altX - 3, altY - 3, redVal, greenVal, blueVal);
gfx.PutPixel(altX - 3, altY - 2, redVal, greenVal, blueVal);
gfx.PutPixel(altX - 3, altY - 1, redVal, greenVal, blueVal);
gfx.PutPixel(altX - 3, altY + 1, redVal, greenVal, blueVal);
gfx.PutPixel(altX - 3, altY + 2, redVal, greenVal, blueVal);
gfx.PutPixel(altX - 3, altY + 3, redVal, greenVal, blueVal);
gfx.PutPixel(altX - 2, altY - 3, redVal, greenVal, blueVal);
gfx.PutPixel(altX - 2, altY + 3, redVal, greenVal, blueVal);
gfx.PutPixel(altX - 1, altY - 3, redVal, greenVal, blueVal);
gfx.PutPixel(altX - 1, altY + 3, redVal, greenVal, blueVal);
gfx.PutPixel(altX, altY, redVal, greenVal, blueVal);
gfx.PutPixel(altX + 1, altY - 3, redVal, greenVal, blueVal);
gfx.PutPixel(altX + 1, altY + 3, redVal, greenVal, blueVal);
gfx.PutPixel(altX + 2, altY - 3, redVal, greenVal, blueVal);
gfx.PutPixel(altX + 2, altY + 3, redVal, greenVal, blueVal);
gfx.PutPixel(altX + 3, altY - 3, redVal, greenVal, blueVal);
gfx.PutPixel(altX + 3, altY - 2, redVal, greenVal, blueVal);
gfx.PutPixel(altX + 3, altY - 1, redVal, greenVal, blueVal);
gfx.PutPixel(altX + 3, altY + 1, redVal, greenVal, blueVal);
gfx.PutPixel(altX + 3, altY + 2, redVal, greenVal, blueVal);
gfx.PutPixel(altX + 3, altY + 3, redVal, greenVal, blueVal);
}

else {
// standard shape
gfx.PutPixel(standX, standY - 4, redVal, greenVal, blueVal);
gfx.PutPixel(standX, standY - 3, redVal, greenVal, blueVal);
gfx.PutPixel(standX, standY - 2, redVal, greenVal, blueVal);
gfx.PutPixel(standX, standY + 2, redVal, greenVal, blueVal);
gfx.PutPixel(standX, standY + 3, redVal, greenVal, blueVal);
gfx.PutPixel(standX, standY + 4, redVal, greenVal, blueVal);
gfx.PutPixel(standX - 4, standY, redVal, greenVal, blueVal);
gfx.PutPixel(standX - 3, standY, redVal, greenVal, blueVal);
gfx.PutPixel(standX - 2, standY, redVal, greenVal, blueVal);
gfx.PutPixel(standX + 2, standY, redVal, greenVal, blueVal);
gfx.PutPixel(standX + 3, standY, redVal, greenVal, blueVal);
gfx.PutPixel(standX + 4, standY, redVal, greenVal, blueVal);
}
}

There's a weird little bug with the color and shape changers. I have to hold the buttons for about a second for them to fully switch over. While I hold the S button both shapes appear and while I hold the C button both colors mix, making a very dim orange. Haven't figured out how to fix it.

Andoxico
Posts: 3
Joined: February 24th, 2017, 9:05 am

Re: My Tutorial 4 Homework

Post by Andoxico » February 24th, 2017, 10:44 am

I changed the "// return reticle to center" block to also set the movement speeds to zero

Andoxico
Posts: 3
Joined: February 24th, 2017, 9:05 am

Re: My Tutorial 4 Homework

Post by Andoxico » February 24th, 2017, 11:11 am

Also moved the color changer if statements in ComposeFrame() to UpdateModel(). Wasn't sure if it was considered logic or not.

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

Re: My Tutorial 4 Homework

Post by chili » February 25th, 2017, 5:19 am

Hmmm, it seems like you're using the s key to toggle shape changing. But think about it, the game runs at 60 fps, so every frame that you have the key depresed, it is toggling. that's hard to control, you have to time it so that when you release it is at the state you want. Human reaction time is def less than 1/60 sec :P

shapeChange = wnd.kbd.KeyIsPressed('S');

If you want to make it a toggle, you'll need to find a way to stop auto toggling every frame. I already do something similar in the homework solution, so it shouldn't be too hard to adapt.
Chili

Post Reply