Author Topic: Help with Starfield  (Read 11906 times)

0 Members and 1 Guest are viewing this topic.

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Help with Starfield
« Reply #15 on: August 10, 2019, 11:01:10 am »
Ya gotta love star fields - 'trig-less' or not - Very cool.
Logic is the beginning of wisdom.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Help with Starfield
« Reply #16 on: August 10, 2019, 11:04:06 am »
And now with 10 warp speeds installed on our TrekDrive!

Code: QB64: [Select]
  1. TYPE Dot_type
  2.     x AS _FLOAT
  3.     y AS _FLOAT
  4.     movex AS _FLOAT
  5.     movey AS _FLOAT
  6.     size AS _FLOAT
  7. DIM dots(10000) AS Dot_type
  8.  
  9.  
  10. SCREEN _NEWIMAGE(1024, 720, 32)
  11. WINDOW (-1000, -1000)-(1000, 1000)
  12.  
  13. dotsinplay = 1000
  14. FOR i = 1 TO dotsinplay
  15.     dots(i).x = RND * 100 - 50
  16.     dots(i).y = RND * 100 - 50
  17.     dots(i).movex = RND * 10 - 5
  18.     dots(i).movey = RND * 10 - 5
  19.     dots(i).color = &HFF000000 + INT(RND * &HFFFFFF) + 1
  20.     dots(i).size = RND * 5
  21.  
  22.  
  23.  
  24.  
  25. warpspeed = 1
  26.     CLS
  27.     i = 1
  28.     DO 'move and replace dots which would go off screen
  29.         dots(i).x = dots(i).x + dots(i).movex * warpspeed
  30.         dots(i).y = dots(i).y + dots(i).movey * warpspeed
  31.         IF dots(i).x < -1000 OR dots(i).x > 1000 or _
  32.            dots(i).y < -1000 OR dots(i).y > 1000 THEN
  33.             dots(i).x = RND * 100 - 50
  34.             dots(i).y = RND * 100 - 50
  35.             dots(i).movex = (RND * 10 - 5)
  36.             dots(i).movey = (RND * 10 - 5)
  37.             dots(i).color = &HFF000000 + INT(RND * &HFFFFFF) + 1
  38.             dots(i).size = RND * 5
  39.         END IF
  40.         CircleFill dots(i).x, dots(i).y, dots(i).size, dots(i).color
  41.         i = i + 1
  42.     LOOP UNTIL i >= dotsinplay
  43.     LOCATE 1, 1: PRINT "Warp Speed:"; warpspeed
  44.  
  45.     a$ = INKEY$
  46.     SELECT CASE a$
  47.         CASE "1" TO "9": warpspeed = VAL(a$)
  48.         CASE "0"
  49.             _AUTODISPLAY
  50.             CLS
  51.             PRINT "THE ENGINES CAN'T TAKE ANY MORE CAPT'N!"
  52.             FOR i = 1 TO 5
  53.                 _DELAY 1
  54.                 FOR j = 1 TO i - 1:
  55.                     PRINT ".";
  56.                 NEXT
  57.                 PRINT "."
  58.             NEXT
  59.             _DELAY 1
  60.             PRINT "BOOOOOOOOOOOOOOMMMMMMM!!!!"
  61.             BEEP
  62.             BEEP
  63.             SYSTEM
  64.     END SELECT
  65.     _DISPLAY
  66.  
  67. SUB CircleFill (CX AS INTEGER, CY AS INTEGER, R AS INTEGER, C AS _UNSIGNED LONG)
  68.     ' CX = center x coordinate
  69.     ' CY = center y coordinate
  70.     '  R = radius
  71.     '  C = fill color
  72.     DIM Radius AS INTEGER, RadiusError AS INTEGER
  73.     DIM X AS INTEGER, Y AS INTEGER
  74.     Radius = ABS(R)
  75.     RadiusError = -Radius
  76.     X = Radius
  77.     Y = 0
  78.     IF Radius = 0 THEN PSET (CX, CY), C: EXIT SUB
  79.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  80.     WHILE X > Y
  81.         RadiusError = RadiusError + Y * 2 + 1
  82.         IF RadiusError >= 0 THEN
  83.             IF X <> Y + 1 THEN
  84.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  85.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  86.             END IF
  87.             X = X - 1
  88.             RadiusError = RadiusError - X * 2
  89.         END IF
  90.         Y = Y + 1
  91.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  92.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  93.     WEND
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Help with Starfield
« Reply #17 on: August 10, 2019, 01:23:07 pm »
Yeah Steve, I debated whether to use your "Gold Standard" CircleFill routine, the one tried and tested here and maybe  even at Walter's forum... and likely long before I came to [abandoned, outdated and now likely malicious qb64 dot net website - don’t go there], but I figured Ken might be more inclined to buy a smaller more easy to understand SUB circleFill using PAINT??? Dang just get him to use a SUB properly to see the convenience!

