Joystick port II with sprites demo.

SYS 64738
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Joystick port II with sprites demo.

Post by Shaun_B » Mon Jul 07, 2014 2:17 am

It's getting late here so this might not be as clean as it should be, but here's a quick example of reading port II and making it move the sprites:

Code: Select all

0 dim x, y, a, b, s, d, i, j, up, down, left, right, fire, vic, po, bit, msb, cmsb, z, ff
10 def fn b(s) = abs(s)
20 fire  = 16
30 right = 8
40 left  = 4
50 up    = 2
60 down  = 1
70 po    = 56320
80 bit   = 127
90 z     = 0
100 cmsb = 251
110 ff   = 255
120 min  = right
130 max  = 334
140 x    = 62
150 poke 53280,z:poke 53281,z
160 print "{clear}"
170 vic = 53248
180 i   = 2042
190 d   = 832
200 s   = 21
210 a   = 13
220 j   = 4
230 poke vic+s, j
240 poke i, a
250 for i = z to x
260 read j
270 poke d+i, j
280 next
290 x   = 64
300 y   = x
310 a   = 4
320 d   = a+1
330 for i = z to down
340 if x <= ff then msb = z:poke vic+fire, peek(vic+fire) and cmsb
350 if x > ff then msb = ff:poke vic+fire, peek(vic+fire) or left
360 poke vic+a, x-msb
370 poke vic+d, y
380 j = bit - peek(po)
390 x = x + fn b(j=right) - fn b(j=left)
400 y = y + fn b(j=up) - fn b(j=down)
410 x = x + fn b(x<min) - fn b(x > max)
420 y = y + fn b(y<z) - fn b(y > ff)
430 if j = fire then next: end
440 i = z
450 next
63999 data ,127,,1,255,192,3,255,224,3,231,224,7,217,240,7,223,240,7,217,240,3,231,224,3,255,224,3,255,224,2,255,160,1,127,64,1,62,64,,156,128,,156,128,,73,,,73,,,62,,,62,,,62,,,28,
As I can't draw, I've used the famous Balloon from the 'up, up, and away' listing in the CBM BASIC Programming manual.

The listing does not handle diagonal movements yet, and fire will end the demonstration. The data has been minimised - where you see ,, should be a zero. As CBM BASIC will read an empty data entry as zero then I've missed them out. Not sure what happens if you were to read a string.

I've recycled some variables as required and defined some as constants (like z for zero and ff for 255).

The check in line 340 and 350 is to set the MSB for sprite 4 so that you may move the sprite across the whole of the visible screen area. There might be a more efficient way of doing this, I'll have to think about that.

As I'm happy that this is working, I'll add a minimised (hopefully more efficient and/or memory friendly) version as soon as. But obviously you don't need a function to get the ABS value as you can just use ABS is place of the fn b() where ever you see it. Also, all of the variables can be minimised to a single letter. There's a lot of lines as well, this can easily be reduced without much effort. And a 'goto' may be added instead of 'illegal' use of the for/next loop. There's probably a more graceful way to exit the program as well.

Regards,

Shaun.


BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.

MarcWalters
Member
Member
Posts: 49
Joined: Wed Jun 11, 2014 2:33 pm
Location: Lake Macquarie, NSW, Australia

Re: Joystick port II with sprites demo.

Post by MarcWalters » Mon Jul 07, 2014 5:12 pm

A few general suggestions. Keep in mind they're personal preferences and not necessarily better. I realise the program is meant to show a variety of possible techniques (such as the defined function).

1) If treating a BASIC variable as a constant, then assign a value once only.
2) If a variable or constant is given a name that describes its role then use it only in the context of that name - if it is to be shared then give it a name that describes its contents.
3) Define a constant instead of an expression to access a frequently used memory location.
4) Where a variable is used in a tight loop, access it as little as possible.

I thought of a way to make the directional code more efficient and handle both axes. It's not tested though:
The movements and boundary tests can be combined, and a conditional test added to avoid unnecessary calculation:

Code: Select all

jb = j and 3 : if jb > 0 then y = ( ( y > z ) - ( y < ff ) ) + ( ( jb = up ) - ( jb = down ) )
jb = j and 12 : if jb > 0 then x = ( ( x > min ) - ( x < max ) ) + ( ( jb = left ) - ( jb = right ) )

satpro

Re: Joystick port II with sprites demo.

Post by satpro » Mon Jul 07, 2014 5:29 pm

:!: Nice contribution, Marc

User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Re: Joystick port II with sprites demo.

Post by Shaun_B » Mon Jul 07, 2014 7:37 pm

Thanks for that Marc. I have to say things are a little clearer now I've slept. I don't recall the time but I didn't get to sleep until around 3am and I was up at 6am - must unplug. So with a clearer head, I'll give your method a go.

Regards,

Shaun.
BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.

User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Re: Joystick port II with sprites demo.

Post by Shaun_B » Tue Jul 08, 2014 12:33 am

Here is a newer (hopefully cleaner?) version which now handles diagonal movements. It's also been refactored somewhat.

Code: Select all

