Author Topic: Inkey$ in combination with 'if'  (Read 4247 times)

0 Members and 1 Guest are viewing this topic.

Offline daantje

  • ZX 81
  • *
  • Posts: 9
  • Karma: 0
    • View Profile
Inkey$ in combination with 'if'
« on: July 03, 2011 »
I'm kind of a beginner in Yabasic but i was trying to make my own version of snake, in the graphicswindow of yabasic. Now I've got a problem because in the snake-game, you would want the snake to keep moving in a certain direction, but when for example 'left' is pressed, it should change direction. So i wanted to give the inkey$ a optional argument, it waits for a second, and if no key is pressed, the snake moves forward. Yet i waits until a key is pressed, which is not the idea. This is the specific part where it seems to go wrong:
(Every block of the snake is a rectangle, (x1,y1 to x2,y2), they change when a key is pressed.)

inkey$(1)
if (inkey$="left") then
x1=x1-40
x2=x2-40
elsif (inkey$="right") then
x1=x1+40
x2=x2+40
elsif (inkey$="up") then
y1=y1-40
y2=y2-40
elsif (inkey$="down") then
y1=y1+40
y2=y2+40
endif

After this, the program will change the x and y automatic, so that the snake moves, and loops (do-loop), to start all over again.

Notging happens until a key is pressed, so the optional argument, the way I placed it, seems to be malfunctioning. I guess it's got something to do with the if-statement... I hope this is fixable.. many thanks already!,

Daan

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2757
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
Re: Inkey$ in combination with 'if'
« Reply #1 on: July 03, 2011 »
Wellcome on board daantje.

You just need another variable to keep track of the last movement.
For example:

Code: [Select]
'direction = 0 = move up
'direction = 1 = move down
'direction = 2 = move left
'direction = 3 = move right
direction = 0 ' initialize direction variable once

'do .... loop

'direction control
if (direction = 2) then
x1=x1-40
x2=x2-40
elsif (direction = 3) then
x1=x1+40
x2=x2+40
elsif (direction = 0) then
y1=y1-40
y2=y2-40
elsif (direction = 1) then
y1=y1+40
y2=y2+40
endif

'key check
if (inkey$="left") then direction = 2
if (inkey$="right") then direction = 3
if (inkey$="up") then direction = 0
if (inkey$="down") then direction = 1


« Last Edit: July 03, 2011 by rbz »
Challenge Trophies Won:

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
Re: Inkey$ in combination with 'if'
« Reply #2 on: July 03, 2011 »
In your code the initial call to inkey$ has a timeout of 1 second. after which execution falls through to the if statements. In each if statement you make a further call to inkey$ but this time without a timeout argument, the program waits forever for a keypress at each of those if statements. The solution is to store the initial result of inkey$ in a variable and use the variable instead...

Code: [Select]
key$ = inkey$(0)
if (key$="left") then
  x1=x1-40
  x2=x2-40
elsif (key$="right") then
  x1=x1+40
  x2=x2+40
elsif (key$="up") then
  y1=y1-40
  y2=y2-40
elsif (key$="down") then
  y1=y1+40
  y2=y2+40
endif

Challenge Trophies Won:

Offline daantje

  • ZX 81
  • *
  • Posts: 9
  • Karma: 0
    • View Profile
Re: Inkey$ in combination with 'if'
« Reply #3 on: July 03, 2011 »
Thanks a lot, they both work! It surprises me how fast I get anwers, really great!