Advertisement
Bmorr

Snek Code 10/20/19

Oct 20th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.80 KB | None | 0 0
  1. import pygame
  2. from random import randint
  3.  
  4.  
  5. scale = 10
  6. width, height = 64, 64
  7. pygame.init()
  8. win = pygame.display.set_mode((width * 10, height * 10))
  9. pygame.display.set_caption("Snek")
  10.  
  11.  
  12. class Snake:
  13.     def __init__(self, x, y, snakes, apples, color=None, is_player=False):
  14.         self.body = [(x, y)]
  15.         self.direction = randint(0, 3)
  16.         self.alive = True
  17.         self.length = 3
  18.  
  19.         self.snakes, self.apples = snakes, apples
  20.         if color is None:
  21.             color = [(255, 0, 0)]
  22.         self.color = color
  23.         self.is_player = is_player
  24.  
  25.         self.ticks = 0
  26.  
  27.     def tick(self):
  28.         if len(self.body) == 0:
  29.             return
  30.         last_x, last_y = self.body[-1]
  31.         # Dead
  32.         if not self.alive:
  33.             self.body.pop(0)
  34.         # Right
  35.         elif self.direction == 0:
  36.             self.body.append((last_x + 1, last_y))
  37.         # Up
  38.         elif self.direction == 1:
  39.             self.body.append((last_x, last_y - 1))
  40.         # Left
  41.         elif self.direction == 2:
  42.             self.body.append((last_x - 1, last_y))
  43.         # Down
  44.         elif self.direction == 3:
  45.             self.body.append((last_x, last_y + 1))
  46.         else:
  47.             print("Direction does not exist.")
  48.         # Removes the oldest segments until it has reached the proper length
  49.         while self.length < len(self.body):
  50.             self.body.pop(0)
  51.         self.collision()
  52.         self.ticks += 1
  53.  
  54.     def collision(self):
  55.         if len(self.body) == 0:
  56.             return
  57.         x, y = head = self.body[-1]
  58.         # Collision with our own body
  59.         if head in self.body[0:-1]:
  60.             self.body.pop(len(self.body) - 1)
  61.             self.alive = False
  62.         # Collision with other snakes
  63.         for snake in self.snakes:
  64.             if snake != self and head in snake.body:
  65.                 self.body.pop(len(self.body) - 1)
  66.                 self.alive = False
  67.         # Collisions with food
  68.         for food in self.apples:
  69.             if head == food:
  70.                 self.apples.remove(food)
  71.                 self.length += 1
  72.         # Outside the map
  73.         if not 0 <= x < width:
  74.             self.body.pop(len(self.body) - 1)
  75.             self.alive = False
  76.         elif not 0 <= y < height:
  77.             self.body.pop(len(self.body) - 1)
  78.             self.alive = False
  79.  
  80.     def set_direction(self, direction):
  81.         if direction % 2 != self.direction % 2:
  82.             self.direction = direction
  83.             return True
  84.         return False
  85.  
  86.  
  87. def draw_tile(x, y, color):
  88.     pygame.draw.rect(win, color, (x * scale + 1, y * scale + 1, scale - 2, scale - 2))
  89.  
  90.  
  91. def main():
  92.     ticks = 0
  93.     snakes, foods = [], [(10, 10)]
  94.     snakes.append(Snake(randint(0, 64), 32, snakes, foods, [(100, 0, 200), (0, 0, 200)], is_player=True))
  95.     snakes.append(Snake(34, 34, snakes, foods, [(100, 0, 200), (0, 0, 200)], is_player=True))
  96.  
  97.     max_foods = 1
  98.     movement_events = []
  99.     clock = pygame.time.Clock()
  100.     running = True
  101.     # Game loop
  102.     while running:
  103.         # Events
  104.         for event in pygame.event.get():
  105.             if event.type == pygame.QUIT:
  106.                 running = False
  107.             elif event.type == pygame.KEYDOWN:
  108.                 if event.key == pygame.K_r:
  109.                     return True
  110.                 if event.key in [pygame.K_w, pygame.K_a, pygame.K_s, pygame.K_d]:
  111.                     movement_events.append(event)
  112.         # Movement
  113.         if len(movement_events) > 0:
  114.             event = movement_events.pop(0)
  115.             key = event.key
  116.             player_controlled = list(filter(lambda x: x.is_player, snakes))
  117.             print("Players:", len(player_controlled))
  118.             for snake in player_controlled:
  119.                 if key == pygame.K_d:
  120.                     print("d")
  121.                     snake.set_direction(0)
  122.                 elif key == pygame.K_w:
  123.                     snake.set_direction(1)
  124.                 elif key == pygame.K_a:
  125.                     snake.set_direction(2)
  126.                 elif key == pygame.K_s:
  127.                     snake.set_direction(3)
  128.  
  129.         for snake in snakes:
  130.             snake.tick()
  131.  
  132.         while len(foods) < max_foods:
  133.             foods.append((randint(0, width - 1), randint(0, height - 1)))
  134.  
  135.         # Drawing
  136.         win.fill((50, 50, 50))
  137.         for snake in snakes:
  138.             body, color = snake.body, snake.color
  139.             for body_index, segment in enumerate(list(reversed(body))):
  140.                 draw_tile(segment[0], segment[1], color[body_index % len(color)])
  141.         for food in foods:
  142.             food_x, food_y = food
  143.             draw_tile(food_x, food_y, (222, 0, 0))
  144.         ticks += 1
  145.         pygame.display.flip()
  146.         clock.tick(10)
  147.  
  148.  
  149. if __name__ == '__main__':
  150.     while True:
  151.         if not main():
  152.             break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement