Discuss Scratch

fresandia10
Scratcher
19 posts

How to create a color detector?

I've created a game (currently unshared since this is a major update) and I'm trying to make the main sprite detect a certain color and show it there.

The backdrop is light brown framed by dark brown. The sprite (usually a blue square, but changes with the holidays, i.e. it's a red envelope on Lunar New Year.) The blue square will find a random spot on the screen, and if it's on the dark brown, it'll find another spot. This will keep on going until the spot is light brown.

So far, I've tried this:

when green flag clicked 
hide

when I receive [actually start v]
repeat until <touching color [#ffbf00]?>
go to [ random position v]
if <touching color [#806000] ?> then
go to [ random position v]
end
end
show

which doesn't work…it keeps on showing up in the same position every time! (And in the darker brown!)

This will happen a lot. When the game starts, the blue square will appear randomly after 2-11 seconds. You get a point if you press it before the timer goes off. (If you don't accomplish this, you lose points. You won't die, but having -234 points is worse than dying, right?)
Afterwards, the square will disappear, whether you successfully pressed it in time or not. It'll do the 2-11 second cooldown again, but this time in a different spot. I want this to always appear in the light brown.

I've got everything figured out except for the spawning thing. Can y'all help me, please?

Check out my coolest project, Perfect Pitch!

PLEASE tell me (on my profile) if you've got an idea fun, cool, SIMPLE game.

BetaBuilds
Scratcher
12 posts

How to create a color detector?

Try this
when I receive [actually start]
repeat until <<not <touching color [dark brown/border color] ?>> and <touching color [lightbrown] ?>>
go to [random position]
end
show

Last edited by BetaBuilds (April 1, 2024 23:13:29)

fresandia10
Scratcher
19 posts

How to create a color detector?

That did help it get out of the dark brown, but it keeps on spawning in the same point every time!

Check out my coolest project, Perfect Pitch!

PLEASE tell me (on my profile) if you've got an idea fun, cool, SIMPLE game.

BetaBuilds
Scratcher
12 posts

How to create a color detector?

Try putting a go to random position before the repeat-until.
fresandia10
Scratcher
19 posts

How to create a color detector?

BetaBuilds wrote:

Try this
when I receive [actually start]
repeat until <<not <touching color [dark brown/border color] ?>> and <touching color [lightbrown] ?>>
go to [random position]
end
show

Unfortunately, that didn't work! The blue square keeps on spawning in the same spot. It's light brown, but I'd like it to go somewhere else.

Edit: Woah, I'm not sure what I did, but I fixed it!

Edit: nope, i lied. it unfixed. This time it's stuck at a new spot.

Last edited by fresandia10 (April 1, 2024 23:32:18)


Check out my coolest project, Perfect Pitch!

PLEASE tell me (on my profile) if you've got an idea fun, cool, SIMPLE game.

SpyCoderX
Scratcher
100+ posts

How to create a color detector?

Could you share the project?

New game! (link)
Have a good day/night!


\(-_-) ::#00AAAA //This is crypto. He protects my signature from the evil kumquats!
Programming is a very powerful skill. As are critical thinking and hard work.
- SpyCoderX


han614698
Scratcher
1000+ posts

How to create a color detector?

There might be only one location on the stage that doesn't touch dark brown and the sprite fits.

<Scratch Wikian | Forum Helper | 6000+ Posts>

Credit to -gge for the icons in my signature | I condensed all this code into five lines using [p] tags, idk why




fresandia10
Scratcher
19 posts

How to create a color detector?

han614698 wrote:

There might be only one location on the stage that doesn't touch dark brown and the sprite fits.

The sprite is quite small, and the frame isn't that large. The spirte does a good job of spawning in light brown areas, but I just want it to appear in a different light brown area each time.

Check out my coolest project, Perfect Pitch!

PLEASE tell me (on my profile) if you've got an idea fun, cool, SIMPLE game.

SpyCoderX
Scratcher
100+ posts

How to create a color detector?

fresandia10 wrote:

han614698 wrote:

There might be only one location on the stage that doesn't touch dark brown and the sprite fits.

The sprite is quite small, and the frame isn't that large. The spirte does a good job of spawning in light brown areas, but I just want it to appear in a different light brown area each time.
Hmm. This is more difficult. You could try something like this:
(areas ::list) //areas
define add areas (x) (y) (size)
add (x) to [areas v]
add (y) to [areas v]
add (size) to [areas v]

define random go to
//run without screen refresh
set [area v] to (pick random (1) to ((length of [areas v])/(3))
set [x v] to (item (area) of [areas v])
set [y v] to (item ((area)+(1)) of [areas v])
set [size v] to (item ((area)+(2)) of [areas v])
go to x: (pick random ((x)-(size ::variables)) to ((x)+(size ::variables))) y: (pick random ((y)-(size ::variables)) to ((y)+(size ::variables)))
repeat until <<touching color [good color] ?> and <not <touching color [bad color] ?>>>
go to x: (pick random ((x)-(size ::variables)) to ((x)+(size ::variables))) y: (pick random ((y)-(size ::variables)) to ((y)+(size ::variables)))

Hope this helps!

New game! (link)
Have a good day/night!


\(-_-) ::#00AAAA //This is crypto. He protects my signature from the evil kumquats!
Programming is a very powerful skill. As are critical thinking and hard work.
- SpyCoderX


fresandia10
Scratcher
19 posts

How to create a color detector?

SpyCoderX wrote:

fresandia10 wrote:

han614698 wrote:

There might be only one location on the stage that doesn't touch dark brown and the sprite fits.

The sprite is quite small, and the frame isn't that large. The spirte does a good job of spawning in light brown areas, but I just want it to appear in a different light brown area each time.
Hmm. This is more difficult. You could try something like this:
(areas ::list) //areas
define add areas (x) (y) (size)
add (x) to [areas v]
add (y) to [areas v]
add (size) to [areas v]

define random go to
//run without screen refresh
set [area v] to (pick random (1) to ((length of [areas v])/(3))
set [x v] to (item (area) of [areas v])
set [y v] to (item ((area)+(1)) of [areas v])
set [size v] to (item ((area)+(2)) of [areas v])
go to x: (pick random ((x)-(size ::variables)) to ((x)+(size ::variables))) y: (pick random ((y)-(size ::variables)) to ((y)+(size ::variables)))
repeat until <<touching color [good color] ?> and <not <touching color [bad color] ?>>>
go to x: (pick random ((x)-(size ::variables)) to ((x)+(size ::variables))) y: (pick random ((y)-(size ::variables)) to ((y)+(size ::variables)))

Hope this helps!

I think this broke it, now the sprite doesn't appear anymore

Last edited by fresandia10 (April 3, 2024 17:17:52)


Check out my coolest project, Perfect Pitch!

PLEASE tell me (on my profile) if you've got an idea fun, cool, SIMPLE game.

SpyCoderX
Scratcher
100+ posts

How to create a color detector?

fresandia10 wrote:

SpyCoderX wrote:

-snip-

I think this broke it, now the sprite doesn't appear anymore
Can you share the project?

New game! (link)
Have a good day/night!


\(-_-) ::#00AAAA //This is crypto. He protects my signature from the evil kumquats!
Programming is a very powerful skill. As are critical thinking and hard work.
- SpyCoderX


fresandia10
Scratcher
19 posts

How to create a color detector?

SpyCoderX wrote:

fresandia10 wrote:

SpyCoderX wrote:

-snip-

I think this broke it, now the sprite doesn't appear anymore
Can you share the project?

Already done. It's called “Quick Click!” and I'm just looking for the spawn script.

Check out my coolest project, Perfect Pitch!

PLEASE tell me (on my profile) if you've got an idea fun, cool, SIMPLE game.

SpyCoderTest
Scratcher
48 posts

How to create a color detector?

fresandia10 wrote:

SpyCoderX wrote:

fresandia10 wrote:

SpyCoderX wrote:

-snip-

I think this broke it, now the sprite doesn't appear anymore
Can you share the project?

Already done. It's called “Quick Click!” and I'm just looking for the spawn script.
It isn't showing up because you have a “hide” block the “when i receive start” forever loop.

Have a good day/night!
This is SpyCoderX's alt

\(-_-) ::#00AAAA //This is crypto. He protects my signature from the evil kumquats!
Programming is a very powerful skill. As are critical thinking and hard work.
- SpyCoderX


fresandia10
Scratcher
19 posts

How to create a color detector?

SpyCoderTest wrote:

fresandia10 wrote:

SpyCoderX wrote:

fresandia10 wrote:

SpyCoderX wrote:

-snip-

I think this broke it, now the sprite doesn't appear anymore
Can you share the project?

Already done. It's called “Quick Click!” and I'm just looking for the spawn script.
It isn't showing up because you have a “hide” block the “when i receive start” forever loop.

But I put the show block, yeah? Why isn't it showing up then?

Check out my coolest project, Perfect Pitch!

PLEASE tell me (on my profile) if you've got an idea fun, cool, SIMPLE game.

SpyCoderTest
Scratcher
48 posts

How to create a color detector?

fresandia10 wrote:

SpyCoderTest wrote:

-snip-

But I put the show block, yeah? Why isn't it showing up then?
Because the hide block is after the show block it overrides the show block.

Have a good day/night!
This is SpyCoderX's alt

\(-_-) ::#00AAAA //This is crypto. He protects my signature from the evil kumquats!
Programming is a very powerful skill. As are critical thinking and hard work.
- SpyCoderX


fresandia10
Scratcher
19 posts

How to create a color detector?

SpyCoderTest wrote:

fresandia10 wrote:

SpyCoderTest wrote:

-snip-

But I put the show block, yeah? Why isn't it showing up then?
Because the hide block is after the show block it overrides the show block.

How does that work then? Should I do the forever loop in the message broadcast then?

Check out my coolest project, Perfect Pitch!

PLEASE tell me (on my profile) if you've got an idea fun, cool, SIMPLE game.

Powered by DjangoBB