Posted:

**Fri Dec 12, 2003 9:37 pm**
by **Mikael**

The following program posted by Joe caught my eye and I modified it by removing 'hookhard' and letting the square move around randomly. It works fine apart from the blinking which didn't occur in the original. I don't know if it's due the the boundry checking code I added, slowing things down. Is there a way to reduce this. I know I could probably use PToolbox's 'sprite' function, but I want to learn to walk with this before I try and run.

//move.pc

main(){

int e, x_pos=80, y_pos=80, x_move, y_move, ran;

graph_on();

while(1){

//draw object

rect(1,x_pos,y_pos,x_pos+10,y_pos+10,0);

ran = random(4)+1;

switch(ran) {

case 1: y_move=-1; break; //up

case 2: y_move=1; break; //down

case 3: x_move=-1; break; //left

case 4: x_move=1; break; //right

}

//erase object

rect(0,x_pos,y_pos,x_pos+10,y_pos+10,0);

//calculate new position

x_pos = x_pos + x_move;

y_pos = y_pos + y_move;

//draw object in new position

if(x_pos<0){//keep object on-screen

x_pos=0;

}

if(x_pos>=150){

x_pos=150;

}

if(y_pos<=15){//avoid the graphics title bar

y_pos=15;

}

if(y_pos>=150){

y_pos=150;

}

rect(1,x_pos,y_pos,x_pos+10,y_pos+10,0);

}

}

Thanks in advance,

Mikael

Posted:

**Sat Dec 13, 2003 12:05 am**
by **jstadolnik**

The boundary code changes you added introduced a delay between the time the square is erased to the time it's redrawn. If you eliminate that delay the block won't blink (e.g. do all your calculations before erasing instead of after).

Joe

The PToolboxLib guy.

http://www.geocities.com/retro_01775/PToolboxLib.htm
Posted:

**Sat Dec 13, 2003 9:06 am**
by **Mikael**

Thanks Joe,

I did as you suggested and things improved alot, but found another problem; with the square leaving a black line behind whenever it reached the uppermost screen boundry - though strangely not the other three. After playing around with the top x coordinates, I found that the only way to stop it was to add an extra 'erase' in the relvant part of the boundry code.

Mikael

Posted:

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

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

Posted:

**Tue Dec 23, 2003 10:18 pm**
by **Mikael**

It's alright, I just worked it out. I solved it with:

}while(y!=16 && x !=150);

Draw();

}

I think I just need a bit more sleep and some glasses as I just noticed that I gave 16 as the y boundary in one function and 17 in the other.[:0]

Mikael