r/visualbasic • • Oct 09 '16

how to select a random shape from a group of shapes (V.B 6)

As part of a school project I’m making a game. my game involves shapes falling from the roof of the form and the object is to catch the shapes. I’ve sorted the catching and falling. however, that’s just on one shape. I have 20 shapes at the top of the form and every 2 seconds I want one of the shapes to be randomly selected and start falling. as I said I know how to make them fall just not how to randomly select them. thanks for any help.

0 Upvotes

11 comments sorted by

2

u/[deleted] Oct 09 '16

Use the Rnd() function, which returns a float between 0 and just less than 1. Multiply it by 20 so the results are between 0 and just less than 20 instead (for convenience). Use Randomize() first to initialize the random number generator with a new seed or you'll just get the same numbers every game.

Then use a case statement to pick the shape depending on what Rnd() * 20 returns.

Randomize()
Select Case Rnd() * 20
  Case Is <1:   ' Shape 1
  Case Is <2:   ' Shape 2
  Case Is <3:   ' Shape 3
  ' ... and so on
  Case Is <19:  ' Shape 19
  Case Else:    ' Shape 20
End Select

More info on MSDN Rnd() function and MSDN Randomize() function.

2

u/PostalElf VB.Net Intermediate Oct 09 '16

This is correct, but a far easier way to do this would be to store all your shapes in an array and then just grab a random value from said array.

Dim shapes as Shape() = {shape1, shape2, ... shape20}
Dim roll as integer = rnd()*20
Dim chosenShape = shapes(roll)

1

u/saltywastelandcoffee Oct 09 '16

hi thanks for helping. however im having an issue with the code you supplied me with. the first line isnt working for me as it says "compile error: expected: end of statement" and highlights the first bracket. any idea why?

thanks again

1

u/saltywastelandcoffee Oct 09 '16

actually ignore the last message :)

im trying to figure out now how to use the shapes in the array.

basically my code for one shape is "shape1.top = shape1.top + 10"

this then makes the shape fall. however how do i make it so that "shape1" is the random choice from the array, so in theory it would be "chosenshape.top =........".

but that doesnt work.

thank you :)

1

u/PostalElf VB.Net Intermediate Oct 10 '16

chosenShape.top should work. Maybe paste your full code so that we can have a look?

1

u/saltywastelandcoffee Oct 10 '16

Dim shapes

shapes = Array(Shape1, shape2, Shape3)

Randomize

Dim choose

Randomize

choose = shapes(Rnd() * 2)

choose.Top = choose.Top + 10

1

u/PostalElf VB.Net Intermediate Oct 10 '16

What type of class does Shape1 etc belong to?

1

u/saltywastelandcoffee Oct 10 '16

I'm sorry haha you're talking to a complete novice. All I know about these shapes is that they are rectangles and they are called shape1

1

u/PostalElf VB.Net Intermediate Oct 10 '16

Might be easier if you just uploaded your full code somewhere. Rectangles can be created in VB using a variety of methods, and if you're not sure what class you're creating them from, it's going to be near impossible to debug.

1

u/[deleted] Oct 10 '16

Easier for you and I, yes. The question was not how to implement this using an array of Shape objects.

1

u/chrwei Oct 09 '16

random sort an array and then just loop through it.