Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Teleporting The Fruit Around | Building a Classic Snake Game
Building a Classic Snake Game
course content

Conteúdo do Curso

Building a Classic Snake Game

bookTeleporting The Fruit Around

Now that we've established the foundation for our fruit buddy, it's time to make it teleport when the snake eats it. Firstly, we need to define the rules for our fruit. Should it teleport randomly, following a predetermined algorithm or to do something else?

To add a layer of challenge, let's make it so the fruit doesn't always spawn conveniently within reach. Instead, we'll introduce an element of unpredictability by making its spawn location random. This means sometimes it will be close by, while other times it will require some effort to reach. The randomness ensures fairness and adds excitement to the gameplay experience.

random

Next, we'll implement the logic to handle the fruit's movement within our game field. We'll ensure that our fruit remains within bounds and avoids collisions with obstacles or other game elements.

Spawn within bounds

We utilize the random function with the range from 0 to fruit.field[0] - 1 for generating random values to ensure that the fruit spawns within the boundaries of the field and cannot appear outside of it.

Note

The random.randint(a, b) function is a part of Python's random module. It generates a random integer between the inclusive range of a and b.

Avoiding collisions

In the future, we don't want the fruit to be able to spawn inside a snake's body or inside other fruit or any object if we choose to add them. Let's think of a way to solve this problem. The simplest method involves generating random positions until we find one that suits our needs.

copy

Note

It does not matter how many times you will run this program it will never output the value of occupied_number.

To achieve this, we'll manage an array containing occupied positions that are unavailable for use. Then, we'll apply the same approach as described earlier, but this time for an array. However, the underlying method remains almost unchanged.

Tarefa
test

Swipe to show code editor

  • Implement a method named move() to relocate the fruit to a random position on the field:
    • Generate a new position for the fruit.
    • Ensure that the generated position avoids occupied positions.

Mark tasks as Completed
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Now that we've established the foundation for our fruit buddy, it's time to make it teleport when the snake eats it. Firstly, we need to define the rules for our fruit. Should it teleport randomly, following a predetermined algorithm or to do something else?

To add a layer of challenge, let's make it so the fruit doesn't always spawn conveniently within reach. Instead, we'll introduce an element of unpredictability by making its spawn location random. This means sometimes it will be close by, while other times it will require some effort to reach. The randomness ensures fairness and adds excitement to the gameplay experience.

random

Next, we'll implement the logic to handle the fruit's movement within our game field. We'll ensure that our fruit remains within bounds and avoids collisions with obstacles or other game elements.

Spawn within bounds

We utilize the random function with the range from 0 to fruit.field[0] - 1 for generating random values to ensure that the fruit spawns within the boundaries of the field and cannot appear outside of it.

Note

The random.randint(a, b) function is a part of Python's random module. It generates a random integer between the inclusive range of a and b.

Avoiding collisions

In the future, we don't want the fruit to be able to spawn inside a snake's body or inside other fruit or any object if we choose to add them. Let's think of a way to solve this problem. The simplest method involves generating random positions until we find one that suits our needs.

copy

Note

It does not matter how many times you will run this program it will never output the value of occupied_number.

To achieve this, we'll manage an array containing occupied positions that are unavailable for use. Then, we'll apply the same approach as described earlier, but this time for an array. However, the underlying method remains almost unchanged.

Tarefa
test

Swipe to show code editor

  • Implement a method named move() to relocate the fruit to a random position on the field:
    • Generate a new position for the fruit.
    • Ensure that the generated position avoids occupied positions.

Mark tasks as Completed
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 1. Capítulo 3
AVAILABLE TO ULTIMATE ONLY
We're sorry to hear that something went wrong. What happened?
some-alt