Is this too complicated to do?
Is this too complicated to do?
Is this too complicated to do?
Is this too complicated to do?
Is this too complicated to do?
Is this too complicated to do? Is this too complicated to do? Is this too complicated to do? Is this too complicated to do? Is this too complicated to do? Is this too complicated to do? Is this too complicated to do? Is this too complicated to do?
Is this too complicated to do? Is this too complicated to do?
Is this too complicated to do?
Go Back  Xtreme Visual Basic Talk > > > Is this too complicated to do?


Reply
 
Thread Tools Display Modes
  #1  
Old 05-26-2011, 04:21 AM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default Is this too complicated to do?


Basically I have movement code for my picture boxes to go right until they hit a point then go down and move back left. I used if statements and it worked good. I've tried implementing arrays with for each, next loops. but I think I just made it worse.



The if statements:

Code:
    If Alien1Dir = "Go2Right" Then
                Alien1.Left += AlienSpeed  'Alien goes right
                Alien1.Image = My.Resources.RedAlienSprite2
            Else
                Alien1.Left -= AlienSpeed  'Alien goes left
                Alien1.Image = My.Resources.RedAlienSprite1
            End If

            If (Alien1.Location.X < 20 And Alien1Hit = False) Or (Alien1.Location.X > 850 And Alien1Hit = False) Then
                If Alien1.Location.X < 20 Then 'The left boundry stop point for Alien.

                    Alien1Dir = "Go2Right"
                Else 'The right boundry stop point for Alien.
                    Alien1Dir = "" 'Alien direction no longer Go2Right, so Alien now goes left.
                End If

                If Alien1.Location.Y < 370 And Alien1Hit = False Then 'Sets the ground point where you are considered invaded...
                    Alien1.SetBounds(Alien1.Location.X, Alien1.Location.Y + 20, 20, 20) 'Drop alien by its own height "20"
                    AlienSpeed += 2 'Increase the speed of the invasion


                End If
            End If







arrays with loops:
Code:
For Each AlienDirArrayItem In AlienDirArray
            If AlienDirArray(alienDIRcounter) = "Go2Right" Then

                For Each AlienArray(alienRightCounter) In AlienArray ' loop all aliens to go right

                    AlienArray(alienRightCounter).Left += AlienSpeed 'Alien goes right
                    AlienArray(alienRightCounter).Image = My.Resources.RedAlienSprite2 ' changes the image when direction is changed to right.
                    alienRightCounter += 1
                Next

            Else

                For Each AlienArray(alienLeftcounter) In AlienArray
                    AlienArray(alienLeftcounter).Left -= AlienSpeed  'Alien goes left
                    AlienArray(alienLeftcounter).Image = My.Resources.RedAlienSprite1 ' changes the image when direction is changed to left.
                    alienLeftcounter += 1
                Next
            End If

        Next AlienDirArrayItem
I thought it would be more elegant, but the loops looks like one big mess. I even spend some time trying to write it out, and the logic goes beyond me atm. I have little experience with loops and arrays and have only started using them in the past week. Give me a recommendation? atm I'm thinking just to leave it as ifs and end ifs. thanks.
Reply With Quote
  #2  
Old 05-26-2011, 07:03 AM
PlausiblyDamp's Avatar
PlausiblyDampIs this too complicated to do? PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

I think you are getting a bit confused between a For Each loop and a For ... Next loop.

At the start you have
Code:
For Each AlienDirArrayItem In AlienDirArray
            If AlienDirArray(alienDIRcounter) = "Go2Right" Then
The line
Code:
For Each AlienDirArrayItem In AlienDirArray
will walk over the AlienDirArray and assign each element to the variable AlienDirArrayItem , every time you hit the Next statement the loop will proceed to the next item in the array.

The following line though
Code:
If AlienDirArray(alienDIRcounter) = "Go2Right" Then
seems to be paying no attention to this AlienDirItem and is just indexing the AlienDirArray array directly using the alienDIRcounter variable which never seems to be updated anywhere.

