Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import sys
- import random
- WINDOWS_HEIGHT = 500
- WINDOWS_WIDTH = 500
- Score = 0
- # цвета для объектов
- WHITE = (255, 255, 255)
- BLACK = (0, 0, 0)
- GRAY = (125, 125, 125)
- BLUE = (0, 0, 255)
- GREEN = (0, 255, 0)
- YELLOW = (225, 225, 0)
- PINK = (230, 50, 230)
- RED = (255, 0, 0)
- DISPLAY = pygame.display.set_mode((WINDOWS_WIDTH, WINDOWS_HEIGHT))
- # FPS
- FPS = 30
- fpsClock = pygame.time.Clock()
- UP = (0, -1)
- DOWN = (0, 1)
- LEFT = (-1, 0)
- RIGHT = (1, 0)
- SPEED = 2
- SIZE = 20
- def add_part_of_snake(snake, snake_tail, head):
- update = [-SIZE * x for x in head]
- if len(snake_tail) == 0:
- snake_tail.append(snake.move(update[0], update[1]))
- else:
- snake_tail.append(snake_tail[len(snake_tail) - 1].move(update[0], update[1]))
- def draw_snake(snake, snake_tail, head):
- tmp = snake.move(0, 0)
- head_speed = [SPEED * x for x in head]
- snake.move_ip(head_speed[0], head_speed[1])
- pygame.draw.rect(DISPLAY, GREEN, snake)
- if len(snake_tail) == 0:
- return
- pygame.draw.rect(DISPLAY, BLACK, snake_tail[len(snake_tail) -1])
- for i in range(0, len(snake_tail) -1):
- snake_tail[len(snake_tail) -1 -i] = snake_tail[len(snake_tail) -2 -i]
- pygame.draw.rect(DISPLAY, BLUE, snake_tail[len(snake_tail) -1 -i])
- snake_tail[0] = tmp
- pygame.draw.rect(DISPLAY, BLUE, snake_tail[0])
- def make_new_apple():
- apple_x = random.randint(0, WINDOWS_WIDTH - SIZE)
- apple_y = random.randint(0, WINDOWS_HEIGHT - SIZE)
- apple = pygame.Rect(apple_x, apple_y, SIZE, SIZE)
- return apple
- def main():
- snake = pygame.Rect(WINDOWS_WIDTH / 2, WINDOWS_WIDTH / 2, SIZE, SIZE)
- snake_tail = list()
- head = UP
- apple = make_new_apple()
- while True:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- return
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_LEFT and head != RIGHT:
- head = LEFT
- if event.key == pygame.K_RIGHT and head != LEFT:
- head = RIGHT
- if event.key == pygame.K_UP and head != DOWN:
- head = UP
- if event.key == pygame.K_DOWN and head != UP:
- head = DOWN
- if event.key == pygame.K_ESCAPE:
- return
- if event.type == pygame.MOUSEBUTTONDOWN:
- if event.button == 2:
- Score + 10
- if snake.bottom > WINDOWS_HEIGHT or snake.top < 0 or snake.left < 0 or snake.right > WINDOWS_WIDTH:
- return
- if snake.colliderect(apple):
- add_part_of_snake(snake, snake_tail, head)
- apple = make_new_apple()
- DISPLAY.fill(BLACK)
- speed_head = [SPEED * x for x in head]
- snake.move_ip(speed_head[0], speed_head[1])
- pygame.draw.rect(DISPLAY, RED , apple)
- draw_snake(snake, snake_tail, head)
- pygame.display.update()
- fpsClock.tick(FPS)
- main()
- pygame.quit()
- sys.exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement