Capturing key arrows, and Pong game (with an edge); help..

Hi, I have a question about two things;

  1. I have been trying to get the keyboard to work for the arrows, but it has not worked for me. This is what I tried:

    • Asynckeydown in 100 period/ms timer
      'If Keyboard.AsyncKeyDown(&h7B) then
      '//do something with the left arrow key
      'end if
      'If Keyboard.AsyncKeyDown(&h7C) then
      '//do something with the right arrow key…
      'end if
      'If Keyboard.AsyncKeyDown(&h7D) then
      '//do something with the down arrow key…
      'end if
      If Keyboard.AsyncKeyDown(&h7E) then
      Paddle.top = Paddle.top - 5
      //do something with the Up arrow key…
      end if

(I think it’s because I don’t know what the asynckey is…)

  1. I have started a pong game, for a school assignment, and have been wondering how to get the ‘pongball’ to hit the rectangle. This is what I tried:

    • Same string as ‘pongball’ in the timers; MoveRight, MoveLeft.
      *Also, I would like the rectangle to dissapear after being hit, I want an array (since I’ll be using doing the same exact thing for more than one rectangle) but don’t know how to do one.

Can you please help me figure out how to do what I need to do?

Thank you in advance.

I don’t have any animation or game developing experience but I might can advise a little about the arrow keys. For whatever control that you are doing the animation with (canvas I would assume) you need to create a new event (key down). That event is passed a variable key that contains the key code pressed. You then can get the key value by using the Asc() function. The codes for left, right, up and down are 28 - 31.

if Asc(key) = 28  then 'left arrow do something
' code here
else if Asc(key) = 29 then ' right arrow do something
' code here
else if Asc(key) = 30 then ' up arrow do something
'code here
else if Asc(key) = 31 then ' down arrow do something
' code here
end if

[quote=167024:@William James]I don’t have any animation or game developing experience but I might can advise a little about the arrow keys. For whatever control that you are doing the animation with (canvas I would assume) you need to create a new event (key down). That event is passed a variable key that contains the key code pressed. You then can get the key value by using the Asc() function. The codes for left, right, up and down are 28 - 31.

if Asc(key) = 28 then 'left arrow do something ' code here else if Asc(key) = 29 then ' right arrow do something ' code here else if Asc(key) = 30 then ' up arrow do something 'code here else if Asc(key) = 31 then ' down arrow do something ' code here end if [/quote]

How would I create a new event? Would it be in the key down section of canvas; is that what you meant by (key down)?

To create an event you right click on a control and select the “add to” menu item and then select “event handler …”. From there you can select which event and in this case you would select key down. Alternatively you could select the control in the navigator and then from the Xojo insert menu select event handler.

I then write the code you provided me with, correct?
if Asc(key) = 28 then 'left arrow do something
’ code here
else if Asc(key) = 29 then ’ right arrow do something
’ code here
else if Asc(key) = 30 then ’ up arrow do something
'code here
else if Asc(key) = 31 then ’ down arrow do something
’ code here
end if

That’s easy enough to figure out. In a keydown event put

for i = 0 to 255
   if Keyboard.AsyncKeydown(i) then label1.text = str(i)
next

(If you want to handle the case where the user holds the key down, use a timer w/AsyncKeydown)

it’s telling me that “i does not exist” and that the “keydown is the same function as the boolean function”.

(I am using windows, by the way…)

This is what I use in a game…

if Keyboard.AsyncKeyDown(124) then shipVel.x = shipVel.x + s 'right if Keyboard.AsyncKeyDown(126) then shipVel.y = shipVel.y - s 'up if Keyboard.AsyncKeyDown(123) then shipVel.x = shipVel.x - s 'left if Keyboard.AsyncKeyDown(125) then shipVel.y = shipVel.y + s 'down

I don’t use KeyDown events because they don’t the way you necessarily want, delayed repeats, etc. Also I’d run the Timer faster than 10 times a second, at least 30fps so a Period of 33 or Period 16 for 60fps.

…they don’t fire the way…

[quote=167035:@Walideh Sayed] “i does not exist”
[/quote]
You need to dim it, obviously. This is forum code. Apply yourself a little.

[quote]“keydown is the same function as the boolean function”.
[/quote]
I have no idea what that means.

You also might want to watch a Xojo webinar on retro gaming that might be helpful.

link text

and the webinar on games,

link text

Thank you for all the helpful comments, fortunately I was able to get the game to work with an email to the xojo company; A very helpful and nice person told me that the code I was looking for was;

if Asc(key) = 28 then 'left arrow do something
MsgBox “left”
elseif Asc(key) = 29 then ’ right arrow do something
’ code here
elseif Asc(key) = 30 then ’ up arrow do something
'code here
elseif Asc(key) = 31 then ’ down arrow do something
’ code here
end if

in my case I used this exact code, to work the keyboard, but I took out the MsgBox and left it with a code.

Thank you for all the help provided, and a special thanks to #William James, #Tim Hare and #Will Shank for being helpful; thank you.

(I still need the ‘pong’ help, by the way…oh and if you don’t mind telling me how a scoreboard works then that’d be great as well. Thanks)

Did you look at the webinars yet?

I did but I could look it over, again.

You can also check out this blog and forum post as well.

link text

[quote=167241:@William James]You can also check out this blog and forum post as well.

link text[/quote]

The webinars didn’t really give me anything about a score board, and the forum is not what I was looking for since it has no score board, by scoreboard, I mean that:

Since I am doing the pong game, I wanted to have the pongball hit the two paddles and I want the scoreboard so that the game resets automatically when one of the paddles miss the pongball more than once;
I know that I need to use ‘Val’ and ‘Stl’ but don’t know what the code is.

(Thank you for all the support, I appreciate it.)