Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from random import randint
- scale = 10
- width, height = 64, 64
- pygame.init()
- win = pygame.display.set_mode((width * 10, height * 10))
- pygame.display.set_caption("Snek")
- class Snake:
- def __init__(self, x, y, snakes, apples, color=None, is_player=False):
- self.body = [(x, y)]
- self.direction = randint(0, 3)
- self.alive = True
- self.length = 3
- self.snakes, self.apples = snakes, apples
- if color is None:
- color = [(255, 0, 0)]
- self.color = color
- self.is_player = is_player
- 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.collision()
- self.ticks += 1
- def collision(self):
- 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 self.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 self.apples:
- if head == food:
- self.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
- return True
- return False
- def draw_tile(x, y, color):
- pygame.draw.rect(win, color, (x * scale + 1, y * scale + 1, scale - 2, scale - 2))
- def main():
- ticks = 0
- snakes, foods = [], [(10, 10)]
- snakes.append(Snake(randint(0, 64), 32, snakes, foods, [(100, 0, 200), (0, 0, 200)], is_player=True))
- snakes.append(Snake(34, 34, snakes, foods, [(100, 0, 200), (0, 0, 200)], is_player=True))
- max_foods = 1
- movement_events = []
- clock = pygame.time.Clock()
- running = True
- # Game loop
- while running:
- # Events
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- elif event.type == pygame.KEYDOWN:
- if event.key == pygame.K_r:
- return True
- 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)
- for snake in snakes:
- snake.tick()
- while len(foods) < max_foods:
- foods.append((randint(0, width - 1), randint(0, height - 1)))
- # Drawing
- win.fill((50, 50, 50))
- 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
- pygame.display.flip()
- clock.tick(10)
- if __name__ == '__main__':
- while True:
- if not main():
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement