Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » How do you make a sprite go to a random position, within complicated boundaries
- 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
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.
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:
Hope this helps!
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
Use this: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.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!
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.
- Discussion Forums
- » Help with Scripts
-
» How do you make a sprite go to a random position, within complicated boundaries