In attempt to help Ken, I also dumped the use of TYPE which does keep all those arrays under one more manage-able name and index. I know others who avoid this structure form of data, it's easier to understand the tenancy to avoid that complexity.

I am afraid Ken is very into his way of doing things like drawing and the blacking out moving figures rather than simple CLS and redraw everything like he is back in 80's when that sort of thing took too much time (ha! or using JB). And why RANDOMIZE TIMER before every use of RND???

I am sorry to see all the folks encouraging the non use of trig, all these people who do know how to use trig. I don't get it, sure Star fields can be done easily without SIN and COS but it seems an ideal time for some SIN COS practice. Does doing Star Fields without trig help Ken?  Oh sure, it helps him avoid SIN and COS that much longer :D

Can anyone do regular polygons without SIN and COS? Probably, but who wants to try?
« Last Edit: August 10, 2019, 01:35:07 pm by bplus »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Help with Starfield
« Reply #18 on: August 10, 2019, 02:20:25 pm »
Can anyone do regular polygons without SIN and COS? Probably, but who wants to try?

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(640, 480, 256)
  2.  
  3. FOR i = 3 TO 10
  4.     CLS
  5.     angle = 360 / i
  6.     a$ = _TRIM$(STR$(angle))
  7.     d$ = "U100"
  8.     PSET (500, 300)
  9.     n = 0
  10.     d$ = "U100"
  11.     FOR j = 1 TO i
  12.         n = n + angle
  13.         a$ = _TRIM$(STR$(n))
  14.         d$ = d$ + " TA" + a$ + " U100"
  15.     NEXT
  16.     DRAW d$
  17.     SLEEP

;D
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Help with Starfield
« Reply #19 on: August 10, 2019, 02:24:43 pm »
Oh yeah draw strings, OK! :)

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Help with Starfield
« Reply #20 on: August 10, 2019, 05:28:49 pm »
Being an inveterate tweaker, I've been messing with this star field thing. While I didn't use SIN & COS in the original code, I reasoned that I could use it to add some navigational fun. SIN will slew the stars left and right with the use of the left and right arrow keys, and plus and minus keys will speed and slow the action.

Well then I thought, hell, use COS to pitch up and down as well. Except that approaching that in the same manner makes the display go batty. Uncomment line 35 to see the effect. I'm not sure why it dislikes one and not the other...

