• Log in or sign up for an account here: https://www.sythe.org/login

Arrow key (control)

X Zero

Forum Addict
Joined
Jan 21, 2007
Sythe Gold
0
Messages
577
Location
Australia, The South
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?
 
what you mean with stop?
 
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.
 
Two Factor Authentication User
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.
 
duelarena.gg
Back
Top