Guest User

Untitled

a guest
Jan 17th, 2020
84
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pygame
  2.  
  3. from pygame import Rect, time
  4. from collections import deque
  5. import random
  6.  
  7.  
  8. class board:
  9. def __init__(self, width, height):
  10. self.width = width
  11. self.height = height
  12. self.board = [[0] * width for _ in range(height)]
  13. self.left = 10
  14. self.top = 10
  15. self.cell_size = 30
  16.  
  17. def set_view(self, left, top, cell_size):
  18. self.left = left
  19. self.top = top
  20. self.cell_size = cell_size
  21.  
  22. def render(self, snake, food1):
  23. for i in range(self.width):
  24. for j in range(self.height):
  25. food_re = []
  26. for f in food1:
  27. food_re.append([f.i, f.j, f.type])
  28. if [i, j] in snake.snake:
  29. pygame.draw.rect(screen, pygame.Color("white"), (
  30. self.left + self.cell_size * i, self.top + self.cell_size * j, self.cell_size, self.cell_size))
  31. elif [i, j, 1] in food_re:
  32. pygame.draw.rect(screen, pygame.Color("green"), (
  33. self.left + self.cell_size * i, self.top + self.cell_size * j, self.cell_size, self.cell_size))
  34. elif [i, j, 2] in food_re:
  35. pygame.draw.rect(screen, pygame.Color("red"), (
  36. self.left + self.cell_size * i, self.top + self.cell_size * j, self.cell_size, self.cell_size))
  37. else:
  38. pygame.draw.rect(screen, pygame.Color("black"), (
  39. self.left + self.cell_size * i, self.top + self.cell_size * j, self.cell_size, self.cell_size))
  40.  
  41.  
  42. class snake:
  43. def __init__(self):
  44. self.snake = deque()
  45. self.snake.append([13, 15])
  46. self.snake.append([14, 15])
  47. self.snake.append([15, 15])
  48. self.dir = 1
  49.  
  50. def move(self):
  51. a = self.snake[-1]
  52. if self.dir == 1:
  53. self.snake.append([(a[0] + 1) % 30, a[1]])
  54. if self.dir == 2:
  55. self.snake.append([(a[0] - 1) % 30, a[1]])
  56. if self.dir == 3:
  57. self.snake.append([a[0], (a[1] + 1) % 30])
  58. if self.dir == 4:
  59. self.snake.append([a[0], (a[1] - 1) % 30])
  60. self.snake.popleft()
  61. if self.snake.count(self.snake[-1]) > 1:
  62. self.snake = deque()
  63. self.snake.append([13, 15])
  64. self.snake.append([14, 15])
  65. self.snake.append([15, 15])
  66. self.dir = 1
  67.  
  68. def remake_dir(self, a):
  69. self.dir = a
  70.  
  71. def get_dir(self):
  72. return self.dir
  73.  
  74.  
  75. class food:
  76. def __init__(self, type = 1):
  77. self.i = 0
  78. self.j = 0
  79. self.type = type
  80.  
  81. def gen(self, snake):
  82. food_re = []
  83. for i in food1:
  84. food_re.append([i.i, i.j])
  85. not_in_snake = []
  86. for i in range(30):
  87. for j in range(30):
  88. if [i, j] not in snake.snake and [i, j] not in food_re:
  89. not_in_snake.append([i, j])
  90. self.i, self.j = random.choice(not_in_snake)
  91.  
  92. def eat(self, snake):
  93. if self.type == 1:
  94. a, b = snake.snake[-1]
  95. if self.i == a and self.j - 1 == b and snake.dir == 3:
  96. snake.snake.append([self.i, self.j])
  97. self.gen(snake)
  98. elif self.i == a and self.j + 1 == b and snake.dir == 4:
  99. snake.snake.append([self.i, self.j])
  100. self.gen(snake)
  101. elif self.i - 1 == a and self.j == b and snake.dir == 1:
  102. snake.snake.append([self.i, self.j])
  103. self.gen(snake)
  104. elif self.i + 1 == a and self.j == b and snake.dir == 2:
  105. snake.snake.append([self.i, self.j])
  106. self.gen(snake)
  107. return snake
  108. elif len(snake.snake) > 0:
  109. a, b = snake.snake[-1]
  110. if self.i == a and self.j - 1 == b and snake.dir == 3:
  111. snake.snake.pop()
  112. self.gen(snake)
  113. elif self.i == a and self.j + 1 == b and snake.dir == 4:
  114. snake.snake.pop()
  115. self.gen(snake)
  116. elif self.i - 1 == a and self.j == b and snake.dir == 1:
  117. snake.snake.pop()
  118. self.gen(snake)
  119. elif self.i + 1 == a and self.j == b and snake.dir == 2:
  120. snake.snake.pop()
  121. self.gen(snake)
  122. return snake
  123.  
  124. pygame.init()
  125. a = board(30, 30)
  126. b = snake()
  127. food1 = []
  128. food1.append(food(1))
  129. for i in range(20):
  130. food1.append(food(2))
  131. food1[i].gen(b)
  132. a.set_view(0, 0, 15)
  133. width = 450
  134. height = 450
  135. size = (width, height)
  136. screen = pygame.display.set_mode(size)
  137. clock = pygame.time.Clock()
  138. running = True
  139. while running:
  140. for event in pygame.event.get():
  141. if event.type == pygame.QUIT:
  142. running = False
  143. if event.type == pygame.KEYDOWN:
  144. a1 = b.get_dir()
  145. if event.key == pygame.K_LEFT and a1 != 1 and a1 != 2:
  146. b.remake_dir(2)
  147. elif event.key == pygame.K_UP and a1 != 3 and a1 != 4:
  148. b.remake_dir(4)
  149. elif event.key == pygame.K_RIGHT and a1 != 1 and a1 != 2:
  150. b.remake_dir(1)
  151. elif event.key == pygame.K_DOWN and a1 != 3 and a1 != 4:
  152. b.remake_dir(3)
  153. for i in food1:
  154. b = i.eat(b)
  155.  
  156. print(len(b.snake))
  157. b.move()
  158. a.render(b, food1)
  159. clock.tick(10)
  160. pygame.display.flip()
  161.  
  162. pygame.quit()
RAW Paste Data