Discuss Scratch

MrG4meLover
Scratcher
1 post

How to Rotate the Screen

Want to add a bit more challenge to your game by rotating the screen? It's surprisingly simple! All you need to do is swap out your current movement scripts for variables. For example, your movement code may look something like this:

when green flag clicked
forever
if <key [up arrow v] pressed?> then
change y by (10)
end
if <key [down arrow v] pressed?> then
change y by (-10)
end
if <key [left arrow v] pressed?> then
change x by (-10)
end
if <key [right arrow v] pressed?> then
change x by (10)
end
end

To change this to get it ready for screen rotation, swap the movement blocks out for variables, like this (make sure x and y variables are for this sprite only):

when green flag clicked
forever
if <key [up arrow v] pressed?> then
change [y v] by (10)
end
if <key [down arrow v] pressed?> then
change [y v] by (-10)
end
if <key [left arrow v] pressed?> then
change [x v] by (-10)
end
if <key [right arrow v] pressed?> then
change [x v] by (10)
end
end

Great! Now the only issue is, you can no longer move your sprite. This is because the variables aren't affecting anything yet. To fix this, create a new My Block and name it “Update Position.” Here, you can connect the variables to the movement.

define Update Position
go to x: (x) y: (y)

Now, add that block to the bottom of your movement script:

when green flag clicked
forever
if <key [up arrow v] pressed?> then
change [y v] by (10)
end
if <key [down arrow v] pressed?> then
change [y v] by (-10)
end
if <key [left arrow v] pressed?> then
change [x v] by (-10)
end
if <key [right arrow v] pressed?> then
change [x v] by (10)
end
Update Position
end

Nice! We can move again! The only problem is that we can't rotate the screen with this and as of now it does the exact same thing as it did before. We still need to add the “Screen Direction” variable. Make it for all sprites.

when green flag clicked
set [Screen Direction v] to [0]

How this will work is that whenever Screen Direction is 0, the screen will be upright and normal. If it's 180, the screen will be upside down, if it's 90 or -90 it will be sideways, and so on.

But now we're encountering the same issue as before: the variable isn't connected to anything. To fix this, and make the screen finally rotate, we'll need to go back to our Update Position block and adjust it a bit. Now, this may look complicated, but it's really not. As long as you copy the equations exactly, it'll work fine.

define Update Position
set x to: (((x) * ([cos v] of ((0) - (Screen Direction)))) - ((y) * ([sin v] of ((0) - (Screen Direction))))
set y to: (((x) * ([sin v] of ((0) - (Screen Direction)))) + ((y) * ([cos v] of ((0) - (Screen Direction))))
point in direction (Screen Direction)

Once you've done that, congrats! You did it! Now, to rotate the screen just drop down a set Screen Direction to (…) block and play around with it.

(credit to http://gamecodeschool.com/essentials/rotating-graphics-in-2d-games-using-trigonometric-functions-part-2/ for help)

Last edited by MrG4meLover (Aug. 7, 2021 23:30:04)

RaikaStudios_Oficial
Scratcher
1 post

How to Rotate the Screen

Powered by DjangoBB