Author Topic: Bouncing Balls over a Plasma sky  (Read 6007 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Bouncing Balls over a Plasma sky
« on: December 25, 2020, 10:03:06 pm »
Playing around with some old source code, trying to turn it into something more interesting.  Here are bouncing balls combined with a plasma background.  Runs kind of slow on my PC, but maybe not on yours... 

- Dav

Code: QB64: [Select]
  1. 'Balls&Plasma.bas
  2.  
  3. SCREEN _NEWIMAGE(700, 700, 32)
  4.  
  5. f = _LOADFONT("cyberbit.ttf", 30): _FONT f
  6.  
  7. balls = 50: size = 40: speed = 10
  8.  
  9. DIM BallX(balls), BallY(balls)
  10. DIM BallXdir(balls), BallYdir(balls)
  11. DIM BallSpeed(balls), BallSize(balls)
  12.  
  13. w = _WIDTH: h = _HEIGHT: w2 = _WIDTH / 2: h2 = _HEIGHT / 2
  14.  
  15. 'Generate random ball data
  16. FOR B = 1 TO balls
  17.     BallX(B) = RND * w: BallY(B) = RND * h
  18.     BallSpeed(B) = RND * speed + 1
  19.     BallSize(B) = 40 + (RND * 50)
  20.  
  21.     CLS
  22.     'compute ball movement
  23.     FOR t = 1 TO balls
  24.         IF BallX(t) > w - BallSize(t) THEN BallXdir(t) = 0
  25.         IF BallX(t) < BallSize(t) THEN BallXdir(t) = 1
  26.         IF BallXdir(t) THEN BallX(t) = BallX(t) + BallSpeed(t) ELSE BallX(t) = BallX(t) - BallSpeed(t)
  27.         IF BallY(t) > h - BallSize(t) THEN BallYdir(t) = 0
  28.         IF BallY(t) < BallSize(t) THEN BallYdir(t) = 1
  29.         IF BallYdir(t) THEN BallY(t) = BallY(t) + BallSpeed(t) ELSE BallY(t) = BallY(t) - BallSpeed(t)
  30.     NEXT
  31.     'draw plasma
  32.     t = TIMER
  33.     FOR x = 0 TO w
  34.         FOR y = 0 TO h STEP 2
  35.             r = SIN(1.1 * t) * h2 - y + h2
  36.             g = SIN(.9 * t) * w2 - x + w2
  37.             B = SIN(x / w2 + t + y / h2) * r
  38.             PSET (x, y), _RGB(r, g, B)
  39.         NEXT
  40.         t = t + .01
  41.     NEXT
  42.     'draw balls
  43.     FOR i = 1 TO balls
  44.         ball BallX(i), BallY(i), BallSize(i), 255 - (4 * i), (4 * i), (5 * i)
  45.         _PRINTSTRING (BallX(i) - 15, BallY(i) - 15), STR$(i)
  46.     NEXT
  47.     _DISPLAY: _LIMIT 60
  48.  
  49.  
  50. SUB ball (BallX, BallY, size, r, g, b)
  51.     FOR s = 1 TO size STEP .5
  52.         CIRCLE (BallX, BallY), s, _RGB(r, g, b)
  53.         r = r - 1: g = g - 1: b = b - 1
  54.     NEXT
  55.  
« Last Edit: December 25, 2020, 10:24:41 pm by Dav »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Bouncing Balls over a Plasma sky
« Reply #1 on: December 26, 2020, 12:56:13 pm »
Cool!
On my i3 Toshiba with HDD runs very well, maybe on another faster machine we need a _limit in the loop.
Programming isn't difficult, only it's  consuming time and coffee

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Bouncing Balls over a Plasma sky
« Reply #2 on: December 26, 2020, 08:05:08 pm »
  Tried to get a realistic collision between balls. Not that easy.  Calculated the distance between balls.

The code will print out the distance and will delay 1 second when there is a collision.

Code: QB64: [Select]
  1. 'Balls&Plasma.bas
  2.  
  3. SCREEN _NEWIMAGE(700, 700, 32)
  4.  
  5. f = _LOADFONT("cyberbit.ttf", 30): _FONT f
  6.  
  7. balls = 2: size = 40: speed = 10
  8.  
  9. DIM BallX(balls), BallY(balls)
  10. DIM BallXdir(balls), BallYdir(balls)
  11. DIM BallSpeed(balls), BallSize(balls)
  12.  
  13. w = _WIDTH: h = _HEIGHT: w2 = _WIDTH / 2: h2 = _HEIGHT / 2
  14.  
  15. 'Generate random ball data
  16. FOR B = 1 TO balls
  17.     BallX(B) = RND * w: BallY(B) = RND * h
  18.     BallSpeed(B) = RND * speed + 1
  19.     BallSize(B) = 40 '+ (RND * 50)
  20.  
  21.     CLS
  22.     'compute ball movement
  23.     FOR t = 1 TO balls
  24.  
  25.  
  26.         d% = ((BallX(1) - BallX(2)) ^ 2 + (BallY(1) - BallY(2)) ^ 2) ^ .5
  27.         PRINT d%
  28.  
  29.         'IF d% <= 80 THEN _DELAY 1
  30.  
  31.  
  32.         IF BallX(t) > w - BallSize(t) THEN BallXdir(t) = 1
  33.         IF BallX(t) < BallSize(t) THEN BallXdir(t) = 0
  34.  
  35.         IF BallY(t) > h - BallSize(t) THEN BallYdir(t) = 1
  36.         IF BallY(t) < BallSize(t) THEN BallYdir(t) = 0
  37.  
  38.         IF BallXdir(t) = 0 THEN BallX(t) = BallX(t) + BallSpeed(t)
  39.         IF BallXdir(t) = 1 THEN BallX(t) = BallX(t) - BallSpeed(t)
  40.  
  41.         IF BallYdir(t) = 0 THEN BallY(t) = BallY(t) + BallSpeed(t)
  42.         IF BallYdir(t) = 1 THEN BallY(t) = BallY(t) - BallSpeed(t)
  43.  
  44.  
  45.  
  46.         ' IF BallYdir(t) = 0 THEN BallY(t) = BallY(t) + BallSpeed(t) ELSE BallY(t) = BallY(t) - BallSpeed(t)
  47.     NEXT
  48.     'draw plasma
  49.     t = TIMER
  50.     FOR x = 0 TO w
  51.         FOR y = 0 TO h STEP 2
  52.             r = SIN(1.1 * t) * h2 - y + h2
  53.             g = SIN(.9 * t) * w2 - x + w2
  54.             B = SIN(x / w2 + t + y / h2) * r
  55.             PSET (x, y), _RGB(r, g, B)
  56.         NEXT
  57.         t = t + .01
  58.     NEXT
  59.     'draw balls
  60.     FOR i = 1 TO balls
  61.         ball BallX(i), BallY(i), BallSize(i), 255 - (4 * i), (4 * i), (5 * i)
  62.         _PRINTSTRING (BallX(i) - 15, BallY(i) - 15), STR$(i)
  63.     NEXT
  64.     _DISPLAY: _LIMIT 60
  65.     IF d% <= 80 THEN _DELAY 1
  66.  
  67.  
  68. SUB ball (BallX, BallY, size, r, g, b)
  69.     FOR s = 1 TO size STEP .5
  70.         CIRCLE (BallX, BallY), s, _RGB(r, g, b)
  71.         r = r - 1: g = g - 1: b = b - 1
  72.     NEXT
  73.  

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Bouncing Balls over a Plasma sky
« Reply #3 on: December 26, 2020, 08:50:57 pm »
@TempodiBasic :  Thanks for trying it out!

@NOVARSEG : That's pretty good.  I may have to borrow that method for something.  Thanks for trying the code out.

I tried to make a little game out of this, clicking away the balls in order, starting at ball 1.  Not perfect as I'm reading squares of mouse x/y input data, and the balls are in circles.  Messy code here warning, but it's a start at least.  I tweaked the plasma code to made it smoother on my slow laptop.

- Dav

Code: QB64: [Select]
  1. 'ClickAwayBalls.bas
  2. 'by Dav, DEC/2020
  3.  
  4. 'Click on balls in order, starting at 1 untill all gone.
  5.  
  6.  
  7. SCREEN _NEWIMAGE(700, 700, 32)
  8.  
  9. f = _LOADFONT("cyberbit.ttf", 30): _FONT f
  10.  
  11. balls = 15: size = 40: speed = 10
  12.  
  13. DIM BallX(balls), BallY(balls)
  14. DIM BallXdir(balls), BallYdir(balls)
  15. DIM BallSpeed(balls), BallSize(balls)
  16. DIM BallShow(balls)
  17.  
  18. w = _WIDTH: h = _HEIGHT: w2 = _WIDTH / 2: h2 = _HEIGHT / 2
  19.  
  20. 'Generate random ball data
  21. FOR B = 1 TO balls
  22.     BallX(B) = RND * w: BallY(B) = RND * h
  23.     BallSpeed(B) = RND * speed + 1
  24.     BallSize(B) = 40 + (RND * 50)
  25.     BallShow(B) = 1
  26.  
  27. curball = 1
  28.  
  29.     CLS
  30.     'compute ball movement
  31.     FOR t = 1 TO balls
  32.         IF BallX(t) > w - BallSize(t) THEN BallXdir(t) = 0
  33.         IF BallX(t) < BallSize(t) THEN BallXdir(t) = 1
  34.         IF BallXdir(t) THEN BallX(t) = BallX(t) + BallSpeed(t) ELSE BallX(t) = BallX(t) - BallSpeed(t)
  35.         IF BallY(t) > h - BallSize(t) THEN BallYdir(t) = 0
  36.         IF BallY(t) < BallSize(t) THEN BallYdir(t) = 1
  37.         IF BallYdir(t) THEN BallY(t) = BallY(t) + BallSpeed(t) ELSE BallY(t) = BallY(t) - BallSpeed(t)
  38.     NEXT
  39.     'draw background
  40.     t = TIMER
  41.     FOR x = 0 TO w
  42.         FOR y = 0 TO h
  43.             r = SIN(1.1 * t) * h2 - y + h2
  44.             PSET (x, y), _RGB(r, r - y, -r)
  45.         NEXT
  46.         t = t + .01
  47.         GOSUB GetMouseClick
  48.     NEXT
  49.  
  50.     IF gameover = 1 THEN
  51.         PLAY "o2l16cegagfefgabgc3l4"
  52.         END
  53.     END IF
  54.  
  55.     'draw balls
  56.     FOR i = 1 TO balls
  57.         IF BallShow(i) = 1 THEN
  58.             ball BallX(i), BallY(i), BallSize(i), 255 - (4 * i), (4 * i), (5 * i)
  59.             _PRINTSTRING (BallX(i) - 15, BallY(i) - 15), STR$(i)
  60.         END IF
  61.     NEXT
  62.  
  63.     LOCATE 1, 1: PRINT "Click ball.."; curball;
  64.     _DISPLAY: _LIMIT 60
  65.  
  66.     'If click on only one ball (no overlayed ones) remove it
  67.     IF found = 1 THEN
  68.         IF firstball = curball THEN
  69.             'erase ball
  70.             ball BallX(firstball), BallY(firstball), BallSize(firstball), 255, 200, 200
  71.             BallShow(firstball) = 0
  72.             PLAY "mbl32o2ceg"
  73.             _DISPLAY: _DELAY .05
  74.             curball = curball + 1
  75.             found = 0
  76.         ELSE
  77.             found = found + 1
  78.             lastfound = firstball
  79.         END IF
  80.     END IF
  81.  
  82.     'If clicked over several balls, highlight the top most one
  83.     IF found > 1 THEN
  84.         'BallShow(lastfound) = 0
  85.         ball BallX(lastfound), BallY(lastfound), BallSize(lastfound), 255, 200, 100
  86.         _PRINTSTRING (BallX(lastfound) - 15, BallY(lastfound) - 15), STR$(lastfound)
  87.         _DISPLAY: PLAY "mbl32o2bo3fo2b"
  88.         found = 0
  89.         _DELAY .1
  90.     END IF
  91.  
  92.     'check if all calls clicked away
  93.     anyleft = 0
  94.     FOR c = 1 TO balls
  95.         IF BallShow(c) = 1 THEN anyleft = anyleft + 1
  96.     NEXT
  97.     IF anyleft = 0 THEN
  98.         gameover = 1
  99.     END IF
  100.  
  101.  
  102.  
  103. '==============
  104. GetMouseClick:
  105. '==============
  106.  
  107. IF _MOUSEBUTTON(1) = 0 THEN done = 0
  108. IF _MOUSEBUTTON(1) AND done = 0 THEN
  109.     mx = _MOUSEX: my = _MOUSEY
  110.     found = 0
  111.     FOR m = 1 TO balls
  112.         IF BallShow(m) = 1 THEN
  113.             IF mx > (BallX(m) - BallSize(m)) AND mx < (BallX(m) + BallSize(m)) THEN
  114.                 IF my > (BallY(m) - BallSize(m)) AND my < (BallY(m) + BallSize(m)) THEN
  115.                     IF found = 0 THEN firstball = m
  116.                     found = found + 1
  117.                     IF found > 1 THEN
  118.                         lastfound = m
  119.                     END IF
  120.                 END IF
  121.             END IF
  122.         END IF
  123.     NEXT
  124.     done = 1
  125.  
  126.  
  127.  
  128. SUB ball (BallX, BallY, size, r, g, b)
  129.     FOR s = 1 TO size STEP .4
  130.         CIRCLE (BallX, BallY), s, _RGB(r, g, b)
  131.         r = r - 1: g = g - 1: b = b - 1
  132.     NEXT
  133.  

ClickAwayBalls.jpg
* ClickAwayBalls.jpg (Filesize: 51.21 KB, Dimensions: 708x728, Views: 141)
« Last Edit: December 26, 2020, 08:52:18 pm by Dav »

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Bouncing Balls over a Plasma sky
« Reply #4 on: December 26, 2020, 10:23:33 pm »
Having trouble with PRINT

   
Quote
d% = ((BallX(1) - BallX(2)) ^ 2 + (BallY(1) - BallY(2)) ^ 2) ^ .5

    LOCATE 10, 10: PRINT d%

    IF d% <= 80 THEN _DELAY 1

So run the code and find that 80 or less than 80 is never printed to the screen when the balls FIRST collide.

Code: QB64: [Select]
  1. 'Balls&Plasma.bas
  2.  
  3. SCREEN _NEWIMAGE(700, 700, 32)
  4.  
  5. f = _LOADFONT("cyberbit.ttf", 30): _FONT f
  6. '_PRINTMODE _KEEPBACKGROUND
  7.  
  8. balls = 2: size = 40: speed = 10
  9.  
  10. DIM BallX(balls), BallY(balls)
  11. DIM BallXdir(balls), BallYdir(balls)
  12. DIM BallSpeed(balls), BallSize(balls)
  13.  
  14. w = _WIDTH: h = _HEIGHT: w2 = _WIDTH / 2: h2 = _HEIGHT / 2
  15.  
  16. 'Generate random ball data
  17. FOR B = 1 TO balls
  18.     BallX(B) = RND * w: BallY(B) = RND * h
  19.     BallSpeed(B) = RND * speed + 1
  20.     BallSize(B) = 40 '+ (RND * 50)
  21.  
  22.     CLS
  23.  
  24.  
  25.  
  26.     'compute ball movement
  27.     FOR t = 1 TO balls
  28.  
  29.         LL:
  30.         IF BallX(t) > w - BallSize(t) THEN BallXdir(t) = 1
  31.         IF BallX(t) < BallSize(t) THEN BallXdir(t) = 0
  32.  
  33.         IF BallY(t) > h - BallSize(t) THEN BallYdir(t) = 1
  34.         IF BallY(t) < BallSize(t) THEN BallYdir(t) = 0
  35.  
  36.         IF BallXdir(t) = 0 THEN BallX(t) = BallX(t) + BallSpeed(t)
  37.         IF BallXdir(t) = 1 THEN BallX(t) = BallX(t) - BallSpeed(t)
  38.  
  39.         IF BallYdir(t) = 0 THEN BallY(t) = BallY(t) + BallSpeed(t)
  40.         IF BallYdir(t) = 1 THEN BallY(t) = BallY(t) - BallSpeed(t)
  41.  
  42.  
  43.     NEXT
  44.  
  45.  
  46.  
  47.     'draw plasma
  48.     ' t = TIMER
  49.     ' FOR x = 0 TO w
  50.     ' FOR y = 0 TO h STEP 2
  51.     ' r = SIN(1.1 * t) * h2 - y + h2
  52.     ' g = SIN(.9 * t) * w2 - x + w2
  53.     ' B = SIN(x / w2 + t + y / h2) * r
  54.     ' PSET (x, y), _RGB(r, g, B)
  55.     ' NEXT
  56.     ' t = t + .01
  57.     ' NEXT
  58.     'draw balls
  59.  
  60.  
  61.  
  62.  
  63.     FOR i = 1 TO balls
  64.         ball BallX(i), BallY(i), BallSize(i), 255 - (4 * i), (4 * i), (5 * i)
  65.         _PRINTSTRING (BallX(i) - 15, BallY(i) - 15), STR$(i)
  66.     NEXT
  67.  
  68.  
  69.     d% = ((BallX(1) - BallX(2)) ^ 2 + (BallY(1) - BallY(2)) ^ 2) ^ .5
  70.  
  71.     LOCATE 10, 10: PRINT d%
  72.  
  73.     IF d% <= 80 THEN _DELAY 1
  74.  
  75.  
  76.     _DISPLAY: _LIMIT 60
  77.  
  78.  
  79.  
  80.  
  81. SUB ball (BallX, BallY, size, r, g, b)
  82.     FOR s = 1 TO size STEP .5
  83.         CIRCLE (BallX, BallY), s, _RGB(r, g, b)
  84.         r = r - 1: g = g - 1: b = b - 1
  85.     NEXT


Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Bouncing Balls over a Plasma sky
« Reply #5 on: December 26, 2020, 10:44:35 pm »
 
Quote
Having trouble with PRINT

No you are having trouble with _DISPLAY!

When you use that, you have to say _DISPLAY every where you want to see PRINT immediately.

You can turn off _DISPLAY with _AUTODISPLAY or just say _DISPLAY again after the PRINT.

@Dav  LINE with BF might be alittle bit faster than PSET according to @SMcNeill.
« Last Edit: December 26, 2020, 10:47:30 pm by bplus »

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Bouncing Balls over a Plasma sky
« Reply #6 on: December 26, 2020, 11:11:14 pm »
bplus you are right .  I put a _DISPLAY right after the PRINT and it works.


Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Bouncing Balls over a Plasma sky
« Reply #7 on: December 26, 2020, 11:35:49 pm »
Got the code tp print distance and relative direction.

Code: QB64: [Select]
  1. 'Balls&Plasma.bas
  2.  
  3. SCREEN _NEWIMAGE(700, 700, 32)
  4.  
  5. f = _LOADFONT("cyberbit.ttf", 30): _FONT f
  6. '_PRINTMODE _KEEPBACKGROUND
  7.  
  8. balls = 2: size = 40: speed = 1
  9.  
  10. DIM BallX(balls), BallY(balls)
  11. DIM BallXdir(balls), BallYdir(balls)
  12. DIM BallSpeed(balls), BallSize(balls)
  13.  
  14. w = _WIDTH: h = _HEIGHT: w2 = _WIDTH / 2: h2 = _HEIGHT / 2
  15.  
  16. 'Generate random ball data
  17. FOR B = 1 TO balls
  18.     BallX(B) = RND * w: BallY(B) = RND * h
  19.     BallSpeed(B) = RND * speed + 1
  20.     BallSize(B) = 40 '+ (RND * 50)
  21.  
  22.     CLS
  23.  
  24.  
  25.     'compute ball movement
  26.     FOR t = 1 TO balls
  27.  
  28.         LL:
  29.         IF BallX(t) > w - BallSize(t) THEN BallXdir(t) = 1
  30.         IF BallX(t) < BallSize(t) THEN BallXdir(t) = 0
  31.  
  32.         IF BallY(t) > h - BallSize(t) THEN BallYdir(t) = 1
  33.         IF BallY(t) < BallSize(t) THEN BallYdir(t) = 0
  34.  
  35.         IF BallXdir(t) = 0 THEN BallX(t) = BallX(t) + BallSpeed(t)
  36.         IF BallXdir(t) = 1 THEN BallX(t) = BallX(t) - BallSpeed(t)
  37.  
  38.         IF BallYdir(t) = 0 THEN BallY(t) = BallY(t) + BallSpeed(t)
  39.         IF BallYdir(t) = 1 THEN BallY(t) = BallY(t) - BallSpeed(t)
  40.  
  41.  
  42.     NEXT
  43.  
  44.  
  45.  
  46.     'draw plasma
  47.     ' t = TIMER
  48.     ' FOR x = 0 TO w
  49.     ' FOR y = 0 TO h STEP 2
  50.     ' r = SIN(1.1 * t) * h2 - y + h2
  51.     ' g = SIN(.9 * t) * w2 - x + w2
  52.     ' B = SIN(x / w2 + t + y / h2) * r
  53.     ' PSET (x, y), _RGB(r, g, B)
  54.     ' NEXT
  55.     ' t = t + .01
  56.     ' NEXT
  57.     'draw balls
  58.  
  59.  
  60.  
  61.  
  62.     FOR i = 1 TO balls
  63.         ball BallX(i), BallY(i), BallSize(i), 255 - (4 * i), (4 * i), (5 * i)
  64.         _PRINTSTRING (BallX(i) - 15, BallY(i) - 15), STR$(i)
  65.     NEXT
  66.  
  67.     d% = ((BallX(1) - BallX(2)) ^ 2 + (BallY(1) - BallY(2)) ^ 2) ^ .5
  68.  
  69.  
  70.     LOCATE 10, 10: PRINT "Distance"
  71.     LOCATE 11, 10: PRINT d%
  72.     LOCATE 12, 10: PRINT
  73.     LOCATE 13, 10: PRINT "Relative"
  74.     LOCATE 14, 10: PRINT "Direction"
  75.     LOCATE 15, 10: PRINT (BallX(1) - BallX(2)) / (BallY(1) - BallY(2))
  76.  
  77.     _DISPLAY
  78.     IF d% <= 80 THEN _DELAY 1
  79.  
  80.     _DISPLAY
  81.     _LIMIT 60
  82.  
  83.  
  84.  
  85.  
  86. SUB ball (BallX, BallY, size, r, g, b)
  87.     FOR s = 1 TO size STEP .5
  88.         CIRCLE (BallX, BallY), s, _RGB(r, g, b)
  89.         r = r - 1: g = g - 1: b = b - 1
  90.     NEXT


Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Bouncing Balls over a Plasma sky
« Reply #8 on: December 27, 2020, 09:49:36 am »
Thanks, @bplus.  I played around with that and put LINE in this new version. 

Gave the game a little challenge now.  Added a game timer, click them all in 20 seconds.   speed increases as you go.  Clicking the wrong ball restarts game.  I lowered _LIMIT as @TempodiBasic suggested.  Hope it doesn't run too fast now.  Have a ball...

- Dav

Code: QB64: [Select]
  1. 'ClickAwayBalls.bas v1.1
  2. 'new: speed increases, added timer, clicking bad choice restarts.
  3. 'by Dav, DEC/2020
  4.  
  5. 'Click on balls in order, starting at 1 untill all gone,
  6. 'before the timer runs out.  Clicking wrong number restarts.
  7.  
  8.  
  9. SCREEN _NEWIMAGE(700, 700, 32)
  10.  
  11. f = _LOADFONT("cyberbit.ttf", 30): _FONT f
  12.  
  13. balls = 15: size = 40: speed = 3
  14.  
  15. DIM BallX(balls), BallY(balls)
  16. DIM BallXdir(balls), BallYdir(balls)
  17. DIM BallSpeed(balls), BallSize(balls)
  18. DIM BallShow(balls)
  19.  
  20. w = _WIDTH: h = _HEIGHT: w2 = _WIDTH / 2: h2 = _HEIGHT / 2
  21.  
  22. restart:
  23.  
  24. 'Generate random ball data
  25. FOR B = 1 TO balls
  26.     BallX(B) = RND * w: BallY(B) = RND * h
  27.     BallSpeed(B) = RND * speed + 1
  28.     BallSize(B) = 40 + (RND * 50)
  29.     BallShow(B) = 1
  30.  
  31. curball = 1
  32.  
  33. gametime = TIMER
  34.  
  35. timelimit = 20
  36.  
  37.     CLS
  38.     'compute ball movement
  39.     FOR t = 1 TO balls
  40.         IF BallX(t) > w - BallSize(t) THEN BallXdir(t) = 0
  41.         IF BallX(t) < BallSize(t) THEN BallXdir(t) = 1
  42.         IF BallXdir(t) THEN BallX(t) = BallX(t) + BallSpeed(t) ELSE BallX(t) = BallX(t) - BallSpeed(t)
  43.         IF BallY(t) > h - BallSize(t) THEN BallYdir(t) = 0
  44.         IF BallY(t) < BallSize(t) THEN BallYdir(t) = 1
  45.         IF BallYdir(t) THEN BallY(t) = BallY(t) + BallSpeed(t) ELSE BallY(t) = BallY(t) - BallSpeed(t)
  46.     NEXT
  47.     'draw background
  48.     t = TIMER
  49.     FOR x = 0 TO w
  50.         FOR y = 0 TO h
  51.             r = SIN(1.1 * t) * h2 - y + h2
  52.             'PSET (x, y), _RGB(r, r - y, -r)
  53.             LINE (x, y)-(x, y), _RGB(r, r - y, -r), BF
  54.         NEXT
  55.         t = t + .01
  56.         GOSUB GetMouseClick
  57.     NEXT
  58.  
  59.     IF gameover = 1 THEN
  60.         PLAY "o2l16cegagfefgabgc3l4"
  61.         SLEEP 3
  62.         GOTO restart
  63.     END IF
  64.  
  65.     'draw balls
  66.     FOR i = 1 TO balls
  67.         IF BallShow(i) = 1 THEN
  68.             ball BallX(i), BallY(i), BallSize(i), 255 - (4 * i), (4 * i), (5 * i)
  69.             _PRINTSTRING (BallX(i) - 15, BallY(i) - 15), STR$(i)
  70.         END IF
  71.     NEXT
  72.  
  73.     LOCATE 1, 1: PRINT "Click ball.."; curball;
  74.     LOCATE 2, 1: PRINT timelimit - INT(TIMER - gametime);
  75.     _DISPLAY: _LIMIT 30
  76.  
  77.     'If click on one ball (no overlayed oned) remove it
  78.     IF found = 1 THEN
  79.         IF firstball = curball THEN
  80.             'erase ball
  81.             ball BallX(firstball), BallY(firstball), BallSize(firstball), 255, 200, 200
  82.             BallShow(firstball) = 0
  83.             PLAY "mbl32o2ceg"
  84.             _DISPLAY: _DELAY .05
  85.             curball = curball + 1
  86.             found = 0
  87.  
  88.             'Increase spped of balls
  89.             FOR s = 1 TO balls
  90.                 BallSpeed(s) = BallSpeed(s) + 1
  91.             NEXT
  92.  
  93.         ELSE
  94.             found = found + 1
  95.             lastfound = firstball
  96.         END IF
  97.     END IF
  98.  
  99.     'If click over several balls, remove top one
  100.     IF found > 1 THEN
  101.         'BallShow(lastfound) = 0
  102.         ball BallX(lastfound), BallY(lastfound), BallSize(lastfound), 255, 200, 100
  103.         _PRINTSTRING (BallX(lastfound) - 15, BallY(lastfound) - 15), STR$(lastfound)
  104.         _DISPLAY: PLAY "mbl16o2fbfbl8f"
  105.         found = 0
  106.         _DELAY .5
  107.         GOTO restart
  108.     END IF
  109.  
  110.     'check if all clicked
  111.     anyleft = 0
  112.     FOR c = 1 TO balls
  113.         IF BallShow(c) = 1 THEN anyleft = anyleft + 1
  114.     NEXT
  115.     IF anyleft = 0 THEN
  116.         gameover = 1
  117.     END IF
  118.  
  119.     IF TIMER - gametime > timelimit THEN
  120.         PLAY "mbo1l4dl8ddl4dl8feeddc#l2d"
  121.         SLEEP 3
  122.         GOTO restart
  123.     END IF
  124.  
  125.  
  126.  
  127. '==============
  128. GetMouseClick:
  129. '==============
  130.  
  131. IF _MOUSEBUTTON(1) = 0 THEN done = 0
  132. IF _MOUSEBUTTON(1) AND done = 0 THEN
  133.     mx = _MOUSEX: my = _MOUSEY
  134.     found = 0
  135.     FOR m = 1 TO balls
  136.         IF BallShow(m) = 1 THEN
  137.             IF mx > (BallX(m) - BallSize(m)) AND mx < (BallX(m) + BallSize(m)) THEN
  138.                 IF my > (BallY(m) - BallSize(m)) AND my < (BallY(m) + BallSize(m)) THEN
  139.                     IF found = 0 THEN firstball = m
  140.                     found = found + 1
  141.                     IF found > 1 THEN
  142.                         lastfound = m
  143.                     END IF
  144.                 END IF
  145.             END IF
  146.         END IF
  147.     NEXT
  148.     done = 1
  149.  
  150.  
  151.  
  152. SUB ball (BallX, BallY, size, r, g, b)
  153.     FOR s = 1 TO size STEP .4
  154.         CIRCLE (BallX, BallY), s, _RGB(r, g, b)
  155.         r = r - 1: g = g - 1: b = b - 1
  156.     NEXT
  157.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Bouncing Balls over a Plasma sky
« Reply #9 on: December 27, 2020, 12:47:15 pm »
I tried game and not enough variation of direction, a couple times the higher balls overlapped smaller the whole 20 secs

FellippeHeitor

  • Guest
Re: Bouncing Balls over a Plasma sky
« Reply #10 on: December 27, 2020, 12:57:04 pm »
Fun game, Dav. I love the plasma effect, I think I'd seen this bit before in one of your previously shared pieces.

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Bouncing Balls over a Plasma sky
« Reply #11 on: December 27, 2020, 01:33:25 pm »
@bplus. Yes, i agree. Happens to me too. I'll figure something out there. I wonder how I can get mouse x/y in a circle instead of a rectangle? I'm gonna think on it.

@FellippeHeitor, i think I did post that little plasma things before.

- Dav
« Last Edit: December 27, 2020, 02:04:25 pm by Dav »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Bouncing Balls over a Plasma sky
« Reply #12 on: December 27, 2020, 02:35:33 pm »
Some more variation in ball movement, center screen, more time, no so big balls but more colors can almost get them all heh heh..

Code: QB64: [Select]
  1. 'ClickAwayBalls.bas v1.1
  2. 'new: speed increases, added timer, clicking bad choice restarts.
  3. 'by Dav, DEC/2020
  4.  
  5. 'Click on balls in order, starting at 1 untill all gone,
  6. 'before the timer runs out.  Clicking wrong number restarts.
  7.  
  8. SCREEN _NEWIMAGE(800, 600, 32)
  9. _DELAY .25
  10.  
  11. f = _LOADFONT("cyberbit.ttf", 30): _FONT f
  12.  
  13. balls = 15: size = 40: speed = 3
  14.  
  15. REDIM BallX(balls), BallY(balls), BallDx(balls), BallDy(balls), BallSize(balls), BallShow(balls), BallC(balls) AS _UNSIGNED LONG
  16.  
  17. w = _WIDTH: h = _HEIGHT: w2 = _WIDTH / 2: h2 = _HEIGHT / 2
  18.  
  19. restart:
  20.  
  21. 'Generate random ball data
  22. FOR B = 1 TO balls
  23.     BallSize(B) = 40 + (RND * 30)
  24.     BallX(B) = BallSize(B) + RND * (w - 2 * BallSize(B)): BallY(B) = BallSize(B) + RND * (h - 2 * BallSize(B))
  25.     a = RND * _PI(2): Ballspeed = 7 + RND * 14
  26.     BallDx(B) = Ballspeed * COS(a): BallDy(B) = Ballspeed * SIN(a)
  27.     BallShow(B) = 1: BallC(B) = _RGB32(RND * 255, RND * 255, RND * 255)
  28.  
  29. curball = 1
  30.  
  31. gametime = TIMER
  32.  
  33. timelimit = 30
  34.  
  35.     CLS
  36.     'compute ball movement
  37.     FOR t = 1 TO balls
  38.         BallX(t) = BallX(t) + BallDx(t) 'move ball then make sure in bounds
  39.         BallY(t) = BallY(t) + BallDy(t)
  40.         IF BallX(t) > w - BallSize(t) THEN BallDx(t) = -BallDx(t): BallX(t) = w - BallSize(t)
  41.         IF BallX(t) < BallSize(t) THEN BallDx(t) = -BallDx(t): BallX(t) = BallSize(t)
  42.         IF BallY(t) > h - BallSize(t) THEN BallDy(t) = -BallDy(t): BallY(t) = h - BallSize(t)
  43.         IF BallY(t) < BallSize(t) THEN BallDy(t) = -BallDy(t): BallY(t) = BallSize(t)
  44.     NEXT
  45.     'draw background
  46.     t = TIMER
  47.     FOR x = 0 TO w
  48.         FOR y = 0 TO h
  49.             r = SIN(1.1 * t) * h2 - y + h2
  50.             'PSET (x, y), _RGB(r, r - y, -r)
  51.             LINE (x, y)-(x, y), _RGB(r, r - y, -r), BF
  52.         NEXT
  53.         t = t + .01
  54.         GOSUB GetMouseClick
  55.     NEXT
  56.  
  57.     IF gameover = 1 THEN
  58.         PLAY "o2l16cegagfefgabgc3l4"
  59.         SLEEP 3
  60.         GOTO restart
  61.     END IF
  62.  
  63.     'draw balls
  64.     FOR i = 1 TO balls
  65.         IF BallShow(i) = 1 THEN
  66.             drawBall BallX(i), BallY(i), BallSize(i), BallC(i)
  67.             _PRINTSTRING (BallX(i) - 15, BallY(i) - 15), RIGHT$("0" + _TRIM$(STR$(i)), 2)
  68.         END IF
  69.     NEXT
  70.  
  71.     LOCATE 1, 1: PRINT "Click ball.."; curball;
  72.     LOCATE 2, 1: PRINT timelimit - INT(TIMER - gametime);
  73.     _DISPLAY: _LIMIT 30
  74.  
  75.     'If click on one ball (no overlayed oned) remove it
  76.     IF found = 1 THEN
  77.         IF firstball = curball THEN
  78.             'erase ball
  79.             drawBall BallX(firstball), BallY(firstball), BallSize(firstball), BallC(firstball)
  80.             BallShow(firstball) = 0
  81.             PLAY "mbl32o2ceg"
  82.             _DISPLAY: _DELAY .05
  83.             curball = curball + 1
  84.             found = 0
  85.  
  86.  
  87.         ELSE
  88.             found = found + 1
  89.             lastfound = firstball
  90.         END IF
  91.     END IF
  92.  
  93.     ''If click over several balls, remove top one
  94.     'IF found > 1 THEN
  95.     '    'BallShow(lastfound) = 0
  96.     '    drawball BallX(lastfound), BallY(lastfound), BallSize(lastfound), 255, 200, 100
  97.     '    _PRINTSTRING (BallX(lastfound) - 15, BallY(lastfound) - 15), STR$(lastfound)
  98.     '    _DISPLAY: PLAY "mbl16o2fbfbl8f"
  99.     '    found = 0
  100.     '    _DELAY .5
  101.     '    GOTO restart
  102.     'END IF
  103.  
  104.     'check if all clicked
  105.     anyleft = 0
  106.     FOR c = 1 TO balls
  107.         IF BallShow(c) = 1 THEN anyleft = anyleft + 1
  108.     NEXT
  109.     IF anyleft = 0 THEN
  110.         gameover = 1
  111.     END IF
  112.  
  113.     IF TIMER - gametime > timelimit THEN
  114.         PLAY "mbo1l4dl8ddl4dl8feeddc#l2d"
  115.         SLEEP 3
  116.         GOTO restart
  117.     END IF
  118.  
  119.  
  120.  
  121. '==============
  122. GetMouseClick:
  123. '==============
  124.  
  125. IF _MOUSEBUTTON(1) = 0 THEN done = 0
  126. IF _MOUSEBUTTON(1) AND done = 0 THEN
  127.     mx = _MOUSEX: my = _MOUSEY
  128.     found = 0
  129.     FOR m = 1 TO balls
  130.         IF BallShow(m) = 1 THEN
  131.             IF mx > (BallX(m) - BallSize(m)) AND mx < (BallX(m) + BallSize(m)) THEN
  132.                 IF my > (BallY(m) - BallSize(m)) AND my < (BallY(m) + BallSize(m)) THEN
  133.                     IF found = 0 THEN firstball = m
  134.                     found = found + 1
  135.                     IF found > 1 THEN
  136.                         lastfound = m
  137.                     END IF
  138.                 END IF
  139.             END IF
  140.         END IF
  141.     NEXT
  142.     done = 1
  143.  
  144.  
  145.  
  146. SUB drawBall (x, y, r, c AS _UNSIGNED LONG)
  147.     DIM rred AS LONG, grn AS LONG, blu AS LONG, rr AS LONG, f
  148.     rred = _RED32(c): grn = _GREEN32(c): blu = _BLUE32(c)
  149.     FOR rr = r TO 0 STEP -1
  150.         f = 1 - rr / r
  151.         fcirc x, y, rr, _RGB32(rred * f, grn * f, blu * f)
  152.     NEXT
  153.  
  154. SUB fcirc (CX AS INTEGER, CY AS INTEGER, R AS INTEGER, C AS _UNSIGNED LONG)
  155.     DIM Radius AS INTEGER, RadiusError AS INTEGER
  156.     DIM X AS INTEGER, Y AS INTEGER
  157.     Radius = ABS(R): RadiusError = -Radius: X = Radius: Y = 0
  158.     IF Radius = 0 THEN PSET (CX, CY), C: EXIT SUB
  159.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  160.     WHILE X > Y
  161.         RadiusError = RadiusError + Y * 2 + 1
  162.         IF RadiusError >= 0 THEN
  163.             IF X <> Y + 1 THEN
  164.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  165.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  166.             END IF
  167.             X = X - 1
  168.             RadiusError = RadiusError - X * 2
  169.         END IF
  170.         Y = Y + 1
  171.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  172.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  173.     WEND
  174.  
  175.  

Quote
I wonder how I can get mouse x/y in a circle instead of a rectangle?

Do you mean is mouse x, y is a ball radius away from ball middle?
if sqr((mx - bx)^2 + (my-by)^2))<= br then 'ball clicked

Code: QB64: [Select]
  1. 'ClickAwayBalls.bas v1.1
  2. 'new: speed increases, added timer, clicking bad choice restarts.
  3. 'by Dav, DEC/2020
  4.  
  5. 'Click on balls in order, starting at 1 untill all gone,
  6. 'before the timer runs out.  Clicking wrong number restarts.
  7.  
  8. SCREEN _NEWIMAGE(800, 600, 32)
  9. _DELAY .25
  10.  
  11. f = _LOADFONT("cyberbit.ttf", 30): _FONT f
  12.  
  13. balls = 15: size = 40: speed = 3
  14.  
  15. REDIM BallX(balls), BallY(balls), BallDx(balls), BallDy(balls), BallSize(balls), BallShow(balls), BallC(balls) AS _UNSIGNED LONG
  16.  
  17. w = _WIDTH: h = _HEIGHT: w2 = _WIDTH / 2: h2 = _HEIGHT / 2
  18.  
  19. restart:
  20.  
  21. 'Generate random ball data
  22. FOR B = 1 TO balls
  23.     BallSize(B) = 40 + (RND * 30)
  24.     BallX(B) = BallSize(B) + RND * (w - 2 * BallSize(B)): BallY(B) = BallSize(B) + RND * (h - 2 * BallSize(B))
  25.     a = RND * _PI(2): Ballspeed = 7 + RND * 14
  26.     BallDx(B) = Ballspeed * COS(a): BallDy(B) = Ballspeed * SIN(a)
  27.     BallShow(B) = 1: BallC(B) = _RGB32(RND * 255, RND * 255, RND * 255)
  28.  
  29. curball = 1
  30.  
  31. gametime = TIMER
  32.  
  33. timelimit = 30
  34.  
  35.     CLS
  36.     'compute ball movement
  37.     FOR t = 1 TO balls
  38.         BallX(t) = BallX(t) + BallDx(t) 'move ball then make sure in bounds
  39.         BallY(t) = BallY(t) + BallDy(t)
  40.         IF BallX(t) > w - BallSize(t) THEN BallDx(t) = -BallDx(t): BallX(t) = w - BallSize(t)
  41.         IF BallX(t) < BallSize(t) THEN BallDx(t) = -BallDx(t): BallX(t) = BallSize(t)
  42.         IF BallY(t) > h - BallSize(t) THEN BallDy(t) = -BallDy(t): BallY(t) = h - BallSize(t)
  43.         IF BallY(t) < BallSize(t) THEN BallDy(t) = -BallDy(t): BallY(t) = BallSize(t)
  44.     NEXT
  45.     'draw background
  46.     t = TIMER
  47.     FOR x = 0 TO w
  48.         FOR y = 0 TO h
  49.             r = SIN(1.1 * t) * h2 - y + h2
  50.             'PSET (x, y), _RGB(r, r - y, -r)
  51.             LINE (x, y)-(x, y), _RGB(r, r - y, -r), BF
  52.         NEXT
  53.         t = t + .01
  54.         GOSUB GetMouseClick
  55.     NEXT
  56.  
  57.     IF gameover = 1 THEN
  58.         PLAY "o2l16cegagfefgabgc3l4"
  59.         SLEEP 3
  60.         GOTO restart
  61.     END IF
  62.  
  63.     'draw balls
  64.     FOR i = 1 TO balls
  65.         IF BallShow(i) = 1 THEN
  66.             drawBall BallX(i), BallY(i), BallSize(i), BallC(i)
  67.             _PRINTSTRING (BallX(i) - 15, BallY(i) - 15), RIGHT$("0" + _TRIM$(STR$(i)), 2)
  68.         END IF
  69.     NEXT
  70.  
  71.     LOCATE 1, 1: PRINT "Click ball.."; curball;
  72.     LOCATE 2, 1: PRINT timelimit - INT(TIMER - gametime);
  73.     _DISPLAY: _LIMIT 30
  74.  
  75.     'If click on one ball (no overlayed oned) remove it
  76.     IF found = 1 THEN
  77.         IF firstball = curball THEN
  78.             'erase ball
  79.             drawBall BallX(firstball), BallY(firstball), BallSize(firstball), BallC(firstball)
  80.             BallShow(firstball) = 0
  81.             PLAY "mbl32o2ceg"
  82.             _DISPLAY: _DELAY .05
  83.             curball = curball + 1
  84.             found = 0
  85.  
  86.  
  87.         ELSE
  88.             found = found + 1
  89.             lastfound = firstball
  90.         END IF
  91.     END IF
  92.  
  93.     ''If click over several balls, remove top one
  94.     'IF found > 1 THEN
  95.     '    'BallShow(lastfound) = 0
  96.     '    drawball BallX(lastfound), BallY(lastfound), BallSize(lastfound), 255, 200, 100
  97.     '    _PRINTSTRING (BallX(lastfound) - 15, BallY(lastfound) - 15), STR$(lastfound)
  98.     '    _DISPLAY: PLAY "mbl16o2fbfbl8f"
  99.     '    found = 0
  100.     '    _DELAY .5
  101.     '    GOTO restart
  102.     'END IF
  103.  
  104.     'check if all clicked
  105.     anyleft = 0
  106.     FOR c = 1 TO balls
  107.         IF BallShow(c) = 1 THEN anyleft = anyleft + 1
  108.     NEXT
  109.     IF anyleft = 0 THEN
  110.         gameover = 1
  111.     END IF
  112.  
  113.     IF TIMER - gametime > timelimit THEN
  114.         PLAY "mbo1l4dl8ddl4dl8feeddc#l2d"
  115.         SLEEP 3
  116.         GOTO restart
  117.     END IF
  118.  
  119.  
  120.  
  121. '==============
  122. GetMouseClick:
  123. '==============
  124.  
  125. IF _MOUSEBUTTON(1) = 0 THEN done = 0
  126. IF _MOUSEBUTTON(1) AND done = 0 THEN
  127.     mx = _MOUSEX: my = _MOUSEY
  128.     found = 0
  129.     FOR m = 1 TO balls
  130.         IF BallShow(m) = 1 THEN
  131.             IF SQR((mx - BallX(m)) ^ 2 + (my - BallY(m)) ^ 2) < BallSize(m) THEN
  132.                 IF found = 0 THEN firstball = m
  133.                 found = found + 1
  134.                 IF found > 1 THEN
  135.                     lastfound = m
  136.                 END IF
  137.             END IF
  138.         END IF
  139.     NEXT
  140.     done = 1
  141.  
  142.  
  143.  
  144. SUB drawBall (x, y, r, c AS _UNSIGNED LONG)
  145.     DIM rred AS LONG, grn AS LONG, blu AS LONG, rr AS LONG, f
  146.     rred = _RED32(c): grn = _GREEN32(c): blu = _BLUE32(c)
  147.     FOR rr = r TO 0 STEP -1
  148.         f = 1 - rr / r
  149.         fcirc x, y, rr, _RGB32(rred * f, grn * f, blu * f)
  150.     NEXT
  151.  
  152. SUB fcirc (CX AS INTEGER, CY AS INTEGER, R AS INTEGER, C AS _UNSIGNED LONG)
  153.     DIM Radius AS INTEGER, RadiusError AS INTEGER
  154.     DIM X AS INTEGER, Y AS INTEGER
  155.     Radius = ABS(R): RadiusError = -Radius: X = Radius: Y = 0
  156.     IF Radius = 0 THEN PSET (CX, CY), C: EXIT SUB
  157.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  158.     WHILE X > Y
  159.         RadiusError = RadiusError + Y * 2 + 1
  160.         IF RadiusError >= 0 THEN
  161.             IF X <> Y + 1 THEN
  162.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  163.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  164.             END IF
  165.             X = X - 1
  166.             RadiusError = RadiusError - X * 2
  167.         END IF
  168.         Y = Y + 1
  169.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  170.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  171.     WEND
  172.  
« Last Edit: December 27, 2020, 02:41:55 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Bouncing Balls over a Plasma sky
« Reply #13 on: December 27, 2020, 02:59:22 pm »
Here it is with less time spent drawing plasma
Code: QB64: [Select]
  1. 'ClickAwayBalls.bas v1.1
  2. 'new: speed increases, added timer, clicking bad choice restarts.
  3. 'by Dav, DEC/2020
  4.  
  5. 'Click on balls in order, starting at 1 untill all gone,
  6. 'before the timer runs out.  Clicking wrong number restarts.
  7.  
  8. SCREEN _NEWIMAGE(800, 600, 32)
  9. _DELAY .25
  10.  
  11. f = _LOADFONT("cyberbit.ttf", 30): _FONT f
  12.  
  13. balls = 15: size = 40: speed = 3
  14.  
  15. REDIM BallX(balls), BallY(balls), BallDx(balls), BallDy(balls), BallSize(balls), BallShow(balls), BallC(balls) AS _UNSIGNED LONG
  16.  
  17. w = _WIDTH: h = _HEIGHT: w2 = _WIDTH / 2: h2 = _HEIGHT / 2
  18.  
  19. restart:
  20.  
  21. 'Generate random ball data
  22. FOR B = 1 TO balls
  23.     BallSize(B) = 40 + (RND * 30)
  24.     BallX(B) = BallSize(B) + RND * (w - 2 * BallSize(B)): BallY(B) = BallSize(B) + RND * (h - 2 * BallSize(B))
  25.     a = RND * _PI(2): Ballspeed = 4 + RND * 8
  26.     BallDx(B) = Ballspeed * COS(a): BallDy(B) = Ballspeed * SIN(a)
  27.     BallShow(B) = 1: BallC(B) = _RGB32(RND * 255, RND * 255, RND * 255)
  28.  
  29. curball = 1
  30.  
  31. gametime = TIMER
  32.  
  33. timelimit = 30
  34.  
  35.     CLS
  36.     'compute ball movement
  37.     FOR t = 1 TO balls
  38.         BallX(t) = BallX(t) + BallDx(t) 'move ball then make sure in bounds
  39.         BallY(t) = BallY(t) + BallDy(t)
  40.         IF BallX(t) > w - BallSize(t) THEN BallDx(t) = -BallDx(t): BallX(t) = w - BallSize(t)
  41.         IF BallX(t) < BallSize(t) THEN BallDx(t) = -BallDx(t): BallX(t) = BallSize(t)
  42.         IF BallY(t) > h - BallSize(t) THEN BallDy(t) = -BallDy(t): BallY(t) = h - BallSize(t)
  43.         IF BallY(t) < BallSize(t) THEN BallDy(t) = -BallDy(t): BallY(t) = BallSize(t)
  44.     NEXT
  45.     'draw background
  46.     t = TIMER
  47.     FOR x = 0 TO w STEP 4
  48.         FOR y = 0 TO h STEP 4
  49.             r = SIN(1.1 * t) * h2 - y + h2
  50.             'PSET (x, y), _RGB(r, r - y, -r)
  51.             LINE (x, y)-STEP(3, 3), _RGB(r, r - y, -r), BF
  52.         NEXT
  53.         t = t + .01
  54.         GOSUB GetMouseClick
  55.     NEXT
  56.  
  57.     IF gameover = 1 THEN
  58.         PLAY "o2l16cegagfefgabgc3l4"
  59.         SLEEP 3
  60.         GOTO restart
  61.     END IF
  62.  
  63.     'draw balls
  64.     FOR i = 1 TO balls
  65.         IF BallShow(i) = 1 THEN
  66.             drawBall BallX(i), BallY(i), BallSize(i), BallC(i)
  67.             _PRINTSTRING (BallX(i) - 15, BallY(i) - 15), RIGHT$("0" + _TRIM$(STR$(i)), 2)
  68.         END IF
  69.     NEXT
  70.  
  71.     LOCATE 1, 1: PRINT "Click ball.."; curball;
  72.     LOCATE 2, 1: PRINT timelimit - INT(TIMER - gametime);
  73.     _DISPLAY: _LIMIT 30
  74.  
  75.     'If click on one ball (no overlayed oned) remove it
  76.     IF found = 1 THEN
  77.         IF firstball = curball THEN
  78.             'erase ball
  79.             drawBall BallX(firstball), BallY(firstball), BallSize(firstball), BallC(firstball)
  80.             BallShow(firstball) = 0
  81.             PLAY "mbl32o2ceg"
  82.             _DISPLAY: _DELAY .05
  83.             curball = curball + 1
  84.             found = 0
  85.  
  86.  
  87.         ELSE
  88.             found = found + 1
  89.             lastfound = firstball
  90.         END IF
  91.     END IF
  92.  
  93.     ''If click over several balls, remove top one
  94.     'IF found > 1 THEN
  95.     '    'BallShow(lastfound) = 0
  96.     '    drawball BallX(lastfound), BallY(lastfound), BallSize(lastfound), 255, 200, 100
  97.     '    _PRINTSTRING (BallX(lastfound) - 15, BallY(lastfound) - 15), STR$(lastfound)
  98.     '    _DISPLAY: PLAY "mbl16o2fbfbl8f"
  99.     '    found = 0
  100.     '    _DELAY .5
  101.     '    GOTO restart
  102.     'END IF
  103.  
  104.     'check if all clicked
  105.     anyleft = 0
  106.     FOR c = 1 TO balls
  107.         IF BallShow(c) = 1 THEN anyleft = anyleft + 1
  108.     NEXT
  109.     IF anyleft = 0 THEN
  110.         gameover = 1
  111.     END IF
  112.  
  113.     IF TIMER - gametime > timelimit THEN
  114.         PLAY "mbo1l4dl8ddl4dl8feeddc#l2d"
  115.         SLEEP 3
  116.         GOTO restart
  117.     END IF
  118.  
  119.  
  120.  
  121. '==============
  122. GetMouseClick:
  123. '==============
  124.  
  125. IF _MOUSEBUTTON(1) = 0 THEN done = 0
  126. IF _MOUSEBUTTON(1) AND done = 0 THEN
  127.     mx = _MOUSEX: my = _MOUSEY
  128.     found = 0
  129.     FOR m = 1 TO balls
  130.         IF BallShow(m) = 1 THEN
  131.             IF SQR((mx - BallX(m)) ^ 2 + (my - BallY(m)) ^ 2) < BallSize(m) THEN
  132.                 IF found = 0 THEN firstball = m
  133.                 found = found + 1
  134.                 IF found > 1 THEN
  135.                     lastfound = m
  136.                 END IF
  137.             END IF
  138.         END IF
  139.     NEXT
  140.     done = 1
  141.  
  142.  
  143.  
  144. SUB drawBall (x, y, r, c AS _UNSIGNED LONG)
  145.     DIM rred AS LONG, grn AS LONG, blu AS LONG, rr AS LONG, f
  146.     rred = _RED32(c): grn = _GREEN32(c): blu = _BLUE32(c)
  147.     FOR rr = r TO 0 STEP -1
  148.         f = 1 - rr / r
  149.         fcirc x, y, rr, _RGB32(rred * f, grn * f, blu * f)
  150.     NEXT
  151.  
  152. SUB fcirc (CX AS INTEGER, CY AS INTEGER, R AS INTEGER, C AS _UNSIGNED LONG)
  153.     DIM Radius AS INTEGER, RadiusError AS INTEGER
  154.     DIM X AS INTEGER, Y AS INTEGER
  155.     Radius = ABS(R): RadiusError = -Radius: X = Radius: Y = 0
  156.     IF Radius = 0 THEN PSET (CX, CY), C: EXIT SUB
  157.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  158.     WHILE X > Y
  159.         RadiusError = RadiusError + Y * 2 + 1
  160.         IF RadiusError >= 0 THEN
  161.             IF X <> Y + 1 THEN
  162.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  163.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  164.             END IF
  165.             X = X - 1
  166.             RadiusError = RadiusError - X * 2
  167.         END IF
  168.         Y = Y + 1
  169.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  170.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  171.     WEND
  172.  
  173.  

