Getting bullets to manually shoot by pressing a spacebar.
Getting bullets to manually shoot by pressing a spacebar.
Getting bullets to manually shoot by pressing a spacebar.
Getting bullets to manually shoot by pressing a spacebar.
Getting bullets to manually shoot by pressing a spacebar.
Getting bullets to manually shoot by pressing a spacebar. Getting bullets to manually shoot by pressing a spacebar. Getting bullets to manually shoot by pressing a spacebar. Getting bullets to manually shoot by pressing a spacebar. Getting bullets to manually shoot by pressing a spacebar. Getting bullets to manually shoot by pressing a spacebar. Getting bullets to manually shoot by pressing a spacebar. Getting bullets to manually shoot by pressing a spacebar.
Getting bullets to manually shoot by pressing a spacebar. Getting bullets to manually shoot by pressing a spacebar.
Getting bullets to manually shoot by pressing a spacebar.
Go Back  Xtreme Visual Basic Talk > > > Getting bullets to manually shoot by pressing a spacebar.


Reply
 
Thread Tools Display Modes
  #1  
Old 05-26-2011, 02:03 AM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Question Getting bullets to manually shoot by pressing a spacebar.


Hi there,

up to now I've have been running my bullets on automatic fire, through 4 separate timers.

My Next major task is to make my space bar shoot bullets. I have tried attempting to code the spacebar, for the first bullet. I was thinking that when the spacebar is pressed it turns on a timer, thus the bullet is fired. Then when the bullet hits the top of the screen or an alien the timer is turned off and bullet location is reset. Now the bullet1 no longer fires. I find the logic complicated but it should work.

my variables:
Code:
 Public BulletMovement As System.Drawing.Point

    Public bullet_case




my keydown sub:

Code:
    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

        Select Case e.KeyCode

            Case Keys.Left
                temp.X = temp.X - 10 ' spaceship movement
            Case Keys.Right
                temp.X = temp.X + 10
            Case Keys.Space

                Select Case bullet_case

                    Case score >= 0
                        Shoot1.Enabled = True


                End Select
        End Select

        If score = 0 Then
            Bullet1.Visible = True
        End If

        If score = 40 Then
            Shoot3.Enabled = True
        End If

        If score = 60 Then
            Shoot4.Enabled = True
        End If

    End Sub

Note: I'm only working on bullet1 now.


Code:
    Private Sub Shoot1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Shoot1.Tick


        If Bullet1.Location.Y > 0 Then
            Bullet1.Show()
            Bullet1.Location = New Point(Bullet1.Location.X, Bullet1.Location.Y - 30)

        End If


    End Sub

    Sub shoot1_stop()

        If Bullet1.Location.Y < 2 Or Bullet1.Bounds.IntersectsWith(Alien1.Bounds) Then
            Bullet1.Location = temp
            Bullet1.Visible = False
            Shoot1.Enabled = False

        End If



    End Sub
    Private Sub Shoot2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Shoot2.Tick
        If Bullet2.Location.Y > 0 Then
            Bullet2.Show()
            Bullet2.Location = New Point(Bullet2.Location.X, Bullet2.Location.Y - 30)

            If Bullet2.Location.Y < 2 Or Bullet2.Bounds.IntersectsWith(Alien1.Bounds) Then
                Bullet2.Location = temp

            End If
        End If
    End Sub
    Private Sub Shoot3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Shoot3.Tick
        If Bullet3.Location.Y > 0 Then
            Bullet3.Show()
            Bullet3.Location = New Point(Bullet3.Location.X, Bullet3.Location.Y - 30)

            If Bullet3.Location.Y < 2 Or Bullet3.Bounds.IntersectsWith(Alien1.Bounds) Then
                Bullet3.Location = temp

            End If
        End If
    End Sub
    Private Sub Shoot4_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Shoot4.Tick
        If Bullet4.Location.Y > 0 Then
            Bullet4.Show()
            Bullet4.Location = New Point(Bullet4.Location.X, Bullet4.Location.Y - 30)

            If Bullet4.Location.Y < 2 Or Bullet4.Bounds.IntersectsWith(Alien1.Bounds) Then
                Bullet4.Location = temp

            End If
        End If
    End Sub
in form load I have this:
Code:
shoot1_stop()
thanks would appreciate any help.
Reply With Quote
  #2  
Old 05-26-2011, 06:40 AM
PlausiblyDamp's Avatar
PlausiblyDampGetting bullets to manually shoot by pressing a spacebar. PlausiblyDamp is offline
Ultimate Contributor

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

Why are you calling shoot1_stop() from the form load? Surely when the form is loading there are no bullets to actually stop anyway.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #3  
Old 05-27-2011, 04:33 AM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

Quote:
Originally Posted by PlausiblyDamp View Post
Why are you calling shoot1_stop() from the form load? Surely when the form is loading there are no bullets to actually stop anyway.
to make sure shoot1 is not running.
Reply With Quote
  #4  
Old 05-29-2011, 01:43 PM
passel's Avatar
passelGetting bullets to manually shoot by pressing a spacebar. passel is offline
Sinecure Expert

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

The only place I see that you test for removing the bullet is in the shoot1_stop sub.
You need to do that test periodically while the bullet is active to see if it should be inactivated.
Since you have the timer for periodic update of the bullet position while it is active, that may be a good place to see if the bullet should be removed as well.
Given the code you have, it seems like just calling the shoot1_stop within your timer would do, since the routine doesn't actually stop the bullet unless a stop condition is met.

Code:
'
    Private Sub Shoot1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Shoot1.Tick


        If Bullet1.Location.Y > 0 Then
            Bullet1.Show()
            Bullet1.Location = New Point(Bullet1.Location.X, Bullet1.Location.Y - 30)

        End If

        shoot1_stop()  'Test to see if bullet has left the screen or hit an alien.

    End Sub
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #5  
Old 05-31-2011, 07:58 AM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

Ok, well After a few days I have refined my code a bit. I cannot figure out a way to make a picture box continuously move up in a form with one spacebar press. I have tried the following code but it does not work.

Code:
     Case Keys.Space
                Bullet1.Visible = True
                BulletMovement.Y = BulletMovement.Y - 10
                Bullet1.Location = BulletMovement
        End Select
When I press the spacebar using this code, the bullet disappears completely. BulletMovement is declared as a system.drawing.point


Code:
     Case Keys.Space
                Bullet1.Visible = True
  
          Bullet1.Location = New Point(Bullet1.Location.X, Bullet1.Location.Y - 30)
        End Select
The bullet moves up by 30 EVERY press though. I want one press and it continuously moves up initiated by the one press.

thanks,

45min
Reply With Quote
  #6  
Old 05-31-2011, 09:10 AM
AtmaWeapon's Avatar
AtmaWeaponGetting bullets to manually shoot by pressing a spacebar. AtmaWeapon is offline
Fabulous Florist

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

For simplicity, let's consider only one bullet; supporting multiple is easy once you have this down.

Auto-fire and shoot on command are closely related. They both follow this pattern:
Code:
<At some time:>
    * If the bullet can be fired:
        * Fire the bullet
For auto-fire, you can be economical and combine the bullet movement logic with the "should a bullet fire?" logic:
Code:
<When the auto-fire timer ticks:>
    * Move the bullet up.
    * If the bullet is out of bounds:
        * Hide it.
    * If the bullet can be fired:
        * Fire the bullet
In this case, "if the bullet can be fired" is a check for whether the picture box is visible or out of bounds. "Fire the bullet" means placing the bullet at the lowest position and making the picture box visible. This works for autofire because each tick you move the bullet, then determine if it should be re-fired.

For manual fire, you cannot combine these two concepts. You can only try to shoot the bullet when the user pushes the button, but you want to animate the bullet whether a button is pressed or not. This is what it'd look like:
Code:
<When the user pushes the button:>
    * If the bullet can be fired:
        * Fire the bullet.

<Each timer tick:>
    * Move the bullet up.
    * If the bullet is out of bounds:
        * Hide it.
The bullet starts not visible. When the user pushes the button, the program notes the bullet can be fired and fires it. Meanwhile, the animation timer keeps moving the bullet upwards. If the user pushes the button while the bullet is visible, it notes the bullet cannot be fired again and ignores the keypress. Eventually, the bullet goes out of bounds and the timer code notices it should be hidden. The next time the user pushes the button the bullet can be fired again.

Separate the logic that shoots the bullet from the logic that moves the bullet. That makes it easier to understand where each piece should go. If you're worried about performance, you can combine the code later once you get it working properly. For the first try it's always best to do what's easiest, then move to code that fits the performance requirements better.
__________________
.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
  #7  
Old 06-18-2011, 06:05 AM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

Quote:
Originally Posted by AtmaWeapon View Post
For simplicity, let's consider only one bullet; supporting multiple is easy once you have this down.

Auto-fire and shoot on command are closely related. They both follow this pattern:
Code:
<At some time:>
    * If the bullet can be fired:
        * Fire the bullet
For auto-fire, you can be economical and combine the bullet movement logic with the "should a bullet fire?" logic:
Code:
<When the auto-fire timer ticks:>
    * Move the bullet up.
    * If the bullet is out of bounds:
        * Hide it.
    * If the bullet can be fired:
        * Fire the bullet
