Skip to content

Commit a166bba

Browse files
authored
Update README.md
1 parent 229f8d6 commit a166bba

1 file changed

Lines changed: 79 additions & 1 deletion

File tree

README.md

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

0 commit comments

Comments
 (0)