O'Reilly logo

Tkinter GUI Application Development Blueprints - Second Edition by Bhaskar Chaudhary

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 (

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required