Chl_Snt

Snake main.py

Jul 26th, 2023
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.09 KB | None | 0 0
  1. import os
  2. import random
  3.  
  4. import pygame
  5. import pygame_menu.themes
  6. import menu
  7.  
  8. pygame.init()
  9. WIDTH = 800
  10. HEIGHT = 600
  11. FPS = 10
  12. screen = pygame.display.set_mode((WIDTH, HEIGHT))
  13. pygame.display.set_caption("Змейка")
  14. clock = pygame.time.Clock()
  15. state = "menu"
  16.  
  17. snake_list = []
  18. x1 = WIDTH / 2
  19. y1 = HEIGHT / 2
  20. snake_block = 40
  21. snake_step = 30
  22.  
  23. x1_change = 0
  24. y1_change = 0
  25. length = 1
  26. i = 0
  27.  
  28. foodx = random.randrange(0, WIDTH - snake_block)
  29. foody = random.randrange(0, HEIGHT - snake_block)
  30.  
  31. label_id = None
  32.  
  33. bg = pygame.mixer.Sound("music/background.mp3")
  34. eat = pygame.mixer.Sound("music/eat.mp3")
  35. game_over = pygame.mixer.Sound("music/gameover.mp3")
  36. bg.set_volume(0.3)
  37.  
  38. bg_image = pygame.image.load("img/bg.png")
  39. bg_image = pygame.transform.scale(bg_image, (WIDTH, HEIGHT))
  40. bg_rect = bg_image.get_rect()
  41.  
  42. names = os.listdir("img/food")
  43. foods = []
  44. for food in names:
  45.     foods.append(pygame.image.load(f"img/food/{food}"))
  46.  
  47. food = pygame.transform.scale(random.choice(foods), (snake_block, snake_block))
  48. food_rect = food.get_rect(x=foodx, y=foody)
  49.  
  50. head_sprites = [
  51.     pygame.image.load("img/snake/HeadR.png"),
  52.     pygame.image.load("img/snake/HeadL.png"),
  53.     pygame.image.load("img/snake/HeadB.png"),
  54.     pygame.image.load("img/snake/HeadT.png")
  55. ]
  56.  
  57. tail_sprites = [
  58.     pygame.image.load("img/snake/tailright.png"),
  59.     pygame.image.load("img/snake/tailleft.png"),
  60.     pygame.image.load("img/snake/taildown.png"),
  61.     pygame.image.load("img/snake/tailup.png")
  62. ]
  63.  
  64. def disable():
  65.     global state
  66.     mainmenu.disable()
  67.     state = "game"
  68.     bg.play(loops=-1)
  69.  
  70.  
  71. def eating_check(xcor, ycor, foodx, foody):
  72.     if foodx - snake_block <= xcor <= foodx + snake_block:
  73.         if foody - snake_block <= ycor <= foody + snake_block:
  74.             eat.play()
  75.             return True
  76.     else:
  77.         return False
  78.  
  79.  
  80. def new_game():
  81.     global foodx, foody, food, food_rect
  82.     global x1_change, y1_change, length, x1, y1
  83.     x1_change = 0
  84.     y1_change = 0
  85.     length = 1
  86.     snake_list.clear()
  87.     x1 = WIDTH / 2
  88.     y1 = HEIGHT / 2
  89.     foodx = random.randrange(0, WIDTH - snake_block)
  90.     foody = random.randrange(0, HEIGHT - snake_block)
  91.     food = pygame.transform.scale(random.choice(foods), (snake_block, snake_block))
  92.     food_rect = food.get_rect(x=foodx, y=foody)
  93.  
  94.  
  95. def loose():
  96.     global play_btn, label_id
  97.     bg.stop()
  98.     game_over.play()
  99.     try:
  100.         label = mainmenu.get_widget(label_id)
  101.         label.set_title(f"Счёт: {length}")
  102.     except:
  103.         label = mainmenu.add.label(f"Счёт: {length}")
  104.         label_id = label.get_id()
  105.         mainmenu.move_widget_index(label, 0)
  106.  
  107.     mainmenu.set_title("ПРОИГРАЛ")
  108.     play_btn.set_title('Играть заново')
  109.     new_game()
  110.     mainmenu.enable()
  111.  
  112.  
  113. def create_mes(msg, color, x, y, font, size):
  114.     font_style = pygame.font.SysFont(font, size)
  115.     mes = font_style.render(msg, True, color)
  116.     screen.blit(mes, [x, y])
  117.  
  118.  
  119. def draw_head(i, snake_list):
  120.     snake_head_img = head_sprites[i]
  121.     snake_head = pygame.transform.scale(snake_head_img, (snake_block, snake_block))
  122.     snake_head_rect = snake_head.get_rect(x=snake_list[-1][0],
  123.                                           y=snake_list[-1][1])
  124.     screen.blit(snake_head, snake_head_rect)
  125.  
  126.  
  127. def draw_tail(i, snake_list):
  128.     snake_tail_img = tail_sprites[i]
  129.     snake_tail = pygame.transform.scale(snake_tail_img, (snake_block, snake_block))
  130.     snake_tail.set_colorkey("white")
  131.     snake_tail_rect = snake_tail.get_rect(x=snake_list[0][0], y=snake_list[0][1])
  132.     screen.blit(snake_tail, snake_tail_rect)
  133.  
  134.  
  135. mainmenu = menu.Menu(screen, pygame_menu.themes.THEME_DARK)
  136. play_btn = mainmenu.add.button('Играть', disable)
  137. mainmenu.add.range_slider("Сложность", 1, (1, 2, 3, 4, 5))
  138. mainmenu.add.button('Выход', quit)
  139.  
  140. run = True
  141. while run:
  142.     clock.tick(FPS)
  143.     events = pygame.event.get()
  144.     for event in events:
  145.         if event.type == pygame.QUIT:
  146.             run = False
  147.         elif event.type == pygame.KEYDOWN:
  148.             if event.key == pygame.K_m:
  149.                 state = "menu"
  150.                 mainmenu.enable()
  151.  
  152.             if event.key == pygame.K_LEFT:
  153.                 x1_change = -snake_step
  154.                 y1_change = 0
  155.                 i = 1
  156.             elif event.key == pygame.K_RIGHT:
  157.                 x1_change = snake_step
  158.                 y1_change = 0
  159.                 i = 0
  160.             elif event.key == pygame.K_UP:
  161.                 y1_change = -snake_step
  162.                 x1_change = 0
  163.                 i = 3
  164.             elif event.key == pygame.K_DOWN:
  165.                 y1_change = snake_step
  166.                 x1_change = 0
  167.                 i = 2
  168.  
  169.     x1 += x1_change
  170.     y1 += y1_change
  171.     # screen.fill("white")
  172.     screen.blit(bg_image, bg_rect)  # Фон
  173.     create_mes(f"Счёт: {length}", "white", 20, 20, "Comic Sans", 35)
  174.     # pygame.draw.rect(screen, "green", [foodx, foody, snake_block, snake_block])  # Еда
  175.  
  176.     screen.blit(food, food_rect)
  177.     snake_head = [x1, y1]
  178.     snake_list.append(snake_head)
  179.     if len(snake_list) > length:
  180.         del snake_list[0]
  181.     for x in snake_list[0:]:
  182.         # pygame.draw.rect(screen, "black", [x[0], x[1], snake_block, snake_block])
  183.         snake_img = pygame.image.load('img/snake/body.png')
  184.         snake = pygame.transform.scale(snake_img, (snake_block, snake_block))
  185.         snake.set_colorkey("white")
  186.         screen.blit(snake, (x[0], x[1]))
  187.  
  188.     draw_head(i, snake_list)
  189.     if length > 1:
  190.         draw_tail(i, snake_list)
  191.  
  192.     if eating_check(x1, y1, foodx, foody):
  193.         foodx = random.randrange(0, WIDTH - snake_block)
  194.         foody = random.randrange(0, HEIGHT - snake_block)
  195.         food = pygame.transform.scale(random.choice(foods), (snake_block, snake_block))
  196.         food_rect = food.get_rect(x=foodx, y=foody)
  197.         length += 1
  198.  
  199.     for x in snake_list[:-1]:
  200.         if x == snake_head:
  201.             loose()
  202.  
  203.     if x1 <= 0 or x1 >= WIDTH:
  204.         loose()
  205.     if y1 <= 0 or y1 >= HEIGHT:
  206.         loose()
  207.  
  208.     mainmenu.flip(events)
  209.     pygame.display.flip()
  210.  
  211. pygame.quit()
  212.  
Add Comment
Please, Sign In to add comment