Discuss Scratch

jvanspronsen
Teacher
2 posts

Going Through Walls

Hi, folks!

This is more a question of algorithm design than which code blocks to use.

I've created a game identical to Chip's Challenge (here's hoping Microsoft won't sue me, haha), but I can't seem to make the walls work properly.

Chip's Challenge

There's just one wall sprite that clones itself a number of times; then all the walls move themselves into the correct x and y positions while the game loads. (Using clones makes it easier change the code for all the walls at once, saves on program size, and makes it possible for me to add a second level if I decide to.)

The main character in the game doesn't technically move; when you press the arrow keys, he stays in the centre of the screen but everything else moves (and his costume changes). Obviously I don't want the main character to walk into a wall, so whenever a wall moves onto the main character, the wall pushes him back the other direction, like this:

whenIreceivemoveRightifxposition=0andyposition=0thenbroadcast moveLeft

The problem with this approach is that if you hold down an arrow key, Scratch is too slow to move you back before you move again, so you end up being able to go through walls. Bad!

The way I see it, I have two options:

(1) I could try to make it so that it won't allow you to move until the previous movement is fully completed. However, none of my previous attempts to do this have been successful. Also, even if I got this to work properly, I worry that it might slow down the character quite a bit.

(2) I could try to make it so that it will only move when it receives the all-clear, but that's hard to program. I tried making a variable that was set to 1 if there was a wall in the way and 0 if there wasn't, but the wall clones set their 0s and 1s in an unpredictable order and usually the last value isn't the proper value. I also tried sending a message if there is a wall, but it doesn't make sense to move forward whenever no message is sent.

Anyone have any suggestions for how to fix this? Thanks!
-Tasteful_Wasteful-
Scratcher
34 posts

Going Through Walls

Hm. I do not know how to help you, Also you may not be able to. Scratch runs at 30fps so it may not be fast enough But good luck!
jvanspronsen
Teacher
2 posts

Going Through Walls

Well, I may have actually fixed it!

I realised that I was playing the “Oof” sound effect before I broadcast the message to move, which delayed it a fraction of a second, so when I swapped the order to make the move first, that made it a lot harder to get through the wall. I'll have to do some more playtesting to be sure, but it looks like it might be fixed!

Powered by DjangoBB