Question Help with coding simple game!

samcp16

Member
Joined
Jan 27, 2012
Messages
8
Programming Experience
1-3
Hello, im creating a simple space invaders type game and I made it so that once a picture box (that being the bullet) has gone from the top of the screen, the bullet can be fired again using the space key, here is the code I currently have for the bullet.

pbBullet.Top = pbBullet.Top - 2 ' move toward 0 i.e. upwards
' when bullet goes off top of screen, set flag to false
If pbBullet.Top > 0 Then
bulletLive = True
Else
bulletLive = False
tmrBullet.Enabled = False ' prevent auto-firing
pbBullet.Hide()

Though I was curious to know what code I would use so that another bullet can be fired before the first one has left the screen and to only fire once the space bar has been pressed, keep in mind I have only been programming for a short while, so simple terminology would be greatly appreciated.

Thanks :)
 
help is here

If you wanted to use this object in other games, I'd make a class of it. But for it to be worth it, you'll almost need to learn how to manipulate GDI+. if you wanted two bullets to be able to appear with pictureboxes, make another picturebox with the exact same parameters as this one. Label it pbBullet2, and copy all the code applying to pbBullet to pbBullet2 and make sure to change the name. This way, you can fire the first one once, and again when it exits the screen. But you can do the same with pbBullet2, and effectively have 2 identical shots on the screen at the same time.

Now, to get them to show up only when the space bar has been pressed. To do this, you need a keypress event on your form. so double click your form. Change the load event button (near the top-right of code window) and change it to keypress. Then, declare a variable in the form's code outside of the sub. It needs to be a boolean. Name it SpacePressed. now inside the sub's code, you need to write this:{ If e.equals(keys.space) then 'insert detecting code here' EndIf. } without the curly braces, and where . you need to detect if BulletLive is true for both Bullets now. If Bulletlive is false for pbbullet, set it to true and start your timer for it. If it is already true, set the next one's parameters. if both are true, then pressing space does nothing. You can make these detections with simple if-elseif-else loops.

 
If you wanted to use this object in other games, I'd make a class of it. But for it to be worth it, you'll almost need to learn how to manipulate GDI+. if you wanted two bullets to be able to appear with pictureboxes, make another picturebox with the exact same parameters as this one. Label it pbBullet2, and copy all the code applying to pbBullet to pbBullet2 and make sure to change the name. This way, you can fire the first one once, and again when it exits the screen. But you can do the same with pbBullet2, and effectively have 2 identical shots on the screen at the same time.

Now, to get them to show up only when the space bar has been pressed. To do this, you need a keypress event on your form. so double click your form. Change the load event button (near the top-right of code window) and change it to keypress. Then, declare a variable in the form's code outside of the sub. It needs to be a boolean. Name it SpacePressed. now inside the sub's code, you need to write this:{ If e.equals(keys.space) then 'insert detecting code here' EndIf. } without the curly braces, and where . you need to detect if BulletLive is true for both Bullets now. If Bulletlive is false for pbbullet, set it to true and start your timer for it. If it is already true, set the next one's parameters. if both are true, then pressing space does nothing. You can make these detections with simple if-elseif-else loops.


thanks!, i got it to work :)
 
Back
Top