*** am i doing wrong (Space Invaders)-VBForums
Results 1 to 5 of 5

Thread: *** am i doing wrong (Space Invaders)

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2014
    Posts
    2

    Post *** am i doing wrong (Space Invaders)

    so i have been coding a space invaders game as a final project but for some reason my array wont move. im pretty sure ive gotten tunnel vision. any advice?



    the code



    Public Class ParentForm
    #Region "Variables"
    Dim blnLeft As Boolean
    Dim blnRight As Boolean

    Dim aryInvaders(15) As PictureBox

    Dim intChangeDirection As Integer = 3




    #End Region

    #Region "Buttons"

    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
    Me.lblTitle.Hide()
    Me.BtnExit.Hide()
    Me.btnStart.Hide()
    Me.pbxTitleInvader.Hide()
    Me.pbxTitleInvader2.Hide()
    Me.pbxTitleinvader3.Hide()
    Me.pbxTitleInvader4.Hide()
    Me.pbxTitleShip.Hide()
    Me.GamePanel.Show()
    Me.Movecomp.Enabled = True

    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnExit.Click
    Me.Close()
    End Sub

    #End Region

    #Region "Timers"
    Private Sub Movecomp_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Movecomp.Tick
    MoveAliens()
    If blnLeft = True Then

    pbxSpaceShip.Left = pbxSpaceShip.Left - 3
    End If
    If blnRight = True Then
    pbxSpaceShip.Left = pbxSpaceShip.Left + 3


    End If
    MoveAliens()
    End Sub
    #End Region

    #Region "KeyPresses"
    Private Sub MoveCompLeft(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    If e.KeyValue = Keys.A Then
    blnLeft = True
    End If
    If e.KeyValue = Keys.D Then
    blnRight = True

    End If
    End Sub

    Private Sub MoveCompStop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
    If e.KeyValue = Keys.A Then
    blnLeft = False
    End If
    If e.KeyValue = Keys.D Then
    blnRight = False
    End If

    End Sub
    #End Region


    Public Sub New()
    InitializeComponent()
    Array()

    End Sub

    Private Sub Array()
    aryInvaders(0) = aryInvaders(1)
    aryInvaders(1) = aryInvaders(2)
    aryInvaders(2) = aryInvaders(3)
    aryInvaders(3) = aryInvaders(4)
    aryInvaders(4) = aryInvaders(5)
    aryInvaders(5) = aryInvaders(6)
    aryInvaders(6) = aryInvaders(7)
    aryInvaders(7) = aryInvaders(8)
    aryInvaders(8) = aryInvaders(9)
    aryInvaders(9) = aryInvaders(10)
    aryInvaders(10) = aryInvaders(11)
    aryInvaders(11) = aryInvaders(12)
    aryInvaders(12) = aryInvaders(13)
    aryInvaders(13) = aryInvaders(14)
    aryInvaders(14) = aryInvaders(15)



    End Sub

    Private Sub MoveAliens()
    For i = 0 To 15
    aryInvaders(i).Left = aryInvaders(i).Left + intChangeDirection

    Next
    End Sub


    End Class

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    3,920

    Re: *** am i doing wrong (Space Invaders)

    Above the replay or post window should be a line of icons and fields. The last one, #, will insert [ Code] [ /Code] tags. If you put your code between those tags it will be easier for people to read as it will retain your formatting (for the most part).

    Where do you create pictureboxes for the aliens?
    You have an array, aryInvaders, but where do you create pictureboxes to put in that array, or if you already had a bunch of pictureboxes on the form for the aliens, then you should have code setting those picturebox into the array.
    For instance, you have the the titleInvader pictureboxes.
    If I comment out the code that hides them, and I assign those four pictureboxes to the array, in your array sub.
    Code:
        aryInvaders(0) = pbxTitleInvader
        aryInvaders(1) = pbxTitleInvader2
        aryInvaders(2) = pbxTitleInvader3
        aryInvaders(3) = pbxTitleInvader4
    And change your move loop to move only those 4, since you won't get null references on them because now they have a picturebox reference in them.
    Code:
      Private Sub MoveAliens()
        For i = 0 To 3
          aryInvaders(i).Left = aryInvaders(i).Left + intChangeDirection
    
        Next
      End Sub
    Then, when you hit the start button, those four pictureboxes will start moving across the panel. Of course, they don't stop but just continue on off the panel, but its a start.

    By the way, this forum is for posting code examples related to games, not asking questions about game code. I've informed the moderators to move the thread.
    Last edited by passel; Dec 23rd, 2014 at 04:07 AM.

  3. #3
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    6,513

    Re: *** am i doing wrong (Space Invaders)

    Moved. codebank is for code submissions rather than questions.
    You can depend upon the Americans to do the right thing. But only after they have exhausted every other possibility - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2014
    Posts
    2

    Re: *** am i doing wrong (Space Invaders)

    Thanks sorry I didn't know about where to post it I'll remember for next time. thanks
    Again

  5. #5
    The DirectXpert Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,165

    Re: *** am i doing wrong (Space Invaders)

    Its not hard to make a space invaders game, so I'm gonna help you get on the right track. First of all, the basic thing is the movement. With that said, your gonna need a structure for your array of sprites that contain X and Y coordinates. The creatures or aliens will be the array of structures, or what I would like to call a sprite list, where each index of the array will contain the position of every alien. The for loop in your "main game loop" (which you better have a game loop or there's no point of continuing further till you get one) will move the sprites, but in increments over time, by going over every alien one by one and updating their positions, so you'll need a time variable to keep track of time, which you can extract from a timeGetTime API (Google it if you never used it before). You will also need another variable being initial time which will also get time from the timeGetTime API, only this will be used to get the start time. The actual time will be obtained by subtracting time from initial time, also known as delta time. So now you have a setup to get real time. Now every 2 seconds, the aliens will increment. After each increment, you will need to check the positions of the left most aliens when moving left, and the right most aliens when moving right.

    How do I check this you ask? During your for loop, get another variable to compare positions. The greater position (right most) will be made by comparing each aliens X coordinate, and if its greater than the last alien, itll obtain that new value, otherwise, check the next alien. Like if alien 1 is at 100, Greater_Position will have 100. Then it checks alien 2. If alien(2).position.X is greater than Greater_Position, Greater_Position = alien(2).position.X, and so on and so forth. Same than for the Lesser position when they move left, only its less than instead. But itll only check the alien if they are visible, as in alive, so you can have an if statement to only update and check positions if they are alive.

    Now once the Greater_Position or Lesser_Positions are obtained, you are gonna check if they hit the edge of the screen. If they did, bring the aliens all a step down in the Y direction, and have them go in the reverse direction. You can keep track of the aliens moving left or right by simply using a boolean variable, so if its false they move right, or true then they move left. Keep bring the aliens down until they reach the player, and boom, game over.

    Collision is important, and there are a bazillion ways to check for collision. It also wouldnt be a fun game without it. 2D Bounding box is typically used and easy to implement, but severely limited because if your bullets are too fast, it could go through the alien! Another method that could potentially be used is called Sweep Collision. Think of point A of your bullet in the previous frame, and point B of your bullet in the next frame creating an invisible rectangular box between the both of them. that box is checked if it is within the bounds of the sprite. It makes sense to use this if you plan on implementing fast bullets. But if the bullets are moving slow, then 2D bounding box collision would suffice.

    The next step would be to change the time they increment as there are lesser and lesser aliens. Originally I said they move every 2 seconds. But if there are 2,3, or even 4 less, that can reduce every 0.2 seconds until it reaches 0.2 seconds or the last alien.

    The shields would be iffy because of the severe limitations of pictureboxes. Personally Id use DirectX instead to work with polygons, and not use pictureboxes at all. That way you'll have more of a professional game going. But thats just me. Take a look at my video game making tutorial in my signature for some tips

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.