├── README.md ├── LICENSE └── snake_game_using_curses.py /README.md: -------------------------------------------------------------------------------- 1 | # Snake-Game-using-Python-Curses 2 | 3 | To know more about algorithm used in this code you can follow this blog https://theailearner.com/2019/03/10/snake-game-using-python-curses/ 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 kang & atul 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /snake_game_using_curses.py: -------------------------------------------------------------------------------- 1 | import random 2 | import curses 3 | import time 4 | 5 | #initialize screen 6 | sc = curses.initscr() 7 | h, w = sc.getmaxyx() 8 | win = curses.newwin(h, w, 0, 0) 9 | 10 | win.keypad(1) 11 | curses.curs_set(0) 12 | 13 | # Initial Snake and Apple position 14 | snake_head = [10,15] 15 | snake_position = [[15,10],[14,10],[13,10]] 16 | apple_position = [20,20] 17 | score = 0 18 | 19 | # display apple 20 | win.addch(apple_position[0], apple_position[1], curses.ACS_DIAMOND) 21 | 22 | prev_button_direction = 1 23 | button_direction = 1 24 | key = curses.KEY_RIGHT 25 | 26 | def collision_with_apple(score): 27 | apple_position = [random.randint(1,h-2),random.randint(1,w-2)] 28 | score += 1 29 | return apple_position, score 30 | 31 | def collision_with_boundaries(snake_head): 32 | if snake_head[0]>=h-1 or snake_head[0]<=0 or snake_head[1]>=w-1 or snake_head[1]<=0 : 33 | return 1 34 | else: 35 | return 0 36 | 37 | def collision_with_self(snake_position): 38 | snake_head = snake_position[0] 39 | if snake_head in snake_position[1:]: 40 | return 1 41 | else: 42 | return 0 43 | 44 | a = [] 45 | while True: 46 | win.border(0) 47 | win.timeout(100) 48 | 49 | next_key = win.getch() 50 | 51 | if next_key == -1: 52 | key = key 53 | else: 54 | key = next_key 55 | 56 | # 0-Left, 1-Right, 3-Up, 2-Down 57 | if key == curses.KEY_LEFT and prev_button_direction != 1: 58 | button_direction = 0 59 | elif key == curses.KEY_RIGHT and prev_button_direction != 0: 60 | button_direction = 1 61 | elif key == curses.KEY_UP and prev_button_direction != 2: 62 | button_direction = 3 63 | elif key == curses.KEY_DOWN and prev_button_direction != 3: 64 | button_direction = 2 65 | else: 66 | pass 67 | 68 | prev_button_direction = button_direction 69 | 70 | # Change the head position based on the button direction 71 | if button_direction == 1: 72 | snake_head[1] += 1 73 | elif button_direction == 0: 74 | snake_head[1] -= 1 75 | elif button_direction == 2: 76 | snake_head[0] += 1 77 | elif button_direction == 3: 78 | snake_head[0] -= 1 79 | 80 | # Increase Snake length on eating apple 81 | if snake_head == apple_position: 82 | apple_position, score = collision_with_apple(score) 83 | snake_position.insert(0, list(snake_head)) 84 | a.append(apple_position) 85 | win.addch(apple_position[0], apple_position[1], curses.ACS_DIAMOND) 86 | 87 | else: 88 | snake_position.insert(0, list(snake_head)) 89 | last = snake_position.pop() 90 | win.addch(last[0], last[1], ' ') 91 | 92 | # display snake 93 | win.addch(snake_position[0][0], snake_position[0][1], '#') 94 | 95 | # On collision kill the snake 96 | if collision_with_boundaries(snake_head) == 1 or collision_with_self(snake_position) == 1: 97 | break 98 | 99 | 100 | sc.addstr(10, 30, 'Your Score is: '+str(score)) 101 | sc.refresh() 102 | time.sleep(2) 103 | curses.endwin() 104 | print(a) 105 | print(w,h) --------------------------------------------------------------------------------