edit: guess have to slow down balls now!
« Last Edit: December 27, 2020, 03:02:10 pm by bplus »

Marked as best answer by Dav on December 27, 2020, 10:56:28 am

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Bouncing Balls over a Plasma sky
« Reply #14 on: December 27, 2020, 03:21:42 pm »
OK low numbered balls move slow, high numbered balls move fast, now you know what to look for besides number:

Code: QB64: [Select]
  1. 'ClickAwayBalls.bas v1.1
  2. 'new: speed increases, added timer, clicking bad choice restarts.
  3. 'by Dav, DEC/2020
  4.  
  5. 'Click on balls in order, starting at 1 untill all gone,
  6. 'before the timer runs out.  Clicking wrong number restarts.
  7.  
  8. SCREEN _NEWIMAGE(800, 600, 32)
  9. _DELAY .25
  10.  
  11. f = _LOADFONT("cyberbit.ttf", 30): _FONT f
  12.  
  13. balls = 15: size = 40: speed = 3
  14.  
  15. REDIM BallX(balls), BallY(balls), BallDx(balls), BallDy(balls), BallSize(balls), BallShow(balls), BallC(balls) AS _UNSIGNED LONG
  16.  
  17. w = _WIDTH: h = _HEIGHT: w2 = _WIDTH / 2: h2 = _HEIGHT / 2
  18.  
  19. restart:
  20.  
  21. 'Generate random ball data
  22. FOR B = 1 TO balls
  23.     BallSize(B) = 40 + (RND * 30)
  24.     BallX(B) = BallSize(B) + RND * (w - 2 * BallSize(B)): BallY(B) = BallSize(B) + RND * (h - 2 * BallSize(B))
  25.     a = RND * _PI(2): Ballspeed = 2 + B
  26.     BallDx(B) = Ballspeed * COS(a): BallDy(B) = Ballspeed * SIN(a)
  27.     BallShow(B) = 1: BallC(B) = _RGB32(RND * 255, RND * 255, RND * 255)
  28.  
  29. curball = 1
  30.  
  31. gametime = TIMER
  32.  
  33. timelimit = 30
  34.  
  35.     CLS
  36.     'compute ball movement
  37.     FOR t = 1 TO balls
  38.         BallX(t) = BallX(t) + BallDx(t) 'move ball then make sure in bounds
  39.         BallY(t) = BallY(t) + BallDy(t)
  40.         IF BallX(t) > w - BallSize(t) THEN BallDx(t) = -BallDx(t): BallX(t) = w - BallSize(t)
  41.         IF BallX(t) < BallSize(t) THEN BallDx(t) = -BallDx(t): BallX(t) = BallSize(t)
  42.         IF BallY(t) > h - BallSize(t) THEN BallDy(t) = -BallDy(t): BallY(t) = h - BallSize(t)
  43.         IF BallY(t) < BallSize(t) THEN BallDy(t) = -BallDy(t): BallY(t) = BallSize(t)
  44.     NEXT
  45.     'draw background
  46.     t = TIMER
  47.     FOR x = 0 TO w STEP 4
  48.         FOR y = 0 TO h STEP 4
  49.             r = SIN(1.1 * t) * h2 - y + h2
  50.             'PSET (x, y), _RGB(r, r - y, -r)
  51.             LINE (x, y)-STEP(3, 3), _RGB(r, r - y, -r), BF
  52.         NEXT
  53.         t = t + .01
  54.         GOSUB GetMouseClick
  55.     NEXT
  56.  
  57.     IF gameover = 1 THEN
  58.         PLAY "o2l16cegagfefgabgc3l4"
  59.         SLEEP 3
  60.         GOTO restart
  61.     END IF
  62.  
  63.     'draw balls
  64.     FOR i = 1 TO balls
  65.         IF BallShow(i) = 1 THEN
  66.             drawBall BallX(i), BallY(i), BallSize(i), BallC(i)
  67.             _PRINTSTRING (BallX(i) - 15, BallY(i) - 15), RIGHT$("0" + _TRIM$(STR$(i)), 2)
  68.         END IF
  69.     NEXT
  70.  
  71.     LOCATE 1, 1: PRINT "Click ball.."; curball;
  72.     LOCATE 2, 1: PRINT timelimit - INT(TIMER - gametime);
  73.     _DISPLAY: _LIMIT 30
  74.  
  75.     'If click on one ball (no overlayed oned) remove it
  76.     IF found = 1 THEN
  77.         IF firstball = curball THEN
  78.             'erase ball
  79.             drawBall BallX(firstball), BallY(firstball), BallSize(firstball), BallC(firstball)
  80.             BallShow(firstball) = 0
  81.             PLAY "mbl32o2ceg"
  82.             _DISPLAY: _DELAY .05
  83.             curball = curball + 1
  84.             found = 0
  85.  
  86.  
  87.         ELSE
  88.             found = found + 1
  89.             lastfound = firstball
  90.         END IF
  91.     END IF
  92.  
  93.     ''If click over several balls, remove top one
  94.     'IF found > 1 THEN
  95.     '    'BallShow(lastfound) = 0
  96.     '    drawball BallX(lastfound), BallY(lastfound), BallSize(lastfound), 255, 200, 100
  97.     '    _PRINTSTRING (BallX(lastfound) - 15, BallY(lastfound) - 15), STR$(lastfound)
  98.     '    _DISPLAY: PLAY "mbl16o2fbfbl8f"
  99.     '    found = 0
  100.     '    _DELAY .5
  101.     '    GOTO restart
  102.     'END IF
  103.  
  104.     'check if all clicked
  105.     anyleft = 0
  106.     FOR c = 1 TO balls
  107.         IF BallShow(c) = 1 THEN anyleft = anyleft + 1
  108.     NEXT
  109.     IF anyleft = 0 THEN
  110.         gameover = 1
  111.     END IF
  112.  
  113.     IF TIMER - gametime > timelimit THEN
  114.         PLAY "mbo1l4dl8ddl4dl8feeddc#l2d"
  115.         SLEEP 3
  116.         GOTO restart
  117.     END IF
  118.  
  119.  
  120.  
  121. '==============
  122. GetMouseClick:
  123. '==============
  124.  
  125. IF _MOUSEBUTTON(1) = 0 THEN done = 0
  126. IF _MOUSEBUTTON(1) AND done = 0 THEN
  127.     mx = _MOUSEX: my = _MOUSEY
  128.     found = 0
  129.     FOR m = 1 TO balls
  130.         IF BallShow(m) = 1 THEN
  131.             IF SQR((mx - BallX(m)) ^ 2 + (my - BallY(m)) ^ 2) < BallSize(m) THEN
  132.                 IF found = 0 THEN firstball = m
  133.                 found = found + 1
  134.                 IF found > 1 THEN
  135.                     lastfound = m
  136.                 END IF
  137.             END IF
  138.         END IF
  139.     NEXT
  140.     done = 1
  141.  
  142.  
  143.  
  144. SUB drawBall (x, y, r, c AS _UNSIGNED LONG)
  145.     DIM rred AS LONG, grn AS LONG, blu AS LONG, rr AS LONG, f
  146.     rred = _RED32(c): grn = _GREEN32(c): blu = _BLUE32(c)
  147.     FOR rr = r TO 0 STEP -1
  148.         f = 1 - rr / r
  149.         fcirc x, y, rr, _RGB32(rred * f, grn * f, blu * f)
  150.     NEXT
  151.  
  152. SUB fcirc (CX AS INTEGER, CY AS INTEGER, R AS INTEGER, C AS _UNSIGNED LONG)
  153.     DIM Radius AS INTEGER, RadiusError AS INTEGER
  154.     DIM X AS INTEGER, Y AS INTEGER
  155.     Radius = ABS(R): RadiusError = -Radius: X = Radius: Y = 0
  156.     IF Radius = 0 THEN PSET (CX, CY), C: EXIT SUB
  157.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  158.     WHILE X > Y
  159.         RadiusError = RadiusError + Y * 2 + 1
  160.         IF RadiusError >= 0 THEN
  161.             IF X <> Y + 1 THEN
  162.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  163.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  164.             END IF
  165.             X = X - 1
  166.             RadiusError = RadiusError - X * 2
  167.         END IF
  168.         Y = Y + 1
  169.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  170.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  171.     WEND
  172.  
  173.  
  174.