Computer Science Canada Animations with arrow keys |
Author: | jwang [ Tue Dec 12, 2017 7:14 pm ] |
Post subject: | Animations with arrow keys |
I am trying to make my picture in the program move using arrow keys. But the arrow keys won't work on the output screen %Set screen up setscreen("nocursor") setscreen("noecho") %Declaration Section var key:string(1) %Procedure section procedure Picture loop cls var pictureID:int:=Pic.FileNew("Lighthouse.jpg") var x,y:int:=0 Pic.Draw(pictureID,x,y,picMerge) getch(key) if key=chr(205) then x:=x+1 elsif key=chr(203) then x:=x-1 elsif key=chr(200) then y:=y-1 elsif key=chr(208) then y:=y+1 end if exit when key=chr(27) end loop end Picture %Main Program Picture |
Author: | Insectoid [ Tue Dec 12, 2017 8:15 pm ] |
Post subject: | RE:Animations with arrow keys |
Try setting key to type char instead of string |
Author: | TuringNightmares [ Thu Dec 14, 2017 10:13 pm ] |
Post subject: | Re: Animations with arrow keys |
The line: var x,y:int:=0 is inside the for loop, so every time the loop executes x and y get re-initialized to 0. Declaring them outside the loop, like so, would prevent them from resetting to 0 and allow the picture to change position: %Procedure section procedure Picture var x,y:int:=0 loop cls var pictureID:int:=Pic.FileNew("Lighthouse.jpg") Pic.Draw(pictureID,x,y,picMerge) getch(key) ... I'd also recommend moving the line: var pictureID:int:=Pic.FileNew("Lighthouse.jpg") outside the loop just so it only assigns pictureID once to prevent using more memory. |