July 04, 2020, 01:47:22 PM

Author Topic: Move character (DEFINEKEY)  (Read 478 times)

Offline round157

  • Sr. Member
  • ****
  • Posts: 352
Move character (DEFINEKEY)
« on: March 26, 2020, 09:12:34 AM »
Today I have time and I read the document of SmallBASIC.

This example.

https://smallbasic.github.io/reference/1015.html

I pressed a cursor key, didn't release. The white line drew, then halted, then drew again. The example uses DEFINEKEY command and a halt(or delay) still exists. Can a halt be avoided?

Offline bplus

  • Full Member
  • ***
  • Posts: 137
Re: DEFINEKEY in document
« Reply #1 on: March 26, 2020, 02:32:17 PM »
Today I have time and I read the document of SmallBASIC.

This example.

https://smallbasic.github.io/reference/1015.html

I pressed a cursor key, didn't release. The white line drew, then halted, then drew again. The example uses DEFINEKEY command and a halt(or delay) still exists. Can a halt be avoided?

If you are talking about the Etch-A-Sketch yes, use arrow keys to set dx = change on x-axis, dy = change on y-axis instead of directlty change the pen or pencil location x, y. Would work like in Snake Game the Snake keeps moving in one direction until an arrow key changes direction.

BTW speaking of Snake Game, I have recently worked out a Smart Snake that always gets its fruit and grows to fill the entire snake pit. It's a Snake AI, should be fairly easy to translate from QB64 https://www.qb64.org/forum/index.php?topic=2348.msg116115#msg116115


Oh wait... you probably want the pen to move as long as you are holding down the arrow and stop when released. Try pseudo

While arrow
x = x + dx : y = y + dy
k = inkey
arrow = ArrowCode(k, arrow)
wend

ArrowCode function returns arrow if k matches arrow else 0 might need a delay as computers loop faster than human reflexes. PS full disclosure, you'd have to make that function too.


Forget pseudo not likely will work. DEFINEKEY gives perfect control of drawing so that you don't overshoot your targets. The stop and go aint bad! at least for v g64

Offline round157

  • Sr. Member
  • ****
  • Posts: 352
Re: DEFINEKEY in document
« Reply #2 on: March 28, 2020, 08:31:14 AM »
Oh wait... you probably want the pen to move as long as you are holding down the arrow and stop when released. Try pseudo

Yes!

In fact, I want to know more about "how to control PNG sprite on screen". Not about "drawing".

Quote
The stop and go aint bad!

"Stop and go" is not too good for controlling player character(a PNG sprite) of any game.

Offline bplus

  • Full Member
  • ***
  • Posts: 137
Re: Move character (DEFINEKEY)
« Reply #3 on: March 28, 2020, 03:51:56 PM »
Quote
"Stop and go" is not too good for controlling player character(a PNG sprite) of any game.

Dang! The title of thread even says "Move Character" sorry, the Etch-A-Sketch example threw me off.

Serious games with sound and images, so that is your objective. I would keep shopping around you know for a compiler with more font, sound and graphics support. Some other 'Basics' geared more for games I've tried: SdlBasic, Naalaa, Rcbasic, QB64, Liberty (JB version) and of course all the lovely pickings you see here at this forum!


Offline round157

  • Sr. Member
  • ****
  • Posts: 352
Re: Move character (DEFINEKEY)
« Reply #4 on: March 30, 2020, 03:53:48 AM »
Quote
"Stop and go" is not too good for controlling player character(a PNG sprite) of any game.

Serious games with sound and images, so that is your objective.

Ha....no. I started looking into this keyboard obstacle because I was curious. I was curious because I read this:(https://www.syntaxbomb.com/index.php/topic,7521.msg347040877.html#msg347040877)

"Chris, I don't know if you're reading this or it's the right place to ask, but a big request of mine would be if inkey could be speeded up. Its slow speed makes it unusable in any kind of real-time control situation like a game."


I also read this: (https://www.syntaxbomb.com/index.php/topic,7521.msg347040913.html#msg347040913)

"The best way to achieve "real time" keyboard support is using the DEFINEKEY function."


And I also read this: (https://www.syntaxbomb.com/index.php/topic,7521.msg347040921.html#msg347040921)

"Hmm, yes, thanks, maybe that's all it is. It is of course dependent on the typematic delay, but it will also depend on the speed the interpreter processes the command. Maybe it's as fast as it should be, whatever that means, I was just surprised that I can't write code with flow control via some keys in the way I expected."


Unfortunately, SmallBASIC really has this keyboard obstacle. In addition, there is no real solution to this keyboard obstacle. :(

Offline lettersquash

  • Jr. Member
  • **
  • Posts: 73
    • Lettersquash Blog
Re: Move character (DEFINEKEY)
« Reply #5 on: April 01, 2020, 12:08:33 AM »
@round157, please don't take anything I said about the inkey command being slow as 'gospel' - I'm not a computer expert at all, and, as I said, after comparing the inkey statement with another language, I realised it might not be a problem related to smallBASIC (or it might, I don't know). From my limited experience, different languages have massive differences in their scope and functioning, doing some things better, faster, easier, etc. than others.

Chris has noted an issue with definekey (that once defined, it can't currently be removed, so every use of the key returns program control to the subroutine given in the definition), and he's put it on his todo list to look into. It's possible that might provide a faster key response than inkey, or not.

I don't have enough experience in other languages to say if a similar thing to inkey is faster or not, although I could probably test it in a few. I've got BBC BASIC for Windows, which seems about the same. I also use AutoHotkey, which is not a BASIC language and was built around hotkey responses, so that might be fast. I'm not much of a gamer, so I don't have a bunch of games with key control to compare with smallBASIC.

If you find key response particularly slow (in Windows), don't forget there's a couple of settings in the Control Panel > Keyboard, one for how long the delay is after the first key press (to avoid accidental doubles) and how long the delay is in between keys input into the buffer thereafter, when you hold a key down.

Offline round157

  • Sr. Member
  • ****
  • Posts: 352
Re: Move character (DEFINEKEY)
« Reply #6 on: April 02, 2020, 01:50:04 PM »
please don't take anything I said about the inkey command being slow as 'gospel'

Ha...okay.

Quote
It's possible that might provide a faster key response than inkey, or not.

Very good news!! :D We look forward to the next version.

Quote
I've got BBC BASIC for Windows, which seems about the same. I also use AutoHotkey, which is not a BASIC language and was built around hotkey responses, so that might be fast.

Thanks for the information. :)

Quote
If you find key response particularly slow (in Windows), don't forget there's a couple of settings

Thanks for your tip.

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal