Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 08:58 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : CMM2: 64 Bouncing Rubber Duckies

Author Message
jeff510
Newbie

Joined: 19/07/2020
Location: United States
Posts: 13
Posted: 04:58am 21 Jul 2020
Copy link to clipboard 
Print this post

This is a silly little script I put together to bounce 64 rubber duckies on the screen.  I'd appreciate feedback on the general approach - using a 2d array to store the sprite data.  If there's a better way, I'd love to know.

DUCKIES.zip
 
capsikin
Guru

Joined: 30/06/2020
Location: Australia
Posts: 341
Posted: 06:58am 21 Jul 2020
Copy link to clipboard 
Print this post

Cool, it's good to see that sort of example.

I prefer to have the object's properties in separate arrays.

e.g. instead of
movers(mover,0) = rnd*SCREEN_RIGHT          'X POS
movers(mover,1) = RND*SCREEN_BOTTOM         'Y POS
movers(mover,2) = rnd*4-2                   'X VEL

you'd have
x_pos(mover) = rnd*SCREEN_RIGHT          'X POS
y_pos(mover) = RND*SCREEN_BOTTOM         'Y POS
x_vel(mover) = rnd*4-2                   'X VEL

or
xPos(mover) = rnd*SCREEN_RIGHT          'X POS
yPos(mover) = RND*SCREEN_BOTTOM         'Y POS
xVel(mover) = rnd*4-2                   'X VEL

I don't know if it's better but I find it easier to read.

If you want, you could include "movers" in the array names, e.g.
movers_x_pos(mover) = rnd*SCREEN_RIGHT          'X POS
movers_y_pos(mover) = RND*SCREEN_BOTTOM         'Y POS

or
x_pos_movers(mover) = rnd*SCREEN_RIGHT          'X POS
y_pos_movers(mover) = RND*SCREEN_BOTTOM         'Y POS


update: I just noticed you can use periods in variable names.
So you could use names like
movers.xVel(mover)
and
movers.yPos(mover)

This suggests they're part of the same "movers" structure, if you're used to some other languages.
Edited 2020-07-21 17:05 by capsikin
 
jeff510
Newbie

Joined: 19/07/2020
Location: United States
Posts: 13
Posted: 03:47pm 21 Jul 2020
Copy link to clipboard 
Print this post

  Quote  update: I just noticed you can use periods in variable names.
So you could use names like
movers.xVel(mover)
and
movers.yPos(mover)

This suggests they're part of the same "movers" structure, if you're used to some other languages.


Perfect! That's much more readable.
 
mkopack73
Senior Member

Joined: 03/07/2020
Location: United States
Posts: 261
Posted: 06:17pm 21 Jul 2020
Copy link to clipboard 
Print this post

Ooh! Totally am going to look at this tonight.  I was just starting to dig into sprites last night and could use some example code to look at.  Thanks!!
 
vegipete

Guru

Joined: 29/01/2013
Location: Canada
Posts: 1132
Posted: 07:13pm 21 Jul 2020
Copy link to clipboard 
Print this post

Them thars a whole lotta duckies!

I like it.

I would _not_ split the arrays into separately named ones. A few times now I started with separate arrays for each aspect of a collection of items to be animated and found processing easier once they were combined.

If you are concerned with readability, assign the array index numbers to named constants:
CONST X_POS = 0
CONST Y_POS = 1
CONST X_VEL = 2
CONST Y_VEL = 3
CONST   RED = 4
CONST  BLUE = 5
CONST GREEN = 6


Then a statement like
SPRITE next mover+1,movers(mover,X_POS ), movers(mover,Y_POS )
is easy to understand.
Because sprites start with number 1, I prefer not to use element 0 for the first sprite. Element 0 could instead hold some globally relevant data - maybe the next sprite to update or whatever. Then the slightly confusing "SPRITE next mover+1, ..." gets simplified to "SPRITE next mover, ..."

Also, you can clean up the flickering by using pages.
For example, add the line "page write 1 : cls" right after selecting the mode.
The, just before the PAUSE command at the end, add the line "page copy 1,0,B"
This combination changes all your drawing to happen on page 1 (which is invisible) and then copies the entire thing to page 0 (which _is_ visible) during the next blanking interval.

I'd love to see a version of bouncing duckies with collision detection, so they bounce off each other.

Great work!
Visit Vegipete's *Mite Library for cool programs.
 
Poppy

Guru

Joined: 25/07/2019
Location: Germany
Posts: 486
Posted: 07:21pm 21 Jul 2020
Copy link to clipboard 
Print this post

There is only one Rubber Duck!

Looking forward to checking this out after finally having got my CMM2!
Edited 2020-07-22 05:21 by Poppy
Andre ... such a GURU?
 
Womble

Senior Member

Joined: 09/07/2020
Location: United Kingdom
Posts: 267
Posted: 09:04pm 21 Jul 2020
Copy link to clipboard 
Print this post

  Poppy said  There is only one Rubber Duck!

Ten Four Poppy
 
jeff510
Newbie

Joined: 19/07/2020
Location: United States
Posts: 13
Posted: 06:04am 22 Jul 2020
Copy link to clipboard 
Print this post

Alright, I've used pages to clean up the flickering, and constants for the indices.  It looks much more clean and readable.

vegipete said:
  Quote  I'd love to see a version of bouncing duckies with collision detection, so they bounce off each other.


I did that too.  It's called DUCKIES_GROUP_COLLIDE.BAS

I tried to figure out a way to get the duckies to orient to their x velocity, but they leave artifacts when they flip, even with a CLS.  

DUCKIES.zip
Edited 2020-07-22 17:11 by jeff510
 
Poppy

Guru

Joined: 25/07/2019
Location: Germany
Posts: 486
Posted: 10:21am 22 Jul 2020
Copy link to clipboard 
Print this post

  Womble said  
  Poppy said  There is only one Rubber Duck!

Ten Four Poppy


Ten Two - Ten Four Womble!
Andre ... such a GURU?
 
capsikin
Guru

Joined: 30/06/2020
Location: Australia
Posts: 341
Posted: 03:51am 13 Aug 2020
Copy link to clipboard 
Print this post

  jeff510 said  Alright, I've used pages to clean up the flickering, and constants for the indices.  It looks much more clean and readable.

vegipete said:
  Quote  I'd love to see a version of bouncing duckies with collision detection, so they bounce off each other.


I did that too.  It's called DUCKIES_GROUP_COLLIDE.BAS

I tried to figure out a way to get the duckies to orient to their x velocity, but they leave artifacts when they flip, even with a CLS.  

DUCKIES.zip


Do you have the version that leaves artifacts?

It would be good to figure out how to change sprite graphics, it's pretty common in games.

(update - if you don't have it I can figure out how to put in the SPRITE SWAP command, which then gets to the harder task of figuring out how to prevent artifacts - they are probably from changing sprites that are underneath other sprites)

(update 2 - I didn't understand SPRITE SWAP as well as I thought)
Edited 2020-08-15 10:04 by capsikin
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025