Ok, I'm trying to write a little game in c++ using very simple graphics. not opengl or anything like that.

Everything compiles fine (There are some warnings, i know how to fix them im just lazy, ill do it later) all of my code worked perfectly until i added the targets. now my code still compiles fine, but my target doesnt behave the way im telling it to.

My problem is when i try to set the variables for one object, it changes the variables for another object that is a member of a different class.
why does it do that? how do i make it stop?

I have 3 classes: a ship class, a target class, and a missile class.

class ship
{
      public:
      double x,           //x coordinate
             y,           //y coordinate
             xspeed,      //x speed
             yspeed,      //y speed
             dir,         //direction            
             health;      //health
      int    visible;     //visible
};
class target
{
      public:
      double x,           //x coordinate
             y,           //y coordinate
             health;      //health
      int    visible;     //visible            
};
class missile
{
      public:
      double x,           //x coordinate
             y,           //y coordinate
             xspeed,      //x speed
             yspeed,      //y speed
             dir;         //direction
      int    active,      //armed
             visible;     //visible        
};

I declare an array of objects for each one

ship ship[2];
target target[0];
missile missile[255];

I have a function to give initial values to the objects variables
I have 2 lines commented out because for some insane reason, they cause a problem

void GameInit()
{
             fps=30;
             
             ship[1].visible=true;
             ship[1].x=100;
             ship[1].y=542;
             ship[1].xspeed=0;
             ship[1].yspeed=0;
             ship[1].dir=90;
             
             ship[2].visible=true;
             ship[2].x=700;
             ship[2].y=542;
             ship[2].xspeed=0;
             ship[2].yspeed=0;
             ship[2].dir=90;
             
             target[0].visible=true;
//             target[0].x=400;
//             target[0].y=100;
             
             scaleship=16;
}

Heres my functions for drawing stuff:

//------------------------------------------------------------------------------PAINT EVERYTHING
void PaintGraphics(HDC hDC)
{
     DrawShips(hDC);
     DrawTargets(hDC);
}



//------------------------------------------------------------------------------DRAW SHIPS
void DrawShips(HDC hDC)
{
     for (int i=1; i<3; i++)
     {
          if (ship[i].visible=true)
          {
                 MoveToEx(hDC,     ship[i].x+(scaleship*cos((ship[i].dir*PI/180)+atan2(0,10))),     ship[i].y-(scaleship*sin((ship[i].dir*PI/180)+atan2(0,10))), NULL);
                 LineTo(hDC,       ship[i].x+(scaleship*cos((ship[i].dir*PI/180)+atan2(5,-10))),    ship[i].y-(scaleship*sin((ship[i].dir*PI/180)+atan2(5,-10))));
                 LineTo(hDC,       ship[i].x+(scaleship*cos((ship[i].dir*PI/180)+atan2(-5,-10))),   ship[i].y-(scaleship*sin((ship[i].dir*PI/180)+atan2(-5,-10))));
                 LineTo(hDC,       ship[i].x+(scaleship*cos((ship[i].dir*PI/180)+atan2(0,10))),     ship[i].y-(scaleship*sin((ship[i].dir*PI/180)+atan2(0,10))));
          }
     }
}



//------------------------------------------------------------------------------DRAW TARGETS
void DrawTargets(HDC hDC)
{
     Ellipse(hDC, target[0].x-16, target[0].y-16, target[0].x+16, target[0].y+16);
     Ellipse(hDC, target[0].x-10, target[0].y-10, target[0].x+10, target[0].y+10);
     Ellipse(hDC, target[0].x-4, target[0].y-4, target[0].x+4, target[0].y+4);
}

And finally heres my code for keyboard checking and my move() function

void keydown()
{
     int key;
     
     for (int i=0; i<209; i++)
     {
          key=-1;
          if (GetAsyncKeyState(i)) {key=i;}
     
          switch (key)
          {
          case VK_F2:
               GameInit();
               break;
          case VK_UP:
               ship[1].xspeed+=thrust*(cos(ship[1].dir*PI/180));
               ship[1].yspeed+=thrust*(-sin(ship[1].dir*PI/180));
               break;
          case VK_LEFT:
               ship[1].dir+=5;
               break;
          case VK_RIGHT:
               ship[1].dir+=-5;
               break;
          case 0x57:
               ship[2].xspeed+=thrust*(cos(ship[2].dir*PI/180));
               ship[2].yspeed+=thrust*(-sin(ship[2].dir*PI/180));
               break;
          case 0x41:
               ship[2].dir+=5;
               break;
          case 0x44:
               ship[2].dir+=-5;
               break;
          case VK_ESCAPE:
               PostQuitMessage(0);                    //End Program
               break;
          }
     }
}



