From 35c8a9a8aa64bac1b1aeb36aa1c5f98ba2cb72e0 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Fri, 22 Apr 2022 01:41:23 +0300 Subject: [PATCH] fix global variables --- pygame/snake/source/assets/scripts/classes.py | 7 ++-- pygame/snake/source/assets/scripts/menu.py | 35 ++++------------ pygame/snake/source/globals.py | 28 +++++++++---- pygame/snake/source/snake.py | 42 ++++++++----------- 4 files changed, 50 insertions(+), 62 deletions(-) diff --git a/pygame/snake/source/assets/scripts/classes.py b/pygame/snake/source/assets/scripts/classes.py index 66d5b14a..96ea5c8e 100644 --- a/pygame/snake/source/assets/scripts/classes.py +++ b/pygame/snake/source/assets/scripts/classes.py @@ -30,7 +30,7 @@ class Cube: class Snake: - def __init__(self, position: tuple, color: tuple, name: str, player_number: int = 1) -> None: + def __init__(self, position: tuple, color: tuple, name: str, player_number: int = 1, multiplayer: bool = False) -> None: self.color = color self.head = Cube(position, self.color) self.body = [] @@ -39,10 +39,11 @@ class Snake: self.direction = (1, 0) self.number = player_number self.name = name + self.multiplayer = multiplayer def move(self) -> None: keys = pygame.key.get_pressed() - if multiplayer: + if self.multiplayer: num_1, num_2 = 1, 2 else: num_1, num_2 = 1, 1 @@ -91,8 +92,8 @@ class Snake: else: from assets.scripts.menu import walls if walls: # end game if goes into the wall - head.move(head.direction) from snake import end_screen + head.move(head.direction) if head.direction[0] == -1 and head.pos[0] < 0: # left to right end_screen() diff --git a/pygame/snake/source/assets/scripts/menu.py b/pygame/snake/source/assets/scripts/menu.py index a61c3eb9..a62809c2 100644 --- a/pygame/snake/source/assets/scripts/menu.py +++ b/pygame/snake/source/assets/scripts/menu.py @@ -7,26 +7,7 @@ MID_WIDTH = WIDTH / 2 MID_HEIGHT = WINDOW_HEIGHT / 2 -def menu() -> None: - while True: - WINDOW.fill(BLACK) - title_label = set_font(50).render("Press any key to start...", 1, WHITE) - WINDOW.blit(title_label, (WIDTH / 2 - title_label.get_width() / 2, WINDOW_HEIGHT / 2 - title_label.get_height() / 2)) - for event in pygame.event.get(): - if event.type == pygame.QUIT: - csv_file = read_score(BASE_PATH) - for line in sort(csv_file, reverse=True): - print(line) - quit() - if event.type == pygame.KEYDOWN: - from snake import main - main() - pygame.display.update() - - def main_menu() -> None: - global run - run = True pygame.display.set_caption("Snake - Menu") while True: WINDOW.fill(BLACK) @@ -61,11 +42,9 @@ def main_menu() -> None: def options() -> None: - global FPS - global multiplayer - global walls pygame.display.set_caption("Snake - Options") while True: + from globals import fps, multiplayer, walls mouse_pos = pygame.mouse.get_pos() WINDOW.fill(BLACK) @@ -83,7 +62,7 @@ def options() -> None: speed_state = {5: "Slow", 10: "Normal", 15: "Fast"} - speed_button = Button((MID_WIDTH, MID_HEIGHT - 100), f"SPEED - {speed_state[FPS]}", 75, GRAY, WHITE) + speed_button = Button((MID_WIDTH, MID_HEIGHT - 100), f"SPEED - {speed_state[fps]}", 75, GRAY, WHITE) multiplayer_button = Button((MID_WIDTH, MID_HEIGHT), f"MULTIPLAYER - {multiplayer_state}", 75, GRAY, WHITE) walls_button = Button((MID_WIDTH, MID_HEIGHT + 100), f"WALLS - {walls_state}", 75, GRAY, WHITE) back_button = Button((MID_WIDTH, MID_HEIGHT + 200), "BACK", 75, GRAY, WHITE) @@ -96,13 +75,13 @@ def options() -> None: quit() if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: if speed_button.check_input(mouse_pos): - if FPS == 5: FPS = 10 - elif FPS == 10: FPS = 15 - elif FPS == 15: FPS = 5 + change_speed() if multiplayer_button.check_input(mouse_pos): - multiplayer = not multiplayer + multiplayer = not multiplayer # switch + switch_multiplayer() if walls_button.check_input(mouse_pos): - walls = not walls + # walls = not walls # switch + switch_walls() if back_button.check_input(mouse_pos): main_menu() diff --git a/pygame/snake/source/globals.py b/pygame/snake/source/globals.py index 677938ab..33358f49 100644 --- a/pygame/snake/source/globals.py +++ b/pygame/snake/source/globals.py @@ -11,9 +11,9 @@ pygame.font.init() BASE_PATH = abspath(dirname(__file__)) FONT = join(BASE_PATH, "fonts", "roboto.ttf") SPRITE_PATH = join(BASE_PATH, "assets", "sprites") -apple_texture = pygame.transform.scale(pygame.image.load(join(SPRITE_PATH, "golden_apple.png")), (CELL_SIZE, CELL_SIZE)) -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)) +APPLE_TEXTURE = pygame.transform.scale(pygame.image.load(join(SPRITE_PATH, "golden_apple.png")), (CELL_SIZE, CELL_SIZE)) +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) @@ -26,9 +26,23 @@ BLUE = (85, 85, 255) set_font = lambda size: pygame.font.Font(FONT, size) # sets font size -run = True -snakes = [] - -FPS = 10 # speed +fps = 10 # speed multiplayer = False walls = False + + +def change_speed() -> None: + global fps + if fps == 5: fps = 10 + elif fps == 10: fps = 15 + elif fps == 15: fps = 5 + + +def switch_multiplayer() -> None: + global multiplayer + multiplayer = not multiplayer + + +def switch_walls() -> None: + global walls + walls = not walls diff --git a/pygame/snake/source/snake.py b/pygame/snake/source/snake.py index aadb0dfd..054c3f37 100755 --- a/pygame/snake/source/snake.py +++ b/pygame/snake/source/snake.py @@ -1,5 +1,5 @@ # Author - Kristiāns Francis Cagulis -# Date - 18.04.2022 +# Date - 22.04.2022 # Title - Snake import pygame @@ -10,10 +10,11 @@ from globals import * from assets.scripts.score import * from assets.scripts.menu import main_menu from assets.scripts.classes import * -from assets.scripts.menu import FPS, multiplayer, walls BASE_PATH = abspath(dirname(__file__)) +snakes = [] + def draw_grid() -> None: x, y = 0, 0 @@ -39,27 +40,26 @@ def collision_check(snakes, snack) -> None: def end_screen() -> None: - global run for snake in snakes: - write_score(snake.name, len(snake.body), BASE_PATH) - run = False + if len(snake.body) > 1: + write_score(snake.name, len(snake.body) - 1, BASE_PATH) + main_menu() def main() -> None: - global snakes + from globals import fps, multiplayer, walls pygame.display.set_caption("Snake") clock = pygame.time.Clock() - snake_one = Snake((randint(0, ROWS - 1), randint(0, COLUMNS - 1)), PURPLE, "test1") + snake_one = Snake((randint(0, ROWS - 1), randint(0, COLUMNS - 1)), PURPLE, "test1", 1, multiplayer) snakes.append(snake_one) if multiplayer: - snake_two = Snake((randint(0, ROWS - 1), randint(0, COLUMNS - 1)), BLUE, "test2", 2) + snake_two = Snake((randint(0, ROWS - 1), randint(0, COLUMNS - 1)), BLUE, "test2", 2, multiplayer) snakes.append(snake_two) - - apple = Snack(apple_texture) + apple = Snack(APPLE_TEXTURE) collision_check(snakes, apple) - poison = Snack(poison_texture) + poison = Snack(POISON_TEXTURE) collision_check(snakes, poison) def redraw_window() -> None: @@ -72,12 +72,12 @@ def main() -> None: poison.draw_snack() if walls: for i in range(ROWS): - cobble_rect = pygame.Rect(i * CELL_SIZE, HEIGHT, WIDTH, CELL_SIZE) - WINDOW.blit(cobblestone_texture, cobble_rect) + COBBLE_RECT = pygame.Rect(i * CELL_SIZE, HEIGHT, WIDTH, CELL_SIZE) + WINDOW.blit(COBBLESTONE_TEXTURE, COBBLE_RECT) pygame.display.update() - while run: - clock.tick(FPS) + while True: + clock.tick(fps) pygame.time.delay(0) for event in pygame.event.get(): @@ -88,13 +88,13 @@ def main() -> None: snake.move() if snake.body[0].pos == apple.pos: snake.add_cube() - apple = Snack(apple_texture) + apple = Snack(APPLE_TEXTURE) collision_check(snakes, apple) if snake.body[0].pos == poison.pos: if len(snake.body) > 1: snake.remove_cube() - poison = Snack(poison_texture) + poison = Snack(POISON_TEXTURE) collision_check(snakes, poison) for i in range(len(snake.body)): @@ -104,10 +104,4 @@ def main() -> None: if __name__ == '__main__': - main_menu() - # for i in range(50, 100): - # write_score(f"test{i}", randint(1, 1_000_000), BASE_PATH) - # csv_file = read_score(BASE_PATH) - # # print(csv_file) - # for line in sort(csv_file, reverse=True): - # print(line) + main_menu() \ No newline at end of file