Skip to content

Commit 28b25d4

Browse files
Merge pull request #333 from rondutta/master
Adding a new file : Snake.py
2 parents 0deb4d6 + 655ac30 commit 28b25d4

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

snake.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# SNAKES GAME
2+
# Use ARROW KEYS to play, SPACE BAR for pausing/resuming and Esc Key for exiting
3+
# Original Author : Sanchit Gangwar
4+
# Modified by : Rayan Dutta
5+
# Minor changes made to keep the game working.
6+
7+
import curses
8+
from curses import KEY_RIGHT, KEY_LEFT, KEY_UP, KEY_DOWN
9+
from random import randint
10+
11+
12+
curses.initscr()
13+
win = curses.newwin(20, 60, 0, 0)
14+
win.keypad(1)
15+
curses.noecho()
16+
curses.curs_set(0)
17+
win.border(0)
18+
win.nodelay(1)
19+
20+
key = KEY_RIGHT # Initializing values
21+
score = 0
22+
23+
snake = [[4,10], [4,9], [4,8]] # Initial snake co-ordinates
24+
food = [10,20] # First food co-ordinates
25+
26+
win.addch(food[0], food[1], '*') # Prints or shows the food
27+
28+
while key != 27: # While Esc key is not pressed
29+
win.border(0)
30+
win.addstr(0, 2, 'Score : ' + str(score) + ' ') # Printing 'Score' and
31+
win.addstr(0, 27, ' SNAKE ') # 'SNAKE' strings
32+
win.timeout(int(150 - (len(snake)/5 + len(snake)/10)%120)) # Increases the speed of Snake as its length increases
33+
34+
prevKey = key # Previous key pressed
35+
event = win.getch()
36+
key = key if event == -1 else event
37+
38+
39+
if key == ord(' '): # If SPACE BAR is pressed, wait for another
40+
key = -1 # one (Pause/Resume)
41+
while key != ord(' '):
42+
key = win.getch()
43+
key = prevKey
44+
continue
45+
46+
if key not in [KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN, 27]: # If an invalid key is pressed
47+
key = prevKey
48+
49+
# Calculates the new coordinates of the head of the snake. NOTE: len(snake) increases.
50+
# This is taken care of later at [1].
51+
snake.insert(0, [snake[0][0] + (key == KEY_DOWN and 1) + (key == KEY_UP and -1), snake[0][1] + (key == KEY_LEFT and -1) + (key == KEY_RIGHT and 1)])
52+
53+
# If snake crosses the boundaries, make it enter from the other side
54+
if snake[0][0] == 0: snake[0][0] = 18
55+
if snake[0][1] == 0: snake[0][1] = 58
56+
if snake[0][0] == 19: snake[0][0] = 1
57+
if snake[0][1] == 59: snake[0][1] = 1
58+
59+
# Exit if snake crosses the boundaries (Uncomment to enable)
60+
#if snake[0][0] == 0 or snake[0][0] == 19 or snake[0][1] == 0 or snake[0][1] == 59: break
61+
62+
# If snake runs over itself
63+
if snake[0] in snake[1:]:
64+
print('Game Over')
65+
break;
66+
67+
if snake[0] == food: # When snake eats the food
68+
food = []
69+
score += 1
70+
while food == []:
71+
food = [randint(1, 18), randint(1, 58)] # Calculating next food's coordinates
72+
if food in snake: food = []
73+
win.addch(food[0], food[1], '*')
74+
else:
75+
last = snake.pop() # [1] If it does not eat the food, length decreases
76+
win.addch(last[0], last[1], ' ')
77+
win.addch(snake[0][0], snake[0][1], '#')
78+
79+
curses.endwin()
80+
print("\nScore - " + str(score))

0 commit comments

Comments
 (0)