r/visualbasic • u/DudeMcRocks • Jan 07 '19
Is it possible to have multiple picture boxes on screen at once moving at different speeds?
So I'm a student in grade 11 and in computer science, I've been working on a falling fruit game where fruit falls from the top and the user catches them in a basket. I want to have 2-3 fruit spawn at the same time but fall at different, constant speeds (or else it would be impossible for the user to collect them all). So for example banana 1 could be moving down 2 pixels per tick meanwhile banana 4 could be moving down 1 pixel per tick, meanwhile strawberry 1 could be moving down 2.5 pixels per tick, etc. Here is the main code I wrote, with some commented lines. The double comments ('') are to just help understand what I mean and what I'm going for. If you have any questions, want the full code, etc feel free to ask :)
Public Sub timerFruitSpawn_Tick(sender As Object, e As EventArgs) Handles timerFruitSpawn.Tick
x += 1 'x as integer
For count As Integer = x To 3 'spawns 3 fruits at once
Randomize()
fruitKind = 5 * Rnd() 'dim fruitKind as integer, determines the type of fruit that will spawn
fruitSpawnPos = 925 * Rnd() 'Dim fruitSpawnPos as integer
rndSpeed = 2.5 * Rnd() 'dim rndSpeed as Double
Select Case fruitKind
Case 0
fruitBanana(x) = New PictureBox
fruitBanana(x).Visible = False
fruitBanana(x).Image = My.Resources.bananna
fruitBanana(x).Location = New Point(fruitSpawnPos, 10)
fruitBanana(x).Width = 80
fruitBanana(x).Height = 80
fruitBanana(x).BackColor = Color.Transparent
Controls.Add(fruitBanana(x))
fruitBanana(x).Parent = picBackground
fruitBanana(x).Visible = True
''fruitBanana(x).DropSpeed = rnd speed (Now ik .DropSpeed isnt a real thing which is where im having trouble creating something like that)
fruits.Add(fruitBanana(x)) 'Dim fruits As New List(Of PictureBox)
If timerFruitDrop.Enabled = False Then
timerFruitDrop.Enabled = True
End If
Case 1
fruitCherry(x) = New PictureBox
fruitCherry(x).Visible = False
fruitCherry(x).Image = My.Resources.cherry
fruitCherry(x).Location = New Point(fruitSpawnPos, 10)
fruitCherry(x).Width = 80
fruitCherry(x).Height = 80
fruitCherry(x).BackColor = Color.Transparent
Controls.Add(fruitCherry(x))
fruitCherry(x).Parent = picBackground
fruitCherry(x).Visible = True
''fruitCherry(x).DropSpeed = rndSpeed
fruits.Add(fruitCherry(x))
If timerFruitDrop.Enabled = False Then
timerFruitDrop.Enabled = True
End If
Case 2
fruitGrapes(x) = New PictureBox
fruitGrapes(x).Visible = False
fruitGrapes(x).Image = My.Resources.grapes
fruitGrapes(x).Location = New Point(fruitSpawnPos, 10)
fruitGrapes(x).Width = 80
fruitGrapes(x).Height = 80
fruitGrapes(x).BackColor = Color.Transparent
Controls.Add(fruitGrapes(x))
fruitGrapes(x).Parent = picBackground
fruitGrapes(x).Visible = True
''fruitGrapes(x).DropSpeed = rndSpeed
fruits.Add(fruitGrapes(x))
If timerFruitDrop.Enabled = False Then
timerFruitDrop.Enabled = True
End If
Case 3
fruitOrange(x) = New PictureBox
fruitOrange(x).Visible = False
fruitOrange(x).Image = My.Resources.orange
fruitOrange(x).Location = New Point(fruitSpawnPos, 10)
fruitOrange(x).Width = 80
fruitOrange(x).Height = 80
fruitOrange(x).BackColor = Color.Transparent
Controls.Add(fruitOrange(x))
fruitOrange(x).Parent = picBackground
fruitOrange(x).Visible = True
''fruitOrange(x).DropSpeed = rndSpeed
fruits.Add(fruitOrange(x))
If timerFruitDrop.Enabled = False Then
timerFruitDrop.Enabled = True
End If
Case 4
fruitPear(x) = New PictureBox
fruitPear(x).Visible = False
fruitPear(x).Image = My.Resources.pear
fruitPear(x).Location = New Point(fruitSpawnPos, 10)
fruitPear(x).Width = 80
fruitPear(x).Height = 80
fruitPear(x).BackColor = Color.Transparent
Controls.Add(fruitPear(x))
fruitPear(x).Parent = picBackground
fruitPear(x).Visible = True
''fruitPear(x).DropSpeed = rndSpeed
fruits.Add(fruitPear(x))
If timerFruitDrop.Enabled = False Then
timerFruitDrop.Enabled = True
End If
Case 5
fruitStrawberry(x) = New PictureBox
fruitStrawberry(x).Visible = False
fruitStrawberry(x).Image = My.Resources.strawberry
fruitStrawberry(x).Location = New Point(fruitSpawnPos, 10)
fruitStrawberry(x).Width = 80
fruitStrawberry(x).Height = 80
fruitStrawberry(x).BackColor = Color.Transparent
Controls.Add(fruitStrawberry(x))
fruitStrawberry(x).Parent = picBackground
fruitStrawberry(x).Visible = True
''fruitStrawberry(x).DropSpeed = rndSpeed
fruits.Add(fruitStrawberry(x))
If timerFruitDrop.Enabled = False Then
timerFruitDrop.Enabled = True
End If
End Select
Next
End Sub
Private Sub timerFruitDrop_Tick(sender As Object, e As EventArgs) Handles timerFruitDrop.Tick
For Each fruit As PictureBox In fruits
fruit.Location = New Point(fruit.Location.X, fruit.Location.Y + ''fruit.DropSpeed) ''I want this to make the certain fruit move at the constant random speed it was given when it was created, and not make all fruits move at the same speed
Next
End Sub