Animation Help Please
Author |
Message |
jakey140
|
Posted: Tue Dec 19, 2006 6:43 pm Post subject: Animation Help Please |
|
|
The following code is what I have came up with in regards to moving a sqaure using the arrow keys... Can somebody please help me modify the program so hat the box doesnt go outside the size of the screen or distort???
code: |
%Declaration section
var startpointx, startpointy : int := 0
var endpointx, endpointy : int := 50
var key : string (1)
%setscreen mode and size
setscreen ("graphics:400;400")
setscreen ("noecho")
setscreen ("nocursor")
setscreen ("offscreenonly")
procedure display
drawfillbox (startpointx, startpointy, endpointx, endpointy, white)
drawfillbox (startpointx + 1, startpointy + 1, endpointx - 1, endpointy - 1, blue)
getch (key)
if key = chr (200) then
startpointy := startpointy + 1
endpointy := endpointy + 1
elsif key = chr (208) then
startpointx := startpointy - 1
endpointy := endpointy - 1
elsif key = chr (205) then
startpointx := startpointx + 1
endpointx := endpointx + 1
else
if key = chr (203) then
startpointx := startpointx - 1
endpointx := endpointx - 1
end if
end if
View.Update
end display
%Main Program
loop
display
exit when key = chr (27)
end loop
|
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
neufelni
|
Posted: Tue Dec 19, 2006 7:23 pm Post subject: (No subject) |
|
|
Well I don't have Turing on my computer anymore so I can't run your program, but I think I can see why it is being distorted(I'm assuming you mean it is changing sizes).
First off, there are many little things that you should change in your code. The first thing is all the setscreens. You can replace them all with just one:
code: | View.Set("graphics:400;400, noecho, nocursor, offscreenonly") |
Next, you only need two variables for the position, an x and a y, as long as you want to keep the box the same size.
And for drawing the boxes, you should use Draw.FillBox rather than drawfillbox.
You can also simplify your if statements. For the right and left arrows, you only need to change your x variables, and for the up ans down arrows you only change the y variables.
You may also want to check out Input.KeyDown for your input.
So here is your improved code. If you have any questions about it feel free to ask. And as I mentioned earlier, I don't have Turing so there may be some problems with this code.
code: | %Declaration section
var x, y : int := 0
var key : string (1)
%setscreen mode and size
View.Set("graphics:400;400, noecho, nocursor, offscreenonly")
procedure display
DrawFillBox (x, y, x + 50, y + 50, blue)
getch (key)
if key = chr (200) and x + 50 not= maxx then
x += 1
elsif key = chr (208) and x not= 0 then
x -= 1
elsif key = chr (205) and y + 50 not= maxy then
y += 1
elsif key = chr (203) and y not= 0 then
y -= 1
end if
View.Update
cls
end display
%Main Program
loop
display
exit when key = chr (27)
end loop |
EDIT: I forgot about keeping the box from going off the screen. All you need is add another condition to each of your if statements so that the box won't move if it will go off the screen. The updated code is above. |
|
|
|
|
 |
uberwalla

|
Posted: Tue Dec 19, 2006 7:32 pm Post subject: (No subject) |
|
|
ok first of all why use Draw.FillBox instead of drawfillbox? theres nothing wrong with it.
anyways to help improve your code id recomend not using getch to move your box. it pauses to get the certain character after every hit so it moves very slow. i'd recomend Input.KeyDown.
heres an a different version of the revised version nick made, which makes it faster. and it will make it also much easier to tell which key u use instead of using keystroke codes. some people may get annoyed having to look them up to test yar code (like me ) anyways also with your keystrokes one was wrong. your left arrow made the box go up and up made it go left. so here ya go:
code: |
%Declaration section
var x, y : int := 0
var key : array char of boolean
%setscreen mode and size
View.Set ("graphics:400;400, noecho, nocursor, offscreenonly")
procedure display
Draw.FillBox (x, y, x + 50, y + 50, blue)
Input.KeyDown (key)
if key (KEY_RIGHT_ARROW) and x + 50 not= maxx then
x += 1
elsif key (KEY_LEFT_ARROW) and x not= 0 then
x -= 1
elsif key (KEY_UP_ARROW) and y + 50 not= maxy then
y += 1
elsif key (KEY_DOWN_ARROW) and y not= 0 then
y -= 1
end if
View.Update
cls
end display
%Main Program
loop
display
exit when key (KEY_ESC)
end loop
|
|
|
|
|
|
 |
ericfourfour
|
Posted: Tue Dec 19, 2006 7:49 pm Post subject: (No subject) |
|
|
uberwalla wrote: ok first of all why use Draw.FillBox instead of drawfillbox? theres nothing wrong with it.
You are right, there is nothing wrong with it. However, Draw.FillBox is better because it teaches you more about modules. When I enter Draw., I know the function/procedure will come from the draw module. drawfillbox on the other hand looks like a procedure that just draws a full box. It does not tell me anything about where it comes from. |
|
|
|
|
 |
uberwalla

|
Posted: Tue Dec 19, 2006 7:59 pm Post subject: (No subject) |
|
|
ahh ok. anyways talking about modules brings me to another question
does making a class or module make a difference? arent they basically the same? like they both store code that you can input
because im going to make a module to put in the predefs and hopefully itll work , but do either work through predefs and stuff if im making any sense  |
|
|
|
|
 |
|
|