My instructor wants us to make two sprites bounce around the screen. They need to reverse directions when they reach the edge of the screen, and reset to a random position if they hit eachother.
I've gotten it to work, but if I change the Xvel or Yvel to anything else the ships will unpredictably fly off the screen after a bounce or two. It's weird. My ships have to move really slow because of this limitation. Any help is appreciated. Thx.
/* Challenge #6 */
#include "DarkGDK.h"
//PROTOTYPE
void adjustSHIPx(int&, int&);
void adjustSHIPy(int&, int&);
//GLOBAL CONSTANTS
const int SHIP_IMG = 1;
const int SHIP2_IMG = 2;
//MAIN
void DarkGDK()
{
dbSyncOn();
dbSyncRate(60);
dbRandomize(dbTimer());
//These are the starting/reset positions.
int STARTx = dbRND(159);
int STARTy = 240;
int STARTx2 = (480+(dbRND(159)));
int STARTy2 = 240;
//These are the updated/live positions.
int shipX = STARTx;
int shipY = STARTy;
int ship2X = STARTx2;
int ship2Y = STARTy2;
//These are the updated/live velocities/directions of movement.
int Xvel = -1;
int Yvel = 1;
int X2vel = 1;
int Y2vel = -1;
dbSetImageColorKey(0,255,0);
dbLoadImage("F:\\Images\\Transfarmer.bmp", SHIP_IMG, 1);
dbSprite(SHIP_IMG, shipX, shipY, SHIP_IMG);//It doesn't draw here for some reason.
dbOffsetSprite(SHIP_IMG, (dbSpriteWidth(SHIP_IMG)/2), (dbSpriteHeight(SHIP_IMG)/2));
dbLoadImage("F:\\Images\\Transfarmer2.bmp", SHIP2_IMG, 1);
dbSprite(SHIP2_IMG, ship2X, ship2Y, SHIP2_IMG);//It doesn't draw here for some reason.
dbOffsetSprite(SHIP2_IMG, (dbSpriteWidth(SHIP2_IMG)/2), (dbSpriteHeight(SHIP2_IMG)/2));
//GAME LOOP
while(LoopGDK())
{
adjustSHIPx(shipX, Xvel);//Checking X position and changing direction if on the edge of screen.
shipX += Xvel;//Incrementing X.
adjustSHIPy(shipY, Yvel);//Checking Y position for edge of screen.
shipY += Yvel;//Incrementing Y.
dbSprite(SHIP_IMG, shipX, shipY, SHIP_IMG);//Drawing ship.
adjustSHIPx(ship2X, X2vel);
ship2X += X2vel;
adjustSHIPy(ship2Y, Y2vel);
ship2Y += Y2vel;
dbSprite(SHIP2_IMG, ship2X, ship2Y, SHIP2_IMG);
if(dbSpriteCollision(SHIP_IMG, SHIP2_IMG))//Checking for collisions.
{
shipX = STARTx;//These values are random like the starting points.
shipY = STARTy;
ship2X = STARTx2;
ship2Y = STARTy2;
}
dbSync();
}
}
//FUNCTION DEFINITION
void adjustSHIPx(int &x, int &xV)//These functions use an int switch to change the direction.
{
if (x==640 || x==0)
xV *= (-1);
}
void adjustSHIPy(int &y, int &yV)//The position and velocity are passed by reference.
{
if (y==480 || y==0)
yV *= (-1);
}