Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import random
- import pygame
- import pygame_menu.themes
- import menu
- pygame.init()
- WIDTH = 800
- HEIGHT = 600
- FPS = 10
- screen = pygame.display.set_mode((WIDTH, HEIGHT))
- pygame.display.set_caption("Змейка")
- clock = pygame.time.Clock()
- state = "menu"
- snake_list = []
- x1 = WIDTH / 2
- y1 = HEIGHT / 2
- snake_block = 40
- snake_step = 30
- x1_change = 0
- y1_change = 0
- length = 1
- i = 0
- foodx = random.randrange(0, WIDTH - snake_block)
- foody = random.randrange(0, HEIGHT - snake_block)
- label_id = None
- bg = pygame.mixer.Sound("music/background.mp3")
- eat = pygame.mixer.Sound("music/eat.mp3")
- game_over = pygame.mixer.Sound("music/gameover.mp3")
- bg.set_volume(0.3)
- bg_image = pygame.image.load("img/bg.png")
- bg_image = pygame.transform.scale(bg_image, (WIDTH, HEIGHT))
- bg_rect = bg_image.get_rect()
- names = os.listdir("img/food")
- foods = []
- for food in names:
- foods.append(pygame.image.load(f"img/food/{food}"))
- food = pygame.transform.scale(random.choice(foods), (snake_block, snake_block))
- food_rect = food.get_rect(x=foodx, y=foody)
- head_sprites = [
- pygame.image.load("img/snake/HeadR.png"),
- pygame.image.load("img/snake/HeadL.png"),
- pygame.image.load("img/snake/HeadB.png"),
- pygame.image.load("img/snake/HeadT.png")
- ]
- tail_sprites = [
- pygame.image.load("img/snake/tailright.png"),
- pygame.image.load("img/snake/tailleft.png"),
- pygame.image.load("img/snake/taildown.png"),
- pygame.image.load("img/snake/tailup.png")
- ]
- def disable():
- global state
- mainmenu.disable()
- state = "game"
- bg.play(loops=-1)
- def eating_check(xcor, ycor, foodx, foody):
- if foodx - snake_block <= xcor <= foodx + snake_block:
- if foody - snake_block <= ycor <= foody + snake_block:
- eat.play()
- return True
- else:
- return False
- def new_game():
- global foodx, foody, food, food_rect
- global x1_change, y1_change, length, x1, y1
- x1_change = 0
- y1_change = 0
- length = 1
- snake_list.clear()
- x1 = WIDTH / 2
- y1 = HEIGHT / 2
- foodx = random.randrange(0, WIDTH - snake_block)
- foody = random.randrange(0, HEIGHT - snake_block)
- food = pygame.transform.scale(random.choice(foods), (snake_block, snake_block))
- food_rect = food.get_rect(x=foodx, y=foody)
- def loose():
- global play_btn, label_id
- bg.stop()
- game_over.play()
- try:
- label = mainmenu.get_widget(label_id)
- label.set_title(f"Счёт: {length}")
- except:
- label = mainmenu.add.label(f"Счёт: {length}")
- label_id = label.get_id()
- mainmenu.move_widget_index(label, 0)
- mainmenu.set_title("ПРОИГРАЛ")
- play_btn.set_title('Играть заново')
- new_game()
- mainmenu.enable()
- def create_mes(msg, color, x, y, font, size):
- font_style = pygame.font.SysFont(font, size)
- mes = font_style.render(msg, True, color)
- screen.blit(mes, [x, y])
- def draw_head(i, snake_list):
- snake_head_img = head_sprites[i]
- snake_head = pygame.transform.scale(snake_head_img, (snake_block, snake_block))
- snake_head_rect = snake_head.get_rect(x=snake_list[-1][0],
- y=snake_list[-1][1])
- screen.blit(snake_head, snake_head_rect)
- def draw_tail(i, snake_list):
- snake_tail_img = tail_sprites[i]
- snake_tail = pygame.transform.scale(snake_tail_img, (snake_block, snake_block))
- snake_tail.set_colorkey("white")
- snake_tail_rect = snake_tail.get_rect(x=snake_list[0][0], y=snake_list[0][1])
- screen.blit(snake_tail, snake_tail_rect)
- mainmenu = menu.Menu(screen, pygame_menu.themes.THEME_DARK)
- play_btn = mainmenu.add.button('Играть', disable)
- mainmenu.add.range_slider("Сложность", 1, (1, 2, 3, 4, 5))
- mainmenu.add.button('Выход', quit)
- run = True
- while run:
- clock.tick(FPS)
- events = pygame.event.get()
- for event in events:
- if event.type == pygame.QUIT:
- run = False
- elif event.type == pygame.KEYDOWN:
- if event.key == pygame.K_m:
- state = "menu"
- mainmenu.enable()
- if event.key == pygame.K_LEFT:
- x1_change = -snake_step
- y1_change = 0
- i = 1
- elif event.key == pygame.K_RIGHT:
- x1_change = snake_step
- y1_change = 0
- i = 0
- elif event.key == pygame.K_UP:
- y1_change = -snake_step
- x1_change = 0
- i = 3
- elif event.key == pygame.K_DOWN:
- y1_change = snake_step
- x1_change = 0
- i = 2
- x1 += x1_change
- y1 += y1_change
- # screen.fill("white")
- screen.blit(bg_image, bg_rect) # Фон
- create_mes(f"Счёт: {length}", "white", 20, 20, "Comic Sans", 35)
- # pygame.draw.rect(screen, "green", [foodx, foody, snake_block, snake_block]) # Еда
- screen.blit(food, food_rect)
- snake_head = [x1, y1]
- snake_list.append(snake_head)
- if len(snake_list) > length:
- del snake_list[0]
- for x in snake_list[0:]:
- # pygame.draw.rect(screen, "black", [x[0], x[1], snake_block, snake_block])
- snake_img = pygame.image.load('img/snake/body.png')
- snake = pygame.transform.scale(snake_img, (snake_block, snake_block))
- snake.set_colorkey("white")
- screen.blit(snake, (x[0], x[1]))
- draw_head(i, snake_list)
- if length > 1:
- draw_tail(i, snake_list)
- if eating_check(x1, y1, foodx, foody):
- foodx = random.randrange(0, WIDTH - snake_block)
- foody = random.randrange(0, HEIGHT - snake_block)
- food = pygame.transform.scale(random.choice(foods), (snake_block, snake_block))
- food_rect = food.get_rect(x=foodx, y=foody)
- length += 1
- for x in snake_list[:-1]:
- if x == snake_head:
- loose()
- if x1 <= 0 or x1 >= WIDTH:
- loose()
- if y1 <= 0 or y1 >= HEIGHT:
- loose()
- mainmenu.flip(events)
- pygame.display.flip()
- pygame.quit()
Add Comment
Please, Sign In to add comment