# Collision not working in game

**URL:** <https://forum.code.org/t/collision-not-working-in-game/31888>\
**Category:** Coding and Debugging Help\
**Created:** [December 10, 2019, 6:46pm UTC](https://forum.code.org/t/collision-not-working-in-game/31888 "2019-12-10T18:46:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![thompam1](https://avatars.discourse-cdn.com/v4/letter/t/f4b2a3/32.png) [@thompam1](https://forum.code.org/u/thompam1)\
**Post date:** [December 10, 2019, 6:46pm UTC](https://forum.code.org/t/collision-not-working-in-game/31888/1 "2019-12-10T18:46:09Z")

</div>

My students are creating their games in Game Lab; however, I have students creating a maze game, but they cannot keep their sprites from going through the walls. They have tried all of the collision commands, but they are not working for them. Does anyone have any suggestions, so I can better assist them?

---

<div class="post-metadata">

**Author:** ![mwood](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mwood/32/4941_2.png) [@mwood](https://forum.code.org/u/mwood)\
**Post date:** [December 10, 2019, 7:20pm UTC](https://forum.code.org/t/collision-not-working-in-game/31888/2 "2019-12-10T19:20:57Z")

</div>

For us to help you best, it would be very helpful for you to answer the following and we can take a look and help you out …

To get help as quickly as possible, here are a few things that are great to include in your post

1. Link to your project: click the “Share” button at the top of the page and paste the link it produces into your post.
2. Are there any particular steps that cause the error?
3. What did you expect the program to do?
4. What did it do instead?

---

<div class="post-metadata">

**Author:** ![mkmietowicz](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mkmietowicz/32/6322_2.png) [@mkmietowicz](https://forum.code.org/u/mkmietowicz)\
**Post date:** [December 11, 2019, 5:14am UTC](https://forum.code.org/t/collision-not-working-in-game/31888/3 "2019-12-11T05:14:12Z")

</div>

Hi @thompam1,

In general it sounds like there needs to be a line that says “if [sprite] is touching [wall], then [go back to start / lose a point / whatever else you want them to do]”. However, like @mwood said, we can help a lot more if we have a link to their code so we can see what they’re thinking. Often it’s just a line or two that needs a minor tweak!

–Michael K.

---

<div class="post-metadata">

**Author:** ![thompam1](https://avatars.discourse-cdn.com/v4/letter/t/f4b2a3/32.png) [@thompam1](https://forum.code.org/u/thompam1)\
**Post date:** [December 11, 2019, 4:52pm UTC](https://forum.code.org/t/collision-not-working-in-game/31888/4 "2019-12-11T16:52:41Z")

</div>

Here is one of my student’s game that has that problem…

> **[Check out what I made](https://studio.code.org/projects/gamelab/oCD3Vd1AsuctmRJmnSa5PGOD54X9byof3RDBzcb8UlU)**
>
> I wrote the code myself with Code.org

---

<div class="post-metadata">

**Author:** ![mwood](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mwood/32/4941_2.png) [@mwood](https://forum.code.org/u/mwood)\
**Post date:** [December 11, 2019, 7:17pm UTC](https://forum.code.org/t/collision-not-working-in-game/31888/5 "2019-12-11T19:17:15Z")

</div>

@thompam1,

Thanks for sharing the code. In the example you shared, I don’t see anywhere where your student has tried to detect collision with the wall and any of the characters. I do see collision detection between the elf and the snowman and between several of the sprites and the present, and I see those collisions all working.

If they tried detecting collisions with the wall, they must have removed that code…

Let us know…

thanks,

Mike

---

<div class="post-metadata">

**Author:** ![thompam1](https://avatars.discourse-cdn.com/v4/letter/t/f4b2a3/32.png) [@thompam1](https://forum.code.org/u/thompam1)\
**Post date:** [December 11, 2019, 7:55pm UTC](https://forum.code.org/t/collision-not-working-in-game/31888/6 "2019-12-11T19:55:45Z")

</div>

Yes, they deleted it. They tried all of the collision commands, and none of them worked.

---

<div class="post-metadata">

**Author:** ![mwood](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mwood/32/4941_2.png) [@mwood](https://forum.code.org/u/mwood)\
**Post date:** [December 11, 2019, 7:59pm UTC](https://forum.code.org/t/collision-not-working-in-game/31888/7 "2019-12-11T19:59:18Z")

</div>

Without spending some time trying to recreate it, I’m wondering if it’s because the walls are all one giant sprite and even though there are some transparent parts of the sprite, you can’t collide with a single wall because anytime you’re inside the rectangular confines of that sprite, you are colliding with the entire sprite.

One solution would be to create multiple sprites and draw the walls with a number of sprites. Of course this would take more time, but I don’t think having one sprite as the entire wall system will work as it is currently designed.

Anyone else have any thoughts on it?

Mike

---

<div class="post-metadata">

**Author:** ![mwood](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mwood/32/4941_2.png) [@mwood](https://forum.code.org/u/mwood)\
**Post date:** [December 11, 2019, 11:29pm UTC](https://forum.code.org/t/collision-not-working-in-game/31888/8 "2019-12-11T23:29:30Z")

</div>

As another quick followup, if you follow [this thread on the forum](https://forum.code.org/t/stay-on-path-in-a-maze/21775), there is an example of how to create a group that contains multiple sprites. You could create a group called “walls” and add all of the individual walls to the group, and then you could set the collision rules with the “walls” group so you didn’t have to check collision with each wall individually. This seems like the most efficient way to handle it, although I am sure there are other ways.

Hope this helps.

Mike

---

<div class="post-metadata">

**Author:** ![infinitestasis](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/infinitestasis/32/5047_2.png) [@infinitestasis](https://forum.code.org/u/infinitestasis)\
**Post date:** [December 12, 2019, 10:06am UTC](https://forum.code.org/t/collision-not-working-in-game/31888/9 "2019-12-12T10:06:23Z")

</div>

I have a solution. I repurposed a super old game. Here it is:  
[https://studio.code.org/projects/gamelab/FCcyZVTK5XzEGMHrgR5i5JlxSkvJCvgo0S6vJjKFDA8/view](https://studio.code.org/projects/gamelab/FCcyZVTK5XzEGMHrgR5i5JlxSkvJCvgo0S6vJjKFDA8/view)  
Drag boxes to create rectangles, press g to export the sprite-group. Press z if you make a mistake. I don’t really have time to do it myself.
