Create the Front End
00:22 The most important aspect of any game front end is providing visual feedback to the players through a graphical interface. Because you are constrained to the text-based console in this example, you’ll take advantage of ANSI escape codes to control things such as text formatting and placement.
01:10 If you’re using Visual Studio Code and it doesn’t resolve the imports, try quitting and reopening the editor. As you can see on-screen, this worked for me, allowing the imports to be resolved correctly.
ConsoleRenderer class overrides the only abstract method responsible for visualizing the game’s current state. In this case, you start by clearing the screen’s content using a helper function, which you can define below the class.
Note that the
print() function automatically ends the text with a newline character. To avoid adding an unnecessary blank line, you can disable this by setting the
end argument. When there’s a winner, you’ll want to distinguish their winning marks with blinking text.
Here you wrap the supplied text with opening and closing ANSI escape codes using an f-string. To render the tic-tac-toe grid filled with player’s marks, you’ll format a multiline template string and use the
textwrap module to remove the indentation.
03:27 While this basic display would work okay, improving this would be desirable. In the next section of the course, you’ll take a look at how to make a couple of improvements to make the game more visually appealing when a winner is declared.
Become a Member to join the conversation.