Results 1 to 15 of 15

Thread: Space invaders alien loops/arrays

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    22

    Space invaders alien loops/arrays

    Been given an assignment to create a space invaders game. Although there's a long way around, is there a way to make my code smarter.

    Here's the section to make a row of invaders so far:

    Code:
    Public Class Form1
        'number of invaders in a row (horizontal)
        Dim numberofinvaders As Integer = 11
        'set of invaders (vertical)
        Dim setofinvaders As Integer = 4
        'creates pictureboxes of the invaders
        Dim invader(numberofinvaders) As PictureBox
        'tells me how many invaderd are left (score system)
        Dim invadersspawned As Integer = 0

    Code:
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'creates an array of pictureboxes (horizontal)
            For lc = 1 To numberofinvaders
    
                invadersspawned = invadersspawned + 1
                invader(lc) = New PictureBox
                invader(lc).Image = imginvader.Images(0)
                invader(lc).Height = 16
                invader(lc).Width = 16
                invader(lc).Location = New System.Drawing.Point((lc * 22) + 10, 10)
                Me.Controls.Add(invader(lc))
    
            Next
    
        End Sub
    So what I want to do more precisely is have the value of setofinvaders to change how many rows of invaders vertically in a loop. I'm pretty new to programming to don't know if this is possible, and if so, don't know how to do it myself.

    Thanks for reading, any tips would be greatly appreciated!

  2. #2
    Addicted Member
    Join Date
    Aug 2010
    Posts
    164

    Re: Space invaders alien loops/arrays

    Hi,

    Forgive me if this is not what you're asking, but couldn't you use another For Loop around "For lc = 1 to numberofinvaders" to spawn each row at a time?

    Code:
    For column as Short = 0 to setofinvaders - 1
           For lc = 1 To numberofinvaders
                invadersspawned = invadersspawned + 1
                invader(lc) = New PictureBox
                invader(lc).Image = imginvader.Images(0)
                invader(lc).Height = 16
                invader(lc).Width = 16
                invader(lc).Location = New System.Drawing.Point((lc * 22) + 10, 10 + column * 10)
                Me.Controls.Add(invader(lc)
            Next
    Next
    Changes are highlighted. Also the constants for the location may vary.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    22

    Re: Space invaders alien loops/arrays

    Quote Originally Posted by acrym View Post
    Hi,

    Forgive me if this is not what you're asking, but couldn't you use another For Loop around "For lc = 1 to numberofinvaders" to spawn each row at a time?

    Code:
    For column as Short = 0 to setofinvaders - 1
           For lc = 1 To numberofinvaders
                invadersspawned = invadersspawned + 1
                invader(lc) = New PictureBox
                invader(lc).Image = imginvader.Images(0)
                invader(lc).Height = 16
                invader(lc).Width = 16
                invader(lc).Location = New System.Drawing.Point((lc * 22) + 10, 10 + column * 10)
                Me.Controls.Add(invader(lc)
            Next
    Next
    Changes are highlighted. Also the constants for the location may vary.
    This is pretty much what I was asking. However, let's say if a bullet hits a invader on the first row, how would I be able to say to only hide the invader on the first row, and not second etc?

  4. #4
    Addicted Member
    Join Date
    Aug 2010
    Posts
    164

    Re: Space invaders alien loops/arrays

    Quote Originally Posted by oliver8765 View Post
    This is pretty much what I was asking. However, let's say if a bullet hits a invader on the first row, how would I be able to say to only hide the invader on the first row, and not second etc?
    That is a bigger undertaking... you could track the position of the bullet (if it's a picturebox) with Location.Y and Location.X each time it moves and check if it is near an invader.

    This is where it gets complicated - you'd need to compensate for the movement of the invaders itself, where the bullet needs to be to "hit" an invader, etc. It might be helpful to create a function that gives you the name of the nearest invader based on the bullet's position.

  5. #5
    New Member
    Join Date
    Oct 2011
    Posts
    12

    Re: Space invaders alien loops/arrays

    hi just wondering i try to change the size of the invaders but if i change it either doesnt work or all of them disappear accept one? also do you know any threads/post about how to move invaders which are in an array and how to shoot one and make only that invader disappear? I would appreciate a quick reply as i need to do it for my ICT assignment help would be greatly appreciated.

  6. #6
    Addicted Member
    Join Date
    Aug 2010
    Posts
    164

    Re: Space invaders alien loops/arrays

    Quote Originally Posted by con322 View Post
    hi just wondering i try to change the size of the invaders but if i change it either doesnt work or all of them disappear accept one? also do you know any threads/post about how to move invaders which are in an array and how to shoot one and make only that invader disappear? I would appreciate a quick reply as i need to do it for my ICT assignment help would be greatly appreciated.
    You should post your code so people can help you. I'm not as good of a programmer as other people here, but I'm pretty sure none of us are psychic.

  7. #7
    New Member
    Join Date
    Oct 2011
    Posts
    12

    Re: Space invaders alien loops/arrays

    Thanks for quick reply ok so here my code in the timer is how the invaders move, I used the code i found above because i am new to arrays anyways what i want to do is have to row of invaders that i can do but i dont know how to make the timer move bothe rows it only moves one can u help?

    Public Class Form1
    'number of invaders in a row (horizontal)
    Dim numberofinvaders As Integer = 11

    'set of invaders (vertical)
    Dim setofinvaders As Integer = 2

    'creates pictureboxes of the invaders
    Dim invader(numberofinvaders) As PictureBox

    'tells me how many invaderd are left (score system)
    Dim invadersspawned As Integer = 0

    Dim alienmove As Integer = 5

    Dim invaders As Integer = 5


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    For column as Short = 0 to setofinvaders - 1
    For lc = 1 To numberofinvaders
    invadersspawned = invadersspawned + 1
    invader(lc) = New PictureBox
    invader(lc).Image = imginvader.Images(0)
    invader(lc).Height = 25
    invader(lc).Width = 25
    invader(lc).Location = New System.Drawing.Point((lc * 30) + 10)
    Me.Controls.Add(invader(lc))
    Next

    For lc = 1 To numberofinvaders
    invadersspawned = invadersspawned + 1
    invader(lc) = New PictureBox
    invader(lc).Image = imginvader.Images(0)
    invader(lc).Height = 25
    invader(lc).Width = 25
    invader(lc).Location = New System.Drawing.Point((lc * 30) + 10,10 + column * 10)
    Me.Controls.Add(invader(lc))
    Next
    Next


    End Sub

    Private Sub tmrinvader_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrinvader.Tick

    For count = 1 To 11

    invader(count).Left = invader(count).Left + invaders

    Next count


    If invader(11).Left + invader(11).Width > Me.Width - invader(11).Width Or invader(1).Left < 0 Then

    invaders = -invaders

    For count = 1 To 11
    invader(count).Top = invader(count).Top + 10
    Next
    End If


    End Sub

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    22

    Re: Space invaders alien loops/arrays

    It's my understanding that you will have to create each row of invaders separately. In my code I have to have invader1, invader2, invader3, invader4 for each row.

  9. #9
    New Member
    Join Date
    Oct 2011
    Posts
    12

    Re: Space invaders alien loops/arrays

    yeah i have done that now thanks em i know i have asked several questions but i am stuck on the collision detection between the invaders as i have created five seperate arrays, can you tell me the code for the collision detection plz

    thanks

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    22

    Re: Space invaders alien loops/arrays

    Don't have access to my code right now, but it went something like this:

    For i = 1 to rowsofinvaders

    if picbullet.bounds.intersectswith(invader1(i).bounds) and invader(i).visible = true then

    invader1(i).visible = false
    end if
    next

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    22

    Re: Space invaders alien loops/arrays

    Don't have access to my code right now, but it went something like this:

    For i = 1 to rowsofinvaders

    if picbullet.bounds.intersectswith(invader1(i).bounds) and invader(i).visible = true then

    invader1(i).visible = false
    end if
    next

  12. #12
    New Member
    Join Date
    Oct 2011
    Posts
    12

    Re: Space invaders alien loops/arrays

    thank em do i need to dim 'i' or something because when i put For i = it underlines i for some reason and also do i just duplicate the code for the other set of invaders

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    22

    Re: Space invaders alien loops/arrays

    Quote Originally Posted by con322 View Post
    thank em do i need to dim 'i' or something because when i put For i = it underlines i for some reason and also do i just duplicate the code for the other set of invaders
    The 'i' is the number in the loop... which in this case corresponds with a invader (assuming you have your invaders in arrays)

  14. #14
    New Member
    Join Date
    Oct 2011
    Posts
    12

    Re: Space invaders alien loops/arrays

    i know u dont have ur code right now but em as soon as i press enter to start the game and the invader timer starts the game stop and highlighs the code above in yellow, do u know why this would happen

  15. #15
    Registered User
    Join Date
    Sep 2013
    Posts
    2

    Re: Space invaders alien loops/arrays

    Hi,

    Am using this section of code above to help write a space invaders type game for a project. Could you give some advice on how to create each row of invaders separately? I'm a bit confused as to how to do this. Thank you heaps.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width