turtle position

Anything QL Software or Programming Related.
stevepoole
Super Gold Card
Posts: 716
Joined: Mon Nov 24, 2014 2:03 pm

turtle position

Post by stevepoole »

Hi All,
Is there any way of finding out the current turtle x and y positions, (using QPC2) ?

This would be very useful in a program I am developing... on holiday, and without all my docs !

Steve.


User avatar
tofro
Font of All Knowledge
Posts: 2702
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: turtle position

Post by tofro »

The graphics cursor x and y (so, effectively the turtle position) is (together with the pen up/down state and the current turtle angle) in the SuperBasic channel table (where n is the SuperBASIC channel number):

Code: Select all

(n*CH.LENCH+BV_CHBAS(A6)) (A6)
The format of each table entry is as follows:
$00 long 		the channellD
$04 float		current graphics cursor (x)
$0a float 		current graphics cursor (y)
$10 float 		turtle angle (degrees)
$16 byte 		pen status
Best have a look at the Technical guide, page 55.


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
User avatar
dilwyn
Mr QL
Posts: 2761
Joined: Wed Dec 01, 2010 10:39 pm

Re: turtle position

Post by dilwyn »

As I haven't used the turtle commands much, I had to have a tinker. The safest way I could think of from SBASIC to read these is with PEEK_F:

x = PEEK_F(\$30\$04) : REMark graphics x coordinate
y = PEEK_F(\$30\$0A) : REMark graphics y coordinate
angle = PEEK_F(\$30\$10) : REMark turtle angle in degrees
pen_status = PEEK(\$30\$16) : REMark 0=penup, 1=pendown

The above work simply with channel #0. Add $28*channel_number after the second $ for other channel numbers, e.g.

DEF FN ANGLE_VALUE(#channel)
RET PEEK_F(\$30\$28*channel+$10)
END DEF ANGLE_VALUE

$28 being the length of a given channel definition block (not sure of this is fixed on SBASIC or not).

Corrections\improvements\criticisms welcome as I'm trying to learn something new.

EDIT: the "angle = PEEK_F..." example above had a typo, missed out one of the backslash characters, now amended.
Last edited by dilwyn on Mon Aug 14, 2023 9:05 am, edited 1 time in total.
Reason: Corrected missing backslash in one of the examples.


User avatar
dilwyn
Mr QL
Posts: 2761
Joined: Wed Dec 01, 2010 10:39 pm

Re: turtle position

Post by dilwyn »

I forgot to mention that since Turbo Toolkit has its own PEEK_F command, which doesn't support the SBASIC extended forms, it may replace the SBASIC definition for the indirected versions (using the $indirection), so it may cause these PEEK_F functions to appear to misbehave with bad parameter errors.


stevepoole
Super Gold Card
Posts: 716
Joined: Mon Nov 24, 2014 2:03 pm

Re: turtle position

Post by stevepoole »

Many thanks Dilwyn,

It is odd that Sinclair did not implement such vital instructions at the outset !

Now I have been able to debug my holiday Amusement Park program, Penny_Drop_bas, the prototype being herewith.

Unip, Lrun and watch the demo. To play, use the '4' key to move the penny left, or '6' to move right. Improve your timings....

And that's it ! Please let me know if you have any problems. (Prototype Tested ok under QPC2).

Steve.
Penny_Drop.zip
(1012 Bytes) Downloaded 32 times


stevepoole
Super Gold Card
Posts: 716
Joined: Mon Nov 24, 2014 2:03 pm

Re: turtle position

Post by stevepoole »

Hi Folks,

Just rewrote the program so that it should work under any SMSQ/E system, not just QPC2....

Steve.
Penny_Drop2.zip
(1.06 KiB) Downloaded 43 times


stevepoole
Super Gold Card
Posts: 716
Joined: Mon Nov 24, 2014 2:03 pm

Re: turtle position

Post by stevepoole »

Hi Again,

Here is the fully commented code, rewritten using more meaningful mnemonics....

Steve.
Penny_Drop3.zip
(1.48 KiB) Downloaded 41 times
P.S: (This is still a prototype. I may make the game more complex soon).


Derek_Stewart
Font of All Knowledge
Posts: 3975
Joined: Mon Dec 20, 2010 11:40 am
Location: Sunny Runcorn, Cheshire, UK

Re: turtle position

Post by Derek_Stewart »

Hi Steve,

Penny_Drop3_bas stopped with an Invalid Parameter at Line 1010
QPC2_Penny_Drop3.png
QPC2_Penny_Drop3.png (7.02 KiB) Viewed 774 times


Regards,

Derek
User avatar
tofro
Font of All Knowledge
Posts: 2702
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: turtle position

Post by tofro »

Derek,

that's likely when you have Turbo Toolkit loaded. It masks the SBASIC PEEK_F with an own implementation that lacks some features to the original. Was mentioned by Dilwyn some posts above.


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
stevepoole
Super Gold Card
Posts: 716
Joined: Mon Nov 24, 2014 2:03 pm

Re: turtle position

Post by stevepoole »

Hi Derek,

Just reloaded the Penny_drop3_bas from the forum and it ran ok under QPCv5.02.... (See Screen_shot).

Dilwyn warns that if you have Turbo TK loaded, it has its own PEEK_F which can cause problems ?

By the way, my QPC2 is configured at 512x256, but I doubt if that may be the cause....

Sorry if this is not helpfull !

Steve.
Capture d’écran (74).png


Post Reply