none
LastKey Function/Method RRS feed

  • Question

  • Here's my code:
    GraphicsWindow.Height = 600
    GraphicsWindow.Width = 600
    GraphicsWindow.Show()
    GraphicsWindow.BrushColor = "MediumBlue"
    player1 = Shapes.AddTriangle(0,0,0,40,50,20)
    Key = GraphicsWindow.LastKey
    GraphicsWindow.KeyDown = KeyDown
    Sub KeyDown
    positive = positive + 5
    negative = negative - 5
    If (Key = "Right") Then
    Shapes.Move(player1, positive, 0)
    ElseIf (Key = "Left") Then
    Shapes.Move(player1, negative, 0)
    Elseif (Key = "Up") Then
    Shapes.Move(player1, 0, positive)
    Elseif (Key = "Down") Then
    Shapes.Move(player1, 0, negative)
    EndIf
    EndSub

    For some reason the Lastkey method/function isn't working. Also, when I don't use lastkey the shape doesn't move smoothly. How do I make this work? Any help would be appreciated.

    Tuesday, February 1, 2011 2:15 PM

Answers

  • GraphicsWindow.KeyDown is an event that calls a sub when any key is pressed. You can use subroutines to do things with these events:

     

    GraphicsWindow.KeyDown=KeyDown

    Sub KeyDown

      'gets the key pressed

      key = GraphicsWindow.LastKey

      GraphicsWindow.DrawText(0, 0, key)

    EndSub

     

    But to continuously get the key which is pressed, use a while loop.

    run = "True"
    x = 0
    y = 0
    shape = Shapes.AddEllipse(20, 20)
    While run = "True"
        If GraphicsWindow.LastKey = "Up" Then
            y = y - 1
            Shapes.Move(shape, x, y)
            Program.Delay(5)
        ElseIf GraphicsWindow.LastKey = "Down" Then
            y = y + 1
            Shapes.Move(shape, x, y)
            Program.Delay(5)
        ElseIf GraphicsWindow.LastKey = "Left" Then
            x = x - 1
            Shapes.Move(shape, x, y)
            Program.Delay(5)
        ElseIf GraphicsWindow.LastKey = "Down" Then
            x = x + 1
            Shapes.Move(shape, x, y)
            Program.Delay(5)
        EndIf
    EndWhile

     

    The code above should help, But it's not a good idea to use "positive = positive + 5" and "negative = negative - 5" outside of If's. When using keydown, you have to press the key again to get another response, events aren't always the way to go.


    ~~AirWaves!!~~
    • Marked as answer by JackCoder Wednesday, February 2, 2011 9:34 PM
    Wednesday, February 2, 2011 3:08 AM