Code: QB64: [Select]
  1. a& = _NEWIMAGE(600, 600, 32)
  2. TYPE star '                                                     define the star data type
  3.     x AS INTEGER
  4.     y AS INTEGER
  5.     z AS INTEGER
  6.     xa AS INTEGER
  7.     ya AS INTEGER
  8.  
  9. DIM SHARED max AS INTEGER: max = 500
  10. DIM SHARED vprt AS INTEGER: vprt = 600 '                        set the viewport setback from viewer's eye
  11. DIM SHARED p(max) AS star '                                     dimension the star data array
  12. DIM SHARED speed AS INTEGER: speed = 2 '                        warp factor
  13.  
  14. PopVoid '                                                       OldMoses said "Let there be light" and the universe was, is and ever shall be
  15. WINDOW (-300, 300)-(300, -300) '                                create viewport window, Cap'n Kirk spends his days staring at this
  16.  
  17. DO '                                                            draw the stars
  18.     x$ = INKEY$
  19.     IF x$ = "" THEN
  20.         xch = 0: ych = 0
  21.     END IF
  22.     IF x$ = CHR$(43) THEN speed = speed + 1
  23.     IF x$ = CHR$(45) THEN speed = speed - 1
  24.     IF x$ = CHR$(0) + CHR$(75) THEN xch = 1 '      steering left/right
  25.     IF x$ = CHR$(0) + CHR$(77) THEN xch = -1
  26.     IF x$ = CHR$(0) + CHR$(72) THEN ych = 1 '      pitching up/down
  27.     IF x$ = CHR$(0) + CHR$(80) THEN ych = -1
  28.     CLS
  29.     FOR x = 1 TO max '                                          iterate through all stars
  30.         p(x).x = p(x).x + SinCalc(p(x).z, xch * 0.3)
  31.         'p(x).y = p(x).y + CosCalc(p(x).z, ych * 0.3)
  32.         p(x).xa = p(x).x / Pythagorus(p(x)) * vprt '            get relative screen position from absolute position for x & y
  33.         p(x).ya = p(x).y / Pythagorus(p(x)) * vprt
  34.         IF ABS(p(x).xa) < 301 AND ABS(p(x).ya) < 301 THEN '     place the star if within the viewport
  35.             PSET (p(x).xa, p(x).ya)
  36.         END IF
  37.         p(x).z = p(x).z - speed '                               move the star closer to the viewer
  38.         IF p(x).z < 0 THEN ReplaceStar x '                      add new stars as existing ones go behind the viewer
  39.     NEXT x
  40.     _DISPLAY '                                                  eliminate screen flicker
  41.     _LIMIT 500 '                                                smooth out the action
  42.  
  43.  
  44. SUB PopVoid '                                                   Do an initial population of stars
  45.  
  46.     FOR x = 1 TO max '                                          place a 'max' # of stars randomly in a 3D space
  47.         RANDOMIZE TIMER
  48.         p(x).x = INT(RND * 15000) - 7500
  49.         p(x).y = INT(RND * 15000) - 7500
  50.         p(x).z = INT(RND * 15000) + 1
  51.     NEXT x
  52.  
  53. END SUB 'PopVoid
  54.  
  55.  
  56. FUNCTION Pythagorus (var1 AS star)
  57.  
  58.     'Use to find distance between two 3D points
  59.  
  60.     horizontal = _HYPOT(ABS(var1.x), ABS(var1.y))
  61.     Pythagorus = _HYPOT(horizontal, ABS(var1.z))
  62.  
  63. END FUNCTION 'Pythagorus
  64.  
  65.  
  66. SUB ReplaceStar (var AS INTEGER) '                              This replaces any star that goes behind the viewer
  67.  
  68.     p(var).x = INT(RND * 15000) - 7500
  69.     p(var).y = INT(RND * 15000) - 7500
  70.     p(var).z = 15000
  71.  
  72. END SUB 'ReplaceStar
  73.  
  74. FUNCTION SinCalc (var1 AS _INTEGER64, var2 AS SINGLE)
  75.  
  76.     'Polar coordinate X finder
  77.     'used to find X coordinate of a speed/distance (var1) and heading/azimuth (var2)
  78.     SinCalc = var1 * SIN(_D2R(var2))
  79.  
  80. END FUNCTION 'SinCalc
  81.  
  82.  
  83. FUNCTION CosCalc (var1 AS _INTEGER64, var2 AS SINGLE)
  84.  
  85.     'Polar coordinate Y finder
  86.     'used to find Y coordinate of a speed/distance (var1) and heading/azimuth (var2)
  87.     CosCalc = var1 * COS(_D2R(var2))
  88.  
  89. END FUNCTION 'CosCalc
  90.  
  91.  

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Help with Starfield
« Reply #21 on: August 10, 2019, 09:36:50 pm »
Well, I finally made it! Well, actually I tried to make it with my own code but ended up using just about all of B+'s example. It sure is teaching me a lot though and I thank you to all of you guys for your example codes. I won't be able to remember the math involved (I know it's simple math), unless someday I use this many times to make things with (because of my memory problems). But I did add to it some things that B+ didn't have, like 3 different colored stars, white, red, and blue (for white, Red Giants, and Blue Giants), and different sizes of stars using CIRCLE and PAINT. I also used an example I found on this thread to use keys to speed up and slow down, which are the up and down arrow keys. Instead of Impulse Speed I use "Cruise Speed" and then Warp 1-10. If you guys, especially B+, don't want this on my website I won't put it on. But I did add credits to the top of the code that you can read below. Thank you again for the help. I might use this in other games or programs (unless you don't want me to). Oh also, I finally know why I couldn't make my own originally, I was very close though, but didn't make just 1 star at a time like this does (except for the first stars).  I know this line must delete all the stars to add motion to them all at once, but does it also make the star tails? I need to experiment with this type of stuff later. :)
 LINE (0, 0)-(_WIDTH, _HEIGHT), _RGBA32(0, 0, 0, 30), BF

Here it is, I added lots of remarks to remember the reasons later and for others.

Code: QB64: [Select]
  1. 'Thank you to B+ and other guys from the QB64.org forum for their code examples to make this program!
  2.  
  3. _TITLE "Warp Speed  - Use Up and Down Arrow Keys"
  4. DIM x(800), y(800), dx(800), dy(800), sz(800), c(800)
  5.  
  6. SCREEN _NEWIMAGE(800, 800, 32)
  7.  
  8. 'Set the default speed, amount of stars, and then make the first stars.
  9. speed = .001
  10. amount = 750
  11. FOR stars = 1 TO amount
  12.     GOSUB make:
  13. NEXT stars
  14.  
  15.     _LIMIT 500
  16.     FOR stars = 1 TO amount
  17.         a$ = INKEY$
  18.         IF a$ = CHR$(27) THEN END
  19.  
  20.         'Detect the arrow keys to make it go faster or slower.
  21.         IF a$ = CHR$(0) + CHR$(72) THEN speed = speed + .001: warp = warp + 1
  22.         IF a$ = CHR$(0) + CHR$(80) THEN speed = speed - .001: warp = warp - 1
  23.         IF warp < .0003 THEN warp = 0
  24.         IF warp > 10 THEN warp = 10
  25.         IF warp > 0 THEN LOCATE 1, 1: PRINT "Warp: "; warp
  26.         IF warp = 0 THEN LOCATE 1, 1: PRINT "Cruise Speed"
  27.         IF speed < .001 THEN speed = .001
  28.         IF speed > .01 THEN speed = .01
  29.  
  30.         'Move the stars.
  31.         x(stars) = x(stars) + dx(stars) * speed
  32.         y(stars) = y(stars) + dy(stars) * speed
  33.  
  34.         'See how many stars are on screen, if there's a certain amount, GOSUB to make: to make a new star.
  35.         r = x(stars) * x(stars) + y(stars) * y(stars)
  36.         IF (r > 600 ^ 2) THEN GOSUB make:
  37.  
  38.         'Draw the stars, c(stars) is the choice of color.
  39.         IF c(stars) < 75 THEN CIRCLE (x(stars) + _WIDTH / 2, -y(stars) + _HEIGHT / 2), sz(stars), _RGB32(255, 255, 255)
  40.         IF c(stars) > 74 AND c(stars) < 85 THEN CIRCLE (x(stars) + _WIDTH / 2, -y(stars) + _HEIGHT / 2), sz(stars), _RGB32(100, 10, 10)
  41.         IF c(stars) > 84 THEN CIRCLE (x(stars) + _WIDTH / 2, -y(stars) + _HEIGHT / 2), sz(stars), _RGB32(10, 10, 200)
  42.  
  43.         'If the size of the star is less than 1, skip paint.
  44.         IF sz(stars) < 1 THEN GOTO nopaint:
  45.  
  46.         'Paint the stars.
  47.         IF c(stars) < 75 THEN PAINT (x(stars) + _WIDTH / 2, -y(stars) + _HEIGHT / 2), _RGB32(255, 255, 255)
  48.         IF c(stars) > 74 AND c(stars) < 85 THEN PAINT (x(stars) + _WIDTH / 2, -y(stars) + _HEIGHT / 2), _RGB32(100, 10, 10)
  49.         IF c(stars) > 84 THEN PAINT (x(stars) + _WIDTH / 2, -y(stars) + _HEIGHT / 2), _RGB32(10, 10, 200)
  50.         nopaint:
  51.     NEXT stars
  52.  
  53.     _DISPLAY
  54.     LINE (0, 0)-(_WIDTH, _HEIGHT), _RGBA32(0, 0, 0, 30), BF
  55.  
  56.  
  57. make:
  58.  
  59. 'Get a random X number using 2 decimal points - RND and .5 and times it by the width of the screen.
  60.  
  61. x(stars) = (RND - .5) * _WIDTH
  62.  
  63. 'Get a random Y number using 2 decimal points - RND and .5 and times it by the height of the screen.
  64.  
  65. y(stars) = (RND - .5) * _HEIGHT
  66.  
  67. 'Get a random star size between .25 and 2.
  68.  
  69. sz(stars) = (RND * 2) + .25
  70.  
  71. 'Get a random number between 1 and 100 to pick 1 of 3 different star colors in the main loop.
  72.  
  73. c(stars) = INT(RND * 100) + 1
  74.  
  75. 'Get any random number between 5 and 10, used for both X and Y, to use in the star movement in the main loop.
  76.  
  77. v = 5 + RND * 5
  78. dx(stars) = x(stars) * v
  79. dy(stars) = y(stars) * v
  80.  
  81.  
« Last Edit: August 10, 2019, 09:46:22 pm by SierraKen »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Help with Starfield
« Reply #22 on: August 10, 2019, 10:06:07 pm »
Yeah, this line:
 LINE (0, 0)-(_WIDTH, _HEIGHT), _RGBA32(0, 0, 0, 30), BF
layers the star field with coats of transparent black, the oldest on screen eventually get covered over, this creates the tail effect, thanks Fellippe, from whom I learned that trick.

Looks pretty good Ken :)

I have this strange urge to stand up and salute your stars!

Update: this is looking like my translation of STxAxTIC's code the locating of stars is signature STxAxTIC.

These lines:
Code: QB64: [Select]
  1.   'See how many stars are on screen, if there's a certain amount, GOSUB to make: to make a new star.
  2.         r = x(stars) * x(stars) + y(stars) * y(stars)
  3.         IF (r > 600 ^ 2) THEN GOSUB make:
  4.  
are measuring the distance of x, y from the center of the screen. A new star is made when the distance is beyond borders of screen.

« Last Edit: August 10, 2019, 10:21:11 pm by bplus »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Help with Starfield
« Reply #23 on: August 10, 2019, 10:16:05 pm »
Thanks B+!  LOL I thought the same thing, red, white, and blue.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Help with Starfield
« Reply #24 on: August 10, 2019, 10:24:53 pm »
This is just directly from your example. Wow it is measurement from the center I think. It's not adding stars, I'll change the remark, thank you.

Edit: B+ the more I think about this line, the more confusing it is to me. And I have no idea why it works. I tried using my program without these 2 lines and it only did the first stars and wouldn't make any more, so I put it back. It's confusing because 600 to the second power equals 360,000 which has nothing to do with X * X plus Y * Y.  For example, if a star made it to X=10, Y=10, then 10 * 10 = 100 and again 10 * 10 = 100 so 100 + 100 = 200, nowhere neat 600 to the second power. What am I missing?

Edit again: And I know its 360000 because I took out the equation and just put in 360000 and the program runs fine, but HOW can r get up that high of a number?? lol



Yeah, this line:
 LINE (0, 0)-(_WIDTH, _HEIGHT), _RGBA32(0, 0, 0, 30), BF
layers the star field with coats of transparent black, the oldest on screen eventually get covered over, this creates the tail effect, thanks Fellippe, from whom I learned that trick.

Looks pretty good Ken :)

