mirror of
https://github.com/kristoferssolo/School.git
synced 2025-10-21 20:10:38 +00:00
fix snake collission
This commit is contained in:
parent
fc80ab7ec5
commit
248dee71f7
@ -5,7 +5,7 @@ from random import randrange
|
||||
|
||||
class Cube:
|
||||
|
||||
def __init__(self, position, color=PURPLE) -> None:
|
||||
def __init__(self, position, color=DARK_PURPLE) -> None:
|
||||
self.pos = position
|
||||
self.direction = (1, 0)
|
||||
self.color = color
|
||||
|
||||
@ -6,6 +6,9 @@ from assets.scripts.classes import Button
|
||||
MID_WIDTH = WIDTH / 2
|
||||
MID_HEIGHT = WINDOW_HEIGHT / 2
|
||||
|
||||
user_name = ["", ""]
|
||||
color_index = [0, 1]
|
||||
|
||||
|
||||
def main_menu() -> None:
|
||||
pygame.display.set_caption("Snake - Menu")
|
||||
@ -25,18 +28,86 @@ def main_menu() -> None:
|
||||
on_hover(buttons)
|
||||
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
quit()
|
||||
if event.type == pygame.QUIT: quit()
|
||||
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
|
||||
if play_button.check_input(mouse_pos):
|
||||
if play_button.check_input(mouse_pos): user_input(0)
|
||||
if options_button.check_input(mouse_pos): options()
|
||||
if score_button.check_input(mouse_pos): scoreboard()
|
||||
if quit_button.check_input(mouse_pos): quit()
|
||||
|
||||
pygame.display.update()
|
||||
|
||||
|
||||
def user_input(player: int) -> None:
|
||||
from snake import main
|
||||
main()
|
||||
if options_button.check_input(mouse_pos):
|
||||
options()
|
||||
if score_button.check_input(mouse_pos):
|
||||
scoreboard()
|
||||
if quit_button.check_input(mouse_pos):
|
||||
quit()
|
||||
global user_name
|
||||
global color_index
|
||||
pygame.display.set_caption("Snake")
|
||||
select_active = True
|
||||
outline_color = WHITE
|
||||
name_rect_w = 140
|
||||
while True:
|
||||
from globals import multiplayer
|
||||
WINDOW.fill(BLACK)
|
||||
mouse_pos = pygame.mouse.get_pos()
|
||||
menu_text = set_font(100).render(f"PLAYER {player + 1}", 1, WHITE)
|
||||
menu_rect = menu_text.get_rect(center=(MID_WIDTH, 125))
|
||||
WINDOW.blit(menu_text, menu_rect)
|
||||
|
||||
back_button = Button((130, WINDOW_HEIGHT - 50), "BACK", 75, GRAY, WHITE)
|
||||
if multiplayer and player == 0:
|
||||
next_button = Button((WIDTH - 130, WINDOW_HEIGHT - 50), "NEXT", 75, GRAY, WHITE)
|
||||
buttons = [back_button, next_button]
|
||||
else:
|
||||
play_button = Button((WIDTH - 130, WINDOW_HEIGHT - 50), "PLAY", 75, GRAY, WHITE)
|
||||
buttons = [back_button, play_button]
|
||||
|
||||
on_hover(buttons)
|
||||
|
||||
name_rect = pygame.Rect(MID_WIDTH - name_rect_w / 2, 200, name_rect_w, 32)
|
||||
pygame.draw.rect(WINDOW, outline_color, name_rect, 2)
|
||||
user_text = set_font(20).render(user_name[player], 1, WHITE)
|
||||
WINDOW.blit(user_text, (name_rect.x + 5, name_rect.y + 5))
|
||||
name_rect_w = max(140, user_text.get_width() + 10)
|
||||
|
||||
color = COLORS[color_index[player]]
|
||||
color_rect = pygame.Rect(MID_WIDTH - 50, 350, 100, 100)
|
||||
pygame.draw.rect(WINDOW, color, color_rect)
|
||||
|
||||
if select_active: outline_color = WHITE
|
||||
else: outline_color = DARK_GRAY
|
||||
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT: quit()
|
||||
|
||||
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||
if event.button == 1:
|
||||
if name_rect.collidepoint(event.pos): select_active = True
|
||||
else: select_active = False
|
||||
|
||||
if back_button.check_input(mouse_pos): main_menu()
|
||||
if multiplayer and player == 0:
|
||||
if next_button.check_input(mouse_pos): user_input(1)
|
||||
else:
|
||||
if play_button.check_input(mouse_pos): main()
|
||||
if color_rect.collidepoint(event.pos):
|
||||
color_index[player] += 1
|
||||
if color_index[player] == len(COLORS) - 1:
|
||||
color_index[player] = 0
|
||||
|
||||
if event.button == 3: # clear user name on mouse right click
|
||||
if name_rect.collidepoint(event.pos): user_name[player] = ""
|
||||
|
||||
if event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_ESCAPE: main_menu()
|
||||
|
||||
if select_active:
|
||||
if event.key == pygame.K_BACKSPACE: user_name[player] = user_name[player][:-1]
|
||||
else: user_name[player] += event.unicode
|
||||
|
||||
if event.key == pygame.K_RETURN or event.key == pygame.K_KP_ENTER:
|
||||
if multiplayer and player == 0: user_input(1)
|
||||
else: main()
|
||||
|
||||
pygame.display.update()
|
||||
|
||||
@ -71,22 +142,14 @@ def options() -> None:
|
||||
on_hover(buttons)
|
||||
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
quit()
|
||||
if event.type == pygame.QUIT: quit()
|
||||
if event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_ESCAPE:
|
||||
main_menu()
|
||||
if event.key == pygame.K_ESCAPE: main_menu()
|
||||
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
|
||||
if speed_button.check_input(mouse_pos):
|
||||
change_speed()
|
||||
if multiplayer_button.check_input(mouse_pos):
|
||||
multiplayer = not multiplayer # switch
|
||||
switch_multiplayer()
|
||||
if walls_button.check_input(mouse_pos):
|
||||
# walls = not walls # switch
|
||||
switch_walls()
|
||||
if back_button.check_input(mouse_pos):
|
||||
main_menu()
|
||||
if speed_button.check_input(mouse_pos): change_speed()
|
||||
if multiplayer_button.check_input(mouse_pos): switch_multiplayer()
|
||||
if walls_button.check_input(mouse_pos): switch_walls()
|
||||
if back_button.check_input(mouse_pos): main_menu()
|
||||
|
||||
pygame.display.update()
|
||||
|
||||
@ -102,14 +165,11 @@ def scoreboard() -> None:
|
||||
on_hover([back_button])
|
||||
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
quit()
|
||||
if event.type == pygame.QUIT: quit()
|
||||
if event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_ESCAPE:
|
||||
main_menu()
|
||||
if event.key == pygame.K_ESCAPE: main_menu()
|
||||
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
|
||||
if back_button.check_input(mouse_pos):
|
||||
main_menu()
|
||||
if back_button.check_input(mouse_pos): main_menu()
|
||||
|
||||
csv_file = read_score(BASE_PATH)
|
||||
for i, line in enumerate(sort(csv_file, reverse=True)[:11]):
|
||||
|
||||
@ -15,14 +15,24 @@ APPLE_TEXTURE = pygame.transform.scale(pygame.image.load(join(SPRITE_PATH, "gold
|
||||
POISON_TEXTURE = pygame.transform.scale(pygame.image.load(join(SPRITE_PATH, "poison.png")), (CELL_SIZE, CELL_SIZE))
|
||||
COBBLESTONE_TEXTURE = pygame.transform.scale(pygame.image.load(join(SPRITE_PATH, "cobblestone.jpeg")), (CELL_SIZE, CELL_SIZE))
|
||||
|
||||
RED = (255, 0, 0)
|
||||
WHITE = (242, 242, 242)
|
||||
GRAY = (204, 204, 204)
|
||||
DARK_GRAY = (51, 51, 51)
|
||||
BLACK = (0, 0, 0)
|
||||
GREEN = (0, 128, 30)
|
||||
PURPLE = (170, 0, 255)
|
||||
DARK_BLUE = (0, 0, 170)
|
||||
DARK_GREEN = (0, 170, 0)
|
||||
DARK_AQUA = (0, 170, 170)
|
||||
DARK_RED = (170, 0, 0)
|
||||
DARK_PURPLE = (170, 170, 0)
|
||||
GOLD = (255, 170, 0)
|
||||
GRAY = (170, 170, 170)
|
||||
DARK_GRAY = (85, 85, 85)
|
||||
BLUE = (85, 85, 255)
|
||||
GREEN = (85, 255, 85)
|
||||
AQUA = (85, 255, 255)
|
||||
RED = (255, 85, 85)
|
||||
LIGHT_PURPLE = (255, 85, 255)
|
||||
YELLOW = (255, 255, 85)
|
||||
WHITE = (242, 242, 242)
|
||||
|
||||
COLORS = [DARK_BLUE, DARK_GREEN, DARK_AQUA, DARK_RED, DARK_PURPLE, GOLD, BLUE, GREEN, AQUA, RED, LIGHT_PURPLE, YELLOW]
|
||||
|
||||
set_font = lambda size: pygame.font.Font(FONT, size) # sets font size
|
||||
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
# Author - Kristiāns Francis Cagulis
|
||||
# Date - 22.04.2022
|
||||
# Date - 23.04.2022
|
||||
# Title - Snake
|
||||
|
||||
import pygame
|
||||
@ -52,11 +52,12 @@ def main() -> None:
|
||||
pygame.display.set_caption("Snake")
|
||||
|
||||
clock = pygame.time.Clock()
|
||||
snake_one = Snake((randint(0, ROWS - 1), randint(0, COLUMNS - 1)), PURPLE, "test1", 1, multiplayer)
|
||||
from assets.scripts.menu import user_name, color_index
|
||||
snake_one = Snake((randint(0, ROWS - 1), randint(0, COLUMNS - 1)), COLORS[color_index[0]], user_name[0], 1, multiplayer)
|
||||
snakes.append(snake_one)
|
||||
|
||||
if multiplayer:
|
||||
snake_two = Snake((randint(0, ROWS - 1), randint(0, COLUMNS - 1)), BLUE, "test2", 2, multiplayer)
|
||||
snake_two = Snake((randint(0, ROWS - 1), randint(0, COLUMNS - 1)), COLORS[color_index[1]], user_name[1], 2, multiplayer)
|
||||
snakes.append(snake_two)
|
||||
apple = Snack(APPLE_TEXTURE)
|
||||
collision_check(snakes, apple)
|
||||
@ -104,6 +105,14 @@ def main() -> None:
|
||||
for i in range(len(snake.body)):
|
||||
if snake.body[i].pos in list(map(lambda z: z.pos, snake.body[i + 1:])):
|
||||
end_screen()
|
||||
if multiplayer:
|
||||
for i in snakes[0].body:
|
||||
if i.pos == snakes[1].head.pos:
|
||||
end_screen()
|
||||
for i in snakes[1].body:
|
||||
if i.pos == snakes[0].head.pos:
|
||||
end_screen()
|
||||
|
||||
redraw_window()
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user