This is the code I have for the game, I am making but there's a problem. There are 9 enemies moving from left to right. I was wondering it there was a way of making just one picturebox but it appearing multiple times with different behaviors and speed.
Make a custom control inheriting from the picturebox, add extra properties such as speed. Then use the speed property in your code to handle the movement.
Public Class EnhancedPictureBox
Inherits PictureBox
Public Property Speed() As Decimal
End Class
When you instantiate an EnhancedPictureBox on your form, you can now set a speed property and use that in your movement code. The same idea for your other behaviours. You can also add new methods or overwrite existing ones to give you additional functionality for your enemies.
Edit:
Hmm, forgot you wanted 1 picturebox, why not instantiate more to give each different properties?
This might help, this here is the code I have for my pictureboxes = Enemies
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
Enemy1.Left = Enemy1.Left + 1
Enemy2.Left = Enemy2.Left - 2
If Enemy1.Left > 1400 Then
Enemy1.Left = -100
End If
If Enemy2.Left < -1400 Then
Enemy2.Left = 800
End If
End Sub
Private Sub Timer2_Tick(sender As System.Object, e As System.EventArgs) Handles Timer2.Tick
Enemy3.Left = Enemy3.Left + 2
Enemy4.Left = Enemy4.Left - 3
If Enemy3.Left > 1400 Then
Enemy3.Left = -100
End If
If Enemy4.Left < -1400 Then
Enemy4.Left = 800
End If
End Sub
Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
Enemy5.Left = Enemy5.Left + 4
Enemy6.Left = Enemy6.Left - 4
If Enemy5.Left > 1400 Then
Enemy5.Left = -100
End If
If Enemy6.Left < -1400 Then
Enemy6.Left = 800
End If
End Sub
If you replaced the existing pictureboxes with the enhancedpicturebox you could then do something like this to provide different speeds
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
Enemy1.Left = Enemy1.Left + (1 * Enemy1.Speed)
Enemy2.Left = Enemy2.Left - (2 * Enemy2.Speed)
If Enemy1.Left > 1400 Then
Enemy1.Left = -100
End If
If Enemy2.Left < -1400 Then
Enemy2.Left = 800
End If
You could also move to make your code more re-usable
Protected lstPB As List(Of PictureBox)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Timer1.Start()
lstPB = New List(Of PictureBox)
lstPB.Add(enemy1)
lstPB.Add(enemy2)
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
MovePB(lstPB)
End Sub
Private Sub MovePB(ByVal PBs As List(Of PictureBox))
For Each p As PictureBox In PBs
p.Left(+(1 * p.speed))
If p.Left > 1400 Then enemy1.Left = -100
Next
End Sub
This way you can add and remove enemies from the collection and have the movement handled for them all.
You need to create the custom picture box though, so it has the speed property.
If you go down the list way of doing it, it will be a list of your custom control, i just wrote PictureBox in my example, I suppose that may have been confusing. i should have said List(of EnhancedPictureBox)
The timer should then make a call to the method for moving, passing in your list. The method could be made more genetic rather than than naming the control instances so it can handle any number of enemies.
What is the enhanced picturebox?
the whole purpose of renaming my pictureboxes as enemy1, 2, 3, 4 is so i don't have to use the name picturebox.
I just want the 1 enemy picturebox to show multiple times :|
Either you are having trouble understanding the idea of instances or I am getting confused over what you are asking.
The Custom Control I mentioned before, can be added to a project by right clicking the project, selecting add, then selecting Custom Control.
The Custom Control looks like this and I called It EnhancedPictureBox for examples sake.
Public Class EnhancedPictureBox
Inherits PictureBox
Public Property Speed() As Integer
End Class
When you drag 2 of these on to a form or type this in code
Dim enemyOne = new EnhancedPictureBox()
Dim enemyTwo = new EnhancedPictureBox()
You are creating two instances of EnhancedPictureBox. So you now have 2, not 1. They are of the same Type, but you have two of them.
If you then had a list
Dim myList = new List(of EnhancedPictureBox)()
myList.Add(enemyOne)
myList.Add(enemyTwo)
You could pass that list into a Method to do something with your EnhancedPictureBoxes.
Private Sub MoveLeft(byval PBList as List(of EnhancedPictureBox))
For Each p As EnhancedPictureBox In PBList
'Move EnhancedPictureBox left at the rate of 1 * Speed Property
p.Left = p.Left + (1 * p.Speed))
'If Going too far, move back 100
If p.Left > 1400 Then p.Left = p.Left -100
Next
End Sub
Now, no matter how many EnhancedPictureBox's you add to your list, they will all be moved left by different rates depending on the Speed property set. You just need to pass the list of EnhancedPictureBox's to the method.
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
MoveLeft(MyList)
End Sub
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.