Discuss Scratch

MineBlock137
Scratcher
31 posts

Is there any way to make a block like *if [some number] is closest to [some number, presumably from a list]*^

This is how I would be using it

when green flag clicked
if <(x position) is closest to (item ( v) of [list v] :: list)> then
...
end

Does this make sense? I may have phrased it poorly and the formatting may be off since this is my first time in the forums

I would have to create a block for this, I guess

Last edited by MineBlock137 (Dec. 5, 2021 18:34:50)


“I'm not superstitious…
…but I am a little stitious.”


- Michael Scott, Ping, Michael the Magic, Michael Scarn, Michael Klump, Date Mike, Mike, Blind Guy McSqueezy, Prison Mike, Caleb Crawdad, Master of Guidance, Michael Wonka, Reginald Poofta, Michael Scotch, Spiros, Orville Tootenbacher, Jesus Christ, Johnny Carson, Michael J Fox, Mr. Fox, Nicki Minaj, The Incredible Mr. Fox, Santa Bond, Beyoncé, Captain Bruisin', Michael Jackson, Ryan's Girlfriend, Dennis the Menace, Eddie Murphy, Spicoli Guy, MC Mike Scott, Michael Snot, Elton, Mr. Scott , or Hot Tie Guy (your pick).
VeryFamus
Scratcher
1000+ posts

Is there any way to make a block like *if [some number] is closest to [some number, presumably from a list]*^

Extremely complicated workaround, so yes’m, but also, no, this block would be good for suggestions and I would support, be sure to be constructive in your post if you do.

some know me as the guy who only posted in suggestions
i have returned once more








Lamp oil, rope, bombs. You want it? It’s yours my friend. As long as you have enough rupies.
MineBlock137
Scratcher
31 posts

Is there any way to make a block like *if [some number] is closest to [some number, presumably from a list]*^

VeryFamus wrote:

Extremely complicated workaround, so yes’m, but also, no, this block would be good for suggestions and I would support, be sure to be constructive in your post if you do.

So from my understanding…
there is a way to do it, its just very complicated
but since it is so complicated maybe I should post this in Suggestions?

(and if I were to post it in suggestions, how could I be creative / better express this idea?)

“I'm not superstitious…
…but I am a little stitious.”


- Michael Scott, Ping, Michael the Magic, Michael Scarn, Michael Klump, Date Mike, Mike, Blind Guy McSqueezy, Prison Mike, Caleb Crawdad, Master of Guidance, Michael Wonka, Reginald Poofta, Michael Scotch, Spiros, Orville Tootenbacher, Jesus Christ, Johnny Carson, Michael J Fox, Mr. Fox, Nicki Minaj, The Incredible Mr. Fox, Santa Bond, Beyoncé, Captain Bruisin', Michael Jackson, Ryan's Girlfriend, Dennis the Menace, Eddie Murphy, Spicoli Guy, MC Mike Scott, Michael Snot, Elton, Mr. Scott , or Hot Tie Guy (your pick).
dave-alt-4
Scratcher
1000+ posts

Is there any way to make a block like *if [some number] is closest to [some number, presumably from a list]*^

when @greenFlag clicked
set [high count v] to [200]
set [high count holder v] to [400] //add things to the list
set [counter v] to [-200]
repeat (400)
add (counter) to [list v]
change [counter v] by (1)
end
set [my variable v] to (pick random (-200.0) to (200))
set [counter v] to [0]
repeat (400)
change [counter v] by (1)
set [counttt v] to ([abs v] of ((my variable) - (item (counter) of [list v]))::operators)
if <(counttt) < (high count)> then
set [high count v] to (counttt)
set [high count holder v] to (counter)
end
end
say (item (high count holder) of [list v]) for (999) secs

backpack this if u want

guide to make good projects <3


