Hey! I'm new on this forum, so if i make a mistake please don't be angry with me.
I'm making a simple game, just two ships trying to "touch" a tin wich run over the screen.

OK, here's the thing: I have problems about how the tin avoid the ships. I recently implement a algorithm that uses Pythagoras to get the distances between the tin and each ship.

I talk spanish so the variables are "nave" (for ships) and "lata" (for tin).
This is the code which calculate the next position of the tin (obviously minimized):
#include <time.h>
#include <math.h>

#define VEL_LATA 5              //[Pix/Frame]
#define TOLERANCIA 40           //[Pix]
#define TOLERANCERCA 36  //[Pix]

#define ALTO 480                //[Pix]

#define ANCHO 640               //[Pix]
#define SIZE_LATAX 17
#define SIZE_LATAY 25

struct POSITION{
    POSITION(){/* sin acción */}
    POSITION(int nx, int ny){x=nx; y=ny;}
    int x;
    int y;

class CSprite{
        POSITION getPos() {return Position;}
        void setPos(POSITION pPos) {Position=pPos;}
        POSITION Position;

CSprite lata;

void Move(CSprite &nave1, CSprite &nave2);
void ClosertoDestination(POSITION &Source, POSITION &Destination);
bool AreCloser(POSITION &v1, POSITION &v2);
bool AreVeryCloser(POSITION &v1, POSITION &v2);

void Move(CSprite &nave1, CSprite &nave2) {
    static POSITION Destination=lata.getPos();
    POSITION Source=lata.getPos();
    POSITION p1=nave1.getPos(), p2=nave2.getPos();

    if(    (Source.y==Destination.y &&Source.x==Destination.x)  ||
            (AreCloser(p1, Source) ||
             AreCloser(p2, Source))

      ) {
        do {
            Destination.x=rand() % ANCHO-SIZE_LATAX+1;
            Destination.y=rand() % ALTO-SIZE_LATAY+1;
            Destination.x-=Destination.x % VEL_LATA;
            Destination.y-=Destination.y % VEL_LATA;
            ClosertoDestination(Source, Destination);
        } while(AreVeryCloser(p1, Source) ||
                AreVeryCloser(p2, Source));
        ClosertoDestination(Source, Destination);


void ClosertoDestination(POSITION &Source, POSITION &Destination) {
    if (Source.x<Destination.x)
    else if (Source.x>Destination.x)

    if (Source.y<Destination.y)
    else if (Source.y>Destination.y)

bool AreCloser(POSITION &v1, POSITION &v2) {
    return (sqrt(pow((v2.x-v1.x),2) + pow((v2.y-v1.y),2) )<TOLERANCIA);

bool AreVeryCloser(POSITION &v1, POSITION &v2) {
    return (sqrt(pow((v2.x-v1.x),2) + pow((v2.y-v1.y),2) )<TOLERANCIACERCA);
Sometimes when the tin approaches a ship (or vice versa) the game stay in the central while and i can´t solve that.
Anyway, if anyone has in mind a better algorithm than mine can say it, because i don't know how "powerful" is this.