....::::Scratch programming tricks::::....
Hello everyone!
I would like to help new people to improve their programs, so I decided to create this topic.
You can find some programming tricks in here (isn't that what the title's saying? )
1. Smoother movement
You can let a sprite move using several different scripts, but which one is the best? You can use this block:
[blocks]
<when[ ]key pressed>
[/blocks]
or this one:
[blocks]
<forever>
<if><key[ ]pressed?>
<end>
<end>
[/blocks]
I always use the second, it is uhm... smoother! I can't really explain the effect of the first one, but you can try it out yourself. Open a text editor, and press the same key a few seconds. You see? The letters don't come in with the same difference in time, it is slower at the beginning. It will be the same with your Scratch program. The second option also has another + : you only need one script for your movement, in stead of 1 script/key.
2. Changing a variable between 1 and 0 - toggling
Again, there are several ways to do it. You can use this:
[blocks]
<when I receive[ change
<if> variable = 1
<set{ variable }to( 0
<else>
<set{ variable }to( 1
<end>
[/blocks]
But you can do it shorter!
[blocks]
<when I receive[ change
<set{ variable }to( (( 1 <-> variable ))
[/blocks]
Get it? when variable is 1 ==> 1 - 1 = 0
when variable is 0 ==> 1 - 0 = 1
It will toggle!
3.A Switching a variable between positive and negative
Switching a number between positive and negative is very easy:
[blocks]
(( <{ variable1 }> <*> -1 ))
[/blocks]
3.B Switching a variable to a negative value
The example will "toggle" a number between positive and negative. If you want it to be only positive, use the "abs" formula (from the math block) Ofcourse it should have to be easy to turn a number always to a negative value, if you see 3.A.
[blocks]
(( <abs( <{ variable1 }> <*> -1 ))
[/blocks]
4. Floor function
(Bit more advanced)
Sometimes you don't want to round to the nearest integer (.4 => 0 and .5 => 1), but you want to round to the nearest lower integer. This is called the "floor" function in a lot of languages. Scratch does not supports this in the math block yet, but there is an easy way around it.
[blocks]
<round( (( <{ variable1 }> <-> 0.5 ))
[/blocks]
I will add some other tips soon!
Note: This is just a list of programming tricks, and i do not have created them all myself. I only thought it would be nice to have them together in a list.
Last edited by JSO (2008-04-07 11:16:26)
Offline
Cool! Nice tips, JSO! I'm sure beginners will find it very useful
Last edited by Llamalover (2008-02-21 13:42:03)
Offline
Great tips. I especially like the 2nd example.
For smoother movement, if you use a forever block instead of the when key pressed, you can do diagonal movements. I think this is because the when key pressed block will only work if no other key is pressed with it so you can not move diagonally.
Offline
Great tips! I love the toggle code. Very clever!
Offline
While JSO's tutorial is nice, it should be pointed out that others have suggested the same toggle method on the forums before:
http://scratch.mit.edu/forums/viewtopic.php?pid=15995#p15995
http://scratch.mit.edu/forums/viewtopic.php?pid=11877#p11877
The idea is obvious enough that there is no need to give credit to others, but praise to JSO should be for the quality of the tutorial, rather than the cleverness of the toggle idea.
Offline
kevin_karplus wrote:
While JSO's tutorial is nice, it should be pointed out that others have suggested the same toggle method on the forums before:
http://scratch.mit.edu/forums/viewtopic.php?pid=15995#p15995
http://scratch.mit.edu/forums/viewtopic.php?pid=11877#p11877
The idea is obvious enough that there is no need to give credit to others, but praise to JSO should be for the quality of the tutorial, rather than the cleverness of the toggle idea.
This comment points out a flaw in using the Forums to attempt to capture "Best Practice" techniques. Good ideas come up, are viewed by the active forum users at that period in time, then get buried by new threads. Only when someone "refreshes" the knowledge like this, does it get seen by another set of forum users. Maybe we need a Sticky topic for useful programming tricks in the Advanced Topics section. It would be nice to have one place to look for the easiest way to implement the Floor function or the ATAN2 function for instance.
Actually, I would prefer something with more structure than the Forums but I'm not sure what that would be at the moment.
Offline
There has been talk about creating a Scratch Wiki for tutorials and programming tips.
I don't know what the status of that is, but it seems to me to be a better approach than the forum for disseminating this sort of information.
Offline
There is already a wiki for the support (scratch.wik.is/support) - maybe someone can add "programming tips" there. I would add way more advanced tricks than these, like Paddle2see already suggested.
Last edited by JSO (2008-03-09 03:38:31)
Offline
JSO, how did you create scratch resources on scratchr.org? when I type scratchr.org it just goes to scratch.mit.edu. how did they allow you to create a site on their server?
Offline
JSO created a site elsewhere that looked very valuable, so the Scratch Team offered to host it for him.
Offline
Kevin is right - at least for the hosting. It's up to you to determine if my site looks valueable .
PS: Same with Jens' Chirp site: chirp.scratchr.org
Offline
Hello
My Name Is Borat
I Am FRom Kasikstan
And Loving It There!
Please Reply!!
Offline
JSO wrote:
I added some new "tips", including the floor function Paddle2See suggested.
At least, my code does the same as the Math.Floor() Function in Javascript and Flash Actionscript. I hope it's right...
Where are your tips located?
Offline
Paddle2See wrote:
JSO wrote:
I added some new "tips", including the floor function Paddle2See suggested.
At least, my code does the same as the Math.Floor() Function in Javascript and Flash Actionscript. I hope it's right...Where are your tips located?
He added them in his first post.
Offline
About the question about the resources site: if you want to ask something about resources.scratchr.org, please ask it in the "scratch resources" thread in the "Show&Tell" forum.
I will give a response here too - it's better then if people just keep asking.
The Scratch team noticed the Scratchresources site because I posted a forum thread (see above). I continuously kept the first post up to date.
Offline