making static and dynamic obstacles in a snake game

I'll get right to it. My problem is that collision detection with objects in my snake game is not accurate since the speed value is between 1 and 5. This makes the value of snake.x or snake.y sometimes not fall within range of the line collide function. If i increase the range, at slow speeds it would seem like the snake hasn't even touched the line, and i'd get a Game Over.

I can make these obstacles by drawing small ellipses but that would tank my performance. So i'm stuck with just having to draw basic shapes.

here's an example of the level and it's code

void  level1()
  {
    strokeWeight(2);
    line(0,250,500,250);
    for(float a=248; a<=252; a+=0.1)
    if(snake.y==a)
    {
      snake.gameOver();
      start=false;
    }
  }

I know I've asked a lot of questions about this particular snake game... but hey, i'm learning :D

Answers

  • Nevermind figured out a way to make it more accurate

    void  level1()
      {
        strokeWeight(2);
        line(0,250,500,250);
        for(float a=200; a<=300; a++)
        if(snake.y==a)
        {
          float colX=snake.x;
          float colY=250;
          fill(255,0,0);
          ellipse(colX,colY,5,5);
          stroke(255,0,0);
          line(snake.x,snake.y,colX,colY);
          if(dist(snake.x,snake.y,colX,colY)<=2)
          {
            snake.gameOver();
          }
        }
      }
    

    The line and ellipse are just for show really.

Sign In or Register to comment.