0 dim xaxis, yaxis, atemp, spr, smem, index, jtemp, up, down, left, right, fire, vic, joy, po, bit, msb, cmsb, zero, ff, sd
10 poke 53280,0:poke 53281,0
20 gosub 63990
30 print "{clear}"
40 xaxis = 64
50 yaxis = xaxis
60 atemp = 4
70 jtemp = atemp+1
80 if xaxis <= ff then msb = zero:poke vic+fire, peek(vic+fire) and cmsb
90 if xaxis > ff then msb = ff:poke vic+fire, peek(vic+fire) or left
100 poke vic+atemp, xaxis-msb
110 poke vic+jtemp, yaxis
120 joy = bit - peek(po)
130 xaxis = xaxis + abs((joy and right)=right) - abs((joy and left)=left)
140 yaxis = yaxis + abs((joy and up)=up) - abs((joy and down)=down)
150 xaxis = xaxis + abs(xaxis<min) - abs(xaxis > max)
160 yaxis = yaxis + abs(yaxis<zero) - abs(yaxis > ff)
170 if joy = fire then end
180 goto 80
63990 fire = 16: right = 8: left  = 4: up = 2: down = 1: po = 56320
63991 bit = 127: zero = 0: cmsb = 251: ff = 255: min = right: max = 334: vic = 53248: sd = 832: spr = 21 
63992 atemp = 13: jtemp = 4: index = 2042
63993 poke vic+spr, jtemp
63994 poke index, atemp
63995 index = 0
63996 read jtemp
63997 if jtemp>-1 then poke sd+index, jtemp: index = index+1: goto 63996
63998 return
63999 data ,127,,1,255,192,3,255,224,3,231,224,7,217,240,7,223,240,7,217,240,3,231,224,3,255,224,3,255,224,2,255,160,1,127,64,1,62,64,,156,128,,156,128,,73,,,73,,,62,,,62,,,62,,,28,,-1
FIrstly, all of the variables, where possible, give you an idea of what they do. po is used in place of port as I got a syntax error. Same with sdata, so I used sd, and there may have been another one.

Most of the constants are set in the sub-routine at line 64990 onwards. The data loop is read until it hits the break condition, which is -1. This is instead of a for/next loop.

The logic to check the MSB is unchanged, and I've removed the function that returns the abs value of the boolean. In place, I've used abs directly.

The diagonal movements are checked with an 'and' gate, using BODMAS logic, the 'and' condition is done first and this is then checked against the constant for the joystick direction. This way, it will check for up and left, or down and left etc... without lots of complicated if...then statements. That may not be a perfect solution, but as it worked when I tested it, I'll take that.

The screen bounds have remained the same, and I've used 'goto' to loop back. Fire exits with the 'end' statement.

I'm not sure how 'quickly' you can handle sprites in BASIC, especially at 1Mhz, but there's obviously a lot of minimisation that you could try, and to save memory obfuscate the variables to one or two characters. You could also switch off interrupts by adding the poke in line 5:

Code: Select all

5 poke 56334, peek(56334) and 254
170 if joy = fire then poke 56334, peek(56334) or 1: end
Line 170 will switch interrupts back on before ending the program as with exception to run/stop + restore, interrupts switched off will disable the keyboard. But the best way to get any sort of performance out of BASIC is to use a good compiler or learn assembly :-)

Regards,

Shaun.
BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.

satpro

Re: Joystick port II with sprites demo.

Post by satpro » Tue Jul 08, 2014 12:51 am

Shawn, can you upload up a .prg for those of us who are too lazy to type?

User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Re: Joystick port II with sprites demo.

Post by Shaun_B » Tue Jul 08, 2014 12:59 am

Sure no worries :-)

Actually, I can't seem to attach files, so here's a link to the prg:

https://dl.dropboxusercontent.com/u/795 ... s/main.prg

Regards,

Shaun.
BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.

User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Re: Joystick port II with sprites demo.

Post by Shaun_B » Tue Jul 08, 2014 9:10 am

I've just realised that line 120 may be changed to:

Code: Select all

120 joy = peek(po)
That's untested so I'm not 100% sure of the results, but as the logic is using 'and', taking off the value in bit may no longer be required. If this is right, the bit constant may be removed altogether (I don't think it's used anywhere else).

I can't test this theory here but I will do so when I get home.

Regards,

Shaun.
BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.

merman
Member
Member
Posts: 146
Joined: Mon Apr 14, 2014 2:50 pm
Location: Skegness

Re: Joystick port II with sprites demo.

Post by merman » Tue Jul 08, 2014 2:48 pm

PORT as a variable gives you an error because it contains the instruction OR. So the interpreter reads it as P OR T

User avatar
buzbard
Member
Member
Posts: 22
Joined: Sun Apr 13, 2014 9:06 pm
Location: Washington State

Re: Joystick port II with sprites demo.

Post by buzbard » Tue Jul 08, 2014 2:52 pm

How the heck did you type in line 63999?
That's almost 5 screen lines, the C64 will only accept 2 screen lines.
I could understand if you had a lot of keywords that could be abbreviated but that's all numbers.

For the msb logic you could use:

Code: Select all

msb = -ff * (xaxis > 255)
And when reading and poking data, a for/next loop is faster and more efficient.
Ray...
It's Ok if you don't agree with me, I can't force you to be right.

Post Reply Previous topicNext topic