Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Losing Condition | Building a Classic Snake Game
Building a Classic Snake Game

book
Losing Condition

Game Over

When the snake collides with its own body or any other obstacle, it signals the end of the game. The game_over() method is invoked to conclude the current game session. To implement this we will simply use our running attribute and set it to false and then reset the game.

py
def game_over(game):
game.running = False
reset(game)

Resetting the Game

After the game ends it's essential to reset the game state for another round. The reset() method restores the game's initial conditions and prepares it for a fresh start:

def reset(game):
game.running = True
game.snake.length = 2
game.snake.positions = [(0, 0)] * 2
game.fruit.position = (self.field[0] - 1, self.field[1] - 1)
copy

Note

Basically, we just stick with the default values provided in the __init__() functions of the game, snake, and fruit classes.

Завдання

Swipe to start coding

  • Add and implement reset() method to the game.
  • Add and implement game_over() method to the game class.

Рішення

def game_over(game):
game.running = False
reset(game)

def reset(game):
game.running = True
game.snake.length = 2
game.snake.positions = [(0, 0)] * 2
game.fruit.position = (game.field[0] - 1, game.field[1] - 1)

Mark tasks as Completed
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 9
AVAILABLE TO ULTIMATE ONLY
some-alt