Author Topic: I'm just trying generate some random circles. Don't know why I'm getting this.  (Read 4416 times)

0 Members and 1 Guest are viewing this topic.

Offline fistfullofnails

  • Newbie
  • Posts: 12
    • View Profile
Code: QB64: [Select]
  1. Dim circles% 'total number of circles
  2. Dim x% 'location of circles
  3. Dim y%
  4. Dim circolor% 'color of circle
  5. Dim rad% 'radius of circle
  6. Dim entity% 'individual circle
  7.  
  8. '----------------------
  9. 'assign circle attributes
  10. '----------------------
  11.  
  12. circles = 5
  13.  
  14.  
  15. For i = 1 To circles
  16.     x = Int(Rnd * 300)
  17.     y = Int(Rnd * 300)
  18.     rad = Int(Rnd * 50)
  19.     red% = Int(Rnd * 255)
  20.     green% = Int(Rnd * 255)
  21.     blue% = Int(Rnd * 255)
  22.     entity(i) = circle(x, y), rad,  _RGB32(red%, green%, blue%)
  23.  

then says:
"Name already in use on current line.  Caused by  (or after)
entity(i) = circle(x, y), rad,  _RGB32(red%, green%, blue%)

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
For one thing

"Dim x%" is not the same as "DIM x as integer"

I thought "DIM x%" might be shorthand for  " DIM x as integer" but x is still a single

x = 10.2

print x.    you get 10.2,  so x is a single not an integer.  The complier allows Dim x% though

The other thing is entity(i) is an array so

"Dim entity%"  should be "dim entity(some value) as integer"
« Last Edit: April 25, 2021, 05:15:43 pm by NOVARSEG »

Offline Unseen Machine

  • Forum Regular
  • Posts: 158
  • Make the game not the engine!
    • View Profile
You cant store a command in an array, especially one thats not been dimensioned.

You need to create a uset defined type for the circle that contains all the required information, x,y, rdaius etc...and then you can use a for loop to store that info...use this and work on it.

Code: QB64: [Select]
  1.   X AS INTEGER
  2.   Y AS INTEGER
  3.   Radius AS INTEGER
  4.  
  5.  
  6. DIM Circs(9) AS Circle
  7.  
  8. FOR i% = 0 TO 9
  9.   Circs(i%).X = (RND * 300)
  10. '// You fill out the rest but know you can store a _RGB32 value in the RGB variable by
  11. '// Circs(i%).RGB _RGB32(.....
  12.  
  13.  

Unseen

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
There is a Circle command that is confused with array, that is the name already in use.

Then you need to set up a graphics screen as commented below

Then all is well!

Code: QB64: [Select]
  1. Dim circles% 'total number of circles
  2. Dim x% 'location of circles
  3. Dim y%
  4. Dim circolor% 'color of circle
  5. Dim rad% 'radius of circle
  6. Dim entity% 'individual circle
  7.  
  8. '----------------------
  9. 'assign circle attributes
  10. '----------------------
  11. Screen _NewImage(300, 300, 32) '<<<<<<<<<<<<<<<<<<<<<<<<<< for graphics commands to work
  12. circles = 5
  13.  
  14.  
  15. For i = 1 To circles
  16.     x = Int(Rnd * 300)
  17.     y = Int(Rnd * 300)
  18.     rad = Int(Rnd * 50)
  19.     red% = Int(Rnd * 255)
  20.     green% = Int(Rnd * 255)
  21.     blue% = Int(Rnd * 255)
  22.     Circle (x, y), rad, _RGB32(red%, green%, blue%)
  23.  
  24.  
  25.  

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Hi fistfullofnails
I want to add 2 feedbacks to your code, all the suggestions to get work your code you have had from the other Qb64 coders before me.
see the 2 attachments

  [ You are not allowed to view this attachment ]  

and
  [ You are not allowed to view this attachment ]  

The warning that you got from IDE is because the parser thought that you are typing an assignation and you cannot use a QB64 keyword as variable or array name (   entity(i) = circle (x,y).....)
« Last Edit: April 25, 2021, 06:10:33 pm by TempodiBasic »
Programming isn't difficult, only it's  consuming time and coffee

Offline fistfullofnails

  • Newbie
  • Posts: 12
    • View Profile
OK, so this is Terry Ritchie's game programming tutorial.  What I've been doing is once I go over a section, I try and go back and do something similar, yet simpler, to prove to myself that I may understand it.  I don't believe that just copying the code and moving on does much for me as to retaining things.  Is he not putting multiple commands into his Cir(Count%) array? For instance, Is  Cir(Count%).c = _RGB32(Red%, Green%, Blue%) not a command?



