Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. import pygame
  2. import sys
  3. import random
  4.  
  5. WINDOWS_HEIGHT = 500
  6. WINDOWS_WIDTH = 500
  7.  
  8. Score = 0
  9.  
  10. # цвета для объектов
  11. WHITE = (255, 255, 255)
  12. BLACK = (0, 0, 0)
  13. GRAY = (125, 125, 125)
  14. BLUE = (0, 0, 255)
  15. GREEN = (0, 255, 0)
  16. YELLOW = (225, 225, 0)
  17. PINK = (230, 50, 230)
  18. RED = (255, 0, 0)
  19.  
  20. DISPLAY = pygame.display.set_mode((WINDOWS_WIDTH, WINDOWS_HEIGHT))
  21.  
  22. # FPS
  23. FPS = 30
  24. fpsClock = pygame.time.Clock()
  25.  
  26. UP = (0, -1)
  27. DOWN = (0, 1)
  28. LEFT = (-1, 0)
  29. RIGHT = (1, 0)
  30.  
  31. SPEED = 2
  32. SIZE = 20
  33.  
  34. def add_part_of_snake(snake, snake_tail, head):
  35. update = [-SIZE * x for x in head]
  36. if len(snake_tail) == 0:
  37. snake_tail.append(snake.move(update[0], update[1]))
  38. else:
  39. snake_tail.append(snake_tail[len(snake_tail) - 1].move(update[0], update[1]))
  40.  
  41. def draw_snake(snake, snake_tail, head):
  42. tmp = snake.move(0, 0)
  43. head_speed = [SPEED * x for x in head]
  44. snake.move_ip(head_speed[0], head_speed[1])
  45. pygame.draw.rect(DISPLAY, GREEN, snake)
  46. if len(snake_tail) == 0:
  47. return
  48. pygame.draw.rect(DISPLAY, BLACK, snake_tail[len(snake_tail) -1])
  49. for i in range(0, len(snake_tail) -1):
  50. snake_tail[len(snake_tail) -1 -i] = snake_tail[len(snake_tail) -2 -i]
  51. pygame.draw.rect(DISPLAY, BLUE, snake_tail[len(snake_tail) -1 -i])
  52. snake_tail[0] = tmp
  53. pygame.draw.rect(DISPLAY, BLUE, snake_tail[0])
  54.  
  55. def make_new_apple():
  56. apple_x = random.randint(0, WINDOWS_WIDTH - SIZE)
  57. apple_y = random.randint(0, WINDOWS_HEIGHT - SIZE)
  58. apple = pygame.Rect(apple_x, apple_y, SIZE, SIZE)
  59. return apple
  60.  
  61. def main():
  62. snake = pygame.Rect(WINDOWS_WIDTH / 2, WINDOWS_WIDTH / 2, SIZE, SIZE)
  63. snake_tail = list()
  64. head = UP
  65. apple = make_new_apple()
  66. while True:
  67. for event in pygame.event.get():
  68. if event.type == pygame.QUIT:
  69. return
  70. if event.type == pygame.KEYDOWN:
  71. if event.key == pygame.K_LEFT and head != RIGHT:
  72. head = LEFT
  73. if event.key == pygame.K_RIGHT and head != LEFT:
  74. head = RIGHT
  75. if event.key == pygame.K_UP and head != DOWN:
  76. head = UP
  77. if event.key == pygame.K_DOWN and head != UP:
  78. head = DOWN
  79. if event.key == pygame.K_ESCAPE:
  80. return
  81. if event.type == pygame.MOUSEBUTTONDOWN:
  82. if event.button == 2:
  83. Score + 10
  84.  
  85. if snake.bottom > WINDOWS_HEIGHT or snake.top < 0 or snake.left < 0 or snake.right > WINDOWS_WIDTH:
  86. return
  87.  
  88. if snake.colliderect(apple):
  89. add_part_of_snake(snake, snake_tail, head)
  90. apple = make_new_apple()
  91.  
  92. DISPLAY.fill(BLACK)
  93. speed_head = [SPEED * x for x in head]
  94. snake.move_ip(speed_head[0], speed_head[1])
  95. pygame.draw.rect(DISPLAY, RED , apple)
  96. draw_snake(snake, snake_tail, head)
  97. pygame.display.update()
  98. fpsClock.tick(FPS)
  99.  
  100. main()
  101. pygame.quit()
  102. sys.exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement