The Food class

Next, we will create the Food class (see 9.04_game_of_snake.py):

class Food:def __init__(self, queue): self.queue = queue self.generate_food()def generate_food(self): x = random.randrange(5, 480, 10) y = random.randrange(5, 295, 10) self.position = (x, y) rectangle_position = (x - 5, y - 5, x + 5, y + 5) self.queue.put({'food': rectangle_position})

The description of the code is as follows:

  • Because we want to process all data centrally from within a queue, we pass the queue as an argument to the __init__ method of the Food class.
  • The __init__ method calls another method called generate_food, which is responsible for generating the snake food at random positions on the canvas.
  • The generate_food method generates a random (

Get Tkinter GUI Application Development Blueprints - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.