Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Problem with list coordinates
- Tronche2Cake
-
100+ posts
Problem with list coordinates
I'm making a rogue-lite, and I have a problem with room generation.
Here's what my program should do:
-Create a starting room within the limits
-Create another room at random coordinates within the limits
-Change the X coordinate randomly until it's equal or adjacent to the starting room's X coordinate
-Change the Y coordinate until it's adjacent to the starting room's Y coordinate
-repeat for however many rooms there are, with rooms always being next to eachother but never having the same coordinates.
However, my script doesn't work and gives a second X coordinate that's not adjacent to the first room's. How can I fix that?
The line of code is very long so I'm gonna give the project's ID, you can use TW to find it. 550274572. It's in the “Level generator” sprite.
Is there a way to solve it, an alternative or a more efficient way to generate rooms?
Any help is welcome.
Here's what my program should do:
-Create a starting room within the limits
-Create another room at random coordinates within the limits
-Change the X coordinate randomly until it's equal or adjacent to the starting room's X coordinate
-Change the Y coordinate until it's adjacent to the starting room's Y coordinate
-repeat for however many rooms there are, with rooms always being next to eachother but never having the same coordinates.
However, my script doesn't work and gives a second X coordinate that's not adjacent to the first room's. How can I fix that?
The line of code is very long so I'm gonna give the project's ID, you can use TW to find it. 550274572. It's in the “Level generator” sprite.
Is there a way to solve it, an alternative or a more efficient way to generate rooms?
Any help is welcome.
Last edited by Tronche2Cake (July 3, 2021 20:19:42)
- Tronche2Cake
-
100+ posts
Problem with list coordinates
hippity hoppity I'm gonna do a bumpitty
- awesome-llama
-
1000+ posts
Problem with list coordinates
I honestly can't make sense of your code. The boolean plugged in to the repeat until always returns true, but when I try and find what is causing this, the blocks used do not seem right, as if they were randomly put together.
My recommendation is to avoid the “brute-force” method as much as possible and instead follow a different procedural approach. There are problems you might not have thought of with having rooms being placed randomly until they are adjacent. Extra computational time, and the potential for a loop to run forever (say if there is no place for a room to go) are two I can think of.
So instead, try to grow the rooms like a tree. This is a well-known method used in games. You can start with the first room like you have already, but the next room is placed adjacent immediately, on one of the sides that are empty. There are only 4 sides so a pick random block can choose this. A bit of extra code will be needed to offset the room in the right direction. Whichever direction is chosen, the room just has to be generated from it, offset by one unit.
My recommendation is to avoid the “brute-force” method as much as possible and instead follow a different procedural approach. There are problems you might not have thought of with having rooms being placed randomly until they are adjacent. Extra computational time, and the potential for a loop to run forever (say if there is no place for a room to go) are two I can think of.
So instead, try to grow the rooms like a tree. This is a well-known method used in games. You can start with the first room like you have already, but the next room is placed adjacent immediately, on one of the sides that are empty. There are only 4 sides so a pick random block can choose this. A bit of extra code will be needed to offset the room in the right direction. Whichever direction is chosen, the room just has to be generated from it, offset by one unit.
- Discussion Forums
- » Help with Scripts
-
» Problem with list coordinates