Skip to content

Commit 2b5c865

Browse files
author
Chris Idema
committed
-changed constants from inner window size to outer window size
1 parent 54dc756 commit 2b5c865

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

snake-game/snake.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33

44
#constants
55

6-
width = 58
7-
height = 18
6+
WINDOW_WIDTH = 60 # number of columns of window box
7+
WINDOW_HEIGHT = 20 # number of rows of window box
8+
'''
9+
Number of blocks in window per line = WINDOW_WIDTH -2.
10+
Block x index ranges from 1 to WINDOW_WIDTH -2.
11+
Number of blocks in window per column = WINDOW_HEIGHT -2.
12+
Block y index ranges from 1 to WINDOW_HEIGHT -2.
13+
'''
814

915
# setup window
1016
curses.initscr()
11-
win = curses.newwin(height+2, width+2, 0, 0) # y,x
17+
win = curses.newwin(WINDOW_HEIGHT, WINDOW_WIDTH, 0, 0) # rows, columns
1218
win.keypad(1)
1319
curses.noecho()
1420
curses.curs_set(0)
@@ -53,9 +59,9 @@
5359

5460
# check if we hit the border
5561
if y == 0: break
56-
if y == height+1: break
62+
if y == WINDOW_HEIGHT-1: break
5763
if x == 0: break
58-
if x == width+1: break
64+
if x == WINDOW_WIDTH -1: break
5965

6066
# if snake runs over itself
6167
if snake[0] in snake[1:]: break
@@ -65,7 +71,7 @@
6571
score += 1
6672
food = ()
6773
while food == ():
68-
food = (randint(1,height), randint(1,width))
74+
food = (randint(1,WINDOW_HEIGHT-2), randint(1,WINDOW_WIDTH -2))
6975
if food in snake:
7076
food = ()
7177
win.addch(food[0], food[1], '#')

0 commit comments

Comments
 (0)