Skip to content

Commit 4f34a8d

Browse files
author
codehouseindia
authored
Merge pull request codehouseindia#279 from Divyanshu-mishra24/patch-1
Added new snake game using python
2 parents c10a5b8 + dbec91d commit 4f34a8d

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

snake game

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

0 commit comments

Comments
 (0)