|
-
Apr 1st, 2001, 01:14 AM
#1
Thread Starter
Member
well, the following code is interesting, all u need is a folder that contains pictures and named the pics as
pic1, pic2, pic3, etc.... and run the program.
also, u must have a imagebox named "imgControl" with index property 0.
now, my problem is dealing with the random function. i wanted pics of the same to be appeared only twice.
i do not allow pics of the same to be appeared once or more then twice. see code below: (especially the random function).
how can i solve that?
Code:
Option Explicit
' Global variables hold the total number of columns and rows
Private TotalCols As Integer
Private TotalRows As Integer
'-----------------------------------------------------------------------------------------
Private Sub Form_Load()
' You must have an image on the form with the following properties:
' .Name = imgControl
' .Index = 0
' .Visible = False
' .Strech = True
' Set the picture property to something (a bitmap, icon, etc.)
TotalCols = InputBox("How many columns do you want?", "Set Columns", "4")
TotalRows = InputBox("How many rows do you want?", "Set Rows", "4")
Call LoadImagesOnForm
End Sub
'--------------------------------------------------------------------------------------
Private Sub LoadImagesOnForm()
Dim ImgCount As Integer
Dim RowCount As Integer
Dim ColCount As Integer
Dim FrmScalHghtDiff As Integer
Dim FrmScalWdthDiff As Integer
Dim i As Integer, num As Integer, k As Integer
Dim filepath As String
With Me
' Get the difference between (Form1.Height and Form1.ScaleHeight)
' and (Form1.Width and Form1.ScaleWidth) for use in resizing form
FrmScalHghtDiff = .Height - .ScaleHeight
FrmScalWdthDiff = .Width - .ScaleWidth
' Set width and height of form so that it resizes to the
' number of images the user requested to display
.Height = (imgControl(0).Height * TotalRows) + FrmScalHghtDiff
.Width = (imgControl(0).Width * TotalCols) + FrmScalWdthDiff
' These loops load images by rows first, then columns, like this:
' 1 2 3
' 4 5 6
' 7 8 9
For RowCount = 1 To TotalRows
For ColCount = 1 To TotalCols
ImgCount = ImgCount + 1
Load imgControl(ImgCount)
'-----------------------------------------------------------
Randomize Timer 'here is the problem...
k = TotalRows * TotalCols
num = 1 + Int(Rnd * k)
filepath = "C:\Unzipped Files\memory2\Icons" & "\im" & num & ".ico"
imgControl(ImgCount).Picture = LoadPicture(filepath)
'------------------------------------------------------------
With imgControl(ImgCount)
.Move (ColCount - 1) * imgControl(0).Width, _
(RowCount - 1) * imgControl(0).Height
.Visible = True
End With ' imgControl(ImgCount)
Next ' ColCount
Next ' RowCount
End Sub
Last edited by ariel_au; Apr 1st, 2001 at 02:08 AM.
-
Apr 1st, 2001, 05:26 AM
#2
just a small comment, but if you have a grid with a prime number in each dimension, then you can't do it.
if you have (in your example) a grid of 3 x 3, then that is 9 squares. if you fill it up with 2 pictures, you have 1 left over. to prevent this, make sure one of your dimensions is a multiple of two.
As for solving your problem, here is a small attempt:
Code:
dim PicsToShow()
redim picstoshow(1 to (x * y) / 2)
For fill = 1 To UBound(picstoshow)
Do
found = False
num = 1 + Int(Rnd * totalpicsondisk)
For test = 1 To fill
If picstoshow(test) = num Then found = True
Next test
Loop While found = True
picstoshow(fill) = num
Next fill
this will fill an array with numbers. Each number will only appear once. Each number will represent one of the pictures on disk. This will make sure that each image is duplicated only one time.
To load them into an array that represents your grid on screen, do this:
Code:
Dim screenarray()
redim screenarray( 1 to x*y)
for initializenum = 1 to ubound(screenarray)
screenarray(initializenum) = 999
next initializenum
For fill = 1 To UBound(screenarray)
For twotimes = 1 To 2
found = False
Do
found = False
num = 1 + Int(Rnd * UBound(picstoshow))
If screenarray(num) <> 999 Then found = True
Loop While found = True
screenarray(num) = picstoshow(fill)
Next twotimes
Next fill
basically this will fill a 2nd array that represents your picture boxes with each number that occurs in the first array - two times.
The advantage of storing the nums in an array is you can hide all the shown pictures by changing the picture box contents to a neutral image, and all you have to do to restore it is check the value in the array. I hope this helps. Picture flip programs are neat.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|