by **Mikael** on Tue Dec 23, 2003 9:59 pm

Following on from my previous post concerning this sample program, I've again modified it and a shortened version appears below:

//MiniMove.pc

#define EVT_PEN_UP 3

Draw();

CheckEvt();

CheckDir(int result);

MoveNorth();

MoveNorthEast();

int evt, x=75, y=75, x_move, y_move;

int UpX, UpY;

int dir, result;

MoveNorth(){

do{

evt=event(0);

UpX=penx(); UpY =peny();

y_move =-3; //set amount to move by

rect(0,x,y,x+10,y+10,0); //erase square

y = y + y_move; //calculate new position

if(y<16){//avoid the graphics title bar

y=16;

}

if(evt==EVT_PEN_UP){

CheckEvt();

}

rect(1,x,y,x+10,y+10,0); //redraw

y_move=0; //zero out move values

sleep(100);//not too fast

}while(y >= 16);

}

MoveNorthEast(){

do{

evt=event(0);

UpX=penx(); UpY =peny();

x_move =3;

y_move =-3;

rect(0,x,y,x+10,y+10,0);

x = x + x_move;

y = y + y_move;

if(x>150){//keep on-screen

x=150;

}

if(y<17){//keep on-screen

y=17;

}

if(evt==EVT_PEN_UP){

CheckEvt();

}

rect(1,x,y,x+10,y+10,0);

x_move=y_move=0;

sleep(100);

}while(y >= 17 || x <= 150);

}

Draw(){

rect(1,x,y,x+10,y+10,0);

evt=event(1);

UpX=penx(); UpY =peny();

CheckEvt();

}

CheckEvt(){

dir=CheckDir(result);

switch(dir){

case 1: MoveNorth();

break;

case 2: MoveNorthEast();

break;

}

}

int CheckDir(int result){

if(UpX > x && UpX < x+10 && UpY < y){//north

result = 1;

return result;

}

if(UpX > x + 10 && UpY < y){//north-east

result = 2;

return result;

}

}

main() {

hookhard(1);

graph_on();

while(1) {

Draw();

CheckEvt();

CheckDir(result);

MoveNorth();

MoveNorthEast();

}

}

The problem lies with the four directions: north-east, north-west etc.

If I press above the square (making it travel north) it stops as it should, but when I try the same thing with any of the diagonal compass points, the square either bounces back, or stops at one boundary and continues sliding until it meets the other. I just can't work out why. Any help appreciated.

Thanks in advance,

Mikael