Conteúdo do Curso
Building a Classic Snake Game
Teleporting 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.
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 ofa
andb
.
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.
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.
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.
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.
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 ofa
andb
.
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.
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.
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.