Think! Is saying if you support a suggestion necessary? No! People can just tell by your constructive post after it. You have more freedom on your tone!
Forumers, especially new, may also get upset if you say “no support!!!” at them! They may take the whole post in a negative tone, and that makes them upset!
Instead, you can kindly say why their suggestion has cons. They will take it as “constructive criticism” and be happy. - dertermenter


Harakou
Scratcher
1000+ posts

Is there any way to make a block like *if [some number] is closest to [some number, presumably from a list]*^

Moved to HWS.

I'm not totally sure what you're asking for. If I understand correctly, you want a block that returns true if and only if item <n> in the list is closer to <x> than all the other items in the list?

MineBlock137 wrote:

So from my understanding…
there is a way to do it, its just very complicated
but since it is so complicated maybe I should post this in Suggestions?

(and if I were to post it in suggestions, how could I be creative / better express this idea?)
It depends on what exactly you want to do, but I think it wouldn't be terribly hard, depending. Something being complicated doesn't mean it belongs in the Suggestions forum. If you just want help writing a script to do that, Help With Scripts is a fine place. You could post in Suggestions if you think it's a block that should be added to Scratch, however. If you wanted to go that route, you should check out our design goals for Scratch and a guide to writing a suggestion.

Last edited by Harakou (Dec. 6, 2021 04:17:09)

deck26
Scratcher
1000+ posts

Is there any way to make a block like *if [some number] is closest to [some number, presumably from a list]*^

Please try again to explain what you mean. A simple example with one x value and a demo list of 5 values for example might make it easier to explain what answer you expect.
ventoaurum
Scratcher
100+ posts

Is there any way to make a block like *if [some number] is closest to [some number, presumably from a list]*^

dave-alt-4 wrote:

when @greenFlag clicked
set [high count v] to [200]
set [high count holder v] to [400] //add things to the list
set [counter v] to [-200]
repeat (400)
add (counter) to [list v]
change [counter v] by (1)
end
set [my variable v] to (pick random (-200.0) to (200))
set [counter v] to [0]
repeat (400)
change [counter v] by (1)
set [counttt v] to ([abs v] of ((my variable) - (item (counter) of [list v]))::operators)
if <(counttt) < (high count)> then
set [high count v] to (counttt)
set [high count holder v] to (counter)
end
end
say (item (high count holder) of [list v]) for (999) secs

backpack this if u want
what even is that?


(signature)
i make bad decisions professionally
MineBlock137
Scratcher
31 posts

Is there any way to make a block like *if [some number] is closest to [some number, presumably from a list]*^

Harakou wrote:

Moved to HWS.

I'm not totally sure what you're asking for. If I understand correctly, you want a block that returns true if and only if item <n> in the list is closer to <x> than all the other items in the list?


Let me try and better go into depth into what I am attempting to do

I am trying to create a Snake game based off of Google Snake, and in that game you are only ever allowed to maneuver the snake on equally divided columns and rows of squares

I have taken the X values and Y values of each column and row and have put all of the X values and Y values into separate lists
When I turn the snake, lets say to the right, I would like Scratch to find the closest column to the snake and automatically move him to it

A little bit like grid-snapping, if you have ever used Photoshop

Could something like this be possible?

when green flag clicked
set (closest column) to closest column of the snake
set x to (closest column)
Sorry if that still isn't clear, I'm trying my best

Last edited by MineBlock137 (Dec. 7, 2021 01:41:24)


“I'm not superstitious…
…but I am a little stitious.”


- Michael Scott, Ping, Michael the Magic, Michael Scarn, Michael Klump, Date Mike, Mike, Blind Guy McSqueezy, Prison Mike, Caleb Crawdad, Master of Guidance, Michael Wonka, Reginald Poofta, Michael Scotch, Spiros, Orville Tootenbacher, Jesus Christ, Johnny Carson, Michael J Fox, Mr. Fox, Nicki Minaj, The Incredible Mr. Fox, Santa Bond, Beyoncé, Captain Bruisin', Michael Jackson, Ryan's Girlfriend, Dennis the Menace, Eddie Murphy, Spicoli Guy, MC Mike Scott, Michael Snot, Elton, Mr. Scott , or Hot Tie Guy (your pick).
MineBlock137
Scratcher
31 posts

