Advertisement
Blobadoodle

snake

Feb 23rd, 2021
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.14 KB | None | 0 0
  1. import pygame
  2. import numpy
  3. import random
  4. import math
  5. pygame.init()
  6. clock = pygame.time.Clock()
  7. screen = pygame.display.set_mode((377, 377))
  8. grid = numpy.zeros((15, 15), dtype="int8")
  9. for i in range(4):
  10.     grid[7][2+i] = 2
  11. apple_pos = [7, 11]
  12. grid[apple_pos[0]][apple_pos[1]] = 5
  13. snake = [[7, 2], [7, 5]]
  14. d = [2, 2]
  15. direction_list = []
  16. keys = [pygame.K_UP, pygame.K_RIGHT, pygame.K_DOWN, pygame.K_LEFT]
  17. colors = [(0, 150, 150), (0, 255, 0), (255, 0, 0)]
  18. frames = 0
  19. moving = False
  20. while True:
  21.     frames += 1
  22.     for event in pygame.event.get():
  23.         if event.type == pygame.QUIT:
  24.             quit()
  25.         if event.type == pygame.KEYDOWN:
  26.             for i in range(len(keys)):
  27.                 if event.key == keys[i]:
  28.                     moving = True
  29.                     direction_list.append(i+1)
  30.     if moving and frames >= 10:
  31.         frames = 0
  32.         if len(direction_list) > 0:
  33.             d[1] = direction_list[0]
  34.             direction_list.pop(0)
  35.         d[0] = grid[snake[0][0]][snake[0][1]]
  36.         last_pos = [snake[0][:], snake[1][:]]
  37.         for i in range(2):
  38.             snake[i][round((d[i] % 2-1)*-1)] += round(5/24*d[i]**4 - 25/12*d[i]**3 + 151/24*d[i]**2 - 65/12*d[i])
  39.             grid[last_pos[i][0]][last_pos[i][1]] = d[i] * i
  40.             if i:
  41.                 for n in range(2):
  42.                     if snake[i][n] < 0 or snake[i][n] > 14:
  43.                         quit()
  44.                 if 5 > grid[snake[i][0]][snake[i][1]] > 0:
  45.                     quit()
  46.                 if snake[i] == apple_pos:
  47.                     grid[last_pos[0][0]][last_pos[0][1]] = d[0]
  48.                     snake[0] = last_pos[0]
  49.                     while grid[apple_pos[0]][apple_pos[1]] != 0:
  50.                         apple_pos = [random.randint(0, 14), random.randint(0, 14)]
  51.                     grid[apple_pos[0]][apple_pos[1]] = 5
  52.                 grid[snake[i][0]][snake[i][1]] = d[1]
  53.     for row in range(len(grid)):
  54.         for pos in range(len(grid[row])):
  55.             pygame.draw.rect(screen, colors[round((math.log(grid[row][pos]+1))-0.15)], (25*pos + 2, 25*row + 2, 23, 23))
  56.     clock.tick(60)
  57.     pygame.display.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement