Advertisement
Guest User

snek.py

a guest
Oct 21st, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.14 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.     def tick(self):
  26.         last_x, last_y = self.body[-1]
  27.         # Right
  28.         if self.direction == 0:
  29.             self.body.append((last_x + 1, last_y))
  30.         # Up
  31.         elif self.direction == 1:
  32.             self.body.append((last_x, last_y - 1))
  33.         # Left
  34.         elif self.direction == 2:
  35.             self.body.append((last_x - 1, last_y))
  36.         # Down
  37.         elif self.direction == 3:
  38.             self.body.append((last_x, last_y + 1))
  39.         else:
  40.             print("Direction does not exist.")
  41.         # Removes the oldest segments until it has reached the proper length
  42.         while self.length < len(self.body):
  43.             self.body.pop(0)
  44.         self.collision()
  45.  
  46.     def collision(self):
  47.         x, y = head = self.body[-1]
  48.         # Collision with our own body
  49.         if head in self.body[0:-1]:
  50.             self.alive = False
  51.         # Collision with other snakes
  52.         for snake in self.snakes:
  53.             if snake != self and head in snake.body:
  54.                 self.alive = False
  55.         # Collisions with food
  56.         if head in self.apples:
  57.             self.length += 1
  58.         # Outside the map
  59.         if not 0 <= x < width:
  60.             self.alive = False
  61.         elif not 0 <= y < height:
  62.             self.alive = False
  63.  
  64.  
  65. def draw_tile(x, y, color):
  66.     pygame.draw.rect(win, color, (x * scale + 1, y * scale + 1, scale - 2, scale - 2))
  67.  
  68.  
  69. def main():
  70.     ticks = 0
  71.     snakes, foods = [], [(10, 10)]
  72.     max_foods = 1
  73.     key_presses = []
  74.  
  75.     running = True
  76.     # Game loop
  77.     while running:
  78.         # Ticking
  79.         for event in pygame.event.get():
  80.             if event.type == pygame.QUIT:
  81.                 running = False
  82.             elif event.type == pygame.KEYDOWN:
  83.                 key_presses.append(event)
  84.  
  85.         for snake in snakes:
  86.             snake.tick()
  87.  
  88.         while len(foods) < max_foods:
  89.             foods.append((randint(0, width - 1), randint(0, height - 1)))
  90.  
  91.         # Drawing
  92.         for snake in snakes:
  93.             body, color = snake.body, snake.color
  94.             for body_index, segment in enumerate(body):
  95.                 segment_x, segment_y = segment
  96.                 pygame.draw.rect(win, color[body_index % len(color)],
  97.                                  (segment_x * scale, segment_y * scale,
  98.                                   (segment_x + 1) * scale, (segment_y + 1) * scale))
  99.         for food in foods:
  100.             food_x, food_y = food
  101.             draw_tile(food_x, food_y, (222, 0, 0))
  102.         pygame.display.update()
  103.  
  104.  
  105. if __name__ == '__main__':
  106.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement