ClearCode

Snake

Apr 1st, 2022
2,746
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.21 KB | None | 0 0
  1. import PySimpleGUI as sg
  2. from time import time
  3. from random import randint
  4.  
  5. def convert_pos_to_pixel(cell):
  6.     tl = cell[0] * CELL_SIZE, cell[1] * CELL_SIZE
  7.     br = tl[0] + CELL_SIZE, tl[1] + CELL_SIZE
  8.     return tl, br
  9.  
  10. def place_apple():
  11.     apple_pos = randint(0,CELL_NUM - 1), randint(0,CELL_NUM - 1)
  12.     while apple_pos in snake_body:
  13.         apple_pos = randint(0,CELL_NUM - 1), randint(0,CELL_NUM - 1)
  14.     return apple_pos
  15.  
  16. # game constants
  17. FIELD_SIZE = 400
  18. CELL_NUM = 10
  19. CELL_SIZE = FIELD_SIZE / CELL_NUM
  20.  
  21. # snake
  22. snake_body = [(4,4),(3,4),(2,4)]
  23. DIRECTIONS = {'left': (-1,0),'right': (1,0), 'up':(0,1), 'down':(0,-1)}
  24. direction = DIRECTIONS['up']
  25.  
  26. # apple
  27. apple_pos = place_apple()
  28. apple_eaten = False
  29.  
  30. sg.theme('Green')
  31. field = sg.Graph(
  32.     canvas_size = (FIELD_SIZE,FIELD_SIZE),
  33.     graph_bottom_left = (0,0),
  34.     graph_top_right = (FIELD_SIZE,FIELD_SIZE),
  35.     background_color = 'black')
  36. layout = [[field]]
  37.  
  38. window = sg.Window('Snake', layout,return_keyboard_events = True)
  39.  
  40. start_time = time()
  41. while True:
  42.     event, values = window.read(timeout = 10)
  43.     if event == sg.WIN_CLOSED: break
  44.     if event == 'Left:37': direction = DIRECTIONS['left']
  45.     if event == 'Up:38': direction = DIRECTIONS['up']
  46.     if event == 'Right:39': direction = DIRECTIONS['right']
  47.     if event == 'Down:40': direction = DIRECTIONS['down']
  48.  
  49.     time_since_start = time() - start_time
  50.     if time_since_start >= 0.5:
  51.         start_time = time()
  52.  
  53.         # apple snake collision
  54.         if snake_body[0] == apple_pos:
  55.             apple_pos = place_apple()
  56.             apple_eaten = True
  57.  
  58.         # snake update
  59.         new_head = (snake_body[0][0] + direction[0],snake_body[0][1] + direction[1])
  60.         snake_body.insert(0,new_head)
  61.         if not apple_eaten:
  62.             snake_body.pop()
  63.         apple_eaten = False
  64.  
  65.         # check death
  66.         if not 0 <= snake_body[0][0] <= CELL_NUM - 1 or \
  67.            not 0 <= snake_body[0][1] <= CELL_NUM - 1 or \
  68.            snake_body[0] in snake_body[1:]:
  69.            break
  70.  
  71.         field.DrawRectangle((0,0),(FIELD_SIZE,FIELD_SIZE), 'black')
  72.  
  73.         tl, br = convert_pos_to_pixel(apple_pos)
  74.         field.DrawRectangle(tl,br,'red')
  75.         # draw snake
  76.         for index, part in enumerate(snake_body):
  77.             tl, br = convert_pos_to_pixel(part)
  78.             color = 'yellow' if index == 0 else 'green'
  79.             field.DrawRectangle(tl,br,color)
  80.        
  81. window.close()
Advertisement
Add Comment
Please, Sign In to add comment