using arrow keys

Status
Not open for further replies.

wannabe_hacker

In Runtime
Messages
155
I am making a 'space invaders' look a like. i need to move the little ship using the arrow keys and i dont know how. if there is no way than can someone direct me as to how to move the ship left or right with some other keys. thanks
 
if you have made the space invaders game in visual basic then to use the arrow keys as far as i know then u would just need to find out when the user presses the relevant arrow key. To do that you would use the following to find out which key was pressed:

on the control you are using on the key press event or something similiar there should be a keyascii event.

If you make a Textbox1.Text =keyascii

and then press one arrow key at a time it will tell you the keyascii that you have to look out for, so if they press the left arrow key then you have to do a select case to detect which key ascii was pressed and then do what ever you want to do once u have detected which key was pressed. I hope that is what you were looking for.

If not let me know exactly what you want!
 
I have no source code as of now. I just want to know how to move the ship from left to right. I want to put that code in first so I can move the ship, if I can't move the ship then there is really no point of the game.
 
I dont really use VB anymore but this might help..
Code:
Option Explicit
Private Const VK_LEFT = &H25
Private Const VK_RIGHT = &H27
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer

Private Sub Form_Load()
    If GetKeyState(VK_LEFT) < 0 Or GetKeyState(VK_RIGHT) < 0 Then
       do something
    Else
       End Sub
    End If
End Sub
 
here I can do an example now cos I am not sure of the keys ascii code. Then I will post it here, I am assuming you will use a picturebox or an image for the ship so you can load an image of the ship to use as the ship , right ?
 
that function that sippine codine or w.e his id is posted looks good but does that only work with the left and right arrow keys ? If so, what about up and down ?
 
Copy and paste the code below in to your visual basic project. Then make a label called lblkeys on your form and run it :p

I was wrong earlier on because I didnt know that the keyascii function variable didnt get or capture the arrow keys !!

=============================================

'This vector contains the state of all keys
'I think KeyCode goes from 0 to 255, but you
'can change this if you want
Dim Keys(255) As Boolean

'Its used to stop the Do-Loop of the Form_Load
Dim StopLoop As Boolean

Private Sub Form_KeyDown(KeyCode As Integer, Shift As _
Integer)
'The key KeyCode is pressed now...
Keys(KeyCode) = True
End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
'The key KeyCode is NOT pressed now...
Keys(KeyCode) = False
End Sub

Private Sub Form_Load()
Dim str As String

Me.Show

'The form must handle the key events
Me.KeyPreview = True

StopLoop = False

Do
'Just check for the arrow keys to see
'if they are pressed. To check form more
'keys, just ask for the correct subindex
'in the "Keys" array
str = ""

If Keys(vbKeyUp) Then str = str + "UP" & vbCrLf
If Keys(vbKeyDown) Then str = str + "DOWN" & vbCrLf
If Keys(vbKeyLeft) Then str = str + "LEFT" & vbCrLf
If Keys(vbKeyRight) Then str = str + "RIGHT" & vbCrLf

'Refresh the label
lblkeys.Caption = str

'Important!! (to not freeze the thing)
DoEvents
Loop Until StopLoop
End Sub

Private Sub Form_Unload(Cancel As Integer)
'Important!! To stop the loop, so the
'form can be unloaded
StopLoop = True
End Sub
 
once you insert that code you just insert a picturebox or an image and insert this code.

Private Sub Picture1_KeyDown(KeyCode As Integer, Shift As Integer)
If Keys(vbKeyUp) Then Picture1.Top = Picture1.Top - 100
If Keys(vbKeyDown) Then Picture1.Top = Picture1.Top + 100
If Keys(vbKeyLeft) Then Picture1.Left = Picture1.Left - 100
If Keys(vbKeyRight) Then Picture1.Left = Picture1.Left + 100
End Sub

if you insert an image instead of a picturebox then obviously you will need to alter the code so it is image1.left or image1.top, etc.

Obviously you will need to change the coding if you change the name of the control.
 
Status
Not open for further replies.
Back
Top Bottom