# 2D Simple Tutorial Top Down Collision Code

#1

Drag 4 arrows (up-down-left-right) onto the screen

CODE:
When up arrow pressed,set value MOVE to 1
"""""""down arrow""""""""""""""""""""""""""""to 2
"""""""left arrow""""""""""""""""""""""""""""""to 3

https://c.gethopscotch.com/p/xslpjsd8f

""""""right arrow"""""""""""""""""""""""""""""to 4

CODE FOR SQUARE SPRITE:
when up arrow pressed.change y by 5
"""""""down arrow""""""""""""""""""""""-5
"""""""right arrow""""""""change x by 5
"""""""left arrow""""""""""""""""""""""""-5

When square bumps anything
check if MOVE =1
change y by -4
else
check if MOVE =2
change y by 4
else
check if MOVE =3
change x by 4
else
check if MOVE =4
change x by -4

The square sprite won't be able to move through any objects when it bumps into them and will bounce back.

CODE FOR A 2nd SQUARE TO BE PUSHED BY 1st SQUARE

When 2nd square bumps anything (or 1st square)
check if MOVE =1
change y by 4
else
check if MOVE =2
change y by -4
else
check if MOVE =3
change x by -4
else
check if MOVE =4
change x by 4

The 2nd Square will now be pushed by the first square up-down-left-right.

The 2nd square will move through surrounding objects unlike the 1st square that will bounce back when bumping any object.

This tutorial done on a pc. Thats why no pics.
If you are making a pacman type game you wont have to use lots of complicated code to detect walls like whats currently been done.
Similar code can be used for a 2d platformer game like "Tunnels of Terror"
If anyone asks i'll show how.
@everyone
@Liza @Ian you should make a simple tutorial on the Hopscotch app so users can easily learn how to make a 2d top down game with the players character not moving through objects/walls and users not having to code for every object/wall bumped into.