Hi all,
Buzbard's trick got me thinking about how to further 'improve' the listing by taking out some more if/then statements, so here's what I eventually came up with:
Code: Select all
10 dim xaxis, yaxis, atemp, spr, index, jtemp, up, down, left
20 dim right, fire, vic, joy, po, bit, cmsb, zero, ff, sd
30 poke 53280,zero:poke 53281,zero
40 gosub 63985
50 print chr$(147)
60 xaxis = 64
70 yaxis = xaxis
80 atemp = left
90 jtemp = atemp+down
100 poke vic+fire, (peek(vic+fire) and cmsb) or (-left*(xaxis>ff))
110 poke vic+atemp, xaxis+(ff*(xaxis>ff))
120 poke vic+jtemp, yaxis
130 joy = bit - peek(po)
140 xaxis = xaxis + abs((joy and right)=right) - abs((joy and left)=left)
150 yaxis = yaxis + abs((joy and up)=up) - abs((joy and down)=down)
160 xaxis = xaxis + abs(xaxis<min) - abs(xaxis > max)
170 yaxis = yaxis + abs(yaxis<zero) - abs(yaxis > ff)
180 on -(joy<>fire) goto 100
190 poke 56334, peek(56334) or 1: rem switches basic interrupts back on
200 end
63985 rem initialisation:
63986 poke 56334, peek(56334) and 254: rem switches off interrupts
63987 fire = 16: right = 8: left = 4: up = 2: down = 1: po = 56320
63988 bit = 127: cmsb = 251: ff = 255: min = right: max = 334: vic = 53248: sd = 832: spr = 21
63989 atemp = 13: jtemp = 4: index = 2042
63990 poke vic+spr, jtemp
63991 poke index, atemp
63992 index = zero
63993 read jtemp
63994 if jtemp>-down then poke sd+index, jtemp: index = index+down: goto 63993
63995 return
63996 rem sprite data:
63997 data ,127,,1,255,192,3,255,224,3,231,224,7,217,240,7,223,240,7,217,240,3,231
63998 data 224,3,255,224,3,255,224,2,255,160,1,127,64,1,62,64,,156,128,,156,128
63999 data ,73,,,73,,,62,,,62,,,62,,,28,,-1
Notice that there is no MSB variable now as it's not needed. Also, with keyword abbreviations, you should be able to type this into a real Commodore (I've not tested that though), but I recommend an IDE like CBM prg Studio as it makes editing far easier.
The main loop (move sprite to joystick thing) is now from lines 100 to 180 (was lines 80 to 180); this is because I've done some line renumbering from the start. Lines 80 and 90 that handled the sprite's 'Most significant bit' on the x-axis is now done in line 100; it clears the MSB first with AND logic, if the x position is greater than 255, the bit will be set: in other words, it will OR 0 or OR 4 after the AND depending on the x position (<=255 or >255 respectively).
Buzbard's trick is then used in line 110; because Commodore Boolean values return -1 if true and zero if false, and 255 * -1 is -255, then we're adding either zero or -255 to the x position
Interrupts have been switched off to save a couple of cycles here and there. There are more speed and efficiency savings that can be made to this listing by combining more lines and obfuscating the variables and such like. I've put a minimized example of this listing on Pastebin here:
Enjoy,
Shaun.