Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from pygame import Rect, time
- from collections import deque
- import random
- class board:
- def __init__(self, width, height):
- self.width = width
- self.height = height
- self.board = [[0] * width for _ in range(height)]
- self.left = 10
- self.top = 10
- self.cell_size = 30
- def set_view(self, left, top, cell_size):
- self.left = left
- self.top = top
- self.cell_size = cell_size
- def render(self, snake, food1):
- for i in range(self.width):
- for j in range(self.height):
- food_re = []
- for f in food1:
- food_re.append([f.i, f.j, f.type])
- if [i, j] in snake.snake:
- pygame.draw.rect(screen, pygame.Color("white"), (
- self.left + self.cell_size * i, self.top + self.cell_size * j, self.cell_size, self.cell_size))
- elif [i, j, 1] in food_re:
- pygame.draw.rect(screen, pygame.Color("green"), (
- self.left + self.cell_size * i, self.top + self.cell_size * j, self.cell_size, self.cell_size))
- elif [i, j, 2] in food_re:
- pygame.draw.rect(screen, pygame.Color("red"), (
- self.left + self.cell_size * i, self.top + self.cell_size * j, self.cell_size, self.cell_size))
- else:
- pygame.draw.rect(screen, pygame.Color("black"), (
- self.left + self.cell_size * i, self.top + self.cell_size * j, self.cell_size, self.cell_size))
- class snake:
- def __init__(self):
- self.snake = deque()
- self.snake.append([13, 15])
- self.snake.append([14, 15])
- self.snake.append([15, 15])
- self.dir = 1
- def move(self):
- a = self.snake[-1]
- if self.dir == 1:
- self.snake.append([(a[0] + 1) % 30, a[1]])
- if self.dir == 2:
- self.snake.append([(a[0] - 1) % 30, a[1]])
- if self.dir == 3:
- self.snake.append([a[0], (a[1] + 1) % 30])
- if self.dir == 4:
- self.snake.append([a[0], (a[1] - 1) % 30])
- self.snake.popleft()
- if self.snake.count(self.snake[-1]) > 1:
- self.snake = deque()
- self.snake.append([13, 15])
- self.snake.append([14, 15])
- self.snake.append([15, 15])
- self.dir = 1
- def remake_dir(self, a):
- self.dir = a
- def get_dir(self):
- return self.dir
- class food:
- def __init__(self, type = 1):
- self.i = 0
- self.j = 0
- self.type = type
- def gen(self, snake):
- food_re = []
- for i in food1:
- food_re.append([i.i, i.j])
- not_in_snake = []
- for i in range(30):
- for j in range(30):
- if [i, j] not in snake.snake and [i, j] not in food_re:
- not_in_snake.append([i, j])
- self.i, self.j = random.choice(not_in_snake)
- def eat(self, snake):
- if self.type == 1:
- a, b = snake.snake[-1]
- if self.i == a and self.j - 1 == b and snake.dir == 3:
- snake.snake.append([self.i, self.j])
- self.gen(snake)
- elif self.i == a and self.j + 1 == b and snake.dir == 4:
- snake.snake.append([self.i, self.j])
- self.gen(snake)
- elif self.i - 1 == a and self.j == b and snake.dir == 1:
- snake.snake.append([self.i, self.j])
- self.gen(snake)
- elif self.i + 1 == a and self.j == b and snake.dir == 2:
- snake.snake.append([self.i, self.j])
- self.gen(snake)
- return snake
- elif len(snake.snake) > 0:
- a, b = snake.snake[-1]
- if self.i == a and self.j - 1 == b and snake.dir == 3:
- snake.snake.pop()
- self.gen(snake)
- elif self.i == a and self.j + 1 == b and snake.dir == 4:
- snake.snake.pop()
- self.gen(snake)
- elif self.i - 1 == a and self.j == b and snake.dir == 1:
- snake.snake.pop()
- self.gen(snake)
- elif self.i + 1 == a and self.j == b and snake.dir == 2:
- snake.snake.pop()
- self.gen(snake)
- return snake
- pygame.init()
- a = board(30, 30)
- b = snake()
- food1 = []
- food1.append(food(1))
- for i in range(20):
- food1.append(food(2))
- food1[i].gen(b)
- a.set_view(0, 0, 15)
- width = 450
- height = 450
- size = (width, height)
- screen = pygame.display.set_mode(size)
- clock = pygame.time.Clock()
- running = True
- while running:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- if event.type == pygame.KEYDOWN:
- a1 = b.get_dir()
- if event.key == pygame.K_LEFT and a1 != 1 and a1 != 2:
- b.remake_dir(2)
- elif event.key == pygame.K_UP and a1 != 3 and a1 != 4:
- b.remake_dir(4)
- elif event.key == pygame.K_RIGHT and a1 != 1 and a1 != 2:
- b.remake_dir(1)
- elif event.key == pygame.K_DOWN and a1 != 3 and a1 != 4:
- b.remake_dir(3)
- for i in food1:
- b = i.eat(b)
- print(len(b.snake))
- b.move()
- a.render(b, food1)
- clock.tick(10)
- pygame.display.flip()
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement