This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2008-02-21 12:59:59

JSO
Community Moderator
Registered: 2007-06-23
Posts: 1000+

Scratch programming tricks

....::::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?  big_smile )

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)


http://oi48.tinypic.com/2v1q0e9.jpg

Offline

 

#2 2008-02-21 13:38:13

Llamalover
Scratcher
Registered: 2007-05-18
Posts: 100+

Re: Scratch programming tricks

Cool! Nice tips, JSO! I'm sure beginners will find it very useful

Last edited by Llamalover (2008-02-21 13:42:03)


Be nice, I'm an old lady  wink

Offline

 

#3 2008-02-21 15:10:37

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Scratch programming tricks

Nice tips!


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#4 2008-02-22 01:27:11

Jens
Scratcher
Registered: 2007-06-04
Posts: 1000+

Re: Scratch programming tricks

Wow, the toggle example impresses me, so simple and so clever, thanks JSO!


Jens Mönig

Offline

 

#5 2008-02-22 16:04:50

Zelda123
Scratcher
Registered: 2007-11-21
Posts: 1000+

Re: Scratch programming tricks

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

 

#6 2008-02-23 05:30:10

MyRedNeptune
Community Moderator
Registered: 2007-05-07
Posts: 1000+

Re: Scratch programming tricks

Great tips! I love the toggle code.  smile  Very clever!


http://i52.tinypic.com/5es7t0.png I know what you're thinking! "Neptune! Get rid of those filthy advertisements and give us back the Zarathustra siggy, you horrible person!" Well, don't worry about it, the Zara siggy will be back soon, new and improved! ^^ Meanwhile, just do what the sig tells you to. >.>

Offline

 

#7 2008-02-23 10:33:10

kevin_karplus
Scratcher
Registered: 2007-04-27
Posts: 1000+

Re: Scratch programming tricks

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

 

#8 2008-02-23 10:45:28

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Scratch programming tricks

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.


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#9 2008-02-23 15:02:59

kevin_karplus
Scratcher
Registered: 2007-04-27
Posts: 1000+

Re: Scratch programming tricks

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

 

#10 2008-03-09 03:37:54

JSO
Community Moderator
Registered: 2007-06-23
Posts: 1000+

Re: Scratch programming tricks

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)


http://oi48.tinypic.com/2v1q0e9.jpg

Offline

 

#11 2008-03-12 17:26:45

midnightleopard
Scratcher
Registered: 2007-09-13
Posts: 1000+

Re: Scratch programming tricks

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?


http://pwp.wizards.com/5103673563/Scorecards/Landscape.png

Offline

 

#12 2008-03-12 18:45:06

kevin_karplus
Scratcher
Registered: 2007-04-27
Posts: 1000+

Re: Scratch programming tricks

JSO created a site elsewhere that looked very valuable, so the Scratch Team offered to host it for him.

Offline

 

#13 2008-03-13 06:16:14

JSO
Community Moderator
Registered: 2007-06-23
Posts: 1000+

Re: Scratch programming tricks

Kevin is right - at least for the hosting. It's up to you to determine if my site looks valueable  big_smile  .

PS: Same with Jens' Chirp site: chirp.scratchr.org


http://oi48.tinypic.com/2v1q0e9.jpg

Offline

 

#14 2008-03-13 06:47:07

John-Smith-Beer
Scratcher
Registered: 2008-03-10
Posts: 1

Re: Scratch programming tricks

Hello

My Name Is Borat

I Am FRom Kasikstan

And Loving It There!

Please Reply!!

Offline

 

#15 2008-03-13 06:49:11

gingerrio
Scratcher
Registered: 2008-03-13
Posts: 1

Re: Scratch programming tricks

ello there m8 aint seen yhoo in ages i remember when we worked on the till in lidls 2gether that weoz a crease

Offline

 

#16 2008-04-05 16:39:01

JSO
Community Moderator
Registered: 2007-06-23
Posts: 1000+

Re: Scratch programming tricks

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...


http://oi48.tinypic.com/2v1q0e9.jpg

Offline

 

#17 2008-04-05 18:40:56

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Scratch programming tricks

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?


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#18 2008-04-05 18:44:59

Zelda123
Scratcher
Registered: 2007-11-21
Posts: 1000+

Re: Scratch programming tricks

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

 

#19 2008-04-05 19:14:01

coolstuff
Community Moderator
Registered: 2008-03-06
Posts: 1000+

Re: Scratch programming tricks

But how did you get Scratch to notice your site? It's not like the Scratch team randomly decides to go looking for Scratch websites!

Offline

 

#20 2008-04-05 21:01:52

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Scratch programming tricks

Thanks Zelda123, I didn't think to look there!


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#21 2008-04-06 03:28:57

JSO
Community Moderator
Registered: 2007-06-23
Posts: 1000+

Re: Scratch programming tricks

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.


http://oi48.tinypic.com/2v1q0e9.jpg

Offline

 

Board footer