All replies

  • In your code you must move

    Key = GraphicsWindow.LastKey
    to

    Sub KeyDown
    Key = GraphicsWindow.LastKey
    'Other code
    EndSub

    When Key = GraphicsWindow.LastKey is outside KeyDown sub it will be always "None"

    If you want smooth animation you must use KeyUp and KeyDown events with while loop. Here is one sample : LXF420 . Use arrows to move paddle


    Sorry My Bad English
    Tuesday, February 1, 2011 2:51 PM
  • I'm new to programming, could you explain what parts of the code to institute for a program that moves a shape in four different directions? Thanks
    Tuesday, February 1, 2011 6:08 PM
  • GraphicsWindow.KeyDown is an event that calls a sub when any key is pressed. You can use subroutines to do things with these events:

     

    GraphicsWindow.KeyDown=KeyDown

    Sub KeyDown

      'gets the key pressed

      key = GraphicsWindow.LastKey

      GraphicsWindow.DrawText(0, 0, key)

    EndSub

     

    But to continuously get the key which is pressed, use a while loop.

    run = "True"
    x = 0
    y = 0
    shape = Shapes.AddEllipse(20, 20)
    While run = "True"
        If GraphicsWindow.LastKey = "Up" Then
            y = y - 1
            Shapes.Move(shape, x, y)
            Program.Delay(5)
        ElseIf GraphicsWindow.LastKey = "Down" Then
            y = y + 1
            Shapes.Move(shape, x, y)
            Program.Delay(5)
        ElseIf GraphicsWindow.LastKey = "Left" Then
            x = x - 1
            Shapes.Move(shape, x, y)
            Program.Delay(5)
        ElseIf GraphicsWindow.LastKey = "Down" Then
            x = x + 1
            Shapes.Move(shape, x, y)
            Program.Delay(5)
        EndIf
    EndWhile

     

    The code above should help, But it's not a good idea to use "positive = positive + 5" and "negative = negative - 5" outside of If's. When using keydown, you have to press the key again to get another response, events aren't always the way to go.


    ~~AirWaves!!~~
    • Marked as answer by JackCoder Wednesday, February 2, 2011 9:34 PM
    Wednesday, February 2, 2011 3:08 AM
  • Thanks for both of your tips, they helped a bunch.
    Wednesday, February 2, 2011 6:53 PM
  • Didn't want to start another thread but can someone double check my code it's at http://smallbasic.com/program/?SPK140. Thanks,

    Jack

    Friday, February 4, 2011 1:00 AM
  • Here's an example of a game code without events. I haven't tested this, so because I use a goto label, it may not work. Copy and Paste the code:

    begin()
    Sub begin
        GraphicsWindow.Show()
        gh = GraphicWindow.Height/2
        gw = GraphicsWindow.Width
        GraphicsWindow.BrushColor = "Green"
        Tri1 = Shapes.AddTriangle(0, 25, 50, 0, 50, 50)
        Shapes.Move(Tri1, 5, gh-25)
        GraphicsWindow.BrushColor = "Red"
        Tri2 = Shapes.AddTriangle(0, 25, 50, 0, 50, 50)
        Shapes.Move(Tri2, GraphicsWindow.Width-55, gh-25)
        playing = "True"
        run1 = "False"
        run2 = "False"
        lives1 = 5
        lives2 = 5
        GraphicsWindow.FontSize = 18
        player1l = Shapes.AddText("Player1 Lives: " + lives1)
        player2l = Shapes.AddText("Player2 Lives: " + lives2)
        Shapes.Move(player1l, 5, 5)
        Shapes.Move(player2l, gw - 100, 5)
        pausing:
        While paused = "True"
            If GraphicsWindow.LastKey = "Escape" Then
                paused = "False"
                Shapes.Move(pause_text)
            EndIf
        EndWhile
        While playing = "True"
            If run1 Then
                animate1()
            EndIf
            If run2 Then
                animate2()
            EndIf
            lk = GraphicsWindow.LastKey
            If lk = "Up" Then
                direc1 = 1
                run1 = "True"
            ElseIf lk = "Right" Then
                direc1 = 2
                run1 = "True"
            ElseIf lk = "Left" Then
                direc1 = 3
                run1 = "True"
            ElseIf lk = "Down" Then
                direc1 = 4
                run1 = "True"
            EndIf

            If lk = "W" Then
                direc2 = 1
                run2 = "True"
            ElseIf lk = "D" Then
                direc2 = 2
                run2 = "True"
            ElseIf lk = "A" Then
                direc2 = 3
                run2 = "True"
            ElseIf lk = "S" Then
                direc2 = 4
                run2 = "True"
            EndIf
            If lk = "Space" Then
                shoot1()
            EndIf
            If lk = "LeftShift" Then
                shoot2()
            EndIf
            If lk = "Escape" Then
                pause()
                Goto pausing
            EndIf
            movebullets()
            removebullets()
            checkifhitplayer()
        EndWhile
    EndSub
    Sub animate1
        If direc1 = 1 Then
            y1 = y1 - 1
            front1="Top"
        ElseIf direc1 = 2 Then
            front1="Right"
            x1  = x1 + 1
        ElseIf direc1 = 3 Then
            front1="Left"
            x1  = x1 - 1
        ElseIf direc1 = 4 Then
            front1="Down"
            y1 = y1 + 1
        EndIf
        Shapes.Move(Tri1, x, y)
    EndSub
    Sub animate2
        If direc2 = 1 Then
            y2 = y1 - 1
            front1="Top"
        ElseIf direc2 = 2 Then
            x1  = x1 + 1
            front1="Right"
        ElseIf direc = 3 Then
            x1  = x1 - 1
            front1="Left"
        ElseIf direc = 4 Then
            y1 = y1 + 1
            front1="Down"
        EndIf
        Shapes.Move(Tri2, x, y)
    EndSub

    Sub shoot1
        GraphicsWindow.BrushColor = "White"
        bn1 = bn1 + 1
        currentdirection1[bn1] = front1
        bullet1[bn1] = Shapes.AddRectangle(10, 10)
        If direc1 = 1 Then
            bl1[bn1]=x1+25
            bt1[bn1]=y1
        ElseIf direc1 = 2 Then
            bl1[bn1]=x1+50
            bt1[bn1]=y1+50
        ElseIf direc1 = 3 Then
            bl1[bn1]=x1
            bt1[bn1]=y1+25
        ElseIf direc1 = 4 Then
            bl1[bn1]=x1+25
            bt1[bn1]=y1+50
        EndIf
        Shapes.Move(bullet1[bn1], bl1[bn1], bt1[bn1])
    EndSub


    Sub shoot2
        GraphicsWindow.BrushColor = "White"
        bn2 = bn2 + 1
        currentdirection2[bn1] = front1
        bullet2[bn2] = Shapes.AddRectangle(10, 10)
        If direc2 = 1 Then
            bl2[bn2]=x2+25
            bt2[bn2]=y2
        ElseIf direc2 = 2 Then
            bl2[bn2]=x2+50
            bt2[bn2]=y2+50
        ElseIf direc2 = 3 Then
            bl2[bn2]=x2
            bt2[bn2]=y2+25
        ElseIf direct = 4 Then
            bl2[bn2]=x2+25
            bt1[bn2]=y2+50
        EndIf
        Shapes.Move(bullet2[bn2], bl2[bn2], bt2[bn2])
    EndSub

    Sub movebullets
        For i = 1 To Array.GetItemCount(bullet1)
            bleft = Shapes.GetLeft(bullet1[i])
            btop = Shapes.GetTop(bullet1[i])
            If currentdirection1[i] = "Left" Then
                nX = bleft-20
            ElseIf currentdirection1[i] = "Up" Then
                nY = btop-20
            ElseIf currentdirection1[i] = "Right" Then
                nX = bleft+30
            ElseIf currentdirection1[i] = "Down" Then
                nY = btop+30
            EndIf
            Shapes.Move(bullet1[i], nX, nY)
        EndFor
        For i = 1 To Array.GetItemCount(bullet2)
            bleft = Shapes.GetLeft(bullet2[i])
            btop = Shapes.GetTop(bullet2[i])
            If currentdirection1[i] = "Left" Then
                nX = bleft-20
            ElseIf currentdirection1[i] = "Up" Then
                nY = btop-20
            ElseIf currentdirection1[i] = "Right" Then
                nX = bleft+30
            ElseIf currentdirection1[i] = "Down" Then
                nY = btop+30
            EndIf
            Shapes.Move(bullet2[i], nX, nY)
        EndFor
    EndSub

    Sub removebullets
        For i = 1 To Array.GetItemCount(bullet1)
            bleft = Shapes.GetLeft(bullet1[i])
            btop = Shapes.GetTop(bullet1[i])
            If bleft < 1 Or bleft > gw Or btop > GraphicsWindow.Height Or btop < 1 Then
                Shapes.Remove(bullet1[i])
            EndIf
        EndFor
        For i = 1 To Array.GetItemCount(bullet2)
            bleft = Shapes.GetLeft(bullet2[i])
            btop = Shapes.GetTop(bullet2[i])
            If bleft < 1 Or bleft > gw Or btop > GraphicsWindow.Height Or btop < 1 Then
                Shapes.Remove(bullet2[i])
            EndIf
        EndFor
    EndSub

    Sub checkifhitplayer
        For i = 1 To Array.GetItemCount(bullet1)
            bleft = Shapes.GetLeft(bullet1[i])
            btop = Shapes.GetTop(bullet1[i])
            pleft = Shapes.GetLeft(Tri1)
            ptop = Shapes.GetTop(Tri1)
            If bleft < pleft+50 And bleft > pleft And btop > ptop And btop < ptop+50 Then
                shotp1()
            EndIf
        EndFor
        For i = 1 To Array.GetItemCount(bullet2)
            bleft = Shapes.GetLeft(bullet2[i])
            btop = Shapes.GetTop(bullet2[i])
            pleft = Shapes.GetLeft(Tri2)
            ptop = Shapes.GetTop(Tri2)
            If bleft < pleft+50 And bleft > pleft And btop > ptop And btop < ptop+50 Then
                shotp2()
            EndIf
        EndFor
    EndSub

    Sub shotp1
        lives1 = lives1-1
        If lives1 < 0 Then
            GraphicsWindow.ShowMessage("Player2 Won!", "Yay!")
            playing="False"
            begin()
        Else
            Shapes.SetText(player1l, "Player1 Lives: " + lives1)
        EndIf
    EndSub
       
    Sub shotp2
        lives2 = lives2-1
        If lives2 < 0 Then
            GraphicsWindow.ShowMessage("Player1 Won!", "Yay!")
            playing="False"
            begin()
        Else
            Shapes.SetText(player2l, "Player2 Lives: " + lives2)
        EndIf
    EndSub

    Sub pause
        paused = "True"
        playing = "False"
        GraphicsWindow.BrushColor = "Violet"
        GraphicsWindow.FontSize = 30
        pause_text = Shapes.AddText("Paused")
    EndSub


    ~~AirWaves!!~~
    Friday, February 4, 2011 5:34 AM