diff --git a/snake.py b/snake.py index eedbab0..fc66010 100644 --- a/snake.py +++ b/snake.py @@ -2,18 +2,39 @@ import curses import random import time -def main(stdscr, start_length): +def main(stdscr, start_length, speed): # Setup curses curses.curs_set(0) # Hide cursor stdscr.nodelay(1) # Non-blocking getch - stdscr.timeout(100) # Wait 100ms for input + + w = 100 + if speed == 1: w = 250 + if speed == 2: w = 225 + if speed == 3: w = 200 + if speed == 4: w = 175 + if speed == 5: w = 150 + if speed == 6: w = 125 + if speed == 7: w = 100 + if speed == 8: w = 75 + if speed == 9: w = 50 + if speed == 10: w = 25 + + stdscr.timeout(w) # Wait 100ms for input + # if speed = 50 then timeout = 100 + # if speed = 100 then timeout = 50 + # if speed = 0 then timeout = 1000 # Snake and food characters head_char = '█' + #head_char = '■' + #head_char = '0' #head_char_up = '▄' #head_char_down = '▀' head_char_up = head_char_down = head_char body_char = '▓' + #body_char = 'Φ' + #body_char = '■' + #body_char = 'O' #body_char = '░' #body_char = '■' #food_char = '💗' @@ -102,7 +123,8 @@ def main(stdscr, start_length): if (new_head[0] in [0, h - 1] or new_head[1] in [0, w - 1] or new_head in snake): - break # Game over + key = stdscr.getch() + break snake.insert(0, new_head) @@ -128,17 +150,20 @@ def main(stdscr, start_length): stdscr.addch(food[0], food[1], food_char, curses.color_pair(2)) # Draw score - stdscr.addstr(0, 2, f' Length: {score} ', curses.color_pair(3)) + stdscr.addstr(0, 2, f' Length: {score} @ {speed}MPH ({w}x{h}) ', curses.color_pair(3)) stdscr.refresh() # Game Over screen - stdscr.clear() + #stdscr.clear() msg = f"GAME OVER! SCORE: {score}" stdscr.addstr(h // 2, (w - len(msg)) // 2, msg, curses.color_pair(3)) - stdscr.addstr(h // 2 + 1, (w - 18) // 2, "Press any key to exit", curses.color_pair(3)) + stdscr.addstr(h // 2 + 1, (w - 18) // 2, "Press Q to exit", curses.color_pair(3)) stdscr.nodelay(0) - stdscr.getch() + key = stdscr.getch() + while key != ord('q'): + key = stdscr.getch() + exit('bye') if __name__ == "__main__": try: @@ -148,5 +173,13 @@ if __name__ == "__main__": start_len = 3 except ValueError: start_len = 3 + + try: + speed = input("Enter snake speed (1 to 10, default 7): ") + speed = int(speed) if speed.strip() else 7 + if speed > 10: speed = 10 + if speed < 1: speed = 1 + except ValueError: + speed = 7 - curses.wrapper(main, start_len) + curses.wrapper(main, start_len, speed)