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

#1 2012-07-21 02:42:09

AddInnovation
Scratcher
Registered: 2011-11-16
Posts: 57

Test script

The car is controlled by two sprites. Each which look like identical cars except for a single dot, for one sprite in front and to the left of the car, and for the other in front and to the right.

The cars always move forwards, so if the car with dot on the left is touching the border colour outside the track, then it's the dot that's touching the colour and the car needs to turn right, so it sets it's variable "turndirection" to 2. If it's not touching the colour outside the track, it set's "turndirection to 0".

If the car with dot on the right is touching the border colour outside the track, then the car needs to turn left, so it sets it's variable "turndirection" to -2, otherwise it is set to 0.

The variable turndirection is local to each car (to make sure one script doesn't override the other).

The left-dot car sets the position for both cars. Within a loop, until the race is over,  it moves forward according to it's speed variable (starting with a value of about 4), and turns according to the script ;

turn  ((turndirection) + ( "turndirection" of s[Cmp1SenseR])) degrees
    when I receive [NextRace]
       set (turndirection) to [0]
       show
        repeat (until (RaceOver) = [1])
            set (Speed) to ( (Speed) of [Cmp1SenseL] )
            if (((touching color c[00CE42]?) and (((turndirection) of [Cmp1SenseL v] ) =[0])) and ((turncnt) < [360]))
                set (turndirection) to ((Speed) * [-2])
                change (turncnt) by [1]
            else
                change (turncnt) by -[1]
                set (turndirection) to [0]
            End
            point in direction ( (direction) of [Cmp1SenseL v] )
            set (x) to ( (x position) of [Cmp1SenseL v] )
            set (y) to ( (y position) of [Cmp1SenseL v] )
           End
        hide
    end

Offline

 

#2 2012-07-21 11:22:43

AtomicBawm3
Scratcher
Registered: 2009-06-27
Posts: 1000+

Re: Test script

AddInnovation wrote:

The car is controlled by two sprites. Each which look like identical cars except for a single dot, for one sprite in front and to the left of the car, and for the other in front and to the right.

The cars always move forwards, so if the car with dot on the left is touching the border colour outside the track, then it's the dot that's touching the colour and the car needs to turn right, so it sets it's variable "turndirection" to 2. If it's not touching the colour outside the track, it set's "turndirection to 0".

If the car with dot on the right is touching the border colour outside the track, then the car needs to turn left, so it sets it's variable "turndirection" to -2, otherwise it is set to 0.

The variable turndirection is local to each car (to make sure one script doesn't override the other).

The left-dot car sets the position for both cars. Within a loop, until the race is over,  it moves forward according to it's speed variable (starting with a value of about 4), and turns according to the script ;

turn cw ((turndirection) + ( [turndirection v] of [Cmp1SenseR v])) degrees
    when I receive [NextRace v]
       set [turndirection v] to [0]
       show
        repeat until <(RaceOver) = [1]>
            set [Speed v] to ( [Speed v] of [Cmp1SenseL v] )
            if <<<touching color [00CE42]?> and <([turndirection v] of [Cmp1SenseL v] ) =[0]>> and <(turncnt) < [360]>>
                set [turndirection v] to ((Speed) * [-2])
                change [turncnt v] by [1]
            else
                change [turncnt v] by [-1]
                set [turndirection v] to [0]
            End
            point in direction ( [direction v] of [Cmp1SenseL v] )
            set x to ( [x position v] of [Cmp1SenseL v] )
            set y to ( [y position v] of [Cmp1SenseL v] )
           End
        hide
    end

Script Fixed


http://i50.tinypic.com/j0yw0p.jpg

Offline

 

#3 2012-07-21 11:32:30

PhirripSyrrip
Scratcher
Registered: 2012-02-11
Posts: 100+

Re: Test script

Impressive script. It works, right? You should upload a project using it. I'd be interested.

EDIT: I think you have already, have you?

Last edited by PhirripSyrrip (2012-07-21 11:33:08)


http://i46.tinypic.com/ao03lk.png

Offline

 

Board footer