Code: QB64: [Select]
  1. '------------------------    -------------
  2. 'variable declaration
  3. '----------------------------------
  4.  
  5. Type CIRCLETYPE
  6.     x As Single
  7.     y As Single
  8.     c As _Unsigned Long
  9.     r As Integer
  10.     xdir As Single
  11.     ydir As Single
  12.  
  13. Const CIRCLES = 50
  14. Const SCREENWIDTH = 640
  15. Const SCREENHEIGHT = 480
  16.  
  17. Dim Cir(CIRCLES - 1) As CIRCLETYPE
  18. Dim Count%
  19. Dim Red%, Green%, Blue%
  20. Dim OkToPaint%
  21.  
  22. '---------------------------------------
  23. 'Main
  24. '-----------------
  25.  
  26. For Count% = 0 To CIRCLES - 1
  27.     Cir(Count%).x = SCREENWIDTH / 2 - 1
  28.     Cir(Count%).y = SCREENHEIGHT / 2 - 1
  29.     Red% = Int(Rnd(1) * 256)
  30.     Green% = Int(Rnd(1) * 256)
  31.     Blue% = Int(Rnd(1) * 256)
  32.     Cir(Count%).c = _RGB32(Red%, Green%, Blue%)
  33.     Cir(Count%).r = Int(Rnd(1) * 40) + 11
  34.     Cir(Count%).xdir = (Rnd(1) * 2 - Rnd(1) * 2) * 2
  35.     Cir(Count%).ydir = (Rnd(1) * 2 - Rnd(1) * 2) * 2
  36. Next Count%

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
OK, so this is Terry Ritchie's game programming tutorial.  What I've been doing is once I go over a section, I try and go back and do something similar, yet simpler, to prove to myself that I may understand it.  I don't believe that just copying the code and moving on does much for me as to retaining things.  Is he not putting multiple commands into his Cir(Count%) array? For instance, Is  Cir(Count%).c = _RGB32(Red%, Green%, Blue%) not a command?



Oh heck I thought you wanted to draw circles!

He is showing you how to use User Defined Type and an array of CircleType.

Code: QB64: [Select]
  1.  Cir(Count%).c = _RGB32(Red%, Green%, Blue%)
This is a color assignment  .c for array Cir at index Count% the _RGB32... is a color

Code: QB64: [Select]
  1. Type CIRCLETYPE
  2.     x As Single
  3.     y As Single
  4.     c As _Unsigned Long
  5.     r As Integer
  6.     xdir As Single
  7.     ydir As Single

See the c AS _Unsigned Long that's a dead give away it's a color.  xdir and ydir indicates your circle centers will be moving around in the screen, these are also called dx, dy meaning they are changes in x and y coordinates.

You really will be needing a Screen command so you can actually draw graphics.

Yeah so far you are just setting up to draw circles moving around on screen.

Next you will have a Screen command and start drawing and then (x, y) of circles will be moved and redrawn in a main loop, if a circle moves off screen or about to it might be bounced back with the next batch of code.
« Last Edit: April 25, 2021, 08:35:05 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Code: QB64: [Select]
  1. Dim OkToPaint%

hmm... that's new, doesn't believe in a CircleFill Sub?

Might be filling circles with Paint but if two (or more) circles are overlapping which do you paint first?
Could get messy.

Update: OK that wasn't in Terry's program, I just checked and he is painting to circle color border so no mix ups.

OkToPaint% definitely a mod for unknown purpose.

« Last Edit: April 25, 2021, 08:54:57 pm by bplus »

Offline fistfullofnails

  • Newbie
  • Posts: 12
    • View Profile
Oh heck I thought you wanted to draw circles!

I was just trying to draw circles.  I was trying to draw simple circles using an array.  It's just that some were saying that I was trying to plug commands into an array in my original code I posted here, when it had appeared to me that was what Ritchie was doing in the tutorial you are looking at that I had posted.  I'm guessing that something like _rgb32(255, 0, 255) isn't considered a command?  I'm once again guessing that  _rgb32(255, 0, 255) just turns out to be a long integer in the end, so that an array would accept it?  While circle(80, 50), 25, _rgb32(255, 0, 255) is an actual command, so wouldn't be accepted into an array?  I don't want to offend anyone here, but I'm not really interested in getting into TYPES and such, when I haven't even come to grips with basic arrays it seems. 

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Quote
I don't want to offend anyone here, but I'm not really interested in getting into TYPES and such, when I haven't even come to grips with basic arrays it seems.

Hey man! you go at your own rate but keep challenging yourself to learn of course. Terry's tutorial probably has to be taken in the order he presents it. If you skip around you might miss something essential.

The code you are posting from was teaching about Type and arrays of Type (UDT's they call them for User Defined Type).

Just doing a circle, all you need an x, y center, a radius r and maybe a color _RGB32( red, green, blue) gets you a color value 1 of 256^3 of them. circle (x, y), r, _rgb32(red, green, blue) where red, green and blue are values from 0 and 255.
Circle (x, y), r, _RGB32(red, green, blue) 'in all generic variables

And don't forget to setup a graphics screen. Screen 12 will do in a pinch but you can custom design your screen size with
Screen _NewImage(myWidth, MyHeight, 32) ' the 32 is for _RGB32( ) colors, _RGB32( ) is called a Function and Functions always return something so usually need a variable = function (a, b, c)

You don't need arrays to hold circle data unless you want to move the circles around on screen.
« Last Edit: April 26, 2021, 01:23:09 am by bplus »

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile


Quote
While circle(80, 50), 25, _rgb32(255, 0, 255) is an actual command, so wouldn't be accepted into an array?

I think that CIRCLE command does not return a value. so

 entity(i) = circle(x, y), rad,  _RGB32(red%, green%, blue%)
won't work

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
here is some code that shows use of CIRCLE command and  I did not use TYPEs


Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(1024, 768, 32)
  2.  
  3. DIM originX AS INTEGER
  4. originX = _WIDTH / 2
  5.  
  6. DIM originY AS INTEGER
  7. originY = _HEIGHT / 2
  8. DIM DirY(1) AS INTEGER
  9. DIM DirX(1) AS INTEGER
  10.  
  11.  
  12. Y(0) = 700
  13. X(0) = originX - (700 - originY)
  14.  
  15. X(1) = originX + (350 - originY)
  16. Y(1) = 350
  17.  
  18. DirX(0) = 1
  19. DirY(0) = -1
  20.  
  21. DirX(1) = -1
  22. DirY(1) = -1
  23.  
  24.     _DELAY .001
  25.     obj = 0
  26.  
  27.     IF (((X(1) - X(0)) ^ 2) + (Y(0) - Y(1)) ^ 2) ^ .5 <= 80 AND tog = 0 THEN
  28.         tog = 1
  29.         _DELAY .6
  30.     END IF
  31.  
  32.     IF (((X(1) - X(0)) ^ 2) + (Y(0) - Y(1)) ^ 2) ^ .5 > 80 AND tog = 1 THEN tog = 0
  33.  
  34.     r = 0: g = 0: B = 0
  35.     FOR S = 1 TO 40
  36.         CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  37.     NEXT
  38.  
  39.     IF X(obj) + 40 = _WIDTH THEN DirX(obj) = -1
  40.     IF X(obj) - 40 = 0 THEN DirX(obj) = 1
  41.     X(obj) = X(obj) + DirX(obj)
  42.  
  43.     IF Y(obj) + 40 = _HEIGHT THEN DirY(obj) = -1
  44.     IF Y(obj) - 40 = 0 THEN DirY(obj) = 1
  45.     Y(obj) = Y(obj) + DirY(obj)
  46.  
  47.  
  48.     r = 255: g = 0: B = 0
  49.     FOR S = 1 TO 40
  50.         CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  51.     NEXT
  52.  
  53.     obj = 1
  54.  
  55.     r = 0: g = 0: B = 0
  56.     FOR S = 1 TO 40
  57.         CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  58.            NEXT
  59.  
  60.     IF X(obj) + 40 = _WIDTH THEN DirX(obj) = -1
  61.     IF X(obj) - 40 = 0 THEN DirX(obj) = 1
  62.     X(obj) = X(obj) + DirX(obj)
  63.  
  64.     IF Y(obj) + 40 = _HEIGHT THEN DirY(obj) = -1
  65.     IF Y(obj) - 40 = 0 THEN DirY(obj) = 1
  66.     Y(obj) = Y(obj) + DirY(obj)
  67.  
  68.     r = 0: g = 255: B = 255
  69.     FOR S = 1 TO 40
  70.         CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  71.     NEXT
  72.  
  73.     _DISPLAY
  74.  
  75.  
  76.  


Explanation of the DIMs

DIM X(1) AS INTEGER   
DIM Y(1) AS INTEGER
DIM DirY(1) AS INTEGER
DIM DirX(1) AS INTEGER

example
DIM X(1) means there are 2 elements to the array  X(0) and X(1)

If you try to assign a value to  X(2)  like  X(2) = 5 you will get a subscript out of range error
« Last Edit: April 26, 2021, 01:32:39 am by NOVARSEG »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
100 random circles
Code: QB64: [Select]
  1. Screen _NewImage(800, 600, 32)
  2. For i = 1 To 100 ' 100 random circles
  3.     Circle (Rnd * _Width, Rnd * _Height), Rnd * 80, _RGB32(Rnd * 256, Rnd * 256, Rnd * 256)
  4.  

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
100 circles ? I'm having trouble with only 2
« Last Edit: April 26, 2021, 01:36:24 am by NOVARSEG »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
100 circles ? I'm having trouble with only 2

Are you talking about bouncing off each other?
https://www.qb64.org/forum/index.php?topic=2292.msg115227#msg115227
« Last Edit: April 26, 2021, 01:43:29 am by bplus »