The View class

Let's start coding our game by first creating a basic View class. This class will be responsible for creating the GUI, checking for game over logic, and most importantly acting as the consumer, taking items from the queue and processing them to update the view (see 9.04_game_of_snake.py):

class View(Tk):
def __init__(self, queue):
Tk.__init__(self)
self.queue = queue
self.create_gui()

def create_gui(self):
self.canvas = Canvas(self, width=495, height=305, bg='#FF75A0')
self.canvas.pack()
self.snake = self.canvas.create_line((0, 0), (0,0),fill='#FFCC4C',
width=10)
self.food = self.canvas.create_rectangle(0, 0, 0, 0,
fill='#FFCC4C', outline='#FFCC4C')
self.points_earned = self.canvas.create_text(455, 15, fill='white',
text='Score:0')

The preceding code should be mostly familiar to you by now as we have written similar code in the past. Note, however, that rather than passing the root instance as an argument to its __init__ method, our View class now inherits from the Tk class. The line Tk.__init__(self) ensures that the root window is available to all methods of this class. This way we can avoid writing a root attribute on every line by referencing root simply as self.

This class will also have code to process items put in the queue. We will code the rest of this class after we have coded the classes that put items in the queue.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.126.56