Jump to content


introduction to c++ with game development: part 6


1 reply to this topic

#1 Wouter-k-9

    New Member

  • Members
  • Pip
  • 3 posts

Posted 11 May 2013 - 08:14 PM

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

Quote

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.

#2 TTTNL

    Member

  • Members
  • PipPip
  • 48 posts

Posted 20 May 2013 - 11:50 AM

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;
}
}
}






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users