Given that alienDIRcounter is never seeming to be updated it means all aliens will be treated as if they are going the same direction.

In this case I think you need to pick either a For ... Next loop or a For Each loop rather than trying to combine the two in this way.

One thing that is not making your life easier is the fact you are maintaining different arrays that are effectively describing the same thing - one array is tracking the direction and another is what is being drawn, this is only going to get worse as you need to track more information (which aliens have got an active bullet, hit points, speed (if they can have different speeds) etc.).

You might want to look at grouping these disparate bits of information into a single class and maintain and array / collection of these classes. This could also allow you to draw the images direct to the form rather than relying on moving pictureboxes - pictureboxes weren't really designed for this type of animiation and you will encounter problems as you progress further.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #3  
Old 06-01-2011, 03:20 AM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

Ok ok, after a lot of fiddling I came up with this...bare with me, It's kinda hard to explain, I hope the comments are helpful, bare with me please.

Initialisation at top of form:

Code:
'   
    Public Alien1Dir As String = "Go2Right"
    Public Alien2Dir As String = "Go2Right"
    Public Alien3Dir As String = "Go2Right"

    Public Alien4Dir As String = "Go2Right"
    Public Alien5Dir As String = "Go2Right"
    Public Alien6Dir As String = "Go2Right"

    Public Alien7Dir As String = "Go2Right"
    Public Alien8Dir As String = "Go2Right"
    Public Alien9Dir As String = "Go2Right"

    Public Alien10Dir As String = "Go2Right"

    Public Alien11Dir As String = "Go2Right"

    Public alien1speed As Integer = 40
    Public alien2speed As Integer = 40
    Public alien3speed As Integer = 40
    Public alien4speed As Integer = 40
    Public alien5speed As Integer = 40



    Public Alien1Hit, Alien2Hit, Alien3hit, alien4hit As Boolean

    Public AlienArray(140) As PictureBox ' Stores each alien picture box as an index, for quick access and looping
    Public AlienDirArray(11) As String
    Public AlienLocationArray(140) As System.Drawing.Point
    Public AlienBooleanArray(4) As Boolean
    Public AlienSpeedArray(0 To 4) As Integer


  

    Public stopalienmovementloop As Boolean = False


    Public DirIndex As Integer = 0
    Public AlienIndex As Integer = 0
    Public BooleanIndex As Integer = 0
    Public AlienSpeedIndex As Integer = 0



Main function with two loops utilising four arrays:

