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.