Skip to content

Commit 11efe65

Browse files
Merge pull request patrickloeber#3 from ChrisIdema/develop
Removed magic numbers and changed initial positions of snake
2 parents 76f49ce + 2b5c865 commit 11efe65

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

snake-game/snake.py

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
import curses
22
from random import randint
33

4+
#constants
5+
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+
'''
14+
415
# setup window
516
curses.initscr()
6-
win = curses.newwin(20, 60, 0, 0) # y,x
17+
win = curses.newwin(WINDOW_HEIGHT, WINDOW_WIDTH, 0, 0) # rows, columns
718
win.keypad(1)
819
curses.noecho()
920
curses.curs_set(0)
1021
win.border(0)
1122
win.nodelay(1) # -1
1223

1324
# snake and food
14-
snake = [(4, 10), (4, 9), (4, 8)]
15-
food = (10, 20)
25+
snake = [(4, 4), (4, 3), (4, 2)]
26+
food = (6, 6)
1627

1728
win.addch(food[0], food[1], '#')
1829
# game logic
@@ -48,9 +59,9 @@
4859

4960
# check if we hit the border
5061
if y == 0: break
51-
if y == 19: break
62+
if y == WINDOW_HEIGHT-1: break
5263
if x == 0: break
53-
if x == 59: break
64+
if x == WINDOW_WIDTH -1: break
5465

5566
# if snake runs over itself
5667
if snake[0] in snake[1:]: break
@@ -60,7 +71,7 @@
6071
score += 1
6172
food = ()
6273
while food == ():
63-
food = (randint(1,18), randint(1,58))
74+
food = (randint(1,WINDOW_HEIGHT-2), randint(1,WINDOW_WIDTH -2))
6475
if food in snake:
6576
food = ()
6677
win.addch(food[0], food[1], '#')

0 commit comments

Comments
 (0)