Introduction to C++ Part 5

D7f3efa27b5340baf2ba145b259a53d1
0
RichardvBr 101 Mar 18, 2012 at 08:28 c++

Hi everyone,

I’m having trouble with the assignment of Part 5:Conditions

I managed to make the tank move to the right and to left, just like they wanted it to.
But now I will also have to make it move up and down, and when it goes down it needs to turn invisible.

This is the code for just right and left:

#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 );
int SpriteX = 0;
int SpriteY = 100;
bool Visible = true;
bool Direction = 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 );

if (Direction == true)
{
  SpriteX += 1;
  if (SpriteX > 500) Direction = false;
}
else
{
  SpriteX -= 1;
  if (SpriteX < 100) Direction = true;
}
}

And this is the code I made that should also make it move and down, it does.. but in a diagonal line and the movement keeps getting smaller and finally the tank goes out of the screen..

#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 );
int SpriteX = 0;
int SpriteY = 100;
bool Visible = true;
bool Direction = 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 );
if (Direction == true)
{
  SpriteY += 1;
  if (SpriteY > 450) Direction = false;
}
else
{
  SpriteY -= 1;
  if (SpriteY < 10) Direction = true;
}

if (Direction == true)
{
  SpriteX += 1;
  if (SpriteX > 500) Direction = false;
}
else
{
  SpriteX -= 1;
  if (SpriteX < 100) Direction = true;
}
}

Could someone please give a hint or tell me what I’m doing wrong?
Much appreciated !

5 Replies

Please log in or register to post a reply.

D7f3efa27b5340baf2ba145b259a53d1
0
RichardvBr 101 Mar 18, 2012 at 17:47

Please, anyone?
I’m really stuck and really want to move on to the next tutorial..

4fccabf5e0461521551187c681346f13
0
kees2125 101 Mar 19, 2012 at 12:06

try useing 2 bools one for x and one for y and whene one is true set the ader one to false

D7f3efa27b5340baf2ba145b259a53d1
0
RichardvBr 101 Mar 19, 2012 at 13:24

#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 );
int SpriteX = 0;
int SpriteY = 100;
bool Visible = true;
bool DirectionY = true;
bool DirectionX = false;
void Game::Tick( float a_DT )
{
// render a single frame here
m_Screen->Clear( 0 );
if (Visible == true) theSprite.Draw( SpriteX, SpriteY, m_Screen );
if (Direction == true)
{
SpriteY += 1;
if (SpriteY > 450) Direction = false;
}
else
{
SpriteY -= 1;
if (SpriteY < 10) Direction = true;
}

if (Direction == true)
{
SpriteX += 1;
if (SpriteX > 500) Direction = false;
}
else
{
SpriteX -= 1;
if (SpriteX < 100) Direction = true;
}
}

04b4dab695f2c351da5e03382edb9284
0
Wouter_k_9 101 Apr 13, 2013 at 13:41

I have just done this. I hope it’s helpfull for you, but the assignment is not very good explained.

// 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 );

int SpriteX = 0;
int 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 += 1;
if (SpriteX > 500) DirectionX = false;
}

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

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

else
{ // he is going up, so it is visible and add to his y
SpriteY -= 1;
Visible = true;
if (SpriteY < 50) DirectionY = true;
}
}
A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Apr 13, 2013 at 17:57

Please use [ code ] … [ /code ] tags to post code on the forum, so that the formatting can be preserved.