//------------------------------------------------------------------------------MOVE OBJECTS
void move()
{
     ship[1].x+=ship[1].xspeed;
     ship[1].y+=ship[1].yspeed;
     
     ship[2].x+=ship[2].xspeed;
     ship[2].y+=ship[2].yspeed;
     
     //Gravity
     ship[1].yspeed+=GRAVITY;
     ship[2].yspeed+=GRAVITY;
}

now for some reason, even though i NEVER change target[0].x or target[0].y, target[0].x is always equal to ship[2].x and target[0].y is always equal to ship[2].y


What in the world am i doing wrong?!?!?

please help me out. i been trying to figure out whats wrong for like 2 days now. please, i need your help.

Recommended Answers

All 4 Replies

1. Your target array is empty. I'm kinda wondering how it compiles personally.

2. Your ship array has only 2 entries, so for (int i=1; i<3; i++) should be for (int i=0; i<2; i++) 3. if (ship[i].visible=true) You probably meant == here.
But since visible is a boolean to begin with, how about just if ( ship[i].visible ) 4. Likewise, gameinit() runs off the ends of the arrays.
Remember arrays start at 0 in C and C++, not 1.

> (There are some warnings, i know how to fix them im just lazy, ill do it later)
This is a very poor attitude IMO.
Sooner or later, you'll be ignoring something you should really be paying attention to. But if you've already got 1000's of warnings, you're not going to spot the sudden critical one you need to be paying attention to.
"Do it later" == "Never" for most practical purposes.

Alright, i made some changes, and im getting slightly different results, but they are still wrong and im still confused

1. I targets are no longer an array. there is only one target now:

target Target;

everywhere in my code that i said "target[0]" has been changed to "Target"

2. Changed my ship array so that it has 2 entries, ship[0] and ship[1]
I knew that i should have done that before, im just lazy sometimes.

3. Fixed my if statement mistake. it was dumb of me to put that in there in the first place.
Thanks for catching it.

4. Fixed all of my warnings:

//------------------------------------------------------------------------------DRAW SHIPS
void DrawShips(HDC hDC)
{
     for (int i=0; i<2; i++)
     {
          if (ship[i].visible)
          {
                 MoveToEx(hDC,     (int)(ship[i].x+(scaleship*cos((ship[i].dir*PI/180)+atan2(0,10)))),     (int)(ship[i].y-(scaleship*sin((ship[i].dir*PI/180)+atan2(0,10)))), NULL);
                 LineTo(hDC,       (int)(ship[i].x+(scaleship*cos((ship[i].dir*PI/180)+atan2(5,-10)))),    (int)(ship[i].y-(scaleship*sin((ship[i].dir*PI/180)+atan2(5,-10)))));
                 LineTo(hDC,       (int)(ship[i].x+(scaleship*cos((ship[i].dir*PI/180)+atan2(-5,-10)))),   (int)(ship[i].y-(scaleship*sin((ship[i].dir*PI/180)+atan2(-5,-10)))));
                 LineTo(hDC,       (int)(ship[i].x+(scaleship*cos((ship[i].dir*PI/180)+atan2(0,10)))),     (int)(ship[i].y-(scaleship*sin((ship[i].dir*PI/180)+atan2(0,10)))));
          }
     }
}



//------------------------------------------------------------------------------DRAW TARGETS
void DrawTargets(HDC hDC)
{
     Ellipse(hDC, (int)(Target.x-16), (int)(Target.y-16), (int)(Target.x+16), (int)(Target.y+16));
     Ellipse(hDC, (int)(Target.x-10), (int)(Target.y-10), (int)(Target.x+10), (int)(Target.y+10));
     Ellipse(hDC, (int)(Target.x-4), (int)(Target.y-4), (int)(Target.x+4), (int)(Target.y+4));
}

Problems:
1. Target.y is now equal to ship[1].x
I am completely baffled, There is no reason for this to happen at all

2. If I uncomment those two lines in GameInit(), ship[1] is not drawn
Again, I am baffled. (It is not draw behind Target, if it was, the corners of ship[1] would be visible)

Alright, I think I fixed all my problems. The code compiles now with no warnings, and everything behaves as I expect it to.
Thanks Salem.

Please correct me if I am wrong about this:
When I declared my array, ship[1], I thought that i was declaring 2 entries, ship[0] and ship[1].
but in reality i was only declaring 1 entry, ship[0]
So when i referred to ship[2] i was exceeding the bounds of my array, thus causing strange problems.

Member Avatar for iamthwee

Yup, that's about right.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.