1

I just started working on SmallBASIC, and I've figured I can make a simple player controller by using a changeable variable which determines the amount of pixels the object is across the graphics window. This is what I've done:

tutle = 300

GraphicsWindow.BrushColor = "Green"
GraphicsWindow.FillEllipse(tutle, 300, 55, 65)

If GraphicsWindow.LastKey = "A" Then
  tutle = tutle + 5
  EndIf 

I've heard that Last Key is the last key you pressed or released, but this doesn't seem to work. I'm sure I've got the KeyDown wrong. What can I do to fix it?

Suever
  • 64,497
  • 14
  • 82
  • 101
Shabby Cat
  • 31
  • 5

4 Answers4

1

Zock u do that and u are going to keep drawing the ellipse so your ellipse will be ovrelapsing the othe ellips that ur creating. I have made multiple games withat shapes. U use shapes not graphics window. It's much faster and cleaner and easier to understand.

  • Well, the way I like to answer things is by showing them what they did wrong, and how to fix it. I don't like dumping loads of information on them unless they want it. If it were me designing this code, the yes, I would use shapes instead of the Graphicswindow. But I was merely showing him where he went wrong, not how to improve his code. – Zock77 Apr 18 '16 at 15:57
  • ok. i like to go and actually show them a much more efficient way to program, so if they decide to do another program similar to this one, then they wont make the same mistake. but i understand where your coming from. – Matthew Apr 25 '16 at 17:26
  • I agree with Zock. There was no hint as to the subject of the program. It could just as easily have been a drawing program as a game involving movement. Actually, the fact the variable being changed is called turtle suggests that it is a drawing program. No, the goal should always be to answer the question directly. Leave to discussion of best practices to follow up questions. – codingCat Apr 25 '16 at 19:54
0

Your code is only running once. You need to be constantly checking for a key stroke. Not just once.

tutle = 300
GraphicsWindow.BrushColor = "Green"


While 1 = 1 '< Every time the code gets to the EndWhile, it goes strait back up to the While statement.
Program.Delay(10)'<Small delay to make it easier on the PC, and to make the shape move a reasonable speed.
If GraphicsWindow.LastKey = "A" Then
 tutle = tutle + 5
EndIf
GraphicsWindow.FillEllipse(tutle, 300, 55, 65)
EndWhile
Zock77
  • 951
  • 8
  • 26
0

There is another issue to keep in mind when using LastKey. It returns the last key, even if that key was pressed five hours ago. Once you press your "A" key, the loop will keep registering the key press until a different key is pressed. Then that key will be last until a third key is pressed.

To get a single key press, hold it until it is let go, and stop at that point, you need to track the key events.

GraphicsWindow.Show()
circ = Shapes.AddEllipse(10,10)
x = GraphicsWindow.Width / 2
y = GraphicsWindow.Height / 2

GraphicsWindow.KeyDown = onKeyDown
GraphicsWindow.KeyUp = onKeyUp
pressed = "False"

While "True"
  If pressed Then
    If GraphicsWindow.LastKey = "Up" then
      y = y - 1
    endif
  EndIf
  Shapes.Move(circ,x,y)
  Program.Delay(20)
EndWhile


Sub onKeyDown
  pressed = "True"
EndSub

Sub onKeyUp
  pressed = "False"
EndSub
codingCat
  • 2,396
  • 4
  • 21
  • 27
  • Correct? The original question was asking about keyboard/controller input. A correct solution has nothing to do with shapes. It would have been just as valid if I had used the Turtle or Graphics window commands in my example. – codingCat Apr 25 '16 at 19:42
  • yeah but in the long run, shapes will ifninitely beat graphics window. Bruh, i built a 10,000 line game on sb using shapes and game loops with animations. I know what im talking about. – Matthew Apr 27 '16 at 19:59
  • ok, use graphics window, but your gonna have to use clear in your whilte "true" loop each time you want to move the shape, unless you want the shape to start looking like your making the game snake. – Matthew Apr 27 '16 at 20:02
  • You are making assumptions about the authors project. This isn't pulpit, and proselytizing on the merits of a point of view when it is off topic and inappropriate – codingCat Apr 28 '16 at 15:19
-1

you would use shapes, not graphics. Graphics paints a static, "sticker as you would".

    Turtle = Shapes.AddRectangle(100, 100)
GraphicsWindow.KeyDown = move
x =0
y = 0
Shapes.Move(Turtle, x, y)
Sub move
  key = GraphicsWindow.LastKey
  Text.ConvertToLowerCase(key)
  If key = "S" Then
    x = x
    y = y +1 ' values are reveresed for y.
    Shapes.Move(Turtle, x, y )
   EndIf 


  endsub

Hope that helps.

Matthew
  • 157
  • 1
  • 9
  • This answer does not address the question. The question was on methods of keyboard input, not on animation/drawing styles. Also, your example has a bug in it. You convert the key to lowercase then compare it to an uppercase "S". This if statement will never be true. – codingCat Apr 25 '16 at 19:45
  • if you would run it, it actually works. btw, i just ran it and two, i addressed him on why the program wasnt running. It wouldnt matter if i told him his input was wrong, if his program was running wrong from the beginning, then telling him that you has the wrong code, would mean nothing. and teh if statement actaully works, by some miracle. try it. – Matthew Apr 27 '16 at 19:57
  • text.convert never returned anything so thats why it works. – Matthew Apr 27 '16 at 20:10