Discuss Scratch

OmegaChicken
Scratcher
100+ posts

How do you make a sprite go to a random position, within complicated boundaries

The boundaries I'm talking about are the edges(which can be solved with an easy
go to x: (pick random (X) to (X)) y: (pick random (Y) to (Y))
And random walls scattered about

But, when I have the walls(think Pac-Man, Tank Trouble, etc.), how do I make a sprite teleport “randomly” without jumping into the middle of a wall.

Last edited by OmegaChicken (June 1, 2023 18:07:01)

No_Normal
Scratcher
100+ posts

How do you make a sprite go to a random position, within complicated boundaries

Use this:

define Teleport -- Run without screen refresh
go to x: (pick random (X1) to (X2)) y: (pick random (Y1) to (Y2))
repeat until <not <touching [Wall v] ?>>
go to x: (pick random (X1) to (X2)) y: (pick random (Y1) to (Y2))
end

Hope this helps!

Last edited by No_Normal (June 1, 2023 18:16:29)

OmegaChicken
Scratcher
100+ posts

How do you make a sprite go to a random position, within complicated boundaries

Indeed it does, thank you!
Arctevious
Scratcher
500+ posts

How do you make a sprite go to a random position, within complicated boundaries

No_Normal wrote:

Use this:

go to x: (pick random (X1) to (X2)) y: (pick random (Y1) to (Y2))
repeat until <not <touching [Wall v] ?>>
go to x: (pick random (X1) to (X2)) y: (pick random (Y1) to (Y2))
end

Hope this helps!
He mentions his desire for the object to start in the center of the walls in the game. I have implemented something similar in my game Pacman, and you're welcome to check it out and use some of the code that's available. The underlying concept involves creating a coordinate system on which the player or sprite moves, resembling a track. By restricting movement to squares within a pixel distance, you eliminate the possibility of the player being offset from the map. However, this approach doesn't address the issue of the player moving through the walls on the grid, which presents another challenge.

To address this, I recommend using different costumes for walls on all sides, allowing you to construct the map using an engine and alleviate concerns about collision detection. With this method, you only need to detect collisions with each clone instead of the entire grid. However, I personally preferred to stamp down all the sprites rather than keeping them as clones to stay within the 300 clone limit.

Powered by DjangoBB