Arrow key (control)

Discussion in 'Programming General' started by X Zero, Jul 24, 2007.

Arrow key (control)
  1. Unread #1 - Jul 24, 2007 at 2:54 AM
  2. X Zero
    Joined:
    Jan 21, 2007
    Posts:
    577
    Referrals:
    0
    Sythe Gold:
    0

    X Zero Forum Addict

    Arrow key (control)

    I was thinking of making a game and the only thing I am having trouble with controls.

    Edit: New Question: How can i make picturebox1 stop before it goes out of the form?
     
  3. Unread #2 - Jul 30, 2007 at 6:24 PM
  4. cooladrrang
    Joined:
    Jan 21, 2007
    Posts:
    125
    Referrals:
    0
    Sythe Gold:
    0

    cooladrrang Active Member

    Arrow key (control)

    what you mean with stop?
     
  5. Unread #3 - Jul 30, 2007 at 9:42 PM
  6. X Zero
    Joined:
    Jan 21, 2007
    Posts:
    577
    Referrals:
    0
    Sythe Gold:
    0

    X Zero Forum Addict

    Arrow key (control)

    like so it can't move in that direction anymore
     
  7. Unread #4 - Aug 1, 2007 at 5:01 AM
  8. Michael3455
    Joined:
    Sep 11, 2005
    Posts:
    134
    Referrals:
    0
    Sythe Gold:
    0

    Michael3455 Active Member

    Arrow key (control)

    if onkeydown(sidecurser) = true and picturebox.locationofside = sideofform
    msgbox("Sorry, no no movy there. :p")
    else picturebox.top -20 'Or whatever it is to move picture boxes. I've not 'done anything with them in a looong time
    end if


    Obviously, that's not the right code :)P), but I'm pretty sure that's the right sort of logic used to stop an image from going of screen. So, basically, when the user presses a key to move, it checks where the image is as well. If the image is touching the side of the form (or a predefined area) it can't move. Else, it moves just like normal.

    Of course, I could be wrong. The only things I've done like this is a simple shooting game with an alien spaceship moving up and down, and that was awhile ago. but I'm pretty sure that's how I did it. It was either that or with collision detection. I think it might have been the latter.
     
  9. Unread #5 - Aug 1, 2007 at 6:19 AM
  10. Flaming Idiots
    Joined:
    Dec 22, 2005
    Posts:
    235
    Referrals:
    1
    Sythe Gold:
    0
    Two Factor Authentication User

    Flaming Idiots Active Member
    Visual Basic Programmers

    Arrow key (control)

    You want to use something like this:

    Code:
        Public Shared Sub MoveControl(ByVal C As Control, ByVal X As Integer, ByVal Y As Integer)
            If C.Parent Is Nothing Then Throw New Exception("It has no parent.")
            Dim NewBounds As New Rectangle(C.Left + X, C.Top + Y, C.Width, C.Height)
            If C.Parent.ClientRectangle.Contains(NewBounds) Then C.Bounds = NewBounds
        End Sub
    
        Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            Select Case e.KeyCode
                Case Keys.Up : MoveControl(Me.PictureBox1, 0, -5)
                Case Keys.Down : MoveControl(Me.PictureBox1, 0, 5)
                Case Keys.Left : MoveControl(Me.PictureBox1, -5, 0)
                Case Keys.Right : MoveControl(Me.PictureBox1, 5, 0)
            End Select
        End Sub
    It is also best that you don't use controls for sprites and draw the sprites directly to the form. It will run a lot faster.
     
< I'm Back | JAVA - starting out >

Users viewing this thread
1 guest


 
 
Adblock breaks this site