Code:
 Sub AlienMovement_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AlienMovement.Tick

        ' Alien movement



        While stopalienmovementloop = False ' keep repeating the inside For Each loop until game is over.

            For Each AlienDirArrayitem In AlienDirArray ' seraches through all boolean variables in alienDirarray

                If AlienDirArray(DirIndex) = "Go2Right" Then ' If the current boolean in index has it's string value set to "Go2Right" Then...
                    AlienArray(AlienIndex).Left += AlienSpeedArray(AlienSpeedIndex) 'Alien goes right (currently crashes here, says, alienspeed is out of array bounds)
                    AlienArray(AlienIndex).Image = My.Resources.RedAlienSprite1 ' changes image when going right

                ElseIf AlienDirArray(DirIndex) = "Go2Left" Then ' If the current boolean in index has it's string value set to "Go2Left" Then...
                    AlienArray(AlienIndex).Left -= AlienSpeedArray(AlienSpeedIndex) 'Alien goes left
                    AlienArray(AlienIndex).Image = My.Resources.RedAlienSprite2 ' changes image when going left
                End If

                'If (current Alien's location is < 20 on the x-axis, AND  current alien hit = False) OR 
                   (current Alien's location is > 850 on the x-axis, AND current alien hit = False) Then... 
                If (AlienArray(AlienIndex).Location.X < 20 And AlienBooleanArray(BooleanIndex) = False) Or _
                   (AlienArray(AlienIndex).Location.X > 850 And AlienBooleanArray(BooleanIndex) = False) Then 
                                                              
                    If AlienArray(AlienIndex).Location.X < 20 Then 'The left boundry stop point for current alien.
                        AlienDirArray(DirIndex) = "Go2Right" ' set the current direction string varaible to "Go2Right" 
                    ElseIf AlienArray(AlienIndex).Location.X > 850 Then ' the right boundary stop point for current alien.
                        AlienDirArray(DirIndex) = "Go2Left" ' set the current direction string varaible to "Go2Left" 
                    End If

                    ' If current alien location is < 370 on the y axis (approaching ground) and current alien hit still = false. Then...
                    '   Get current alien's dimensions, and drop it by it's own height "20"
                    If (AlienArray(AlienIndex).Location.Y < 370 And AlienBooleanArray(DirIndex) = False) Then 
                        AlienArray(AlienIndex).SetBounds(AlienArray(AlienIndex).Location.X, AlienArray(AlienIndex).Location.Y + 20, 20, 20) 
                        AlienSpeedArray(AlienSpeedIndex) += 2
                    End If
                End If

                DirIndex += 1
                AlienIndex += 1
                BooleanIndex += 1
                AlienSpeedIndex += 1

            Next AlienDirArrayitem ' Next index in alienDirArray (gets the next direction string variable for the next alien)

        End While ' end alien movement loop

character limit (10 000) was reached so I took AlienArraysub out.



AlienDirectionArraysub:

Code:
 Sub AlienDirArraySub()

        AlienDirArray(0) = Alien1Dir
        AlienDirArray(1) = Alien2Dir
        AlienDirArray(2) = Alien3Dir
        AlienDirArray(3) = Alien4Dir
        AlienDirArray(4) = Alien5Dir
        AlienDirArray(5) = Alien6Dir
        AlienDirArray(7) = Alien8Dir
        AlienDirArray(9) = Alien10Dir
        AlienDirArray(10) = Alien11Dir

    End Sub
alien boolean Array sub:
Code:
Sub alienboolean_Arraysub()
        AlienBooleanArray(0) = Alien1Hit
        AlienBooleanArray(1) = Alien2Hit
        AlienBooleanArray(2) = Alien3hit
        AlienBooleanArray(3) = alien4hit
    End Sub

Alienspeed array sub:
Code:
Sub AlienspeedArraysub()

        AlienSpeedArray(0) = alien1speed
        AlienSpeedArray(1) = alien2speed
        AlienSpeedArray(2) = alien3speed
        AlienSpeedArray(3) = alien4speed
    End Sub

my form load:

Code:
 Alien1Hit = False
        Alien2Hit = False
        Alien3hit = False
        alien4hit = False

        AlienArraysub()
        AlienDirArraySub()
        alienboolean_Arraysub()
        AlienspeedArraysub()

As you can see, it crashes on alienspeedindex. I do not why. It is not set outside the array bounds. my alienspeedindex = 0 upon start up, and doesn't even get to the bottom to get incremented once. So index = 0 should be a valid index within the array. I'll attach an image to clarify. I know I'm almost there, just some things are not right atm.

Error Image: http://i.imgur.com/QHV5V.png

thanks once again,

45min

Last edited by passel; 06-05-2011 at 03:04 AM.
Reply With Quote
  #4  
Old 06-01-2011, 09:30 AM
DrPunk's Avatar
DrPunkIs this too complicated to do? DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

Maybe it's the AlienIndex of AlienArray that it's moaning about.
__________________
There are no computers in heaven!
Reply With Quote
  #5  
Old 06-01-2011, 09:56 AM
AtmaWeapon's Avatar
AtmaWeaponIs this too complicated to do? AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

I think you need to decide how big your arrays should be. Some are 141 elements large. Some are 5. One's got 12 elements. Since you're getting an "index out of bounds" error, I bet you're confusing yourself and accidentally letting an index variable get too big.

Everything would be a heck of a lot easier if there were an Alien class. Any time you find yourself writing several arrays or variables to keep track of logical groups of items, you should remind yourself that VB .NET has classes and other tools for abstraction.

What's abstraction? It's one of a programmer's most fundamental tools. The process of abstraction is a process of using simple concepts to describe more complicated concepts. Analogies, similes, and metaphors are all abstractions. If you've never seen me and I need to describe my face to you, I could give you a long list of facial features. Or, I could say "I look like a young Harrison Ford" and you'd get the same result for less work (and I'd be lying!)

Code is hard to write when you let complexity creep in. Right now you have to remember that there are three arrays for this, and some of the arrays are shorter so to figure out which index to use you do that, and there's this array over here that manages something else. Wouldn't it be nice if you could say, "Move this alien." rather than, "Get a speed from the speed aray using this calculation, determine direction based on this array, and update the location to represent movement by speed and direction."?

The first thing I'd do is get rid of the different-sized arrays. Maybe you're worried about memory performance? Don't be. Your goal in version 1 is to make it work. You can worry about performance later when it works. Programmers are very bad at predicting where performance is slow; wait until you can measure. Anyway, here's a start:
Code:
Dim _alienSpeed(NumberOfAliens) As Integer
Dim _alienDirection(NumberOfAliens) As AlienDirection
Dim _alienLocation(NumberOfAliens) As Point
What's AlienDirection? It's an enum I made up:
Code:
Public Enum AlienDirection
    Left,
    Right
End Enum
Remembering which string means left and which means right is hard. Some people might observe you could use a Boolean, but then you have to remember if True means left or right. An enum lets you use a symbolic name so there's no confusion. (And booleans won't let you add Up or Down easily...)

Moving an alien is worth writing a function. There's nothing more unreadable than this:
Code:
For i As Integer = 0 To SomeArray.Length - 1
    ' 300 lines of code
Next
A better abstraction is to move the code into a sub or function so you can do this:
Code:
For i As Integer = 0 To NumberOfAliens - 1
    MoveAlien(i)
Next
What's that code look like? Well, we need it to look at the direction and modify the location based on the speed. If you're clever, you'll note that moving left involves subtraction and moving right involves addition; so to move left we add -speed and to move right we just add speed. After moving the alien, we check against threshold values to determine if its direction should change.
Code:
Sub MoveAlien(ByVal index As Integer)
    Dim location As Point = _alienLocation(index)
    Dim speed As Integer = _alienSpeed(index)
    Dim alienDirection As AlienDirection = alienDirection(index)

    If alienDirection = AlienDirection.Left Then
        speed = -speed
    End If

    location = new Point(location.X + speed, location.Y)
    _alienLocation(index) = location

    ' If alien's too far, make it move away 
    If location.X > rightThreshold Then
        alienDirection = AlienDirection.Left
    ElseIf location.X < leftThreshold Then
        alienDirection = AlienDirection.Right
    End If
End Sub
Whew, that's already a lot more simple! How could it get easier? Well, a class would let us abstract more out of the main codebase. We know aliens have a location, speed, direction, and need to be moved. That suggests this:
Code:
Public Class Alien
    Public Property Direction As AlienDirection
    Public Property Location As Point
    Public Property Speed As Integer

    Public Sub Move()
        Dim speed As Integer = Speed
        If Direction = AlienDirection.Left Then
            speed = -speed
        End If

        Dim newLocation As Point = New Point(Location.X + speed, Location.Y)
        Location = newLocation

        If Location.X > rightThreshold Then
            Direction = AlienDirection.Left
        ElseIf Location.X < leftThreshold Then
            Direction = AlienDirection.Right
        End If
    End Sub
End Class
Now instead of many arrays, you only need one:
Code:
Private _aliens(NumberOfAliens - 1) As Alien
When the form loads, you can initialize it:
Code:
For i As Integer = 0 To _aliens.Length - 1
    _aliens(i) = New Alien()
    _aliens(i).Location = ...
    ...
Next
When it's time to move aliens, all you have to do is ask them to move:
Code:
For Each alien In _aliens
    alien.Move()
Next
But you're representing them with picture boxes; how do you make sure the picture boxes match up with the aliens? Well, you could have a picture box array:
Code:
Private _alienImages(NumberOfAliens - 1) As PictureBox
Once that's initialized, your "move all aliens" code could look like this:
Code:
For index As Integer = 0 To _aliens.Length - 1
    _aliens(index).Move()
    _alienImages(index).Location = _aliens(index).Location
Next
But look at that, now we've got an extra array laying about. We have to remember to update the picture box array when alien locations change. That sounds like where we were before. Can you figure out a way to abstract that complexity away?

Don't try to copy/paste the code, there's plenty of blanks to fill in. You might want to experiment with it in a different project and see if it works out for you.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #6  
Old 06-01-2011, 10:44 AM
PlausiblyDamp's Avatar
PlausiblyDampIs this too complicated to do? PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Atma beat me to it but as I knocked a small demo app together I will include it anyway...

It is just another quick example of using a class to hold related information rather than multiple arrays, similar to Atma's suggestion it also moves the movement logic into the class itself.

The class that handles the thing being drawn in this case is just called Sprite and looks like
Code:
Public Class Sprite

    Public Property Location As Point
    Public Property Movement As Point

    Private image As Image
    Private imageSize As Size

    Public Sub New(pic As Image)
        image = pic
        imageSize = image.Size
    End Sub

    Public Sub UpdatePosition()
        Location += Movement
        If Location.X < 0 OrElse (Location.X + imageSize.Width) > World.WorldSize.Width Then
            Movement = New Point(Movement.X * -1, Movement.Y)
        End If

        If Location.Y < 0 OrElse (Location.Y + imageSize.Height) > World.WorldSize.Height Then
            Movement = New Point(Movement.X, Movement.Y * -1)
        End If
    End Sub

    Public Sub Draw(g As Graphics)
        g.DrawImageUnscaled(image, Location)
    End Sub

End Class
A Sprite's position is tracked by it's Location and every time it moves it is moved by the distance stored in the Movement property - in a more fully designed system these wouldn't necessarily be public properties but for now it gets the point across.

The constructor
Code:
 
Public Sub New(pic As Image)
    image = pic
    imageSize = image.Size
End Sub
simply takes an image to display and the Draw method renders the image to whatever graphics object we pass in.

The bulk of the logic is in the UpdatePosition routine which simply changes the Location based on the Movement property and switches direction if we reach an edge.

The form itself just creates a list of these objects
Code:
 Dim things As New List(Of Sprite)
and assigns one of two random images, a random start location and a random movement to each one. A timer on the form simply has the code
Code:
For Each thing In things
    thing.UpdatePosition()
Next

PictureBox1.Invalidate()
which causes each Sprite to update itself and then gets the picturebox which they will be drawn on to request a repaint.

The painting of the picturebox is done by
Code:
Private Sub PictureBox1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

Dim g As Graphics = e.Graphics
g.Clear(Color.Gray)
For Each thing In things
    thing.Draw(g)
Next
End Sub
which simply gets each Sprite to draw itself at it's new Location.
Attached Files
File Type: zip WindowsApplication1.zip (27.8 KB, 11 views)
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #7  
Old 06-02-2011, 06:39 AM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

Hey, thank you for all your help, atma and PlausiblyDamp I tried running your demo however it loads with many errors. :S is it vb.net 2010 version?

I will need time to digest this info. will let you know how I go once done.
Reply With Quote
  #8  
Old 06-02-2011, 07:32 AM
PlausiblyDamp's Avatar
PlausiblyDampIs this too complicated to do? PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

It was built under 2010, you should be able to create a new project under 2008 (possibly even 2005) and just add all the files to this project to get it working though. I didn't use any features specific to 2010.

http://social.msdn.microsoft.com/For...7-a7c598d0f85e might be worth a look as it explains how to convert the project from 2010 to 2008.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags

Last edited by PlausiblyDamp; 06-02-2011 at 12:45 PM.
Reply With Quote
  #9  
Old 06-03-2011, 01:25 AM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

I use vb.net 2010. so it should be working :S I've extracted everything.
Reply With Quote
  #10  
Old 06-03-2011, 04:34 AM
PlausiblyDamp's Avatar
PlausiblyDampIs this too complicated to do? PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

What errors are you getting? I just downloaded the attachment again and it ran just fine.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #11  
Old 06-03-2011, 09:18 AM
AtmaWeapon's Avatar
AtmaWeaponIs this too complicated to do? AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

I just tried it out for confirmation and it works with no problems for me. You should describe the errors because odds are there's something subtle going on.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #12  
Old 06-03-2011, 09:47 AM
TRSDOSBasic79's Avatar
TRSDOSBasic79 TRSDOSBasic79 is offline
Contributor
 
Join Date: Oct 2003
Location: Pennsylvania
Posts: 422
Default

I too downloaded it and I'm a noob to .Net. It works great here. No errors at all.
Reply With Quote
  #13  
Old 06-03-2011, 07:59 PM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

I downloaded it a second time, and it actually worked! hmm it pays to do things more than once.
Reply With Quote
  #14  
Old 06-05-2011, 02:53 AM
passel's Avatar
passelIs this too complicated to do? passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,028
Default

Of course, since this is for Alien Invaders, having the movement and direction part of of the class is redundant and doesn't make sense.
All the aliens move in the same direction and at the same speed.
When any alien hits the edges, they all drop down and reverse direction.
When any alien reaches the ground, the game is over.
The speed of the block of aliens increases as their members are reduced in number.

So, I would think the basic logic would be along the lines.
Code:
Initialize Game  'Loop through the aliens and put them in their starting positions, 
                 'block left justified, player left justified, set speed to initial slow speed and alien movement direction moving to the right.

Sub Game Timer Tic
  For all active user bullets Do
    move bullet
    if it collided with an alien, remove alien and user bullet
    if it collided with an alien bullet, remove alien bullet and user bullet
    if it left the gaming area, remove from active list
  Loop
  For all active alien bullets Do
    move bullet
    if it hit an obstruction, damage or remove the obstruction
    if it hit the player, remove a life and if no more lives, end game
    if it hit the ground, remove it
  Loop

  If there are no active aliens left then
    Total Score for this round.
    Initialize for the next round
  Else
    For all active aliens do
      Move alien in the current direction
      If the current direction is right 
        If the alien has reached the right edge then
          Flag the need to switch directions
        End If
      Else 'we moving left
        If the alien has reached the left edge then
          Flag the need to switch directions
        End If
      End If
    Loop

    If the Switch direction flag is set then
      Change the direction to the opposite
      For all active aliens Do
        Move the alien down a row
        If the alien has reached the ground Then
          Flag game over
        End If
      Loop
    End If

    If game over then
      Do post game processing
    else
      If number of aliens left is less then next speed threshold
        Increase speed to the next increment
        Set the speed change threshold number to the next value to be compared
      End If
      If time and condition met to fire another alien bullet then
        Choose an alien to fire the bullet and add the bullet to the alien active bullet list
      End If  
    End If  'Game over
  End If 'No aliens left
End Sub
Adding a user bullet to the user's active bullet list would be done in the fire event for the user (if valid of course).
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 06-05-2011 at 03:07 AM.
Reply With Quote
  #15  
Old 06-05-2011, 10:02 AM
AtmaWeapon's Avatar
AtmaWeaponIs this too complicated to do? AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

When I build something complicated, I like to start with a simple abstraction and slowly flesh out the desired behavior. Keeping speed and direction on the individual aliens means I can work with one alien and gives me the flexibility to decide if I want Space Invaders or Galaga. Suppose I decide Space Invaders.

It's when I add a second column that it becomes clear I'd like to link the speed of two aliens, but alas, another abstraction helps; what if I make a Row that represents 8 aliens? Then I can move speed and direction to that row. Finding the right-most and left-most alien is now required when determining when to switch directions, but that's easy enough and the gameworld might be able to update it.

It's when the second row is added that an interesting decision comes to mind: should I clone the game or add a twist where rows alternate direction? If I want a clone I can certainly simplify to one speed and direction, but it might be fun to switch stuff up!

Going the other way seems harder for a newbie to pick up IMO. If you start with an 8x8 playfield and one speed/direction, "find the rightmost alien per row" seems like a monumental task. A 2D array can help, but doesn't get you all the way there. Because you start with 64 aliens, you try to solve the problem in terms of 64 aliens, and that's harder than solving it in terms of 8 rows. Maybe that's just how I think though.

Anyway, I'm not discrediting your answer, just defending my stance.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #16  
Old 06-06-2011, 03:08 PM
passel's Avatar
passelIs this too complicated to do? passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,028
Default

I don't have a problem with using classes and for thinking ahead to having a more comprehensive and flexible capability that can be expanded on, but that can be a complication for someone new to the wide range of possibilities we have at our disposal.

On the other hand, I sometimes like to define the requirements and then implement those requirements with as little code as necessary, as long as it is simple and clear.
As you say finding the rightmost alien per row in an 8x8 playfield, especially if you use a two dimensional matrix, can be daunting. My position is keep it simple. I don’t care which alien goes over the left or right border first.

The requirements of the game are that all aliens move in lockstep, in the same direction. That requirement simplifies our code since we have to loop and move all the aliens in only one direction we can check to see if any one (or more) of them hit the edge during the move. Logically the rightmost alien(s) will be the first to go off the right side, so we need to switch direction, but which particular alien(s) it was that hit the edge doesn’t matter.

Since all aliens move the same amount in the same direction, the array or list we use to track them does not need to be structured by row and/or column. A simple one dimensional array will suffice. The only chore is to initially place the aliens in the right starting position. After that, the movement of all the aliens is relative to their current position, so again, very simple code, just adding the same motion to all active aliens.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #17  
Old 06-07-2011, 09:20 AM
AtmaWeapon's Avatar
AtmaWeaponIs this too complicated to do? AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Your approach is effective and in many ways conceptually easier to understand. I like to use abstraction because it's a tool that took me years to master and I don't want other people to struggle as I did. Generally, people use simple games as tutors and it works out they're the worst possible programming tutors since they tend to penalize design choices that are important in real-world application development. Games focus on performance and simplicity rather than flexibility and complexity. That's a whole different world. I suppose I shouldn't make the assumption that everyone wants to be an app developer though.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #18  
Old 06-09-2011, 01:33 AM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

I wanted to give space invaders a spin, and make each alien have it's individual speed and direction.
Reply With Quote
  #19  
Old 06-09-2011, 01:52 AM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

passel any chance you could repost that app with comments on every line? I can't believe so little code does so much. With comments I might be able to follow it. I'm also still tring to get my head around classes. Thanks in advance.
Reply With Quote
  #20  
Old 06-09-2011, 10:49 AM
passel's Avatar
passelIs this too complicated to do? passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,028
Default

I'm not sure what app you want me to repost.
I didn't post any in this thread. I only outlined, pseudo code if you want to call it that, the basic logic for an approach to start implementing the classic space invaders game. I didn't go into details about how you would do collision detection, or how you maintain the "list" of active aliens, bullets, etc. (i.e. how you track (add/remove) items in the lists, or how you would do the graphics, control speed, etc.
I haven't written a version of Space Invaders myself, and technically, I'm much more fluent in VB6 than I am in VB.Net.

If you're referring to the "code" in post 14 above, then essentially those are the "comments". I might insert ' in front of all the lines to make them comments, and then insert real code to implement the comments.

I don't believe I can justify the time to try to implement some of the above as an entertaining exercise, but on the other hand I should try to become more comfortable working in VB.Net, as that has potential for being supportable at my job, whereas VB6 is not.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Is this too complicated to do?
Is this too complicated to do?
Is this too complicated to do? Is this too complicated to do?
Is this too complicated to do?
Is this too complicated to do?
Is this too complicated to do? Is this too complicated to do? Is this too complicated to do? Is this too complicated to do? Is this too complicated to do? Is this too complicated to do? Is this too complicated to do?
Is this too complicated to do?
Is this too complicated to do?
 
Is this too complicated to do?
Is this too complicated to do?
 
-->