introduction to c++ with game development: part 6

04b4dab695f2c351da5e03382edb9284
0
Wouter_k_9 101 May 11, 2013 at 20:14

hello,

I am following the introduction to c++ with game development tutorial and I have problems with the assignment. The first part of the assignment I understand, but the second part I don’t understand. you have to

Add a gravity influence to the calculation of the new tank’s position, to make it come down automatically instead of bouncing against the top every time.

This is what I have so far.

// Template, major revision 3
// IGAD/NHTV - Jacco Bikker - 2006-2009
#include "string.h"
#include "surface.h"
#include "stdlib.h"
#include "template.h"
#include "game.h"
using namespace Tmpl8;
void Game::Init()
{
// put your initialization code here; will be executed once
}
Sprite theSprite( new Surface("assets/ctankbase.tga"), 16 );
float SpriteX = 0;
float SpriteY = 0;
bool Visible = true;
bool DirectionX = true;
bool DirectionY = true;
void Game::Tick( float a_DT )
{
// render a single frame here
m_Screen->Clear( 0 );
if (Visible == true) theSprite.Draw( SpriteX, SpriteY, m_Screen );
// alter our coords
if (DirectionX == true)
{ // he is moving right so add to his x
  SpriteX += 0.5;
  if (SpriteX > 600) DirectionX = false;
}

else
{ // he is moving left so sub tract from his x
  SpriteX -= 0.5;
  if (SpriteX < 0) DirectionX = true;
}

if (DirectionY == true)
{ // he is going down, so he is invisible and sub tract from his y
  SpriteY += 0.5;
  if (SpriteY > 440) DirectionY = false;
}

else
{ // he is going up, so it is visible and add to his y
  SpriteY -= 0.9;
  if (SpriteY < 0) DirectionY = true;
}
}

I hope someone can help me.

1 Reply

Please log in or register to post a reply.

991c178cbb1237d5e8e147aae5782cb9
0
TTTNL 101 May 20, 2013 at 11:50

Sorry i’m a bit late. This might not be the best way to do this, but it seems to work rather well.

// Template, major revision 3
// IGAD/NHTV - Jacco Bikker - 2006-2009
#include "string.h"
#include "surface.h"
#include "stdlib.h"
#include "template.h"
#include "game.h"
using namespace Tmpl8;

Sprite theSprite( new Surface("assets/ctankbase.tga"), 16 );
float SpriteX = 0;
float SpriteY = 0;
bool Visible = true;
bool DirectionX = true;
bool DirectionY = true;
float acc = 0;
void Game::Init()
{
// put your initialization code here; will be executed once
}
void Game::Tick( float a_DT )
{
// render a single frame here
m_Screen->Clear( 0 );
if (Visible == true) theSprite.Draw( SpriteX, SpriteY, m_Screen );
// alter our coords
if (DirectionX == true)
{ // he is moving right so add to his x
  SpriteX += 0.5;
  if (SpriteX > 600) DirectionX = false;
}
else
{ // he is moving left so sub tract from his x
  SpriteX -= 0.5;
  if (SpriteX < 0) DirectionX = true;
}
if (DirectionY == true)   //Down
{
acc += 0.0005;   //Speeds up exponentially while going down
SpriteY += acc; //
if (SpriteY > 440)   //Bottom of screen
{
  DirectionY = false;
}
}
else
{
acc -= 0.0005;  //Slows down exponentially while going up
SpriteY -= acc; //
if (SpriteY < 0 || acc<0 ) //Top of the screen or when acc < 0 it is no longer going up so it has to go down again
{
  DirectionY = true;
}
}
}