Page 1 of 2 12 LastLast
Results 1 to 40 of 62

Thread: moving with keys

  1. #1

    Thread Starter
    Lively Member SmashX's Avatar
    Join Date
    Nov 2002
    Location
    The Space between dreams and reality
    Posts
    86

    moving with keys

    ok i did some wokin and iv come alon quite nicely ((right...))
    anyhoo im making a new game and i have some questions.

    i have an image in a picture box, and i would like for it to do the following:

    when i press the up arrow on the keyboard the img would go up until it reaches the top of the picture box.

    when i press the down arrow i would like the img to go down until it reaches the bottom of the picture box.

    ____

    im not sure how to assign differnt keys, such as the up arrow and down arrow, i know it has somthing to do with the keydown and keyup code area but im lost after that....

  2. #2
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    Keys

    I don't know much about pictures within pictures... but for the keys:

    VB Code:
    1. Dim Keys(0 to 255)
    2. Form1_Keydown (......)
    3. 'Print Keycode
    4. Keys(KeyCode) = True
    5. end sub
    6.  
    7. Form1_KeyUp (......)
    8. Keys(KeyCode) = False
    9. end sub

    get rid of ' before print keycode to get the codes for arrow keys (I think it's somehere around 40...)
    Don't pay attention to this signature, it's contradictory.

  3. #3
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Look at this thread...it may be come inn handy...

    http://vbforums.com/showthread.php?s=&threadid=219053

  4. #4
    Hyperactive Member dogfish227's Avatar
    Join Date
    Oct 2002
    Location
    GA
    Posts
    409
    the arrow keys are:
    37 up
    38 right
    39 down
    40 left

    as far as moveing the image in the picbox the easyiest way would probaly be to use the bitblt api call

    if you dont know how it works i copyed the text from vbapi.com(currently shut down but i downloaded the whole site)

    BitBlt Function
    Declare Function BitBlt Lib "gdi32.dll" (ByVal hdcDest As Long, ByVal nXDest As Long, ByVal nYDest As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hdcSrc As Long, ByVal nXSrc As Long, ByVal nYSrc As Long, ByVal dwRop As Long) As Long

    Platforms

    Windows 95: Supported.
    Windows 98: Supported.
    Windows NT: Requires Windows NT 3.1 or later.
    Windows 2000: Supported.
    Windows CE: Requires Windows CE 1.0 or later.

    Description & Usage
    BitBlt performs a bit-block transfer of a rectangular portion of an image from one device to another. The dimensions of the transfered rectangle are perfectly preserved. The function can perform a variety of raster operations to transfer the block from the source device to the target device.

    Return Value
    If an error occured, the function returns 0 (Windows NT, 2000: use GetLastError to get the error code). If successful, the function returns a non-zero value.

    Visual Basic-Specific Issues
    None.

    Parameters

    hdcDest
    A handle to the device context of the device which receives the transfered image block.
    nXDest
    The x-coordinate of the point to position the upper-left corner of the transfered image block.
    nYDest
    The y-coordinate of the point to position the upper-left corner of the transfered image block.
    nWidth
    The width in pixels of the image block.
    nHeight
    The height in pixels of the image block.
    hdcSrc
    A handle to the device context of the device which contains the image block to transfer.
    nXSrc
    The x-coordinate of the upper-left corner of the image block to transfer.
    nYSrc
    The y-coordinate of the upper-left corner of the image block to transfer.
    dwRop
    One of the following flags identifying the raster operation to use to transfer the image block. Each raster operation uses the RGB color value of the source and/or target pixel to determine the new color of the target pixel.
    BLACKNESS
    Fill the destination rectangle with the color whose index is 0 in the physical palette (which is black by default).
    CAPTUREBLT
    Windows 98, 2000: Include any windows layered on top of the window being used in the resulting image.
    DSTINVERT
    Invert the colors in the destination rectangle.
    MERGECOPY
    Merge the colors of the source rectangle with the specified pattern using the bitwise AND operator.
    MERGEPAINT
    Merge the colors of the inverted source rectangle with the colors of the destination rectangle using the bitwise OR operator.
    NOMIRRORBITMAP
    Windows 98, 2000: Prevent the bitmap from being mirrored.
    NOTSRCCOPY
    Copy the inverted source rectangle to the destination rectangle.
    NOTSRCERASE
    Combine the colors of the source and destination rectangles using the bitwise OR operator and then invert the resulting color.
    PATCOPY
    Copy the specified pattern into the destination bitmap.
    PATINVERT
    Combine the colors of the specified pattern with the colors of the destination rectangle using the bitwise XOR operator.
    PATPAINT
    Combine the colors of the specified pattern with the colors of the inverted source rectangle using the bitwise OR operator. Combine the result of that operation with the colors of the destination rectangle using the bitwise OR operator.
    SRCAND
    Combine the colors of the source and destination rectangles using the bitwise AND operator.
    SRCCOPY
    Copy the source rectangle directly into the destination rectangle.
    SRCERASE
    Combine the inverted colors of the destination rectangle with the colors of the source rectange using the bitwise AND operator.
    SRCINVERT
    Combine the colors of the source and destination rectangles using the bitwise XOR operator.
    SRCPAINT
    Combine the colors of the source and destination rectangles using the bitwise OR operator.
    WHITENESS
    Fill the destination rectangle with the color whose index is 1 in the physical palette (which is white by default).

    Constant Definitions

    Const BLACKNESS = &H42
    ' Const CAPTUREBLT = ???
    Const DSTINVERT = &H550009
    Const MERGECOPY = &HC000CA
    Const MERGEPAINT = &HBB0226
    ' Const NOMIRRORBITMAP = ???
    Const NOTSRCCOPY = &H330008
    Const NOTSRCERASE = &H1100A6
    Const PATCOPY = &HF00021
    Const PATINVERT = &H5A0049
    Const PATPAINT = &HFB0A09
    Const SRCAND = &H8800C6
    Const SRCCOPY = &HCC0020
    Const SRCERASE = &H440328
    Const SRCINVERT = &H660046
    Const SRCPAINT = &HEE0086
    Const WHITENESS = &HFF0062
    Example

    ' This code is licensed according to the terms and conditions listed here.

    ' Copy a rectanglular image from window Form1 to window Form2
    ' exactly (using SRCCOPY). The rectangle has a width of 100 and a height of
    ' 50. The upper-left corner of the source block is (350, 250); the block
    ' is placed at (0,0) in Form2.
    Dim retval As Long ' return value

    ' Transfer the image exactly as described above.
    retval = BitBlt(Form2.hDC, 0, 0, 100, 50, Form1.hDC, 350, 250, SRCCOPY)



    this should get you started post again if you need more help

  5. #5

    Thread Starter
    Lively Member SmashX's Avatar
    Join Date
    Nov 2002
    Location
    The Space between dreams and reality
    Posts
    86


    uhhhhh...
    ((that last post confuses the hell out of me no offence to ther poster but that scares me))

    but ok now that i have the keycodes where should i add them to the code?

  6. #6
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    To use this you need to put that in the GeneralSection
    VB Code:
    1. Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    in your code ( in a loop!) you can use this one to get the Arrowkeys
    VB Code:
    1. If (GetAsyncKeyState(vbKeyLeft)) Then
    2.                     'move image to the left
    3.                 ElseIf (GetAsyncKeyState(vbKeyRight)) Then
    4.                     'move image to the right
    5.                 ElseIf (GetAsyncKeyState(vbKeyUp)) Then
    6.                     'move image up
    7.                 ElseIf (GetAsyncKeyState(vbKeyDown)) Then
    8.                     'move image down
    9.                 End If
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  7. #7
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by SmashX


    uhhhhh...
    ((that last post confuses the hell out of me no offence to ther poster but that scares me))

    but ok now that i have the keycodes where should i add them to the code?
    Did you look at my post at all???

  8. #8

    Thread Starter
    Lively Member SmashX's Avatar
    Join Date
    Nov 2002
    Location
    The Space between dreams and reality
    Posts
    86
    Originally posted by NoteMe
    Did you look at my post at all???
    yes i did.

    opus thanks. thats what i was asking for.

  9. #9
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    I'm just wondering...which of these lines did you not understand in my code that you understod in Opus code:

    VB Code:
    1. If GetAsyncKeyState(VK_RIGHT) <> 0 Then
    2.       intX = intX + 25
    3.     End If
    4.     If GetAsyncKeyState(VK_LEFT) <> 0 Then
    5.       intX = intX - 25
    6.     End If
    7.     If GetAsyncKeyState(VK_UP) <> 0 Then
    8.       intY = intY - 25
    9.     End If
    10.     If GetAsyncKeyState(VK_DOWN) <> 0 Then
    11.       intY = intY + 25
    12.     End If
    13.     If GetAsyncKeyState(65) <> 0 Then
    14.         intX2 = intX2 - 25
    15.     End If
    16.     If GetAsyncKeyState(83) <> 0 Then
    17.         intY2 = intY2 + 25
    18.     End If
    19.     If GetAsyncKeyState(68) <> 0 Then
    20.         intX2 = intX2 + 25
    21.     End If
    22.     If GetAsyncKeyState(87) <> 0 Then
    23.         intY2 = intY2 - 25
    24.     End If

  10. #10

    Thread Starter
    Lively Member SmashX's Avatar
    Join Date
    Nov 2002
    Location
    The Space between dreams and reality
    Posts
    86
    im sorry i dident see that.

    um i got a new question.

    where would i put that code?
    i have it in a loop..but...where do i put that?

  11. #11
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by SmashX
    im sorry i dident see that.

    um i got a new question.

    where would i put that code?
    i have it in a loop..but...where do i put that?
    If you want to use a loop, you just make a sub called GameLoop or what ever, and call the sub when your game is starting....Or you could use a timer, and skip the loop, and set the timer enabled property to true when you want your game to start...

  12. #12

    Thread Starter
    Lively Member SmashX's Avatar
    Join Date
    Nov 2002
    Location
    The Space between dreams and reality
    Posts
    86
    ok

    how should i go about that?

    do i just add the code in the timer1_time part?
    Last edited by SmashX; Dec 16th, 2002 at 12:30 PM.

  13. #13
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by SmashX
    ok

    how should i go about that?

    do i just add the code in the timer1_time part?
    Yes you do..maybe you shoul take an other look at my code...It has answers to all the questions you have been asking and more to0...
    Attached Files Attached Files

  14. #14

    Thread Starter
    Lively Member SmashX's Avatar
    Join Date
    Nov 2002
    Location
    The Space between dreams and reality
    Posts
    86
    hehehe
    ya this is helping...

  15. #15
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by SmashX
    hehehe
    ya this is helping...
    It is always importent to look at all the posts...

  16. #16

    Thread Starter
    Lively Member SmashX's Avatar
    Join Date
    Nov 2002
    Location
    The Space between dreams and reality
    Posts
    86

    its not workin...

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetAsyncKeyState Lib _
    4.                 "user32" _
    5.                 (ByVal vKey As Long) As Integer
    6. Const VK_DOWN = &H28
    7. Const VK_LEFT = &H25
    8. Const VK_RIGHT = &H27
    9. Const VK_UP = &H26
    10.  
    11. Dim intX As Integer
    12. Dim intY As Integer
    13. Dim intY2 As Integer
    14. Dim intX2 As Integer
    15.  
    16. Private Sub Form_Load()
    17.    
    18.     intX = 200
    19.     intY = 200
    20.     intX2 = 200
    21.     intY2 = 250
    22.    
    23.     With Me
    24.         .AutoRedraw = True
    25.         .KeyPreview = True
    26.         .PaintPicture imgsuit2.Picture, intX, intY
    27.         .PaintPicture imgsuit.Picture, intX2, intY2
    28.     End With
    29.    
    30.     Timer1.Interval = 10
    31.     Timer1.Enabled = True
    32.    
    33. End Sub
    34.  
    35. Private Sub Timer1_Timer()
    36.  
    37.     If GetAsyncKeyState(VK_RIGHT) <> 0 Then
    38.       intX = intX + 25
    39.     End If
    40.     If GetAsyncKeyState(VK_LEFT) <> 0 Then
    41.       intX = intX - 25
    42.     End If
    43.     If GetAsyncKeyState(VK_UP) <> 0 Then
    44.       intY = intY - 25
    45.     End If
    46.     If GetAsyncKeyState(VK_DOWN) <> 0 Then
    47.       intY = intY + 25
    48.     End If
    49.     If GetAsyncKeyState(65) <> 0 Then
    50.         intX2 = intX2 - 25
    51.     End If
    52.     If GetAsyncKeyState(83) <> 0 Then
    53.         intY2 = intY2 + 25
    54.     End If
    55.     If GetAsyncKeyState(68) <> 0 Then
    56.         intX2 = intX2 + 25
    57.     End If
    58.     If GetAsyncKeyState(87) <> 0 Then
    59.         intY2 = intY2 - 25
    60.     End If
    61.    
    62.     Me.Cls
    63.     Me.PaintPicture imgsuit2.Picture, intX, intY
    64.     Me.PaintPicture imgsuit.Picture, intX2, intY2
    65. End Sub

    all i did was change the picture names and its not workin.

  17. #17
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Did you change the names of the picture boxes to

    imgsuit2
    imgsuit

    ???

    or just the name of the images..???

  18. #18

    Thread Starter
    Lively Member SmashX's Avatar
    Join Date
    Nov 2002
    Location
    The Space between dreams and reality
    Posts
    86
    ok

    i added that code to my new project. it was blank and so i added a pic and named it imgsuit and imgsuit2.
    i also added a timmer.

    the only thing differnt is i have a picture box for the picture to swim around in, would that effect it?

  19. #19
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Nope..it is something else that is wrong...but I don't kknow what you did...are you using Pictureboxes or Image controls???
    Attached Files Attached Files

  20. #20

    Thread Starter
    Lively Member SmashX's Avatar
    Join Date
    Nov 2002
    Location
    The Space between dreams and reality
    Posts
    86
    it was the picture box.

    after i got rid of that it worked fine.

    is there a way to get rid of that pic that says there the whole time. cuz there is one img that moves and another that just stays there. i dont want that to be visible.

    ((BTW: im adding you onto my credits page. ))

  21. #21
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by SmashX
    it was the picture box.

    after i got rid of that it worked fine.

    is there a way to get rid of that pic that says there the whole time. cuz there is one img that moves and another that just stays there. i dont want that to be visible.

    ((BTW: im adding you onto my credits page. ))
    In my example it is a two player game...you move the other picture using A,S,D,W. If you don't want that. You can delete the picture box, and delete this lines in your code...:

    VB Code:
    1. Dim intY2 As Integer
    2. Dim intX2 As Integer
    3. ----------------------------------
    4. intX2 = 200
    5. intY2 = 250
    6. -----------------------------------
    7. .PaintPicture imgsuit.Picture, intX2, intY2
    8. ---------------------------------------------------
    9. If GetAsyncKeyState(65) <> 0 Then
    10.         intX2 = intX2 - 25
    11.     End If
    12.     If GetAsyncKeyState(83) <> 0 Then
    13.         intY2 = intY2 + 25
    14.     End If
    15.     If GetAsyncKeyState(68) <> 0 Then
    16.         intX2 = intX2 + 25
    17.     End If
    18.     If GetAsyncKeyState(87) <> 0 Then
    19.         intY2 = intY2 - 25
    20.     End If
    21. ---------------------------------------------------------------
    22.     Me.PaintPicture imgsuit.Picture, intX2, intY2

    try to look at all the code ans see if you can learn something. You should have seen this if you read the code...

  22. #22
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    BTW thank The Hobo not me...it was he that made the code in the first place...I have only fixed it a bit...

  23. #23

    Thread Starter
    Lively Member SmashX's Avatar
    Join Date
    Nov 2002
    Location
    The Space between dreams and reality
    Posts
    86
    ok.
    will do.

  24. #24
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    Sorry NoteMe,
    I wasn't trying to outsmart you, I just figured he would be stalled by the amount of code you gave as a start. My intention was to get him started and as you said
    see if you can learn something
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  25. #25
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by opus
    Sorry NoteMe,
    I wasn't trying to outsmart you, I just figured he would be stalled by the amount of code you gave as a start. My intention was to get him started and as you said
    I wasn't blaming you at all...just wanted to make a point about that he already had got the answers to all his question if he just looked at it...

  26. #26

    Thread Starter
    Lively Member SmashX's Avatar
    Join Date
    Nov 2002
    Location
    The Space between dreams and reality
    Posts
    86


    ok next on my list on things that i need to aquire knolage for are:

    being able to have my little guy to shoot things. from his current position on the screne.

    i need to set bounderys so the img cant go off the screen...i need help with that one, i orginaly had a pic box...i think of somthin...

    ima gunna add a collision code for the shooting part.

    find a way to have the astroids or enimes move in the screen...

    hmmm this is gunna take some time...
    Last edited by SmashX; Dec 17th, 2002 at 12:46 PM.

  27. #27

    Thread Starter
    Lively Member SmashX's Avatar
    Join Date
    Nov 2002
    Location
    The Space between dreams and reality
    Posts
    86
    ok my brain hurts...i cant figure out how to get the little guy to shoot...

    AHHHHHHHHHHHHHHHHHH!!!!!!!!!

    theres got to be somthing i dont know...grrr...i hate not knowing...lol...

    all i have to do is have a line extend form the img's current position...im drawing a blank.

  28. #28
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Do you want the shooting to be a line??? Or do you want it to be a small object like a bullet that shoots out from the man???

  29. #29

    Thread Starter
    Lively Member SmashX's Avatar
    Join Date
    Nov 2002
    Location
    The Space between dreams and reality
    Posts
    86
    its like a laser shot.

    so im usin a line, and moving it with x1 and x2

  30. #30
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    So what is the problem???

  31. #31

    Thread Starter
    Lively Member SmashX's Avatar
    Join Date
    Nov 2002
    Location
    The Space between dreams and reality
    Posts
    86
    i cant get it to shoot from where the img is. i just shoots from one spot.

  32. #32
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Look at this...is this something you are trying too do???
    Attached Files Attached Files

  33. #33

    Thread Starter
    Lively Member SmashX's Avatar
    Join Date
    Nov 2002
    Location
    The Space between dreams and reality
    Posts
    86
    yea.

    except i have that picture moving around.

  34. #34
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by SmashX
    yea.

    except i have that picture moving around.

    Yes exactly, so now you have the code to move the guy, and you have the code to shoot a laser from his hand...so no you have to put it togheter...that sould be no match for you...

  35. #35

    Thread Starter
    Lively Member SmashX's Avatar
    Join Date
    Nov 2002
    Location
    The Space between dreams and reality
    Posts
    86
    ok

    ill work on it some more.

    thanks

  36. #36

    Thread Starter
    Lively Member SmashX's Avatar
    Join Date
    Nov 2002
    Location
    The Space between dreams and reality
    Posts
    86
    YAY!!!!
    I GOT IT!

    all i had to do is use the intX and intY to place the line where the pic was.

    but then i had a nother problem with the line so i changed to a shape. and i then i would move indepndently across the screen.

    with that little break thru i could think clearly.

    im now trying an attempt at setting bounderys so that my img cant sweet off the screen.

  37. #37
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Nice going...I know you can do a lot if you out your mind to it...

    im now trying an attempt at setting bounderys so that my img cant sweet off the screen.

    Draw a drawing of it on a piece of paper with all the X and y coordinates, then it's simpler to see...I do that when I make games...

  38. #38

    Thread Starter
    Lively Member SmashX's Avatar
    Join Date
    Nov 2002
    Location
    The Space between dreams and reality
    Posts
    86
    ya...

    i know what i have to do ...i just dont know how...

    VB Code:
    1. if imgsuit.top <= 0 then
    2.      .............((drawing a blank))stop?
    3. :rolleyes:
    4.      end if

  39. #39
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    VB Code:
    1. If imgsuit.top <= 0 Then
    2.     imgsuit.top = 0
    3. End If

    This should do it...
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  40. #40

    Thread Starter
    Lively Member SmashX's Avatar
    Join Date
    Nov 2002
    Location
    The Space between dreams and reality
    Posts
    86
    that dident work

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width