Hey guys,
I have an assignment in my C++ class to create a "game" if you'd call it that, where a sprite follows around the mouse cursor.
Please don't misunderstand my intentions, I have already created the code so I'm not looking for someone to do my homework for me
However, I was wondering if anyone could help me with advice.
The problem I am having is deciding how I want to get the sprite to follow my cursor. The sprite needs to follow it, but not stay stuck to it. My code currently just sticks the sprite to the cursor.
What my code does right now is, I have a function called Sprite::follow that simply pastes the image. I have a While LoopGDK loop that does boy.follow(mouseX, mouseY)...
What I think it should be doing is, doing a dbWait that only pauses the sprite and not the cursor's movement (I have another sprite that's supposed to stick to the cursor) It's difficult to describe... My code is below to help better describe it:
#include "DarkGDK.h"
class Sprite
{
private:
int imageNumber;
public:
Sprite(int, char []);
void display(int, int) const;
int getImageNumber() const;
void setImageNumber(int);
void follow(int, int);
};
// The constructor
Sprite::Sprite(int image, char filename[])
{
// Set the image number
imageNumber = image;
// Load the image for the sprite
dbLoadImage(filename, imageNumber);
}
// This display member function displays the sprite
void Sprite::display(int x, int y) const
{
dbPasteImage( imageNumber, x, y );
}
// Accessor functions
int Sprite::getImageNumber() const
{
return imageNumber;
}
// Mutator functions
void Sprite::setImageNumber(int imageNum)
{
imageNumber = imageNum;
}
// Follow function
void Sprite::follow(int x, int y)
{
// wait 100
// then move to mouse coordinates
// dbWait(100);
dbPasteImage( imageNumber, x, y );
}
// The DarkGDK function
void DarkGDK()
{
DWORD white = dbRGB(255,255,255);
// create a sprite object
Sprite boy(1, "boy1.jpg");
dbLoadImage("boy2.jpg", 2);
dbLoadImage("boy3.jpg", 3);
dbLoadImage("boy4.jpg", 4);
dbLoadImage("boy5.jpg", 5);
Sprite car(6, "car.jpg");
dbSyncOn(); //set the refresh rate
dbSyncRate(60);
int x=0, y=0;
int mouseX;
int mouseY;
// Game loop
while ( LoopGDK() )
{
dbCLS(white);
if (x == 640) x = 0;
x++;
if ((x / 20) % 5 == 0)
{
boy.setImageNumber(1);
boy.follow(mouseX, mouseY);
}
else if ((x / 20) % 5 == 1)
{
boy.setImageNumber(2);
boy.follow(mouseX, mouseY);
}
else if ((x / 20) % 5 == 2)
{
boy.setImageNumber(3);
boy.follow(mouseX, mouseY);
}
else if ((x / 20) % 5 == 3)
{
boy.setImageNumber(4);
boy.follow(mouseX, mouseY);
}
else
{
boy.setImageNumber(5);
boy.follow(mouseX, mouseY);
}
mouseX = dbMouseX();
mouseY = dbMouseY();
car.display(mouseX, mouseY);
dbSync();
}
}
I hope I have not been confusing, I am having a hard time describing what I need to do here... I'll try to re-explain:
- There is a car sprite (a single image) that should be attached to the cursor's X and Y coordinates
- There is a boy sprite (a set of images that move, instead of a single image) that follows the car sprite, but does not attach to the mouse's X and Y coordinates like the car sprite does.
- Therefore, there should be some delay in the boy sprite's movement.
My issue is here.
How can I get some delay in only the boy sprite's movement, as he follows the car sprite? I tried doing the dbWait in my code, as you can see, but ended up commenting it out because dbWait pauses movement of ALL sprites. So it made the movement very choppy, like playing a new game on an old system.
Hope to hear back soon.
Regards,
-Proxin