I have this strange urge to stand up and salute your stars!

Update: this is looking like my translation of STxAxTIC's code the locating of stars is signature STxAxTIC.
Code: QB64: [Select]
  1.  
  2. These lines:
  3.   'See how many stars are on screen, if there's a certain amount, GOSUB to make: to make a new star.
  4.         r = x(stars) * x(stars) + y(stars) * y(stars)
  5.         IF (r > 600 ^ 2) THEN GOSUB make:
  6.  
are measuring the distance of x, y from the center of the screen. A new star is made when the distance is beyond borders of screen.
« Last Edit: August 10, 2019, 10:38:44 pm by SierraKen »

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Help with Starfield
« Reply #25 on: August 10, 2019, 10:28:26 pm »
Nicely done Ken, I've been intrigued by the alpha overlay tip too, makes a nice comet trail effect. Kind of hoping I'll think of a project I can use it in myself.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Help with Starfield
« Reply #26 on: August 10, 2019, 10:35:02 pm »
Thanks OldMoses, this is almost all B+'s example code, so I am grateful to learn from that.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Help with Starfield
« Reply #27 on: August 10, 2019, 10:44:47 pm »
It’s a simple version of Pytagorean’s Theorem.  When it comes to a right triangle, the hypotenuse is twice the sum of the squares of the other 2 sides....