Is there any way to make a block like *if [some number] is closest to [some number, presumably from a list]*^

dave-alt-4 wrote:

when @greenFlag clicked
set [high count v] to [200]
set [high count holder v] to [400] //add things to the list
set [counter v] to [-200]
repeat (400)
add (counter) to [list v]
change [counter v] by (1)
end
set [my variable v] to (pick random (-200.0) to (200))
set [counter v] to [0]
repeat (400)
change [counter v] by (1)
set [counttt v] to ([abs v] of ((my variable) - (item (counter) of [list v]))::operators)
if <(counttt) < (high count)> then
set [high count v] to (counttt)
set [high count holder v] to (counter)
end
end
say (item (high count holder) of [list v]) for (999) secs

backpack this if u want

thank you very much i will just need a moment to piece this together in my mind lmao

“I'm not superstitious…
…but I am a little stitious.”


- Michael Scott, Ping, Michael the Magic, Michael Scarn, Michael Klump, Date Mike, Mike, Blind Guy McSqueezy, Prison Mike, Caleb Crawdad, Master of Guidance, Michael Wonka, Reginald Poofta, Michael Scotch, Spiros, Orville Tootenbacher, Jesus Christ, Johnny Carson, Michael J Fox, Mr. Fox, Nicki Minaj, The Incredible Mr. Fox, Santa Bond, Beyoncé, Captain Bruisin', Michael Jackson, Ryan's Girlfriend, Dennis the Menace, Eddie Murphy, Spicoli Guy, MC Mike Scott, Michael Snot, Elton, Mr. Scott , or Hot Tie Guy (your pick).
Harakou
Scratcher
1000+ posts

Is there any way to make a block like *if [some number] is closest to [some number, presumably from a list]*^

MineBlock137 wrote:

Let me try and better go into depth into what I am attempting to do

I am trying to create a Snake game based off of Google Snake, and in that game you are only ever allowed to maneuver the snake on equally divided columns and rows of squares

I have taken the X values and Y values of each column and row and have put all of the X values and Y values into separate lists
When I turn the snake, lets say to the right, I would like Scratch to find the closest column to the snake and automatically move him to it

A little bit like grid-snapping, if you have ever used Photoshop

Could something like this be possible?

when green flag clicked
set (closest column) to closest column of the snake
set x to (closest column)
Sorry if that still isn't clear, I'm trying my best
Hmm, that sounds a lot like grid snapping, which we actually have a wiki article on: https://en.scratch-wiki.info/wiki/Implementing_Grids
The short explanation of this math is that you divide by your grid size, round, then multiply again to get the coordinate of the nearest grid-edge. If you wanted to stick with your list-based solution, you could calculate the column number by just dividing by your grid size and rounding. (You might need to add or subtract half of the grid size before rounding to get it to snap in the right direction consistently.) For example, if you have a grid size of 10:
set [nearest_column v] to (round ((x position) / (10)))
Or this:
set [nearest_column v] to (round (((x position) - (5)) / (10)))
dave-alt-4's script will also work if you need a general-purpose way to find the closest item in the list, but because the values in your list are all consistently spaced, it ends up being a bit overkill and will be slower than just doing it with some arithmetic.

Last edited by Harakou (Dec. 7, 2021 02:00:26)

deck26
Scratcher
1000+ posts

Is there any way to make a block like *if [some number] is closest to [some number, presumably from a list]*^

But no real need to do any of that - https://scratch.mit.edu/projects/42045208 is much simpler.

Even if you don't want to use that method this is trivial. If you have a regular grid so the x coordinates are 30 apart the snake's head needs to more 30 right or left to move to an adjacent horizontal cell. You can do something similar for y so there is no need to do grid snapping or checking lots of cells to find the nearest as it is obvious where the next cell is.

Powered by DjangoBB