Advertisement
anujpandey

snake-game

Oct 20th, 2023 (edited)
626
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.88 KB | Source Code | 0 0
  1. import random
  2. import curses
  3. import threading
  4. from curses import textpad
  5. import os
  6.  
  7. OPPOSITE_DIRECTION_DICT = {
  8.     curses.KEY_UP: curses.KEY_DOWN,
  9.     curses.KEY_DOWN: curses.KEY_UP,
  10.     curses.KEY_RIGHT: curses.KEY_LEFT,
  11.     curses.KEY_LEFT: curses.KEY_RIGHT
  12. }
  13.  
  14. DIRECTIONS_LIST = [curses.KEY_RIGHT, curses.KEY_LEFT, curses.KEY_DOWN, curses.KEY_UP]
  15. SNAKE = '#'
  16. FOOD = '@'
  17. BLANK = ' '
  18.  
  19.  
  20. def beep():
  21.     def beep_sound():
  22.         os.system('afplay /System/Library/Sounds/Sosumi.aiff')
  23.  
  24.     t = threading.Thread(target=beep_sound)
  25.     t.start()
  26.  
  27.  
  28. def create_food(snake, box):
  29.     """Simple function to find coordinates of food which is inside box and not on snake body"""
  30.     food = None
  31.     while food is None:
  32.         food = [random.randint(box[0][0] + 1, box[1][0] - 1),
  33.                 random.randint(box[0][1] + 1, box[1][1] - 1)]
  34.         if food in snake:
  35.             food = None
  36.     return food
  37.  
  38.  
  39. def main():
  40.     # initial settings
  41.     std_scr = curses.initscr()
  42.     curses.curs_set(0)
  43.     curses.noecho()
  44.     curses.cbreak()
  45.  
  46.     global FOOD
  47.     global SNAKE
  48.     FOOD = curses.ACS_PI
  49.     SNAKE = curses.ACS_CKBOARD
  50.     std_scr.keypad(True)
  51.     std_scr.nodelay(True)
  52.     std_scr.timeout(100)
  53.  
  54.     # create a game box
  55.     sh, sw = std_scr.getmaxyx()
  56.     box = [[3, 3], [sh - 3, sw - 3]]  # [[ul_y, ul_x], [dr_y, dr_x]]
  57.     textpad.rectangle(std_scr, box[0][0], box[0][1], box[1][0], box[1][1])
  58.  
  59.     # create snake and set initial direction
  60.     snake = [[sh // 2, sw // 2 + 1], [sh // 2, sw // 2], [sh // 2, sw // 2 - 1]]
  61.     direction = curses.KEY_RIGHT
  62.  
  63.     # draw snake
  64.     for y, x in snake:
  65.         std_scr.addch(y, x, SNAKE)
  66.  
  67.     # create food
  68.     food = create_food(snake, box)
  69.     std_scr.addch(food[0], food[1], FOOD)
  70.  
  71.     # print score
  72.     score = 0
  73.     score_text = "Score: {}".format(score)
  74.     std_scr.addstr(1, sw // 2 - len(score_text) // 2, score_text)
  75.  
  76.     while 1:
  77.         # non-blocking input
  78.         key = std_scr.getch()
  79.  
  80.         # set direction if user pressed any arrow key and that key is not opposite of current direction
  81.         if key in DIRECTIONS_LIST and key != OPPOSITE_DIRECTION_DICT[direction]:
  82.             direction = key
  83.  
  84.         # find next position of snake head
  85.         head = snake[0]
  86.         new_head = [head[0], head[1] + 1]
  87.         if direction == curses.KEY_LEFT:
  88.             new_head = [head[0], head[1] - 1]
  89.         elif direction == curses.KEY_DOWN:
  90.             new_head = [head[0] + 1, head[1]]
  91.         elif direction == curses.KEY_UP:
  92.             new_head = [head[0] - 1, head[1]]
  93.  
  94.         # insert and print new head
  95.         std_scr.addch(new_head[0], new_head[1], SNAKE)
  96.         snake.insert(0, new_head)
  97.  
  98.         # if snake head is on food
  99.         if snake[0] == food:
  100.             # update score
  101.             curses.beep()
  102.             beep()
  103.             curses.flash()
  104.             score += 1
  105.             score_text = "Score: {}".format(score)
  106.             std_scr.addstr(1, sw // 2 - len(score_text) // 2, score_text)
  107.  
  108.             # create new food
  109.             food = create_food(snake, box)
  110.             std_scr.addch(*food, FOOD)
  111.  
  112.             # increase speed of game
  113.             std_scr.timeout(100 - (len(snake) // 3) % 90)
  114.         else:
  115.             # shift snake's tail
  116.             std_scr.addstr(snake[-1][0], snake[-1][1], BLANK)
  117.             snake.pop()
  118.  
  119.         # conditions for game over
  120.         if (snake[0][0] in [box[0][0], box[1][0]] or
  121.                 snake[0][1] in [box[0][1], box[1][1]] or
  122.                 snake[0] in snake[1:]):
  123.             msg = "Game Over!"
  124.             std_scr.addstr(sh // 2, sw // 2 - len(msg) // 2, msg)
  125.             std_scr.nodelay(False)
  126.             std_scr.getch()
  127.             break
  128.  
  129.     curses.curs_set(1)
  130.     curses.nocbreak()
  131.     std_scr.keypad(False)
  132.     curses.echo()
  133.     curses.endwin()
  134.     quit()
  135.  
  136.  
  137. if __name__ == '__main__':
  138.     main()
  139.  
Tags: py
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement