Discuss Scratch

Wydrfell
New to Scratch
2 posts

Detect collision between sprite and rainbow line

Is there a way to detect a collision between a sprite and a line that the sprite has drawn, which (the line) is also constantly changing colours via the “change pen color by x” code block?

Thanks in advance!
Zoman23
Scratcher
72 posts

Detect collision between sprite and rainbow line

I have found the answer to the question. If this doesn’t work look at the other solutions below.

You can use variables as colours, and detect that colour as the variable.

when green flag clicked 
Set variable colour to 0
Forever(
Change variable colour by 1
Set pen colour to variable colour)

You could make it detect red green and blue (it won’t be accurate, but It would work depending on how fast that sprite is moving). If you need precision make it detect more colours. This definitely isn’t a clean solution but it is the only way I can think of to make this work. Maybe someone more experienced in scratch could think of a more robust way.

You could also make a line of fixed colour draw, and then stamp it, and then draw the rainbow line on top. You could detect when you are touching the colour of the fixed colour line. The stamp ‘imprints’ it temporarily into the background so it won’t be affected by the rainbow line.

Last edited by Zoman23 (July 8, 2020 16:28:19)


My online cloud chat, Hooter.
My 3D shooter, Terror.
Cutie_Pooge
Scratcher
500+ posts

Detect collision between sprite and rainbow line

To constantly change the colour of the line just do
forever
clear
set pen color to (colour)
go to x: (line point 1 x) y: (line point 1 y)
pen down
go to x: (line point 2 x) y: (line point 2 y)
pen up
end
if you want to detect touching the line then put this after the pen up:
go to x: (my x) y: (my y)
set [collision v] to [0]
if <touching color (colour) ?> then
set [collision v] to [1]
end

Last edited by Cutie_Pooge (July 8, 2020 16:31:24)


I will not help much around the forums anymore, if you still want my help go to my main account. Still occasionally help.
Age 18
Bit of experience in coding in Scratch, Python, C++ and Rust.
2D Slideshow framework v0.2.0.
Terrible in art and sound.
Wydrfell
New to Scratch
2 posts

Detect collision between sprite and rainbow line

@Cutie_Pooge Thanks for the reply, but perhaps i wasn't too clear, the line is already constantly changing in colour resulting in a rainbow coloured line. My issue is, while we can detect collisions with the touching colour function, there doesn't seem to be an simple solution to detect a collision between a sprite and a line whose color is not guarantee. With a rainbow line there's no guarantee that my sprite will touch the portion of the rainbow line that contains (for example) red. So the issue now is how do I detect a collision with a line where the color does not matter as there is no way to guarantee the player of a game will cross the rainbow line at a specific colour.

@

Zoman23 wrote:

I have found the answer to the question. If this doesn’t work look at the other solutions below.

You can use variables as colours, and detect that colour as the variable.

when green flag clicked 
Set variable colour to 0
Forever(
Change variable colour by 1
Set pen colour to variable colour)

You could make it detect red green and blue (it won’t be accurate, but It would work depending on how fast that sprite is moving). If you need precision make it detect more colours. This definitely isn’t a clean solution but it is the only way I can think of to make this work. Maybe someone more experienced in scratch could think of a more robust way.

You could also make a line of fixed colour draw, and then stamp it, and then draw the rainbow line on top. You could detect when you are touching the colour of the fixed colour line. The stamp ‘imprints’ it temporarily into the background so it won’t be affected by the rainbow line.

The stamp idea seems promising, I had considered targeting specific values of colours so that the gap between each color would not exceed the width of the sprite. But unfortunely this is to help a child with their project so im trying not to make things too complicated, and was trying to see if there were simpler and cleaner solutions. Also (unfortunately) the child is very adamant on using a rainbow line.

With regards to setting a color variable to keep track of the colour, i had tried this but got stuck at how to compare the colour number to an actual colour.

i.e. if sprite touches a colour and that colour == the numeric representation of that colour
Cutie_Pooge
Scratcher
500+ posts

Detect collision between sprite and rainbow line

nvm what I wrote, Scratch-Minion has a way better solution.

Last edited by Cutie_Pooge (July 9, 2020 08:48:29)


I will not help much around the forums anymore, if you still want my help go to my main account. Still occasionally help.
Age 18
Bit of experience in coding in Scratch, Python, C++ and Rust.
2D Slideshow framework v0.2.0.
Terrible in art and sound.
Scratch-Minion
Scratcher
1000+ posts

Detect collision between sprite and rainbow line

It is not an easy thing to do in Scratch.


Here is a modification of your project to detect rainbow lines where you change pen color by 10 as it is drawn.

https://scratch.mit.edu/projects/410516293/

The project plays the pop sound when touching the line rather than stopping.

——————————

Basically, the project tests if the sprite is touching color 0 or if it is touching color 10 or if it is touching color 20 … or if it is touching color 90.
ie. it tests if it is touching any of the ten colors.

I have added more comments inside the project.

One note:
I have not used “change pen color by 10” as after 100, this doesn't work in Scratch 3 as color 0 = color 101 = color 202 etc rather than color 0 = color 100 = color 200 etc.
I have used a variable and “set pen color to (pen color + 10) mod 100” to get around this bug in Scratch 3.

carot3_testing
Scratcher
2 posts

Detect collision between sprite and rainbow line

I attempted to do this without any touching blocks, just with list collisions. The colour constantly changes when you move. See it here

Powered by DjangoBB