In this case, "if the bullet can be fired" is a check for whether the picture box is visible or out of bounds. "Fire the bullet" means placing the bullet at the lowest position and making the picture box visible. This works for autofire because each tick you move the bullet, then determine if it should be re-fired.

For manual fire, you cannot combine these two concepts. You can only try to shoot the bullet when the user pushes the button, but you want to animate the bullet whether a button is pressed or not. This is what it'd look like:
Code:
<When the user pushes the button:>
    * If the bullet can be fired:
        * Fire the bullet.

<Each timer tick:>
    * Move the bullet up.
    * If the bullet is out of bounds:
        * Hide it.
The bullet starts not visible. When the user pushes the button, the program notes the bullet can be fired and fires it. Meanwhile, the animation timer keeps moving the bullet upwards. If the user pushes the button while the bullet is visible, it notes the bullet cannot be fired again and ignores the keypress. Eventually, the bullet goes out of bounds and the timer code notices it should be hidden. The next time the user pushes the button the bullet can be fired again.

Separate the logic that shoots the bullet from the logic that moves the bullet. That makes it easier to understand where each piece should go. If you're worried about performance, you can combine the code later once you get it working properly. For the first try it's always best to do what's easiest, then move to code that fits the performance requirements better.
umm how do you make vb.net ignore a key (spacebar) on keydown event?


here's is some code, it partially works, but not fully. If i hold down my space bar I can see the bullet going up to the top, then repeating itself. But I need the space bar to press once, bullet flies to the end, hides, then resets to the location where it began, to repeat this process over and over again.

code:

Code:
  Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        Dim keeploop As Boolean = True

        Select Case e.KeyCode

            Case Keys.Left
                temp.X = temp.X - 10 ' spaceship movement
            Case Keys.Right
                temp.X = temp.X + 10

            Case Keys.Space

                BulletcanFire()
                BulletcannotFire()

        End Select

        ' all this above needs work
    End Sub
    Sub BulletcanFire()

        If Bullet1.Location.Y < 399 Then
            Bullet1.Visible = True
            Bullet1canshoot = True
        End If

    End Sub

    Sub BulletcannotFire()
        If (Bullet1.Location.Y < 2 Or Bullet1.Bounds.IntersectsWith(Alien1.Bounds)) Then
            Bullet1.Location = temp
            Bullet1.Visible = False
        End If

    End Sub

    Private Sub AnimateShoot_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AnimateShoot.Tick
        Bullet1.Location = New Point(Bullet1.Location.X, Bullet1.Location.Y - 30)
    End Sub
I do not know how to ignore a key press if a certain condition is taking place. Need advice on this.

Last edited by 45minutes; 06-18-2011 at 06:39 AM.
Reply With Quote
  #8  
Old 06-19-2011, 12:03 AM
passel's Avatar
passelGetting bullets to manually shoot by pressing a spacebar. passel is offline
Sinecure Expert

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

You really shouldn't be doing movement code within the Key Event routines.
You should have a single timer where all your periodic game logic is performed, such as ship movments, bullet movements, collision detection, etc.
The Key events should just flag the state of your controls, which your periodic event will test and react to.
If you press the left key down, set a "left key down" flag so your periodic process will move the ship left.
If the left key comes up, clear the "left key down" flag so your periodic process will stop moving the ship left.
If you press the space bar, and it wasn't currently down then latch a variable showing that the space bar was pressed down and enable a bullet, if valid. Don't process other space keyDowns (i.e. ignore auto repetition of the space key), until you've had a space keyUp event.
__________________
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
Getting bullets to manually shoot by pressing a spacebar.
Getting bullets to manually shoot by pressing a spacebar.
Getting bullets to manually shoot by pressing a spacebar. Getting bullets to manually shoot by pressing a spacebar.
Getting bullets to manually shoot by pressing a spacebar.
Getting bullets to manually shoot by pressing a spacebar.
Getting bullets to manually shoot by pressing a spacebar. Getting bullets to manually shoot by pressing a spacebar. Getting bullets to manually shoot by pressing a spacebar. Getting bullets to manually shoot by pressing a spacebar. Getting bullets to manually shoot by pressing a spacebar. Getting bullets to manually shoot by pressing a spacebar. Getting bullets to manually shoot by pressing a spacebar.
Getting bullets to manually shoot by pressing a spacebar.
Getting bullets to manually shoot by pressing a spacebar.
 
Getting bullets to manually shoot by pressing a spacebar.
Getting bullets to manually shoot by pressing a spacebar.
 
-->