Simply put, in a circle, r ^ 2 = x ^ 2 + y ^ 2



Run the tutorial in reply #4 here, and see if it helps you understand it: https://www.qb64.org/forum/index.php?topic=1583.msg108049#msg108049
« Last Edit: August 10, 2019, 10:47:05 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Help with Starfield
« Reply #28 on: August 10, 2019, 10:48:02 pm »
A-HA! I found the reason B+! Those 2 lines just detect to see if any star goes past 600, either vertically or horizontally. It's just a trick to see how far the farthest stars have gone in that 1 direction. And since all of the directions move the same speed and length, only 1 direction is needed. If any of them passed up 600, it would make another new star in make: Knowing this, I changed the 600 to 800 because I had changed the screen size to 800 x 800 when I made the program. And now the stars even look better!!! Here is a better version:

Code: QB64: [Select]
  1. 'Thank you to B+ and other guys from the QB64.org forum for their code examples to make this program!
  2.  
  3. _TITLE "Warp Speed  - Use Up and Down Arrow Keys"
  4. DIM x(800), y(800), dx(800), dy(800), sz(800), c(800)
  5.  
  6. SCREEN _NEWIMAGE(800, 800, 32)
  7.  
  8. 'Set the default speed, amount of stars, and then make the first stars.
  9. speed = .001
  10. amount = 750
  11. FOR stars = 1 TO amount
  12.     GOSUB make:
  13. NEXT stars
  14.  
  15.     _LIMIT 500
  16.     FOR stars = 1 TO amount
  17.         a$ = INKEY$
  18.         IF a$ = CHR$(27) THEN END
  19.  
  20.         'Detect the arrow keys to make it go faster or slower.
  21.         IF a$ = CHR$(0) + CHR$(72) THEN speed = speed + .001: warp = warp + 1
  22.         IF a$ = CHR$(0) + CHR$(80) THEN speed = speed - .001: warp = warp - 1
  23.         IF warp < .0003 THEN warp = 0
  24.         IF warp > 10 THEN warp = 10
  25.         IF warp > 0 THEN LOCATE 1, 1: PRINT "Warp: "; warp
  26.         IF warp = 0 THEN LOCATE 1, 1: PRINT "Cruise Speed"
  27.         IF speed < .001 THEN speed = .001
  28.         IF speed > .01 THEN speed = .01
  29.  
  30.         'Move the stars.
  31.         x(stars) = x(stars) + dx(stars) * speed
  32.         y(stars) = y(stars) + dy(stars) * speed
  33.  
  34.         'Detect to see if any star goes past the screen 800 x 800, if so, GOSUB make: to make a new star.
  35.         r = x(stars) * x(stars) + y(stars) * y(stars)
  36.         IF (r > 800 ^ 2) THEN GOSUB make:
  37.  
  38.         'Draw the stars, c(stars) is the choice of color.
  39.         IF c(stars) < 75 THEN CIRCLE (x(stars) + _WIDTH / 2, -y(stars) + _HEIGHT / 2), sz(stars), _RGB32(255, 255, 255)
  40.         IF c(stars) > 74 AND c(stars) < 85 THEN CIRCLE (x(stars) + _WIDTH / 2, -y(stars) + _HEIGHT / 2), sz(stars), _RGB32(100, 10, 10)
  41.         IF c(stars) > 84 THEN CIRCLE (x(stars) + _WIDTH / 2, -y(stars) + _HEIGHT / 2), sz(stars), _RGB32(10, 10, 200)
  42.  
  43.         'If the size of the star is less than 1, skip paint.
  44.         IF sz(stars) < 1 THEN GOTO nopaint:
  45.  
  46.         'Paint the stars.
  47.         IF c(stars) < 75 THEN PAINT (x(stars) + _WIDTH / 2, -y(stars) + _HEIGHT / 2), _RGB32(255, 255, 255)
  48.         IF c(stars) > 74 AND c(stars) < 85 THEN PAINT (x(stars) + _WIDTH / 2, -y(stars) + _HEIGHT / 2), _RGB32(100, 10, 10)
  49.         IF c(stars) > 84 THEN PAINT (x(stars) + _WIDTH / 2, -y(stars) + _HEIGHT / 2), _RGB32(10, 10, 200)
  50.         nopaint:
  51.     NEXT stars
  52.  
  53.     'This stops flickering.
  54.     _DISPLAY
  55.  
  56.     'Erase the stars for motion but keep a transparent black for the trails.
  57.     LINE (0, 0)-(_WIDTH, _HEIGHT), _RGBA32(0, 0, 0, 30), BF
  58.  
  59.  
  60. make:
  61.  
  62. 'Get a random X number using 2 decimal points - RND and .5 and times it by the width of the screen.
  63.  
  64. x(stars) = (RND - .5) * _WIDTH
  65.  
  66. 'Get a random Y number using 2 decimal points - RND and .5 and times it by the height of the screen.
  67.  
  68. y(stars) = (RND - .5) * _HEIGHT
  69.  
  70. 'Get a random star size between .25 and 2.
  71.  
  72. sz(stars) = (RND * 2) + .25
  73.  
  74. 'Get a random number between 1 and 100 to pick 1 of 3 different star colors in the main loop.
  75.  
  76. c(stars) = INT(RND * 100) + 1
  77.  
  78. 'Get any random number between 5 and 10, used for both X and Y, to use in the star movement in the main loop.
  79.  
  80. v = 5 + RND * 5
  81. dx(stars) = x(stars) * v
  82. dy(stars) = y(stars) * v
  83.  
  84.  
  85.  

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Help with Starfield
« Reply #29 on: August 10, 2019, 10:53:37 pm »
True SMcNeill, but it's still only moving right or moving down because if a star went to the left or up, that equation wouldn't work.