Guest User

Untitled

a guest
Jun 24th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1. import pygame
  2. import random
  3. from time import sleep
  4. from pygame.locals import *
  5. BLACK = (0,0,0)
  6. WHITE = (255,255,255)
  7.  
  8. window_height = 600
  9. window_width = 800
  10. screen = pygame.display.set_mode((window_width, window_height))
  11. pygame.display.set_caption("Snake")
  12. fps = 10
  13. fps_clock = pygame.time.Clock()
  14. pygame.font.init()
  15. font = pygame.font.SysFont('Comic San MS', 25)
  16.  
  17.  
  18. class Food:
  19. def __init__(self,x,y):
  20. self.x = x
  21. self.y = y
  22.  
  23. def generate(self):
  24. apple = pygame.image.load('apple.png')
  25. apple = pygame.transform.scale(apple, (25,25))
  26. screen.blit(apple,(self.x, self.y))
  27. # pygame.draw.rect(screen, WHITE, pygame.Rect(self.x, self.y, 10, 10))
  28.  
  29. class Snake:
  30. def __init__(self, x, y, food_pos):
  31. self.cells =[(x-40,y),(x-20,y),(x,y)]
  32. self.dir_x = 1
  33. self.dir_y = 0
  34. self.food_pos = food_pos
  35.  
  36. def move(self):
  37. del self.cells[0]
  38. last_cell = self.cells[-1]
  39. self.cells.append((last_cell[0]+self.dir_x*20, last_cell[1]+self.dir_y*20))
  40.  
  41. def draw(self):
  42. head = pygame.image.load('snake.png')
  43. body = pygame.image.load('snake.png')
  44.  
  45. if self.dir_x == 1 and self.dir_y == 0:
  46. pass
  47. elif self.dir_x == -1 and self.dir_y == 0:
  48. head = pygame.transform.rotate(head, 180)
  49. body = pygame.transform.flip(body, True, False)
  50. elif self.dir_x == 0 and self.dir_y == 1:
  51. head = pygame.transform.rotate(head, -90)
  52. body = pygame.transform.rotate(body, -90)
  53. else:
  54. head = pygame.transform.rotate(head, 90)
  55. body = pygame.transform.rotate(body, 90)
  56.  
  57.  
  58. head = pygame.transform.scale(head, (18, 18))
  59. body = pygame.transform.scale(body, (18, 18))
  60.  
  61.  
  62.  
  63. for i in self.cells:
  64. if i != self.cells[-1]:
  65. screen.blit(body, (i[0], i[1]))
  66. else:
  67. screen.blit(head, (i[0], i[1]))
  68. # pygame.draw.rect(screen, WHITE, pygame.Rect(i[0],i[1],18,18))
  69.  
  70. def grow(self):
  71. self.cells.insert(0, self.cells[0])
  72.  
  73. def hit_ceiling(self):
  74. if self.cells[-1][1] <=0:
  75. return True
  76. else:
  77. return False
  78.  
  79. def hit_floor(self):
  80. if self.cells[-1][1] + 20 >= window_height:
  81. return True
  82. else:
  83. return False
  84.  
  85. def hit_wall(self):
  86. if self.cells[-1][0] <= 0 or self.cells[-1][0]+20 >= window_width:
  87. return True
  88. else:
  89. return False
  90.  
  91. class Score:
  92. def __init__(self):
  93. self.score = 0
  94.  
  95. def draw(self):
  96. textsurface = font.render('Your Score: {0}'.format(self.score), False, WHITE)
  97. screen.blit(textsurface, (window_width-150, 40))
  98.  
  99.  
  100. class Game:
  101. def __init__(self):
  102. self.q = 0
  103. snake_x = window_width/2
  104. snake_y = window_height/2
  105. x = 20 * random.randint(1, 19)
  106. y = 20 * random.randint(1, 14)
  107. self.food = Food(x, y)
  108. self.food.generate()
  109. self.snake = Snake(snake_x, snake_y, (self.food.x, self.food.y))
  110. self.score = Score()
  111.  
  112. def collision(self, x1, y1, x2, y2):
  113. if x2 < x1 < x2 + 20:
  114. if y2 < y1 < y2 + 20:
  115. return True
  116. else:
  117. return False
  118.  
  119. def draw(self):
  120. screen.fill(BLACK)
  121. img = pygame.image.load('background.jpg')
  122. img = pygame.transform.scale(img, (window_width-40, window_height-40))
  123.  
  124. screen.blit(img, (20,20))
  125. # pygame.draw.rect(screen, BLACK, (10, 10, window_width-20, window_height-20))
  126.  
  127. def update(self):
  128. self.draw()
  129. self.food.generate()
  130. self.snake.food_pos = (self.food.x, self.food.y)
  131. self.snake.move()
  132. self.snake.draw()
  133. if self.snake.hit_ceiling() or self.snake.hit_floor() or self.snake.hit_wall():
  134. self.q = 1
  135.  
  136.  
  137. for i in range(len(self.snake.cells)-1):
  138. if self.snake.cells[i] == self.snake.cells[-1]:
  139. self.q = 1
  140.  
  141. if self.collision(self.snake.cells[-1][0]+10, self.snake.cells[-1][1]+10, self.food.x, self.food.y):
  142. x = 20 * random.randint(1, 38)
  143. y = 20 * random.randint(1, 28)
  144. self.food = Food(x, y)
  145. self.snake.grow()
  146. self.food.generate()
  147. self.score.score += 1
  148. self.score.draw()
  149.  
  150.  
  151. def main():
  152. pygame.init()
  153. game = Game()
  154. flag = True
  155. while flag:
  156. for event in pygame.event.get():
  157. if event.type == KEYDOWN:
  158. if event.key == 273 and not game.snake.dir_y == 1:
  159. game.snake.dir_y = -1
  160. game.snake.dir_x = 0
  161. elif event.key == 274 and not game.snake.dir_y == -1:
  162. game.snake.dir_y = 1
  163. game.snake.dir_x = 0
  164. elif event.key == 276 and not game.snake.dir_x == 1:
  165. game.snake.dir_y = 0
  166. game.snake.dir_x = -1
  167. elif event.key == 275 and not game.snake.dir_x == -1:
  168. game.snake.dir_y = 0
  169. game.snake.dir_x = 1
  170. if event.type == QUIT:
  171. flag = False
  172. break
  173. game.update()
  174. if game.q == 1:
  175. screen.fill(BLACK)
  176. font = pygame.font.SysFont('Comic San MS', 35)
  177. gameover = font.render("Game Over", 1, WHITE)
  178. w = gameover.get_width()
  179. h = gameover.get_height()
  180. screen.blit(gameover, (window_width//2 - w//2, window_height//2 - h//2))
  181.  
  182. pygame.display.update()
  183. fps_clock.tick(fps)
  184.  
  185. if __name__ == '__main__':
  186. main()
Add Comment
Please, Sign In to add comment