File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # Snake Game
2+
3+ This is a simple console-based ** Snake** game written in Python.
4+
5+ The game generates a snake which controled by ←↓↑→.
6+ Your task is to reach the π(or half π) and ovoid to hit the tail or the edge of the screen.
7+
8+ If the snake hits the π(or half π),the snake will be longer and another π will appear,the game continues.
9+ If the snake hit the tail or the edge of the screen, the game will exit and "give" you a error(Don't worry, this game doesn't have any problem).
10+
11+
12+ ---
13+
14+ ## How to Run
15+
16+ 1 . Make sure you have ** Python 3** installed.
17+
18+ 2 . Open a terminal and navigate to the folder where the script is located:
19+ ``` bash
20+ cd snake_game2
21+ # # Made by A (shipl1974, https:/github.com/shipl1974)
Original file line number Diff line number Diff line change 1+ # IF ERROR OF CURSOR THEN, Open CMD as Admin And TYPE-----> pip install windows-curses. please use CMD to run
2+ import random
3+ import curses
4+
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 (food [0 ],food [1 ], curses .ACS_PI )
22+
23+ key = curses .KEY_RIGHT
24+
25+
26+ while True :
27+ next_key = w .getch ()
28+ key = key if next_key == - 1 else next_key
29+
30+ if snake [0 ][0 ] in [0 ,sh ] or snake [0 ][1 ] in [0 , sw ] or snake [0 ] in snake [1 :]:
31+ curses .endwin ()
32+ quit ()
33+
34+ new_head = [snake [0 ][0 ], snake [0 ][1 ]]
35+
36+ if key == curses .KEY_DOWN :
37+ new_head [0 ]+= 1
38+ if key == curses .KEY_UP :
39+ new_head [0 ]-= 1
40+ if key == curses .KEY_LEFT :
41+ new_head [1 ]-= 1
42+ if key == curses .KEY_RIGHT :
43+ new_head [1 ]+= 1
44+
45+
46+ snake .insert (0 , new_head )
47+
48+ if snake [0 ] == food :
49+ food = None
50+ while food is None :
51+ nf = [
52+ random .randint (1 ,sh - 1 ),
53+ random .randint (1 , sw - 1 )
54+ ]
55+ food = nf if nf not in snake else None
56+ w .addch (food [0 ],food [1 ], curses .ACS_PI )
57+ else :
58+ tail = snake .pop ()
59+ w .addch (tail [0 ], tail [1 ], " " )
60+
61+ w .addch (snake [0 ][0 ], snake [0 ][1 ], curses .ACS_CKBOARD )
62+
You can’t perform that action at this time.
0 commit comments