Jump to content


Introduction to C++ Part 5

c++

5 replies to this topic

#1 RichardvBrunschot

    Member

  • Members
  • PipPip
  • 11 posts

Posted 18 March 2012 - 08:28 AM

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 !

#2 RichardvBrunschot

    Member

  • Members
  • PipPip
  • 11 posts

Posted 18 March 2012 - 05:47 PM

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

#3 kees2125

    New Member

  • Members
  • Pip
  • 2 posts

Posted 19 March 2012 - 12:06 PM

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

#4 RichardvBrunschot

    Member

  • Members
  • PipPip
  • 11 posts

Posted 19 March 2012 - 01:24 PM

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

#5 Wouter-k-9

    New Member

  • Members
  • Pip
  • 3 posts

Posted 13 April 2013 - 01:41 PM

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


#6 Reedbeta

    DevMaster Staff

  • Administrators
  • 5340 posts
  • LocationSanta Clara, CA

Posted 13 April 2013 - 05:57 PM

Please use [ code ] ... [ /code ] tags to post code on the forum, so that the formatting can be preserved.
reedbeta.com - developer blog, OpenGL demos, and other projects





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users