Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Shortening scripts
- mayolettucebacon
-
Scratcher
47 posts
Shortening scripts
so im currently working on this game and it involves a similar movement pattern to jsab (just shapes and beats)
ive got everything correct but this script feels fat and clunky

(Btw xkey is (<right arrow key pressed?> - <left arrow key pressed?>) and ykey is (<up arrow key pressed?> - <down arrow key pressed?>))
The way the script works is when you hold the arrow keys it points to the direction you are moving to
It works but I want a more efficient script
Is there a way to make this script more compact and less clunky?
ive got everything correct but this script feels fat and clunky

(Btw xkey is (<right arrow key pressed?> - <left arrow key pressed?>) and ykey is (<up arrow key pressed?> - <down arrow key pressed?>))
The way the script works is when you hold the arrow keys it points to the direction you are moving to
It works but I want a more efficient script
Is there a way to make this script more compact and less clunky?
Last edited by mayolettucebacon (Aug. 29, 2022 09:31:08)
- Scratch-Minion
-
Scratcher
1000+ posts
Shortening scripts
This will work!
where: xkey is (<right arrow key pressed?> - <left arrow key pressed?>) and ykey is (<up arrow key pressed?> - <down arrow key pressed?>)
We are using the fact that tan 45 = 1 and tan 135 = -1.
However, tan 225 (or tan -135) = 1 too and tan 315 (or tan -45) = -1 too.
atan works backwards, but only returns angles from 0 to 180.
If key left arrow is pressed we know that the angle is 225 or 315 so we adjust by 180 degrees.
if <<(xkey) = [0]> and <(ykey) = [0]>> then
... // code as before
else
point in direction ((90) - (([atan v] of ((ykey) / (xkey))) + ((180) * <<key [left arrow v] pressed?> = [1]>)))
end
where: xkey is (<right arrow key pressed?> - <left arrow key pressed?>) and ykey is (<up arrow key pressed?> - <down arrow key pressed?>)
We are using the fact that tan 45 = 1 and tan 135 = -1.
However, tan 225 (or tan -135) = 1 too and tan 315 (or tan -45) = -1 too.
atan works backwards, but only returns angles from 0 to 180.
If key left arrow is pressed we know that the angle is 225 or 315 so we adjust by 180 degrees.
- mayolettucebacon
-
Scratcher
47 posts
Shortening scripts
This will work!thxif <<(xkey) = [0]> and <(ykey) = [0]>> then
... // code as before
else
point in direction ((90) - (([atan v] of ((ykey) / (xkey))) + ((180) * <<key [left arrow v] pressed?> = [1]>)))
end
where: xkey is (<right arrow key pressed?> - <left arrow key pressed?>) and ykey is (<up arrow key pressed?> - <down arrow key pressed?>)
We are using the fact that tan 45 = 1 and tan 135 = -1.
However, tan 225 (or tan -135) = 1 too and tan 315 (or tan -45) = -1 too.
atan works backwards, but only returns angles from 0 to 180.
If key left arrow is pressed we know that the angle is 225 or 315 so we adjust by 180 degrees.
- Scratch-Minion
-
Scratcher
1000+ posts
Shortening scripts
As an extra note, I think I like your bigger script more as the next programmer will be able to understand it when they read it.
An alternative way to cut down the code slightly would be to nest some of the if blocks.
eg.
If ykey = 0 and xkey = 0
…
Else
If ykey = 0 or xkey = 0 (then angle will be a multiple of 90)
… calculation here using ykey = +/-1 or xkey=+/-1
Else (angle will be 45 plus a multiple of 90)
… calculation here using ykey = +/-1 or xkey=+/-1
An alternative way to cut down the code slightly would be to nest some of the if blocks.
eg.
If ykey = 0 and xkey = 0
…
Else
If ykey = 0 or xkey = 0 (then angle will be a multiple of 90)
… calculation here using ykey = +/-1 or xkey=+/-1
Else (angle will be 45 plus a multiple of 90)
… calculation here using ykey = +/-1 or xkey=+/-1
- Discussion Forums
- » Help with Scripts
-
» Shortening scripts