Movement of aliens as one in a space game

shaz1

Member
Joined
Jul 22, 2013
Messages
9
Programming Experience
Beginner
Hi,
I am trying to create a space aliens type game where the aliens all move left or right of the screen and then once they hit the side wall move down in one whole section. However at the moment only 2 are moving at a time, and am not sure what the problem is. Code is below and any help would be appreciated. Thanks

Public Class frmGame

PrivateSub Form1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) HandlesMyBase

.KeyUp
Select Case e.KeyCode
Case Keys.Right
tmrRight.Enabled = False
Case Keys.Left
tmrLeft.Enabled = False
End Select
End Sub



Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) HandlesMyBase.KeyDown
Select Case e.KeyCode
Case Keys.Right
tmrright.Enabled = True
tmrLeft.Enabled = False
Case Keys.Left
tmrleft.Enabled = True
tmrright.Enabled = False
Case Keys.Space And Missile.Visible = False
missile.Top = picship.Top
missile.Left = picShip.Let + (picShip.Width / 2) - (missile.Width / 2)
missile.Visible = True
End Select
End Sub




PrivateSub tmrleft_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrleft.Tick
picship.Left -= 4
End Sub



PrivateSub tmrright_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrright.Tick
picship.Left += 4
End Sub



Dim invaderspeed AsInteger
Dim invaderdrop As Integer
Dim Missilespeed As Integer
Const NumofInvaders As Integer = 18
Dim iRight(NumofInvaders) As Boolean
Dim invaders(NumofInvaders) As PictureBox
Dim x As Integer
Dim killed As Integer




PrivateSub TmrMain_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TmrMain.Tick
firemissile()
moveinvader()
checkhit()
checkgameover()
End Sub




PrivateSub

moveinvader()
For Me.x = 1 To NumofInvaders
If iRight(x) = True Then
invaders(x).Left += invaderspeed
Else
invaders(x).Left -= invaderspeed
End If

If invaders(x).Left + invaders(x).width > Me.ClientRectangle.Width Then
iRight(x) = False
invaders(x).Top += invaderdrop
End If

If invaders(x).Left < Me.ClientRectangle.Left Then
iRight(x) = True
invaders(x).Top += invaderdrop
End If
Next
End Sub



Private Sub frmGame_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
Dim obj As Object, x As Integer = 0
For Each obj In Me.Controls
If TypeOf obj is PictureBox AndAlso obj.Tag = "invaders" Then
invaders(x) = obj
x += 1
End If
Next obj
loadsettings()
Loadinvaders()
End Sub

Private Sub loadsettings()
Missilespeed = 50
missile.Visible = False
For Me.x = 1 To NumofInvaders
iRight(x) = True
Next
invaderspeed = 2
invaderdrop = 40
End Sub

Private Sub checkgameover()
'not yet done

Private Sub firemissile()
If missile.Visible = True Then
missile.Top -= Missilespeed
End If
If missile.Top + missile.Height <= Me.ClientRectangle.Top Then
missile.Visible = False
End If
End Sub

Private Sub checkhit()
For Me.x = 1 To NumofInvaders
If (missile.Top + missile.Height >= invaders(x).Top) And (missile.Top <= invaders(x).Top + invaders(x).Height) And (missile.Left + missile.Width >= invaders(x).left) And (missile.Left <= invaders(x).Left + invaders(x).Width) And missile.Visible = True And invaders(x).Visible = True Then
invaders(x).Visible = False
missile.Visible = False
killed += 1
End If
Next
End Sub

Private Sub LoadInvaders()
invaders(1) = picEnemy1
invaders(2) = picEnemy2
invaders(3) = picEnemy3
invaders(4) = picEnemy4
invaders(5) = picEnemy5
invaders(6) = picEnemy6
invaders(7) = picEnemy7
invaders(8) = picEnemy8
invaders(9) = picEnemy9
invaders(10) = picEnemy10
invaders(11) = picEnemy11
invaders(12) = picEnemy12
invaders(13) = picEnemy13
invaders(14) = picEnemy14
invaders(15) = picEnemy15
invaders(16) = picEnemy16
invaders(17) = picEnemy17
invaders(18) = picEnemy18
End Sub

End Class








 
Don't use individual PictureBoxes for everything. Use one big PictureBox and use GDI+ to draw everything on it. That one PictureBox will get repainted in one go so everything else will move in unison.
 
Hi,

This sounds like a good idea but I haven't tried this before. Would you have some example code I could look at?
Thanks
 
I don't have any specific links to GDI+ tutorials so I'd suggest a web search because there are some out there. Here are a couple of GDI+ examples I've created myself:

Very Simple Drawing Program
Manipulating GDI+ Drawings
Draw Common "Picture" on All Controls

What you'll probably want to do is use a Timer to update the drawing regularly. Invalidate the parts that have or may have changed and then Update and, in the Paint event handler, draw everything. If you use a PictureBox as the canvas then it's already double-buffered so will reduce flicker as much as possible.
 

Latest posts

Back
Top