Help needed for 2 player snake game collision issue (lesson 22)

One of my students is making a 2 player snake game. the goal is to box your opponent in. the ships should be destroyed when they hit the other player’s line but not when they hit their own. Right now the sprites aren’t destroyed when hitting either line. We’ve both worked hard to solve this problem, but I can’t figure it out.

https://studio.code.org/projects/gamelab/VuICpuZ4RjduPe5x0Ub_DX38E6-apfaspTKGJM3QKM4

I suspect part of the problem is coming from how he is creating the lines themselves. I was able to adjust the code so the original l1 and l2 (single dots) destroyed the player sprites, but the other instances of l1 and l2 did not.

Take a look at lines 51 & 52. Right now, it says setCollider.“circle”. Trying changing “circle” to “rectangle”. It seemed to work for me after that.

1 Like

Using upwards of 100 sprites to detect collisions is overkill and will definitely require a lot of computation. I have 2 solutions.

  1. A 2d grid with tiles that will be filled when the snake passes it. If another snake passes it dies. The higher the density the more accurate. (Just like tron)
  2. Form lines at intervals and do segment collision when a snake moves.

Hi! We tried this, but we still aren’t getting collision with the lines being drawn themselves. They do destroy each other when they hit the original l1 and l2 sprite only. Please help again and thank you!

My best guess is that by putting a sprite creation command inside a draw loop, you are creating hundreds of sprites and each time you create a new one, it is replacing the previous one in memory so it can only detect a collision with the most recent one. I can’t say for sure that is what is happening, but I believe that to be the case, so you won’t be able to detect a collision between any one blue sprite and any one red sprite (only the two most recently created).

Now having said that, I wish I had a suggestion on how to create this game in a different way. I’m guessing there is a way, but I personally am not sure where to start, at least using the basic building blocks of Gamelab.

Anyone else out there have any ideas?

Mike

For my Atari breakout remake, i used a group for the blocks.

Maybe it could go like this:

var tail = createGroup();
Var player = createSprite();
bar gameover = false;

function draw() {

If (player.isTouching(tail)) {
gameover = true
}

tail.add(createSprite(player.x,player.y);

1 Like

Thank you for the suggestions. Unfortunately we couldn’t get it to work, so my student adjusted his game.