Advertisement
Bmorr

Snake Royale

Oct 24th, 2019
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.93 KB | None | 0 0
  1. import pygame
  2. from random import randint, shuffle
  3. from copy import deepcopy
  4. from uuid import uuid1 as ranuuid
  5.  
  6.  
  7. scale = 10
  8. width, height = 64, 64
  9. pygame.init()
  10. win = pygame.display.set_mode((width * 10, height * 10))
  11. pygame.display.set_caption("Snek")
  12.  
  13. def nothing():
  14.     return randint(0, 3)
  15.  
  16. class Snake:
  17.     def __init__(self, x, y, color=None, controller=None):
  18.         self.body = [(x, y)]
  19.         self.direction = randint(0, 3)
  20.         self.alive = True
  21.         self.length = 3
  22.         self.uuid = ranuuid()
  23.  
  24.         if color is None:
  25.             color = [(255, 0, 0)]
  26.         self.color = color
  27.  
  28.         self.is_player = controller is None
  29.         self.controller = controller
  30.  
  31.         self.ticks = 0
  32.  
  33.     def tick(self):
  34.         if len(self.body) == 0:
  35.             return
  36.         last_x, last_y = self.body[-1]
  37.         # Dead
  38.         if not self.alive:
  39.             self.body.pop(0)
  40.         # Right
  41.         elif self.direction == 0:
  42.             self.body.append((last_x + 1, last_y))
  43.         # Up
  44.         elif self.direction == 1:
  45.             self.body.append((last_x, last_y - 1))
  46.         # Left
  47.         elif self.direction == 2:
  48.             self.body.append((last_x - 1, last_y))
  49.         # Down
  50.         elif self.direction == 3:
  51.             self.body.append((last_x, last_y + 1))
  52.         else:
  53.             print("Direction does not exist.")
  54.         # Removes the oldest segments until it has reached the proper length
  55.         while self.length < len(self.body):
  56.             self.body.pop(0)
  57.         self.ticks += 1
  58.  
  59.     def collision(self, snakes, apples):
  60.         if len(self.body) == 0:
  61.             return
  62.         x, y = head = self.body[-1]
  63.         # Collision with our own body
  64.         if head in self.body[0:-1]:
  65.             self.body.pop(len(self.body) - 1)
  66.             self.alive = False
  67.         # Collision with other snakes
  68.         for snake in snakes:
  69.             if snake != self and head in snake.body:
  70.                 self.body.pop(len(self.body) - 1)
  71.                 self.alive = False
  72.         # Collisions with food
  73.         for food in apples:
  74.             if head == food:
  75.                 apples.remove(food)
  76.                 self.length += 1
  77.         # Outside the map
  78.         if not 0 <= x < width:
  79.             self.body.pop(len(self.body) - 1)
  80.             self.alive = False
  81.         elif not 0 <= y < height:
  82.             self.body.pop(len(self.body) - 1)
  83.             self.alive = False
  84.  
  85.     def set_direction(self, direction):
  86.         if direction % 2 != self.direction % 2:
  87.             self.direction = direction % 4
  88.             return True
  89.         return False
  90.  
  91.     def get_head(self):
  92.         return self.body[-1]
  93.  
  94. def default_think(snake: Snake, snakes: list, apples: list):
  95.     if not snake.alive:
  96.         return snake.direction
  97.     x, y = head = snake.get_head()
  98.     dir = snake.direction
  99.  
  100.     filled_tiles = [] + snake.body
  101.     for snek in snakes:
  102.         if snek.alive and snek != snake and snek.uuid != snake.uuid:
  103.             filled_tiles += snek.body
  104.             head_x, head_y = snek.get_head()
  105.             filled_tiles += [(head_x, head_y - 1), (head_x, head_y + 1), (head_x - 1, head_y), (head_x + 1, head_y)]
  106.  
  107.     for num in range(-1, 65):
  108.         filled_tiles += [(-1, num), (64, num), (num, -1), (num, 64)]
  109.  
  110.     # for spot in filled_tiles:
  111.     #     pygame.draw.rect(win, (255, 0, 0), (spot[0] * scale, spot[1] * scale, scale, scale))
  112.  
  113.     # The four possible directions
  114.     head_up, head_down = (x, y - 1), (x, y + 1)
  115.     head_left, head_right = (x - 1, y), (x + 1, y)
  116.  
  117.     if dir == 0:
  118.         x += 1
  119.     if dir == 1:
  120.         y -= 1
  121.     if dir == 2:
  122.         x -= 1
  123.     if dir == 3:
  124.         y += 1
  125.  
  126.     if (x, y) in filled_tiles or randint(1, 100) == 69:
  127.         possible_directions = []
  128.         if head_up not in filled_tiles:
  129.             possible_directions.append(1)
  130.         if head_down not in filled_tiles:
  131.             possible_directions.append(3)
  132.         if head_left not in filled_tiles:
  133.             possible_directions.append(2)
  134.         if head_right not in filled_tiles:
  135.             possible_directions.append(0)
  136.  
  137.         shuffle(possible_directions)
  138.         if len(possible_directions):
  139.             return possible_directions[0]
  140.  
  141.     return dir
  142.  
  143.  
  144. def ben_think(snake: Snake, snakes: list, apples: list):
  145.     # Hahahahaa you THOUGHT you would get to see my code!
  146.     return snake.direction
  147.  
  148. def draw_tile(x, y, color):
  149.     pygame.draw.rect(win, color, (x * scale + 1, y * scale + 1, scale - 2, scale - 2))
  150.  
  151. def ran_color(min_avg=0):
  152.     r, g, b = randint(0, 255), randint(0, 255), randint(0, 255)
  153.     while r + g + b / 3 < min_avg:
  154.         r, g, b = randint(0, 255), randint(0, 255), randint(0, 255)
  155.     return r, g, b
  156.  
  157.  
  158. def main():
  159.     ticks = 0
  160.     snakes, foods = [], [(10, 10)]
  161.     def random_open_spot():
  162.         filled = [] + [bod for bod in [snake.body for snake in snakes]] + foods
  163.         ran = (randint(0, 64), randint(0, 64))
  164.         while ran in filled:
  165.             ran = (randint(0, 64), randint(0, 64))
  166.         return ran
  167.  
  168.     # snakes.append(Snake(34, 34, snakes, foods, [(100, 0, 200), (0, 0, 200)]))
  169.     for i in range(25):
  170.         ran = random_open_spot()
  171.         snakes.append(Snake(ran[0], ran[1], [ran_color(150) for _ in range(0, randint(0, 1) + 1)], default_think))
  172.  
  173.     max_foods = 1
  174.     movement_events = []
  175.     clock = pygame.time.Clock()
  176.     running = True
  177.  
  178.     tps = 10
  179.     # Game loop
  180.     while running:
  181.         keys = pygame.key.get_pressed()
  182.         # Events
  183.         for event in pygame.event.get():
  184.             if event.type == pygame.QUIT:
  185.                 running = False
  186.             elif event.type == pygame.KEYDOWN:
  187.                 if event.key == pygame.K_ESCAPE:
  188.                     running = False
  189.                 if event.key == pygame.K_r:
  190.                     return True
  191.                 if event.key == pygame.K_c:
  192.                     foods.clear()
  193.                 if event.key in [pygame.K_w, pygame.K_a, pygame.K_s, pygame.K_d]:
  194.                     movement_events.append(event)
  195.         # Movement
  196.         if len(movement_events) > 0:
  197.             event = movement_events.pop(0)
  198.             key = event.key
  199.             player_controlled = list(filter(lambda x: x.is_player, snakes))
  200.             print("Players:", len(player_controlled))
  201.             for snake in player_controlled:
  202.                 if key == pygame.K_d:
  203.                     print("d")
  204.                     snake.set_direction(0)
  205.                 elif key == pygame.K_w:
  206.                     snake.set_direction(1)
  207.                 elif key == pygame.K_a:
  208.                     snake.set_direction(2)
  209.                 elif key == pygame.K_s:
  210.                     snake.set_direction(3)
  211.         win.fill((50, 50, 50))
  212.         last_snakes = deepcopy(snakes)
  213.         for snake in snakes:
  214.             if snake.controller is not None:
  215.                 snake.set_direction(snake.controller(snake, last_snakes, foods))
  216.             snake.tick()
  217.  
  218.         for snake in snakes:
  219.             snake.collision(snakes, foods)
  220.  
  221.         while len(foods) < max_foods:
  222.             foods.append((randint(0, width - 1), randint(0, height - 1)))
  223.  
  224.         # Drawing
  225.         for snake in snakes:
  226.             body, color = snake.body, snake.color
  227.             for body_index, segment in enumerate(list(reversed(body))):
  228.                 draw_tile(segment[0], segment[1], color[body_index % len(color)])
  229.         for food in foods:
  230.             food_x, food_y = food
  231.             draw_tile(food_x, food_y, (222, 0, 0))
  232.         ticks += 1
  233.         if ticks % 100 == 0:
  234.             max_foods += 1
  235.             # Makes the shorter snakes longer
  236.             for snake in snakes:
  237.                 snake.length = int(snake.length * 1.1)
  238.         pygame.display.flip()
  239.         if keys[pygame.K_SPACE]:
  240.             clock.tick(tps * 5)
  241.         else:
  242.             clock.tick(tps)
  243.  
  244.  
  245. if __name__ == '__main__':
  246.     while True:
  247.         if not main():
  248.             break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement