Guest User

Untitled

a guest
Sep 12th, 2020
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. import pygame, sys, random
  2.  
  3. class snake(object):
  4. def __init__(self):
  5. self.length = 1
  6. self.positions = [((SCREEN_WIDTH / 2), (SCREEN_HEIGHT / 2))]
  7. self.direction = random.choice([UP, DOWN, LEFT, RIGHT])
  8. self.color = (17, 24, 37)
  9.  
  10. def get_head_position(self):
  11. return self.postitions[0]
  12.  
  13. def turn(self, point):
  14. if self.length > 1 and (point[0] * -1, point[1] * -1) == self.direction:
  15. return
  16. else:
  17. self.direction = point
  18.  
  19. def move(self):
  20. cur = self.get_head_position()
  21. x, y = self.direction()
  22. new = (((cur[0] + (x*GRIDSIZE)) % SCREEN_WIDTH), (cur[1] + (y*GRIDSIZE)) % SCREEN_HEIGHT)
  23. if len(self.positions) > 2 and new in self.positions[2]:
  24. self.reset()
  25. else:
  26. self.positions.insert(0, new)
  27. if len(self.positions) > self.length:
  28. self.positions.pop()
  29.  
  30. def reset(self):
  31. self.length = 1
  32. self.postitions = [((SCREEN_WIDTH / 2), (SCREEN_HEIGHT /2))]
  33. self.direction = random.choice(UP. DOWN, LEFT, RIGHT)
  34.  
  35. def draw(self, surface):
  36. for p in self.positions:
  37. r = pygame.Rect((p[0], p[1]), (GRIDSIZE, GRIDSIZE))
  38. pygame.draw.rect(surface, self.color, r)
  39. pygame.draw.rect(surface, (93, 216, 228), r, 1)
  40.  
  41. def handle_keys(self):
  42. for event in pygame.event.get():
  43. if event.type == pygame.QUIT:
  44. pygame.quit()
  45. sys.exit()
  46. elif event.type == pygame.KEYDOWN:
  47. if event.key == pygame.K_UP:
  48. self.turn(UP)
  49. elif event.key == pygame.K_DOWN:
  50. self.turn(DOWN)
  51. elif event.key == pygame.K_LEFT:
  52. self.turn(LEFT)
  53. elif event.key == pygame.K_RIGHT:
  54. self.turn(RIGHT)
  55.  
  56.  
  57. class food(object):
  58. def __init__(self):
  59. self.postition = (0, 0)
  60. self.color = (223, 163, 49)
  61. self.randomize_postition()
  62.  
  63. def randomize_postition(self):
  64. self.postition = (random.randit(0, GRID_WIDTH-1) * GRIDSIZE, random.radit(0, GRID_HEIGHT-1) * GRIDSIZE)
  65.  
  66. def draw(self, surface):
  67. r = pygame.Rect((self.position[0], self.postition[1]), (GRIDSIZE, GRIDSIZE))
  68. pygame.draw.rect(surface, self.color, r)
  69. pygame.draw.rect(surface, (93, 216, 228), r, 1)
  70.  
  71.  
  72. def drawGrid(surface):
  73. for y in range(0, int(GRID_HEIGHT))
  74. for x in range(0, int(GRID_WIDTH))
  75. if (x + y) % 2 == 0:
  76. r = pygame.Rect((x*GRIDSIZE, y*GRIDSIZE), (GRIDSIZE, GRIDSIZE))
  77. pygame.draw.rect(surface, (93, 216, 228), r)
  78. else:
  79. rr = pygame.Rect((x*GRIDSIZE, y*GRIDSIZE), (GRIDSIZE, GRIDSIZE))
  80. pygame.draw.rect(surface, 84, 194, 205), rr)
  81.  
  82. SCREEN_WIDTH = 480
  83. SCREEN_HEIGHT = 480
  84.  
  85. GRIDSIZE = 20
  86. GRID_WIDTH = SCREEN_HEIGHT / SCREEN_WIDTH
  87. GRID_HEIGHT = SCREEN_WIDTH / SCREEN_HEIGHT
  88.  
  89. UP = (0, 1)
  90. DOWN = (0, 1)
  91. LEFT = (-1, 0)
  92. RIGHT = (1, 0)
  93.  
  94. def main():
  95. pygame.init()
  96.  
  97. clock = pygame.time.Clock()
  98. screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), 0, 32)
  99.  
  100. surface = pygame.Surface(screen.get_size())
  101. surface = surface.convert()
  102. drawGrid(surface)
  103.  
  104. snake = snake()
  105. food = food()
  106.  
  107. score = 0
  108. While (True):
  109. clock.tick(10)
  110. snake.handle_keys()
  111. drawGrid(surface)
  112. snake.move()
  113. if snake.get_head_position() == food.position:
  114. snake.length += 1
  115. score += 1
  116. food.randomize_postition()
  117. snake.draw(surface)
  118. food.draw(surface)
  119. screen.blit(Surface, (0, 0))
  120. text = myfont.render('Score {0}'.format(score), 1, (0, 0, 0))
  121. screen.blit(text, (5, 10))
  122. pygame.display.update()
  123.  
  124. main()
  125.  
Advertisement
Add Comment
Please, Sign In to add comment