John Fraser Computer Contest Club

Forums

Post Reply
Forum Home > Turing > 8 directional arrow key movement

Stanley
Moderator
Posts: 5

Here is the code for movement via the keyboard arrows. :)

You can tweak the velocity (the variable s)



 

View.Set ("graphics: max, max")

View.Set ("offscreenonly")

 

var x, y : int % variable for the x and y positions

var s : int % speed

var chars : array char of boolean % this holds the key pressed down

 

x := maxx div 2 % beginning x position

y := maxy div 2 % beginning y position

s:= 10

 

loop

Input.KeyDown (chars) % store whatever key is pressed into chars

 

if chars (KEY_UP_ARROW) then % check for up arrow

y := y + s

end if

if chars (KEY_DOWN_ARROW) then % check for down arrow

y := y - s

end if

if chars (KEY_RIGHT_ARROW) then % check for right arrow

x := x + s

end if

if chars (KEY_LEFT_ARROW) then % check for left arrow

x := x - s

end if

 

if x < 0 then % ensures x cannot move off screen

x := x

elsif x > maxx then

x := maxx - x

end if

if y < 0 then % ensures y cannot move off screen

y := y

elsif y > maxy then

y := maxy - y

end if

 

drawoval (x, y, 10, 10, blue)

delay (10)

View.Update

cls

end loop

 


--

- Stanley

January 30, 2010 at 11:00 AM Flag Quote & Reply

You must login to post.