read height, width only once
authorGeoffrey Allott <geoffrey@allott.email>
Tue, 6 Dec 2022 09:42:19 +0000 (09:42 +0000)
committerGeoffrey Allott <geoffrey@allott.email>
Tue, 6 Dec 2022 09:42:19 +0000 (09:42 +0000)
snake.py

index 79a2ba08cf7aeaed4772ab3b149071475d3a4882..9e5c8ebddb3abd69a98ba29005876788ec779dde 100644 (file)
--- 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: