Ok well none of it is too ideal but they change direction because you're only looking at the player direction.
What you'll want to do is have a separate variable for each picture box that tells it which direction it needs to travel in.
You could set up an Enum (e.g enumDirection) to have values like: None, Top, Down, Left, Right
Then for example declare a variable like
Dim picDirection1 As enumDirection = None
Then in your timer, check to see if the picDirection1 is None, if it is; set the direction it needs to travel in. So if it has to travel left, set picDirection1 = Left.
If picDirection1 is not None, then move it in the direction it needs to go.
You'll probably want a different Sub to call to move it, where you pass on the picturebox and the direction and have the sub move it accordingly.
I hope I've explained that well enough. I'm busy and in my phone.
Oh, it's got someone else's name on it. I thought that was a bit much to write from their phone.
Never mind it's showing up under your name.
' ---- missing Function or Sub, or are these not locals?
Dim playerGunner As Boolean = False
Dim playerLeft As Boolean = False
Dim playerRight As Boolean = False
Dim playerUp As Boolean = False
Dim playerDown As Boolean = False
Dim bullet1 As Boolean = False
Dim bullet2 As Boolean = False
Dim bullet3 As Boolean = False
Dim bullet4 As Boolean = False
Case Keys.Space ' ---- missing Switch
If playerGunner = True Then
If picBullet1.Visible = False
Then bullet1 = True
CentreOn(picChar, picBullet1)
picBullet1.Visible = True
tmrBullet.Enabled = True
picAmmo1.Visible = False
ElseIf picBullet1.Visible = True And picBullet2.Visible = False Then
bullet2 = True
CentreOn(picChar, picBullet2)
picBullet2.Visible = True
tmrBullet.Enabled = True
icAmmo2.Visible = False
ElseIf picBullet1.Visible = True And picBullet2.Visible = True And picBullet3.Visible = False Then
bullet3 = True CentreOn(picChar, picBullet3)
picBullet3.Visible = True tmrBullet.Enabled = True
picAmmo3.Visible = False
ElseIf picBullet1.Visible = True And picBullet2.Visible = True And picBullet3.Visible = True And picBullet4.Visible = False Then
bullet4 = True
CentreOn(picChar, picBullet4)
picBullet4.Visible = True
tmrBullet.Enabled = True
picAmmo4.Visible = False
End If
End If
Private Sub tmrBullet_Tick(sender As Object, e As EventArgs) Handles tmrBullet.Tick
If playerRight = True Then
If bullet1 = True Then
picBullet1.Left += 20
End If
If bullet2 = True Then
picBullet2.Left += 20 End If
If bullet3 = True Then
picBullet3.Left += 20
End If
If bullet4 = True Then
picBullet4.Left += 20
End If
End If
If playerLeft = True Then
If bullet1 = True Then
picBullet1.Left -= 20
End If
If bullet2 = True Then
picBullet2.Left -= 20
End If
If bullet3 = True Then
picBullet3.Left -= 20
End If
If bullet4 = True Then
picBullet4.Left -= 20
End If
End If
If playerUp = True Then
If bullet1 = True Then
picBullet1.Top -= 20
End If
If bullet2 = True Then
picBullet2.Top -= 20
End If
If bullet3 = True Then
picBullet3.Top -= 20
End If
If bullet4 = True Then
picBullet4.Top -= 20
End If
End If
If playerDown = True Then
If bullet1 = True Then
picBullet1.Top += 20
End If
If bullet2 = True Then
picBullet2.Top += 20
End If
If bullet3 = True Then
picBullet3.Top += 20
End If
If bullet4 = True Then
picBullet4.Top += 20
End If
End If
End Sub
This comment is just for formatting, you can do the formatting next time.
You call this a bug, I call it the last bullet bender. Based on that code, well, you should store the directions of each bullet fired because you make them go in the direction the player is facing right now. You can simplify the code by making a Dim bullet1dir As Integer, Dim bullet2dir As Integer and inside these if Bullet1 = true you nest some ifs, example:
If bullet1dir = 1 Then (lets say move the bullet upwards with picBullet1.Top -= 40), if Bullet1Dir = 2 Then move it to the left etc.
Although I'd strongly recommend learning OOP and making classes and collections to better and easier manage such things in games, when I will have some time I bookmarked this post and I will make a game similar to this but with OOP principles... and yes, you can create PictureBoxes programatically for as many bullets as you wish to fire.
3
u/sa_sagan VB.Net Master May 02 '22
What exactly are you having problems figuring out?
1
u/[deleted] May 02 '22
Code:
Dim playerGunner As Boolean = False
Dim playerLeft As Boolean = False
Dim playerRight As Boolean = False
Dim playerUp As Boolean = False
Dim playerDown As Boolean = False
Dim bullet1 As Boolean = False
Dim bullet2 As Boolean = False
Dim bullet3 As Boolean = False
Dim bullet4 As Boolean = False
Case Keys.Space
If playerGunner = True Then
If picBullet1.Visible = False Then
bullet1 = True
CentreOn(picChar, picBullet1)
picBullet1.Visible = True
tmrBullet.Enabled = True
picAmmo1.Visible = False
ElseIf picBullet1.Visible = True And
picBullet2.Visible = False Then
bullet2 = True
CentreOn(picChar, picBullet2)
picBullet2.Visible = True
tmrBullet.Enabled = True
picAmmo2.Visible = False
ElseIf picBullet1.Visible = True And
picBullet2.Visible = True And
picBullet3.Visible = False Then
bullet3 = True
CentreOn(picChar, picBullet3)
picBullet3.Visible = True
tmrBullet.Enabled = True
picAmmo3.Visible = False
ElseIf picBullet1.Visible = True And
picBullet2.Visible = True And
picBullet3.Visible = True And
picBullet4.Visible = False Then
bullet4 = True
CentreOn(picChar, picBullet4)
picBullet4.Visible = True
tmrBullet.Enabled = True
picAmmo4.Visible = False
End If
End If
Private Sub tmrBullet_Tick(sender As Object, e As EventArgs) Handles tmrBullet.Tick
If playerRight = True Then
If bullet1 = True Then
picBullet1.Left += 20
End If
If bullet2 = True Then
picBullet2.Left += 20
End If
If bullet3 = True Then
picBullet3.Left += 20
End If
If bullet4 = True Then
picBullet4.Left += 20
End If
End If
If playerLeft = True Then
If bullet1 = True Then
picBullet1.Left -= 20
End If
If bullet2 = True Then
picBullet2.Left -= 20
End If
If bullet3 = True Then
picBullet3.Left -= 20
End If
If bullet4 = True Then
picBullet4.Left -= 20
End If
End If
If playerUp = True Then
If bullet1 = True Then
picBullet1.Top -= 20
End If
If bullet2 = True Then
picBullet2.Top -= 20
End If
If bullet3 = True Then
picBullet3.Top -= 20
End If
If bullet4 = True Then
picBullet4.Top -= 20
End If
End If
If playerDown = True Then
If bullet1 = True Then
picBullet1.Top += 20
End If
If bullet2 = True Then
picBullet2.Top += 20
End If
If bullet3 = True Then
picBullet3.Top += 20
End If
If bullet4 = True Then
picBullet4.Top += 20
End If
End If
End Sub
Edit: just so u know this is in different private subs in case its not obvious
1
u/RJPisscat May 02 '22
Where's the code?
1
u/[deleted] May 02 '22
You call this a bug, I call it the last bullet bender. Based on that code, well, you should store the directions of each bullet fired because you make them go in the direction the player is facing right now. You can simplify the code by making a Dim bullet1dir As Integer, Dim bullet2dir As Integer and inside these if Bullet1 = true you nest some ifs, example:
If bullet1dir = 1 Then (lets say move the bullet upwards with picBullet1.Top -= 40), if Bullet1Dir = 2 Then move it to the left etc.
Although I'd strongly recommend learning OOP and making classes and collections to better and easier manage such things in games, when I will have some time I bookmarked this post and I will make a game similar to this but with OOP principles... and yes, you can create PictureBoxes programatically for as many bullets as you wish to fire.