Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from random import randint, shuffle
- from copy import deepcopy
- from uuid import uuid1 as ranuuid
- scale = 10
- width, height = 64, 64
- pygame.init()
- win = pygame.display.set_mode((width * 10, height * 10))
- pygame.display.set_caption("Snek")
- def nothing():
- return randint(0, 3)
- class Snake:
- def __init__(self, x, y, color=None, controller=None):
- self.body = [(x, y)]
- self.direction = randint(0, 3)
- self.alive = True
- self.length = 3
- self.uuid = ranuuid()
- if color is None:
- color = [(255, 0, 0)]
- self.color = color
- self.is_player = controller is None
- self.controller = controller
- self.ticks = 0
- def tick(self):
- if len(self.body) == 0:
- return
- last_x, last_y = self.body[-1]
- # Dead
- if not self.alive:
- self.body.pop(0)
- # Right
- elif self.direction == 0:
- self.body.append((last_x + 1, last_y))
- # Up
- elif self.direction == 1:
- self.body.append((last_x, last_y - 1))
- # Left
- elif self.direction == 2:
- self.body.append((last_x - 1, last_y))
- # Down
- elif self.direction == 3:
- self.body.append((last_x, last_y + 1))
- else:
- print("Direction does not exist.")
- # Removes the oldest segments until it has reached the proper length
- while self.length < len(self.body):
- self.body.pop(0)
- self.ticks += 1
- def collision(self, snakes, apples):
- if len(self.body) == 0:
- return
- x, y = head = self.body[-1]
- # Collision with our own body
- if head in self.body[0:-1]:
- self.body.pop(len(self.body) - 1)
- self.alive = False
- # Collision with other snakes
- for snake in snakes:
- if snake != self and head in snake.body:
- self.body.pop(len(self.body) - 1)
- self.alive = False
- # Collisions with food
- for food in apples:
- if head == food:
- apples.remove(food)
- self.length += 1
- # Outside the map
- if not 0 <= x < width:
- self.body.pop(len(self.body) - 1)
- self.alive = False
- elif not 0 <= y < height:
- self.body.pop(len(self.body) - 1)
- self.alive = False
- def set_direction(self, direction):
- if direction % 2 != self.direction % 2:
- self.direction = direction % 4
- return True
- return False
- def get_head(self):
- return self.body[-1]
- def default_think(snake: Snake, snakes: list, apples: list):
- if not snake.alive:
- return snake.direction
- x, y = head = snake.get_head()
- dir = snake.direction
- filled_tiles = [] + snake.body
- for snek in snakes:
- if snek.alive and snek != snake and snek.uuid != snake.uuid:
- filled_tiles += snek.body
- head_x, head_y = snek.get_head()
- filled_tiles += [(head_x, head_y - 1), (head_x, head_y + 1), (head_x - 1, head_y), (head_x + 1, head_y)]
- for num in range(-1, 65):
- filled_tiles += [(-1, num), (64, num), (num, -1), (num, 64)]
- # for spot in filled_tiles:
- # pygame.draw.rect(win, (255, 0, 0), (spot[0] * scale, spot[1] * scale, scale, scale))
- # The four possible directions
- head_up, head_down = (x, y - 1), (x, y + 1)
- head_left, head_right = (x - 1, y), (x + 1, y)
- if dir == 0:
- x += 1
- if dir == 1:
- y -= 1
- if dir == 2:
- x -= 1
- if dir == 3:
- y += 1
- if (x, y) in filled_tiles or randint(1, 100) == 69:
- possible_directions = []
- if head_up not in filled_tiles:
- possible_directions.append(1)
- if head_down not in filled_tiles:
- possible_directions.append(3)
- if head_left not in filled_tiles:
- possible_directions.append(2)
- if head_right not in filled_tiles:
- possible_directions.append(0)
- shuffle(possible_directions)
- if len(possible_directions):
- return possible_directions[0]
- return dir
- def ben_think(snake: Snake, snakes: list, apples: list):
- # Hahahahaa you THOUGHT you would get to see my code!
- return snake.direction
- def draw_tile(x, y, color):
- pygame.draw.rect(win, color, (x * scale + 1, y * scale + 1, scale - 2, scale - 2))
- def ran_color(min_avg=0):
- r, g, b = randint(0, 255), randint(0, 255), randint(0, 255)
- while r + g + b / 3 < min_avg:
- r, g, b = randint(0, 255), randint(0, 255), randint(0, 255)
- return r, g, b
- def main():
- ticks = 0
- snakes, foods = [], [(10, 10)]
- def random_open_spot():
- filled = [] + [bod for bod in [snake.body for snake in snakes]] + foods
- ran = (randint(0, 64), randint(0, 64))
- while ran in filled:
- ran = (randint(0, 64), randint(0, 64))
- return ran
- # snakes.append(Snake(34, 34, snakes, foods, [(100, 0, 200), (0, 0, 200)]))
- for i in range(25):
- ran = random_open_spot()
- snakes.append(Snake(ran[0], ran[1], [ran_color(150) for _ in range(0, randint(0, 1) + 1)], default_think))
- max_foods = 1
- movement_events = []
- clock = pygame.time.Clock()
- running = True
- tps = 10
- # Game loop
- while running:
- keys = pygame.key.get_pressed()
- # Events
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- elif event.type == pygame.KEYDOWN:
- if event.key == pygame.K_ESCAPE:
- running = False
- if event.key == pygame.K_r:
- return True
- if event.key == pygame.K_c:
- foods.clear()
- if event.key in [pygame.K_w, pygame.K_a, pygame.K_s, pygame.K_d]:
- movement_events.append(event)
- # Movement
- if len(movement_events) > 0:
- event = movement_events.pop(0)
- key = event.key
- player_controlled = list(filter(lambda x: x.is_player, snakes))
- print("Players:", len(player_controlled))
- for snake in player_controlled:
- if key == pygame.K_d:
- print("d")
- snake.set_direction(0)
- elif key == pygame.K_w:
- snake.set_direction(1)
- elif key == pygame.K_a:
- snake.set_direction(2)
- elif key == pygame.K_s:
- snake.set_direction(3)
- win.fill((50, 50, 50))
- last_snakes = deepcopy(snakes)
- for snake in snakes:
- if snake.controller is not None:
- snake.set_direction(snake.controller(snake, last_snakes, foods))
- snake.tick()
- for snake in snakes:
- snake.collision(snakes, foods)
- while len(foods) < max_foods:
- foods.append((randint(0, width - 1), randint(0, height - 1)))
- # Drawing
- for snake in snakes:
- body, color = snake.body, snake.color
- for body_index, segment in enumerate(list(reversed(body))):
- draw_tile(segment[0], segment[1], color[body_index % len(color)])
- for food in foods:
- food_x, food_y = food
- draw_tile(food_x, food_y, (222, 0, 0))
- ticks += 1
- if ticks % 100 == 0:
- max_foods += 1
- # Makes the shorter snakes longer
- for snake in snakes:
- snake.length = int(snake.length * 1.1)
- pygame.display.flip()
- if keys[pygame.K_SPACE]:
- clock.tick(tps * 5)
- else:
- clock.tick(tps)
- if __name__ == '__main__':
- while True:
- if not main():
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement