Ry12

Hi
I just began creating a space invaders game and im having difficulties programming motion for the aliens.
Some of the aliens move back and forth like they're supposed to, but some get stuck at the turning point. Can someone please look at this program to see if they can fix this problem
Thanks

http://www.users.on.net/~rohanlal/spaceinvaders5.zip


Re: Visual Basic General space invaders

Derek Smyth

Hi Ry,

Wanted to say I had a look at your code but couldn't run it because I don't have VB installed. Tried to read it through but it's a little difficult to picture whats going on even though your comments are excellent. Without VB there isn't much help I can provided but I wanted to post to let you know about object orientated programming and how it helps in writing games.

Object orientated program isn't really supported in VB (not really properly) but you can still create classes, which are like templates for creating and using things in an application. Generally what would make your program better would be to have classes for different interacting components of your game.

Give you an example....

An alien class that all information and all the methods relating to an alien are held, so an alien could have X and Y coordinate, an image, a speed, a destroyed flag, a fire() sub, or a destroy() sub, and so on. You would use this class as a template for creating little alien objects in your program.

Another example of a class might be an alien invasion class and this would contain all the information needed to control a group of aliens as a complete unit, so you could have a number of aliens in the invasion as well as a moveLeft(), moveRight(), moveDown() method which moves all the aliens in the invasion as a complete unit.

At the moment all your logic is mixed together in a number of large methods. If you used classes you could simplify it a lot. Now I'm maybe talking a re-write which was not why you posted here, but if you create another game then it's something to think about.

The class module in VB lets you create a class. Even do a search on google if you want more info, or ask in the forums, or reply and I'll give you more information.

Hope you get the problem sorted.






Re: Visual Basic General space invaders

Derek Smyth

Hi,

Thought I'd create a better example.... so here is an alien class, this isn't VB but VB.NET instead so this won't copy directly over but this is just to show you the concept.

Public Class Alien

'data about an alien

Public X As Integer 'its x position

Public Y As Integer 'its y position

Public IsDestroyed As Boolean 'it has been destroyed

Public Image As Bitmap 'its image

'things an alien does

Public Sub Shoots()

End Sub

Public Sub GetsDestroyed()

Me.IsDestroyed = True

End Sub

End Class

You see there is information about an alien and there are some things an alien does. For example it shoots, it gets destroyed, and so on. The data about the alien is it's X, Y postion.

You could say that 10 aliens might make up a row of an invasion so that could also be a class.

Public Class RowOfAliens

'an row contains 10 aliens, you can see it build an array that uses the alien class

Public Aliens(10) As Alien

Public Sub CreateRow()

'adds 10 aliens to the row (set the row up, Alien(0) is left most alien, Alien(10) is right most alien)

Aliens(0) = New Alien() 'create an alien based on the template

Aliens(1) = New Alien() '

Aliens(2) = New Alien() '

Aliens(3) = New Alien() '

Aliens(4) = New Alien() '

'and so on

End Sub

Public Sub MoveRowDown()

For Each alien As Alien In Aliens

alien.X = alien.X - 10 'move all the aliens in the row down by changing its positon

Next

Aliens(3).Shoots() 'alien 3 shoots....

End Sub

Public Sub MoveRowRight()

End Sub

Public Sub MoveRowLeft()

End Sub

End Class

Then you can imagine that an invasion is made up of 5 rows so we can say that is a class.

Public Class AlienInvasion

'an alien invasion contains 5 rows of aliens

Public AlienRows(5) As RowOfAliens

Public Sub CreateInvasion()

'adds 5 rows of aliens to the invasion

AlienRows(0) = New RowOfAliens() '

AlienRows(0).CreateRow() 'set the row up

AlienRows(1) = New RowOfAliens() '

AlienRows(1).CreateRow() 'set the row up

AlienRows(2) = New RowOfAliens() '

AlienRows(2).CreateRow() 'set the row up

AlienRows(3) = New RowOfAliens() '

AlienRows(3).CreateRow() 'set the row up

'and so on

End Sub

Public Sub MoveInvasionDown()

For Each row As RowOfAliens In AlienRows

row.MoveRowDown()

Next

AlienRows(1).Aliens(2).GetsDestroyed()

' alien 2 on row 1 gets destroyed

End Sub

Public Sub MoveInvasionLeft()

For Each row As RowOfAliens In AlienRows

row.MoveRowLeft()

Next

End Sub

End Class

You see how all the bits all build up to make a big picture and that there is less code and it's a bit more readable. You can see that an alien shoots or gets destroyed.

Now the last object will the Game object which will be made up of an alien invasion and a space ship and a couple of timers to control the whole process. Ok this is very simple example but hopefully I've shown up a different approach that lends itself quite well to programming games.






Re: Visual Basic General space invaders

Ry12


Thanks for the help but my code doesn't need to be perfect as this is for my first programming class. Also, we don't have any option about what language we use so I have to stick with basic. Derek, if you look at my code you'll see that I've got the form and aliens setup, my next step is to get motion for the aliens working properly.




Re: Visual Basic General space invaders

Derek Smyth

Hi Ry,

Yeah I looked at your code but, without being able to run it, I found it very difficult to see what was going on. I understand you don't want to re-write your application as you have put a lot of work into it, so the approach I detailed above was for your next game. The one that will make you millions.

I'm really sorry I can't help more (don't have a copy of VB anymore) and perhaps I shouldn't of answered this post but I thought it was worth showing you another way to approach your games development in future. Best of luck.




Re: Visual Basic General space invaders

Anthony D. Green

Disclaimer: This response isn't guaranteed to be helpful.

Ry,

I know you probably didn't expect to need to do this but you may want to strongly recommend that your teacher dump the old VB6 and download free copies of Microsoft Visual Basic 2005 Express and start teaching current and marketable job skills.

It seems like a lot to ask but you're taking a programming class, and I would hope that means you actually want to learn programming. Some would say that you can learn that and the fundamentals in any language but that doesn't mean you should waste your time learning fortran and turbo pascal. Not because these languages aren't or weren't useful but because you'll be learning in a restricted environment how to solve old problems that modern languages have already solved.That's time better spent learning how to solve the problems of today with the best practices of today.

I'm not suggesting that you stage any protests, only make an honest effort to advance the quality of your own education. If you or your teacher would like to discuss this further I'd be happy to. I used to work with the Chicago Public Schools and have had similar discussions many times before.