Enemy = One Picturebox Not Happening :(

MrMk2gtx

Active member
Joined
Apr 1, 2011
Messages
29
Programming Experience
Beginner
Public Class Form1
Dim SRight As Boolean 'Shooter moving right 
Dim SLeft As Boolean 'Shooter moving left 
Dim ShooterSpeed As Integer 'How much the shooter moves 
Dim ShotSpeed As Integer 'How much the shot moves 
Dim MoveRight(5) As Boolean 
Dim MoveLeft(5) As Boolean 
Dim Speed(5) As Integer 
Dim Paused As Boolean Const NumOfEnemys As Integer = 15 
Dim Enemys(NumOfEnemys) As PictureBox 'Makes the invader array 
Dim x As Integer 

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick 'when the timer tickes: 
MoveShooter() 
FireShot() 
CheckHit() 

For Me.x = 1 To 5 

Enemys(x).Left = Enemys(x).Left + Speed(x) 
If Enemys(x).Left > 1400 Then 
Enemys(x).Left = -100 
End If 

If Enemys(x).Left [COLOR=#000080]<= Me.ClientRectangle.Left Then 
MoveRight(x) = True End If 
If Enemys(x).Left + Enemys(x).Width >[/COLOR]= Me.ClientRectangle.Right Then

MoveRight(x) = False End If 
If Enemys(x).Left >= Me.ClientRectangle.Top Then 
MoveLeft(x) = False End If 
If Enemys(x).Left + Enemys(x).Height [COLOR=#000080]<= Me.ClientRectangle.Bottom Then MoveLeft(x) = True End If 
Next 
End Sub 

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
LoadSettings() 
Enemys(1) = Enemy1 
Enemys(2) = Enemy2 
Enemys(3) = Enemy3 
Enemys(4) = Enemy4 
Enemys(5) = Enemy5 

MoveRight(1) = True 
MoveRight(2) = False 
MoveRight(3) = False 
MoveRight(4) = True 
MoveRight(5) = False 

MoveLeft(1) = True 
MoveLeft(2) = True 
MoveLeft(3) = False 
MoveLeft(4) = False 
MoveLeft(5) = False 

For Me.x = 1 To 5 
Speed(x) = x 
Next 
End Sub 

Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown 
If e.KeyValue = Keys.Right Then ' if the right arrow key is pressed 
SRight = True 
SLeft = False 
End If 
If e.KeyValue = Keys.Left Then ' if the left arrow key is pressed 
SLeft = True 
SRight = False 
End If 
If e.KeyValue = Keys.Space And Shot.Visible = False Then ' if the space is pressed and the shot has not been fired 
Shot.Top = Shooter.Top ' shot comes out the shooters top 
Shot.Left = Shooter.Left + (Shooter.Width / 2) - (Shot.Width / 2) 'shot is centered in the shooter Shot.Visible = True 
End If 
End Sub 

Private Sub MoveShooter() 
If SRight = True And Shooter.Left + Shooter.Width < Me.ClientRectangle.Width Then 
Shooter.Left += ShooterSpeed 'shooter moves to the right End If 
If SLeft = True And Shooter.Left >[/COLOR] Me.ClientRectangle.Left Then Shooter.Left -= ShooterSpeed 'shooter moves to the left End If End Sub 

Private Sub Form1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp 
If e.KeyValue = Keys.Right Then 
SRight = False ' shooter is not moving to the right 
End If 

If e.KeyValue = Keys.Left Then 
SLeft = False ' shooter is not moving to the left 
End If 
End Sub 

Private Sub LoadSettings() 
Paused = False ' game is not paused 
ShotSpeed = 10 ' how fast the shot moves 
ShooterSpeed = 8 ' how fast the shooter moves 
Shot.Visible = False 
End Sub 

Private Sub FireShot() If Shot.Visible = True Then Shot.Top -= ShotSpeed ' shot moves up End If If Shot.Top + Shot.Height < Me.ClientRectangle.Top Then Shot.Visible = False ' shot hits the top of the window and is ready to fire        End If End Sub 

Private Sub CheckHit()
For Me.x = 1 To NumOfEnemys If (Shot.Top + Shot.Height >= Enemys(x).Top) And (Shot.Top [COLOR=#000080]<= Enemys(x).Top + Enemys(x).Height) And (Shot.Left + Shot.Width >[/COLOR]= Enemys(x).Left) And (Shot.Left <= Enemys(x).Left + Enemys(x).Width) And Shot.Visible = True And Enemys(x).Visible = True Then Enemys(x).Visible = False Enemys(x).Left = -100 Enemys(x).Visible = True Shot.Visible = False ShotDown += 1 End If Next End Sub 

Private Sub Form1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress 
If e.KeyChar = "P" Or e.KeyChar = "p" Then 
If Paused = True Then 'unpauses game 
TimerMain.Enabled = True Paused = False Else 
TimerMain.Enabled = False 'pauses game 
Paused = True End If
End If 
End Sub
End Class
 
Maybe you could provide a more specific description that "not happening". Exactly what behaviour do you expect and what behaviour do you see? Is there an error? If so, where does it occur and what's the error message?
 

Latest posts

Back
Top