Skip to content

Commit b500a69

Browse files
authored
Merge pull request larymak#99 from oyerohabib/oyerohabib-2
python snake game
2 parents 8918391 + 03c0bda commit b500a69

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Python-project-Scripts.
1+
# Python-project-Scripts.
22

33
[![Open in Visual Studio Code](https://open.vscode.dev/badges/open-in-vscode.svg)](https://open.vscode.dev/larymak/Python-project-Scripts)
44
![Contributors](https://img.shields.io/github/contributors/larymak/Python-project-Scripts?style=plastic)

SNAKE_GAME/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Snake Game
2+
3+
### This is a simple snack game in built using python. the snake start with a small length and keeps growing as it keeps feeding on the food. The game is over when it hits the end of the terminal box or eats itself.
4+
5+
## Requirements
6+
- curses
7+
- random
8+
9+
## How to run
10+
- open you command line and navigate to the path of the file
11+
- do a pip install curses && pip install random
12+
- then type python snake.py to run
13+
14+
## The game in action is like this:
15+
16+
<img src="https://github.com/oyerohabib/Python-project-Scripts/blob/oyerohabib-2/SNAKE_GAME/snake-game.png">

SNAKE_GAME/snake-game.png

11.2 KB
Loading

SNAKE_GAME/snake.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import random
2+
import curses
3+
4+
def play_game():
5+
s = curses.initscr()
6+
curses.curs_set(0)
7+
sh, sw = s.getmaxyx()
8+
w = curses.newwin(sh, sw, 0, 0)
9+
w.keypad(1)
10+
w.timeout(100)
11+
12+
snk_x = sw/4
13+
snk_y = sh/2
14+
snake = [
15+
[snk_y, snk_x],
16+
[snk_y, snk_x-1],
17+
[snk_y, snk_x-2]
18+
]
19+
20+
food = [sh/2, sw/2]
21+
w.addch(int(food[0]), int(food[1]), curses.ACS_PI)
22+
23+
key = curses.KEY_RIGHT
24+
25+
while True:
26+
next_key = w.getch()
27+
key = key if next_key == -1 else next_key
28+
29+
if snake[0][0] in [0, sh] or snake[0][1] in [0, sw] or snake[0] in snake[1:]:
30+
curses.endwin()
31+
quit()
32+
33+
new_head = [snake[0][0], snake[0][1]]
34+
35+
if key == curses.KEY_DOWN:
36+
new_head[0] += 1
37+
if key == curses.KEY_UP:
38+
new_head[0] -= 1
39+
if key == curses.KEY_LEFT:
40+
new_head[1] -= 1
41+
if key == curses.KEY_RIGHT:
42+
new_head[1] += 1
43+
44+
snake.insert(0, new_head)
45+
46+
if snake[0] == food:
47+
food = None
48+
while food is None:
49+
nf = [
50+
random.randint(1, sh-1),
51+
random.randint(1, sw-1)
52+
]
53+
food = nf if nf not in snake else None
54+
w.addch(food[0], food[1], curses.ACS_PI)
55+
else:
56+
tail = snake.pop()
57+
w.addch(int(tail[0]), int(tail[1]), ' ')
58+
59+
w.addch(int(snake[0][0]), int(snake[0][1]), curses.ACS_CKBOARD)
60+
61+
user_input = input("Do you want to play the snake game, type 'yes' or 'no': ").lower()
62+
63+
if user_input == "yes":
64+
play_game()
65+
else:
66+
quit()

0 commit comments

Comments
 (0)