Results 1 to 5 of 5

Thread: Random Picture Location...

  1. #1

    Thread Starter
    Hyperactive Member Sacofjoea's Avatar
    Join Date
    May 2000
    Location
    Never Never Land
    Posts
    472
    I am needing help with randomizing the locations of a few pictures...

    I have a chess type design board, and I need the program to randomly put 5 pictures in different places on the board, each time the user loads the program... Also, if able, I want the program to decide which pictures to display, and how many of that kind, but always display 5 pictures on the board...

    An example on how to do this would be really helpfull!

    -Andre
    That arranged can be

  2. #2
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    So where exactly do you have the problem?

    You have stated exactly what you need and you know exactly what you need to do... Where exactly are you failing to be able to do it???

    Or do you just want some poor slob to write the code for you so you don't have to do it yourself


  3. #3
    Hyperactive Member
    Join Date
    Sep 2000
    Location
    NJ, USA
    Posts
    326

    jhb

    Why not just generate random numbers to calculate the location coordiantes for each image on the form?

  4. #4

    Thread Starter
    Hyperactive Member Sacofjoea's Avatar
    Join Date
    May 2000
    Location
    Never Never Land
    Posts
    472
    My problem is I dont know where to start..and i was looking for help... There is a lot more to my program than this small detail. If you dont have any help, please dont respond cause it then turns this into a chit chat thread.
    That arranged can be

  5. #5
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    Well I do have something that will help but probably not in the way you were hoping.

    The problem as I see it is the ability to take a problem and break it down into smaller and manageable pieces... a common problem programmers face at one time or another.

    So rather than giving you a fish I will try and teach you HOW to fish


    You have stated the requirements of your program :

    • Chess Type Board (ie grid, could use 2 dimensional array)
    • Randomly place "pictures" on the board
    • Randomly decide which pictures & how many


    So lets tackle each part :

    Chess Type Board
    Obviously you would use a grid where the width of your board divided by the number of squares would tell you how many pixels per square and allow you to dimension the array.

    What you would end up with then is something like this :

    Code:
    Dim iBoard() as Integer
    
    iWidth = 10
    iHeight = 10
    
    iSquareWidth = PictureBox.Width / iWidth
    iSquareHeight = PictureBox.Height / iHeight
    
    Redim iBoard(iWidth,iHeight)
    So placing an item on the board would go like this :

    Code:
    iXPos = 3
    iYPos = 2
    
    imgGraphic.Left = (iXPos - 1) * iSquareWidth
    imgGraphic.Top = (iYPos - 1) * iSquareHeight
    iBoard(iXPos-1, iYPos-1) = 1 ' Indicates piece here
    The rest you can work out yourself.

    Randomly Place Pictures on the Board
    Obviously this means you require a random seed and to generate random numbers and also check that nothing is there already.

    Code:
    iPieces = 5
    
    Randomize
    For iItem = 1 to iPieces
      iXPos = Int(Rnd * iWidth) + 1
      iYPos = Int(Rnd * iHeight) + 1
      Do
        If iBoard(iXPos-1,iYPos-1) <> 0 Then
          iXPos = Int(Rnd * iWidth) + 1
          iYPos = Int(Rnd * iHeight) + 1
        Else
          PlacePiece(iXPos,iYPos) ' See above
          Exit Do
        End If
      Loop
    Next iItem
    So now you have the number of pieces you require, making sure they are not on top of each other and in random positions

    Randomly decide which pictures & how many
    This means you are now using not just 0 (Nothing) and 1 (Something) but instead would be setting the value of iBoard to be the type of picture.

    So above you would include the Type of picture (indicated with an integer) and keep track of how many you have left by making an array where each element corresponded to the number of the board type and the value said how many were left to place.

    Conclusion
    So you see that there isn't really any problem at all... all you need to do is break it down into seperate pieces, place each of those pieces into a function or a subroutine and then call those subroutines when you require them.

    Start with something simple and then expand and modify your own code to include things like type and how many.


    I hope it helps


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width