Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Animations with arrow keys
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
jwang




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Tue Dec 12, 2017 8:15 pm   Post subject: RE:Animations with arrow keys

Try setting key to type char instead of string
TuringNightmares




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: