How to move a Sprite from point a to b?

By Phil-83

Supporter (7)

Аватар пользователя Phil-83

01-07-2011, 13:22

Hi,

First to say, I am a BASIC newbie, but I programmed years ago in QBASIC, so I still know some basics ;-)

What I want to program:
A simple playfield with 2 goals on each side. 2 or more robots (small sprites) are set to their start points and then they have to collect
small points, which are gathered over the playfield. When a robot collects a point, he has to bring it to his goal, then collect the next point.

The robot or robot team which has the higher score when no more points are left wins... I know there is no interactivity in this version of the

"game", but I want to program some simple AI-routines first before I implement a player controlled robot...

I didnt have any problems drawing the playfield and a first point to collect and one robot:

10 REM DRAW PLAYFIELD
20 SCREEN 3,0
30 LINE (6,1)-(254,1)
40 LINE (254,1)-(254,190)
50 LINE (254,190)-(6,190)
60 LINE (6,1)-(6,190)
70 PAINT (18,18),12,15
80 REM DRAW GOALS
90 LINE (6,64)-(32,64)
100 LINE (32,64)-(32,128)
110 LINE (6,128)-(32,128)
120 LINE (220,64)-(254,64)
130 LINE (220,64)-(220,128)
140 LINE (220,128)-(254,128)
150 REM ROBOT SPRITE
160 SPRITE ON
170 A1$=CHR$(&B00011000)
180 A1$=CHR$(&B00101100)
190 A1$=CHR$(&B00111100)
200 A1$=CHR$(&B00011000)
210 A$=A1$+A2$+A3$+A4$
220 SPRITE$(0)=A$
230 REM DRAW POINT TO COLLECT
240 PX=67
250 PY=80
260 PSET (PX,PY),11
270 REM DRAW ROBOT
280 X=2
290 Y=2
300 PUT SPRITE 0,(X,Y),8,0

... this is the point I dont know further. I have 1 point and 1 "Robot-Sprite". How can I move the sprite to the point in the shortest way?
I experimented a bit:

310 IF X=
320 IF Y=
330 IF X=>PX THEN X=X-1
340 IF Y=>PY THEN Y=Y-1
350 IF X=PX THEN 360 ELSE 300:IF Y=PY THEN 360 ELSE 300
360 GOTO 360

Now the sprite moves in 45 degree angles only... How can I move it directly to the point?

Thanks in advance!

Для того, чтобы оставить комментарий, необходимо регистрация или !login

By wolf_

Ambassador_ (10088)

Аватар пользователя wolf_

01-07-2011, 13:35

170 A1$=CHR$(&B00011000)
180 A1$=CHR$(&B00101100)
190 A1$=CHR$(&B00111100)
200 A1$=CHR$(&B00011000)
210 A$=A1$+A2$+A3$+A4$

observe and discover!

By wolf_

Ambassador_ (10088)

Аватар пользователя wolf_

01-07-2011, 13:39

As for the path for the shortest way from A to B, google for 'bresenham', that's the algo to draw lines ( like LINE(20,53)-(74,147) )

By JohnHassink

Ambassador (5655)

Аватар пользователя JohnHassink

01-07-2011, 13:40

Well, I think maybe finetune it a bit? Like for instance:

You calculate the difference between X and PX and put it in temporary variable 'DF'.
Then you can do

IF X
IF DF=<4 THEN PX=PX+1 : GOTO *THERE*
IF DF=<8 THEN PX=PX+2 : GOTO *THERE*
IF DF=<12 THEN PX=PX+3 : GOTO *THERE*
IF DF=<16 THEN PX=PX+4 : GOTO *THERE*

*THERE*

*FURTHER*
IF X>PX THEN DF=X-PX ELSE GOTO *SOMEWHERE*
IF DF=<4 THEN PX=PX-1 : GOTO ...

etc. etc.

Something like that.
I'm sure there's probably a more efficient way, but I'm not really good at concentrating at the moment Smile so that's how I would do it right now if I were to program such a routine.

But it's probably solvable in 1 or 2 lines. But don't look at me.. Wink