Week 11- Snake Game with Dynamic Obstacles

Concept:

When I first read the assignment to use Cellular Automata to create dynamic and interesting patterns, I had no idea what to do. I was stuck for a while. Then, I remembered the classic Snake game I used to play and thought, “What if I could make it more exciting?”

I decided to add obstacles that change over time, making the grid feel alive and unpredictable. Using Cellular Automata, the obstacles evolve and move during the game, creating a challenge that grows as the game progresses. This small twist made the game much more engaging and fun.

Embedded Sketch:

Logic of the Code:

In my game, Cellular Automata is used to generate dynamic and evolving obstacles. Here’s how it works step by step:

  • Starting Grid: The grid starts with some random cells marked as “alive” (obstacles). This randomness ensures the starting grid is different every time you play.
  • Cellular Automata Logic: Every few frames, the code checks each cell on the grid to decide if it should become an obstacle, stay as one, or disappear. This decision depends on how many neighboring cells are also alive. For example:
    • If a cell is an obstacle and has too many or too few neighbors, it disappears.
    • If a cell is empty but has the right number of alive neighbors, it becomes an obstacle. This logic ensures the obstacles move and evolve naturally over time.
  • Obstacle Management: To keep the game challenging, I added a function to make sure there are always at least 10 obstacles on the grid. If the number of obstacles drops too low, the code randomly adds new ones.
  • Pulsing Effect for Obstacles: To make the obstacles visually dynamic, I added a pulsing effect. This effect changes the color intensity of obstacles smoothly over time using a sine wave. The sin() function creates a smooth wave that oscillates between values, giving the obstacles a “breathing” effect. Here’s the code:
    if (grid[x][y] === 1) {
      fill(150 + sin(frameCount * 0.05) * 50, 50, 50); // Pulsing red effect
    } else {
      fill(30);
    }
    

    In this code:

  • frameCount is a built-in variable that counts the number of frames since the sketch started. This makes the color change over time.
  • sin(frameCount * 0.05) creates a wave that oscillates between -1 and 1.
  • By multiplying the wave value by 50 and adding 150, the color smoothly pulses between lighter and darker shades of red. This effect makes the obstacles look alive and dynamic, matching the Cellular Automata theme.

Challenges:
One big challenge was balancing the speed of the Cellular Automata and the snake. At first, the obstacles were changing way too fast, making the game unplayable. To fix this, I slowed down how often the obstacles update compared to the snake’s movement. This kept the game smooth and manageable.

Another challenge was ensuring there were always enough obstacles on the grid. Sometimes, the Cellular Automata rules would clear too many obstacles, leaving the grid empty. To solve this, I added a check to add new obstacles whenever the number dropped below a certain threshold.

Future Improvements:
Here are some things I’d like to add in the future:

  • Player Options: Let players choose how obstacles behave, like making them grow faster or slower.
  • Dynamic Difficulty: Make the game harder as the player scores more points by speeding up the snake or increasing obstacle density.
  • Enhanced Visuals: Add glowing effects or different colors for obstacles to make the game look even better.
  • Sound Effects: Include sounds for eating food, hitting obstacles, or speeding up.

Leave a Reply

Your email address will not be published. Required fields are marked *