Discuss Scratch

atomic-REDACTED-2009
Scratcher
15 posts

Random Cell Generation Help

I need help with a random(or pseudorandom) cell generation. It is fine if it needs pen blocks, but it would be better if it doesn't.

Last edited by atomic-REDACTED-2009 (Oct. 11, 2021 11:17:31)


define the Meaning of Life
if <(Life) = [42]> then
say [There is another computer that is better than me.]
end
LeshanaJr
Scratcher
67 posts

Random Cell Generation Help

Hmmmmmm…. Cell generation….. This is actually pretty easy.
We need about 2 cell states here, just to keep it basic. Alive cells & dead cells. Now, in order to track this, the cell sprite will need 2 different costumes and also a variable called
(cellstatus)

Now, assuming we have this, let's start our simulation with about one alive cell. Let's assume this thing wants to expand, so let's give it a random chance to clone itself.

when green flag clicked
set (cellstatus) to [alive]
if <(pick random (1) to (10)) = (1)> then
create clone of [myself v]
end
However, there is a fault with this script. This is just assuming that we have a spot to put the new cell in since it has to be right next to a cell. So, we must implement a grid system. The one made for this script is infinite. We also need to make sure there is open space, so let's make a cloneX and cloneY.
We also must ensure that alive cells are the only ones that are able to make clones, so let's do that, too.
(grid :: grid)
(xpos)
(ypos)
(cloneX)
(cloneY)
Now, let's do this again.
First off, we need to make sure the grid is reset every time the green flag is clicked.
when green flag clicked
repeat (length of [grid v] :: list)
delete (1 v) of [grid v]
end
Next, we need to
when green flag clicked
set (xpos) to [0]
set (ypos) to [0]
add (join (join [X] (xpos)) (join [Y] (ypos))) to [grid v]
set(cloneX) to ((xpos) + (pick random [-1] to [1]))
set(cloneY) to ((ypos) + (pick random[-1] to [1]))
set (cellstatus) to [alive]

define cellcreation
if <<(pick random (1) to (10)) = (1)> and <(cellstatus) = [alive]>> then
if<[grid v] contains (join (join [X] (cloneY)) (join [Y] (cloneY))) ?> then
set(cloneX) to ((xpos) + (pick random [-1] to [1]))
set(cloneY) to ((ypos) + (pick random[-1] to [1]))
else
add (join (join [X] (cloneY)) (join [Y] (cloneY))) to [grid v]
create clone of [myself v]
set(cloneX) to ((xpos) + (pick random [-1] to [1]))
set(cloneY) to ((ypos) + (pick random[-1] to [1]))
end
end
What I basically did here is the entire cell creating process. If there is a cell in the place where the cell wants a duplicated cell to be, it can't go there. However, if there isn't a cell in the place where a cell wants a duplicated cell to be, it will go there.
Now, let's make the code for the clone.

when I start as a clone
set [xpos v] to (letter (1) of (item ((length of [grid v] :: list) v) of [grid v] :: list))
set [ypos v] to (letter (3) of (item ((length of [grid v] :: list) v) of [grid v] :: list))
go to x: (xpos) y: (ypos)

Now that we have this, we want to make it to where cells can die.

when green flag clicked
forever
wait (1) secs
cellcreation
if <(pick random [1] to [5]) = [1]> then
set (cellstatus) to [dead]
end
end
Now, this last bit of code that I just did will start trying to create new cells and sets the cell's status to dead if a 20% chance occurs.
That's about it in terms of basic cell generation. If you have any questions regarding the code, I'll gladly answer them!

Last edited by LeshanaJr (Oct. 11, 2021 12:59:06)


This is LeshanaJr's signature.

Certified pro scratcher |π² years old | pro gamer |
very pro games that LeshanaJr has made
Bullet Boss Rush, FNF Engine V3.1, cool new i loops
atomic-REDACTED-2009
Scratcher
15 posts

Random Cell Generation Help

Thanks! I'll try it out if I can.

define the Meaning of Life
if <(Life) = [42]> then
say [There is another computer that is better than me.]
end
atomic-REDACTED-2009
Scratcher
15 posts

Random Cell Generation Help

It works, but I can't space the cells. I want the cells to be spaced as if they were random tiles from a tile sprite sheet.

define the Meaning of Life
if <(Life) = [42]> then
say [There is another computer that is better than me.]
end
LeshanaJr
Scratcher
67 posts

Random Cell Generation Help

Oh, so you'll probably want to either add an offset or multiply their x and y positions by a certain number to get that spacing.

This is LeshanaJr's signature.

Certified pro scratcher |π² years old | pro gamer |
very pro games that LeshanaJr has made
Bullet Boss Rush, FNF Engine V3.1, cool new i loops
atomic-REDACTED-2009
Scratcher
15 posts

Random Cell Generation Help

I'll try that out.

define the Meaning of Life
if <(Life) = [42]> then
say [There is another computer that is better than me.]
end

Powered by DjangoBB