Re: good and bad news (object)
- Posted by "Cuny, David at DSS" <David.Cuny at DSS.CA.GOV> Jun 26, 2000
- 437 views
Ferdinand Greyling wrote: > All I want to know is how to press a key and then > the object on the screen moves. OK, here's an example. It's just about the same as you would write in in QBasic. The get_key() routine is the same as ASC(INKEY$()). The code should be pretty self-explanatory -- David Cuny -- the cursor() statement include graphics.e integer key, x, y, newX, newY, updated -- force the screen to update updated = 1 -- position of the 'ship' x = 10 y = 10 -- these duplicate x and y newX = x newY = y constant -- key codes None = -1, Esc = 27, Up = 328, Down = 336, Left = 331, Right = 333, -- screen limits MinX = 1, MaxX = 25, MinY = 1, MaxY = 64, -- true and false True = 1, False = 0 -- erase the screen clear_screen() -- hide cursor cursor( NO_CURSOR ) -- infinite loop; press Esc to exit while 1 do -- get a key key = get_key() -- translate the key if key = None then -- no key press, ignore elsif key = Esc then -- break key, leave loop exit elsif key = Up then -- up key, move up if there's room if x > MinX then newX = x - 1 updated = True end if elsif key = Down then -- down key, move down if there's room if x < MaxX then newX = x + 1 updated = True end if elsif key = Left then -- left key, move left if there's room if y > MinY then newY = y - 1 updated = True end if elsif key = Right then -- right key, move right if there's room if y < MaxY then newY = y + 1 updated = True end if else -- key isn't handled -- display it for debugging position( 1, 1 ) ? key end if -- need to redraw the screen? if updated = True then -- erase old image position( x, y ) puts( 1, " " ) -- get the new position x = newX y = newY -- draw new image position( x, y ) puts( 1, "<-*->" ) -- clear the update screen flag updated = False end if end while