mirror of
https://github.com/kristoferssolo/School.git
synced 2025-10-21 20:10:38 +00:00
added 2nd snake
This commit is contained in:
parent
ffeadc58ac
commit
ae6584b703
@ -10,18 +10,19 @@ BLACK = (0, 0, 0)
|
|||||||
WHITE = (255, 255, 255)
|
WHITE = (255, 255, 255)
|
||||||
GREEN = (0, 128, 30)
|
GREEN = (0, 128, 30)
|
||||||
PURPLE = (170, 0, 255)
|
PURPLE = (170, 0, 255)
|
||||||
|
BLUE = (85, 85, 255)
|
||||||
RANDOM_COLOR = (randint(0, 255), randint(0, 255), randint(0, 255))
|
RANDOM_COLOR = (randint(0, 255), randint(0, 255), randint(0, 255))
|
||||||
|
|
||||||
SQUARE_SIZE = 30
|
SQUARE_SIZE = 30
|
||||||
ROWS, COLUMNS = 30, 20
|
ROWS, COLUMNS = 30, 20
|
||||||
WIDTH, HEIGHT = ROWS * SQUARE_SIZE, COLUMNS * SQUARE_SIZE
|
WIDTH, HEIGHT = ROWS * SQUARE_SIZE, COLUMNS * SQUARE_SIZE
|
||||||
|
|
||||||
RANDOM_POS = (randint(0, ROWS - 1), randint(0, COLUMNS - 1))
|
|
||||||
|
|
||||||
WINDOW = pygame.display.set_mode((WIDTH, HEIGHT))
|
WINDOW = pygame.display.set_mode((WIDTH, HEIGHT))
|
||||||
|
pygame.font.init()
|
||||||
|
|
||||||
|
|
||||||
class Cube(object):
|
class Cube(object):
|
||||||
|
|
||||||
def __init__(self, start, color=RANDOM_COLOR) -> None:
|
def __init__(self, start, color=RANDOM_COLOR) -> None:
|
||||||
self.pos = start
|
self.pos = start
|
||||||
self.dirnx = 1
|
self.dirnx = 1
|
||||||
@ -36,7 +37,6 @@ class Cube(object):
|
|||||||
# eyes
|
# eyes
|
||||||
def draw(self, surface, eyes=False) -> None:
|
def draw(self, surface, eyes=False) -> None:
|
||||||
distance = WIDTH // ROWS
|
distance = WIDTH // ROWS
|
||||||
# print(distance)
|
|
||||||
i = self.pos[0]
|
i = self.pos[0]
|
||||||
j = self.pos[1]
|
j = self.pos[1]
|
||||||
|
|
||||||
@ -51,6 +51,7 @@ class Cube(object):
|
|||||||
|
|
||||||
|
|
||||||
class Snake(object):
|
class Snake(object):
|
||||||
|
|
||||||
def __init__(self, pos, color) -> None:
|
def __init__(self, pos, color) -> None:
|
||||||
self.color = color
|
self.color = color
|
||||||
self.head = Cube(pos, self.color)
|
self.head = Cube(pos, self.color)
|
||||||
@ -65,26 +66,47 @@ class Snake(object):
|
|||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
keys = pygame.key.get_pressed()
|
keys = pygame.key.get_pressed()
|
||||||
|
if self.color == PURPLE:
|
||||||
|
if keys[pygame.K_LEFT]: # turn left
|
||||||
|
self.dirnx = -1
|
||||||
|
self.dirny = 0
|
||||||
|
self.turns[self.head.pos[:]] = [self.dirnx, self.dirny]
|
||||||
|
|
||||||
if keys[pygame.K_LEFT] or keys[pygame.K_a]: # turn left
|
elif keys[pygame.K_RIGHT]: # turn right
|
||||||
self.dirnx = -1
|
self.dirnx = 1
|
||||||
self.dirny = 0
|
self.dirny = 0
|
||||||
self.turns[self.head.pos[:]] = [self.dirnx, self.dirny] # turn right
|
self.turns[self.head.pos[:]] = [self.dirnx, self.dirny]
|
||||||
|
|
||||||
elif keys[pygame.K_RIGHT] or keys[pygame.K_d]:
|
elif keys[pygame.K_UP]: # turn up
|
||||||
self.dirnx = 1
|
self.dirnx = 0
|
||||||
self.dirny = 0
|
self.dirny = -1
|
||||||
self.turns[self.head.pos[:]] = [self.dirnx, self.dirny]
|
self.turns[self.head.pos[:]] = [self.dirnx, self.dirny]
|
||||||
|
|
||||||
elif keys[pygame.K_UP] or keys[pygame.K_w]: # turn up
|
elif keys[pygame.K_DOWN]: # turn down
|
||||||
self.dirnx = 0
|
self.dirnx = 0
|
||||||
self.dirny = -1
|
self.dirny = 1
|
||||||
self.turns[self.head.pos[:]] = [self.dirnx, self.dirny]
|
self.turns[self.head.pos[:]] = [self.dirnx, self.dirny]
|
||||||
|
|
||||||
elif keys[pygame.K_DOWN] or keys[pygame.K_s]: # turn down
|
elif self.color == BLUE:
|
||||||
self.dirnx = 0
|
if keys[pygame.K_a]: # turn left
|
||||||
self.dirny = 1
|
self.dirnx = -1
|
||||||
self.turns[self.head.pos[:]] = [self.dirnx, self.dirny]
|
self.dirny = 0
|
||||||
|
self.turns[self.head.pos[:]] = [self.dirnx, self.dirny]
|
||||||
|
|
||||||
|
elif keys[pygame.K_d]: # turn right
|
||||||
|
self.dirnx = 1
|
||||||
|
self.dirny = 0
|
||||||
|
self.turns[self.head.pos[:]] = [self.dirnx, self.dirny]
|
||||||
|
|
||||||
|
elif keys[pygame.K_w]: # turn up
|
||||||
|
self.dirnx = 0
|
||||||
|
self.dirny = -1
|
||||||
|
self.turns[self.head.pos[:]] = [self.dirnx, self.dirny]
|
||||||
|
|
||||||
|
elif keys[pygame.K_s]: # turn down
|
||||||
|
self.dirnx = 0
|
||||||
|
self.dirny = 1
|
||||||
|
self.turns[self.head.pos[:]] = [self.dirnx, self.dirny]
|
||||||
|
|
||||||
for i, head in enumerate(self.body):
|
for i, head in enumerate(self.body):
|
||||||
head_pos = head.pos[:]
|
head_pos = head.pos[:]
|
||||||
@ -133,6 +155,9 @@ class Snake(object):
|
|||||||
self.body[-1].dirnx = dx
|
self.body[-1].dirnx = dx
|
||||||
self.body[-1].dirny = dy
|
self.body[-1].dirny = dy
|
||||||
|
|
||||||
|
def remove_cube(self) -> None:
|
||||||
|
self.body.pop(-1)
|
||||||
|
|
||||||
def draw(self, surface) -> None:
|
def draw(self, surface) -> None:
|
||||||
for i, head in enumerate(self.body):
|
for i, head in enumerate(self.body):
|
||||||
if i == 0:
|
if i == 0:
|
||||||
@ -154,23 +179,24 @@ def draw_grid(surface) -> None:
|
|||||||
pygame.draw.line(surface, WHITE, (0, y), (WIDTH, y))
|
pygame.draw.line(surface, WHITE, (0, y), (WIDTH, y))
|
||||||
|
|
||||||
|
|
||||||
def redraw_window(snake, snack) -> None:
|
def redraw_window(sprites: list) -> None:
|
||||||
WINDOW.fill(BLACK)
|
WINDOW.fill(BLACK)
|
||||||
draw_grid(WINDOW)
|
draw_grid(WINDOW)
|
||||||
snake.draw(WINDOW)
|
for sprite in sprites:
|
||||||
snack.draw(WINDOW)
|
sprite.draw(WINDOW)
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
|
|
||||||
|
|
||||||
def random_snack(rows, columns, item) -> tuple:
|
def random_snack(items, rows=ROWS, columns=COLUMNS) -> tuple:
|
||||||
positions = item.body
|
for item in items:
|
||||||
while True:
|
positions = item.body
|
||||||
x = randrange(rows)
|
while True:
|
||||||
y = randrange(columns)
|
x = randrange(rows)
|
||||||
if len(list(filter(lambda z: z.pos == (x, y), positions))) > 0:
|
y = randrange(columns)
|
||||||
continue
|
if len(list(filter(lambda z: z.pos == (x, y), positions))) > 0:
|
||||||
else:
|
continue
|
||||||
break
|
else:
|
||||||
|
break
|
||||||
return x, y
|
return x, y
|
||||||
|
|
||||||
|
|
||||||
@ -178,31 +204,36 @@ def main() -> None:
|
|||||||
FPS = 10
|
FPS = 10
|
||||||
run = True
|
run = True
|
||||||
clock = pygame.time.Clock()
|
clock = pygame.time.Clock()
|
||||||
snake = Snake(RANDOM_POS, PURPLE)
|
snake_one = Snake((randint(0, ROWS - 1), randint(0, COLUMNS - 1)), PURPLE)
|
||||||
snack = Cube(random_snack(ROWS, COLUMNS, snake), color=GREEN)
|
snake_two = Snake((randint(0, ROWS - 1), randint(0, COLUMNS - 1)), BLUE)
|
||||||
|
snakes = [snake_one, snake_two]
|
||||||
|
snack = Cube(random_snack(snakes), color=GREEN)
|
||||||
|
poison = Cube(random_snack(snakes), color=RED)
|
||||||
while run:
|
while run:
|
||||||
clock.tick(FPS)
|
clock.tick(FPS)
|
||||||
pygame.time.delay(50)
|
pygame.time.delay(50)
|
||||||
snake.move()
|
for snake in snakes:
|
||||||
if snake.body[0].pos == snack.pos:
|
snake.move()
|
||||||
snake.add_cube()
|
if snake.body[0].pos == snack.pos:
|
||||||
snack = Cube(random_snack(ROWS, COLUMNS, snake), color=GREEN)
|
snake.add_cube()
|
||||||
for i in range(len(snake.body)):
|
snack = Cube(random_snack(snakes), color=GREEN)
|
||||||
if snake.body[i].pos in list(map(lambda z: z.pos, snake.body[i + 1:])):
|
if snake.body[0].pos == poison.pos:
|
||||||
print(f"Score: {len(snake.body)}")
|
snake.remove_cube()
|
||||||
run = False
|
poison = Cube(random_snack(snakes), color=RED)
|
||||||
# snake.reset(RANDOM_POS)
|
for i in range(len(snake.body)):
|
||||||
redraw_window(snake, snack)
|
if snake.body[i].pos in list(map(lambda z: z.pos, snake.body[i + 1:])):
|
||||||
|
print(f"Score: {len(snake.body)}")
|
||||||
|
run = False
|
||||||
|
redraw_window(list(set(snakes + [snack, poison])))
|
||||||
|
|
||||||
|
|
||||||
set_font = lambda size: pygame.font.SysFont("roboto", size) # sets font size
|
set_font = lambda size: pygame.font.SysFont("arial", size) # sets font size
|
||||||
|
|
||||||
|
|
||||||
def main_menu() -> None:
|
def main_menu() -> None:
|
||||||
while True:
|
while True:
|
||||||
WINDOW.fill(BLACK)
|
WINDOW.fill(BLACK)
|
||||||
# title_label = set_font(50).render("Press any key to start...", 1, WHITE)
|
title_label = set_font(50).render("Press any key to start...", 1, WHITE)
|
||||||
title_label = pygame.font.SysFont("roboto", 50).render("Press any key to start...", 1, WHITE)
|
|
||||||
WINDOW.blit(title_label, (WIDTH / 2 - title_label.get_width() / 2, HEIGHT / 2 - title_label.get_height() / 2))
|
WINDOW.blit(title_label, (WIDTH / 2 - title_label.get_width() / 2, HEIGHT / 2 - title_label.get_height() / 2))
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user