From f9ae92bee09be6a9b52e925043542ed1c5865639 Mon Sep 17 00:00:00 2001 From: Geoffrey Allott Date: Tue, 6 Dec 2022 09:42:19 +0000 Subject: [PATCH] read height, width only once --- snake.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/snake.py b/snake.py index 79a2ba0..9e5c8eb 100644 --- a/snake.py +++ b/snake.py @@ -46,13 +46,13 @@ class Fruit: class GameArea: def __init__(self, *, win: "curses window", highscore_file: str): self.win = win - height, width = win.getmaxyx() + self.height, self.width = win.getmaxyx() self.tiles = [ - [TileContents.Wall for _ in range(width)], - *[[TileContents.Wall] + [TileContents.Empty for _ in range(width - 2)] + [TileContents.Wall] for _ in range(height - 3)], - [TileContents.Wall for _ in range(width)], + [TileContents.Wall for _ in range(self.width)], + *[[TileContents.Wall] + [TileContents.Empty for _ in range(self.width - 2)] + [TileContents.Wall] for _ in range(self.height - 3)], + [TileContents.Wall for _ in range(self.width)], ] - self.snake = [(height // 2, width // 2)] + self.snake = [(self.height // 2, self.width // 2)] self.fruit = [] self.tiles[self.snake[0][0]][self.snake[0][1]] = TileContents.Snake self.dir = Direction.Right @@ -152,8 +152,7 @@ class GameArea: def pause(self): self.paint() - height, width = self.win.getmaxyx() - self.win.addstr(height // 2, (width - 6) // 2, 'PAUSED') + self.win.addstr(self.height // 2, (self.width - 6) // 2, 'PAUSED') self.win.refresh() sleep(1) self.win.nodelay(False) @@ -169,10 +168,9 @@ class GameArea: def game_over(self): self.paint() - height, width = self.win.getmaxyx() - self.win.addstr(height // 2, (width - 9) // 2, 'GAME OVER') + self.win.addstr(self.height // 2, (self.width - 9) // 2, 'GAME OVER') if self.score > self.orig_highscore: - self.win.addstr(height // 2 + 2, (width - 10) // 2, 'HIGH SCORE') + self.win.addstr(self.height // 2 + 2, (self.width - 10) // 2, 'HIGH SCORE') self.win.refresh() self.win.nodelay(False) while True: -- 2.34.1