Atari ST Programming: STOS BASIC Sprite Movement

in Retro Technology6 months ago

My project-11.png

In the last part of this STOS BASIC Tutorial we loaded a sprite but it just sat there, let's fix that ...

One of the ways that STOS BASIC helps us to build interactive programs and games is in the sprite movement features because they are not only easy, but they do their thing without our constant input.

STOS checks and changes each sprite 50 times per second unless you tell it otherwise, and this happens on interrupts, independent of whatever your code is doing at the same time.

Why is this useful?

Consider a shoot 'em up game where your player fires a missile. In a regular coding environment, you might have to check for collisions or if it flew off screen based on the x/y coordinates of the missile in relation to other game objects and move it incrementally as part of your game loop.

In STOS, however, you can simply tell the missile the direction to move and how fast, limiting it to the screen area, and only check collisions with objects using the built-in sprite collision features.

Simple STOS Sprite Movement

STOS offers both Move X and Move Y for horizontal and vertical movement.

cls : mode 0 : curs off : hide on
colour 11,$0
load "c:\sprite.mbk"
sprite 1,100,100,1
move y 1,"(1,-4,5)(1,-1,20)(10,-1,10)(10,1,10)(1,1,20)L"
move on
wait key
default 

Here we specify that it is sprite 1 we wish to move, then a sequence of movement commands in (brackets). Each movement command has a timing, how many pixels to move, and how many times to do that movement.

At the end I added the special command L which tells STOS to loop the movements until told to stop.

You can also set an endpoint which is a pixel row, for example, 199 which would stop that sprite's movement when the sprite reaches that exact row of pixels.

Custom Mouse Pointer

Another fun option is to set the mouse pointer to your sprite:

cls : mode 0 : curs off 
colour 11,$0
load "c:\sprite.mbk"
change mouse 4
b=0
while b=0
b=mouse key
wend
default 

The first three mouse pointers (arrow, hand, clock) are built-in so your custom sprite number 1 would be mouse 4, and so on.

In my example we wait for a mouse click instead of a keypress by checking if any buttons are down in a continuous loop, if the result is anything other than zero then we continue.

Joystick Control

As you would no doubt
Screen Shot 2022-04-04 at 13.50.49.png
expect, STOS also has routines for joystick control (or in my case, a SNES style joypad).

Just learn from my mistake and ensure your controller is enabled before you spend hours banging your head against your keyboard in frustration!

cls : mode 0 : curs off : hide on
colour 11,$0
load "c:\sprite.mbk"
j=0 : q=0 : x=100 : y=100
while q=0
j=joy
if fire then q=1
if jleft then x=x-1
if jright then x=x+1
if jup then y=y-1
if jdown then y=y+1
sprite 1,x,y,1
wend
default

This simple program continuously checks for the fire button to see if we should quit, otherwise, it checks each joystick direction and increments or decrements our character's position accordingly.

Keyboard Control

Back in the day, even with the Atari ST, keyboard controls were popular with games. First of all, with any wedge-shaped computer-in-a-keyboard system everyone was guaranteed to own a keyboard, which couldn't be said for joysticks, and secondly keyboard control offered a lot more opportunities for complex control systems that require more than just directions and fire (consider flight simulators and adventure games).

If your keyboard controls are found in the ASCII character set, you can simply check if it matches the key being pressed like so:

cls : mode 0 : curs off : hide on
colour 11,$0
load "c:\sprite.mbk"
k$="" : q=0 : x=100 : y=100
while q=0
k$=inkey$
if k$ = "q" then q=1
if k$ = "a" then x=x-1
if k$ = "d" then x=x+1
if k$ = "w" then y=y-1
if k$ = "s" then y=y+1
sprite 1,x,y,1
wend
default

What if the key you want to look for does not have a character representation? For example the escape key, cursor keys, function keys?

In that case STOS fills the scancode with the numeric representation of the key that was pressed, otherwise scancode contains 0.

cls : mode 0 : curs off : hide on
colour 11,$0
load "c:\sprite.mbk"
k$="" : q=0 : x=100 : y=100
while q=0
k$=inkey$
if k$ = "q" then q=1
if k$ = "a" then x=x-1
if k$ = "d" then x=x+1
if k$ = "w" then y=y-1
if k$ = "s" then y=y+1
locate 0,0
print scancode;
sprite 1,x,y,1
wend
default

Screen Shot 2022-04-04 at 14.24.02.png

Next Up

Now the fun begins, next time we will put everything together and start building simple games so we can get truly interactive!
Sort:  

very nice retro programming post !1UP

Don-1UP-Cheers-Cartel-250px.png

You have received a 1UP from @dynamicrypto!

The following @oneup-cartel family members will soon upvote your post:
@stem-curator
And they will bring !PIZZA 🍕

Learn more about our delegation service to earn daily rewards. Join the family on Discord.

PIZZA!

PIZZA Holders sent $PIZZA tips in this post's comments:
@curation-cartel(12/20) tipped @makerhacks (x1)

Learn more at https://hive.pizza.