mirror of
https://github.com/kristoferssolo/School.git
synced 2025-10-21 20:10:38 +00:00
fix global variables
This commit is contained in:
parent
d862e22189
commit
35c8a9a8aa
@ -30,7 +30,7 @@ class Cube:
|
|||||||
|
|
||||||
class Snake:
|
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.color = color
|
||||||
self.head = Cube(position, self.color)
|
self.head = Cube(position, self.color)
|
||||||
self.body = []
|
self.body = []
|
||||||
@ -39,10 +39,11 @@ class Snake:
|
|||||||
self.direction = (1, 0)
|
self.direction = (1, 0)
|
||||||
self.number = player_number
|
self.number = player_number
|
||||||
self.name = name
|
self.name = name
|
||||||
|
self.multiplayer = multiplayer
|
||||||
|
|
||||||
def move(self) -> None:
|
def move(self) -> None:
|
||||||
keys = pygame.key.get_pressed()
|
keys = pygame.key.get_pressed()
|
||||||
if multiplayer:
|
if self.multiplayer:
|
||||||
num_1, num_2 = 1, 2
|
num_1, num_2 = 1, 2
|
||||||
else:
|
else:
|
||||||
num_1, num_2 = 1, 1
|
num_1, num_2 = 1, 1
|
||||||
@ -91,8 +92,8 @@ class Snake:
|
|||||||
else:
|
else:
|
||||||
from assets.scripts.menu import walls
|
from assets.scripts.menu import walls
|
||||||
if walls: # end game if goes into the wall
|
if walls: # end game if goes into the wall
|
||||||
head.move(head.direction)
|
|
||||||
from snake import end_screen
|
from snake import end_screen
|
||||||
|
head.move(head.direction)
|
||||||
if head.direction[0] == -1 and head.pos[0] < 0: # left to right
|
if head.direction[0] == -1 and head.pos[0] < 0: # left to right
|
||||||
end_screen()
|
end_screen()
|
||||||
|
|
||||||
|
|||||||
@ -7,26 +7,7 @@ MID_WIDTH = WIDTH / 2
|
|||||||
MID_HEIGHT = WINDOW_HEIGHT / 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:
|
def main_menu() -> None:
|
||||||
global run
|
|
||||||
run = True
|
|
||||||
pygame.display.set_caption("Snake - Menu")
|
pygame.display.set_caption("Snake - Menu")
|
||||||
while True:
|
while True:
|
||||||
WINDOW.fill(BLACK)
|
WINDOW.fill(BLACK)
|
||||||
@ -61,11 +42,9 @@ def main_menu() -> None:
|
|||||||
|
|
||||||
|
|
||||||
def options() -> None:
|
def options() -> None:
|
||||||
global FPS
|
|
||||||
global multiplayer
|
|
||||||
global walls
|
|
||||||
pygame.display.set_caption("Snake - Options")
|
pygame.display.set_caption("Snake - Options")
|
||||||
while True:
|
while True:
|
||||||
|
from globals import fps, multiplayer, walls
|
||||||
mouse_pos = pygame.mouse.get_pos()
|
mouse_pos = pygame.mouse.get_pos()
|
||||||
|
|
||||||
WINDOW.fill(BLACK)
|
WINDOW.fill(BLACK)
|
||||||
@ -83,7 +62,7 @@ def options() -> None:
|
|||||||
|
|
||||||
speed_state = {5: "Slow", 10: "Normal", 15: "Fast"}
|
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)
|
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)
|
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)
|
back_button = Button((MID_WIDTH, MID_HEIGHT + 200), "BACK", 75, GRAY, WHITE)
|
||||||
@ -96,13 +75,13 @@ def options() -> None:
|
|||||||
quit()
|
quit()
|
||||||
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
|
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
|
||||||
if speed_button.check_input(mouse_pos):
|
if speed_button.check_input(mouse_pos):
|
||||||
if FPS == 5: FPS = 10
|
change_speed()
|
||||||
elif FPS == 10: FPS = 15
|
|
||||||
elif FPS == 15: FPS = 5
|
|
||||||
if multiplayer_button.check_input(mouse_pos):
|
if multiplayer_button.check_input(mouse_pos):
|
||||||
multiplayer = not multiplayer
|
multiplayer = not multiplayer # switch
|
||||||
|
switch_multiplayer()
|
||||||
if walls_button.check_input(mouse_pos):
|
if walls_button.check_input(mouse_pos):
|
||||||
walls = not walls
|
# walls = not walls # switch
|
||||||
|
switch_walls()
|
||||||
if back_button.check_input(mouse_pos):
|
if back_button.check_input(mouse_pos):
|
||||||
main_menu()
|
main_menu()
|
||||||
|
|
||||||
|
|||||||
@ -11,9 +11,9 @@ pygame.font.init()
|
|||||||
BASE_PATH = abspath(dirname(__file__))
|
BASE_PATH = abspath(dirname(__file__))
|
||||||
FONT = join(BASE_PATH, "fonts", "roboto.ttf")
|
FONT = join(BASE_PATH, "fonts", "roboto.ttf")
|
||||||
SPRITE_PATH = join(BASE_PATH, "assets", "sprites")
|
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))
|
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))
|
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))
|
COBBLESTONE_TEXTURE = pygame.transform.scale(pygame.image.load(join(SPRITE_PATH, "cobblestone.jpeg")), (CELL_SIZE, CELL_SIZE))
|
||||||
|
|
||||||
RED = (255, 0, 0)
|
RED = (255, 0, 0)
|
||||||
WHITE = (242, 242, 242)
|
WHITE = (242, 242, 242)
|
||||||
@ -26,9 +26,23 @@ BLUE = (85, 85, 255)
|
|||||||
|
|
||||||
set_font = lambda size: pygame.font.Font(FONT, size) # sets font size
|
set_font = lambda size: pygame.font.Font(FONT, size) # sets font size
|
||||||
|
|
||||||
run = True
|
fps = 10 # speed
|
||||||
snakes = []
|
|
||||||
|
|
||||||
FPS = 10 # speed
|
|
||||||
multiplayer = False
|
multiplayer = False
|
||||||
walls = 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
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
# Author - Kristiāns Francis Cagulis
|
# Author - Kristiāns Francis Cagulis
|
||||||
# Date - 18.04.2022
|
# Date - 22.04.2022
|
||||||
# Title - Snake
|
# Title - Snake
|
||||||
|
|
||||||
import pygame
|
import pygame
|
||||||
@ -10,10 +10,11 @@ from globals import *
|
|||||||
from assets.scripts.score import *
|
from assets.scripts.score import *
|
||||||
from assets.scripts.menu import main_menu
|
from assets.scripts.menu import main_menu
|
||||||
from assets.scripts.classes import *
|
from assets.scripts.classes import *
|
||||||
from assets.scripts.menu import FPS, multiplayer, walls
|
|
||||||
|
|
||||||
BASE_PATH = abspath(dirname(__file__))
|
BASE_PATH = abspath(dirname(__file__))
|
||||||
|
|
||||||
|
snakes = []
|
||||||
|
|
||||||
|
|
||||||
def draw_grid() -> None:
|
def draw_grid() -> None:
|
||||||
x, y = 0, 0
|
x, y = 0, 0
|
||||||
@ -39,27 +40,26 @@ def collision_check(snakes, snack) -> None:
|
|||||||
|
|
||||||
|
|
||||||
def end_screen() -> None:
|
def end_screen() -> None:
|
||||||
global run
|
|
||||||
for snake in snakes:
|
for snake in snakes:
|
||||||
write_score(snake.name, len(snake.body), BASE_PATH)
|
if len(snake.body) > 1:
|
||||||
run = False
|
write_score(snake.name, len(snake.body) - 1, BASE_PATH)
|
||||||
|
main_menu()
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
global snakes
|
from globals import fps, multiplayer, walls
|
||||||
pygame.display.set_caption("Snake")
|
pygame.display.set_caption("Snake")
|
||||||
|
|
||||||
clock = pygame.time.Clock()
|
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)
|
snakes.append(snake_one)
|
||||||
|
|
||||||
if multiplayer:
|
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)
|
snakes.append(snake_two)
|
||||||
|
apple = Snack(APPLE_TEXTURE)
|
||||||
apple = Snack(apple_texture)
|
|
||||||
collision_check(snakes, apple)
|
collision_check(snakes, apple)
|
||||||
poison = Snack(poison_texture)
|
poison = Snack(POISON_TEXTURE)
|
||||||
collision_check(snakes, poison)
|
collision_check(snakes, poison)
|
||||||
|
|
||||||
def redraw_window() -> None:
|
def redraw_window() -> None:
|
||||||
@ -72,12 +72,12 @@ def main() -> None:
|
|||||||
poison.draw_snack()
|
poison.draw_snack()
|
||||||
if walls:
|
if walls:
|
||||||
for i in range(ROWS):
|
for i in range(ROWS):
|
||||||
cobble_rect = pygame.Rect(i * CELL_SIZE, HEIGHT, WIDTH, CELL_SIZE)
|
COBBLE_RECT = pygame.Rect(i * CELL_SIZE, HEIGHT, WIDTH, CELL_SIZE)
|
||||||
WINDOW.blit(cobblestone_texture, cobble_rect)
|
WINDOW.blit(COBBLESTONE_TEXTURE, COBBLE_RECT)
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
|
|
||||||
while run:
|
while True:
|
||||||
clock.tick(FPS)
|
clock.tick(fps)
|
||||||
pygame.time.delay(0)
|
pygame.time.delay(0)
|
||||||
|
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
@ -88,13 +88,13 @@ def main() -> None:
|
|||||||
snake.move()
|
snake.move()
|
||||||
if snake.body[0].pos == apple.pos:
|
if snake.body[0].pos == apple.pos:
|
||||||
snake.add_cube()
|
snake.add_cube()
|
||||||
apple = Snack(apple_texture)
|
apple = Snack(APPLE_TEXTURE)
|
||||||
collision_check(snakes, apple)
|
collision_check(snakes, apple)
|
||||||
|
|
||||||
if snake.body[0].pos == poison.pos:
|
if snake.body[0].pos == poison.pos:
|
||||||
if len(snake.body) > 1:
|
if len(snake.body) > 1:
|
||||||
snake.remove_cube()
|
snake.remove_cube()
|
||||||
poison = Snack(poison_texture)
|
poison = Snack(POISON_TEXTURE)
|
||||||
collision_check(snakes, poison)
|
collision_check(snakes, poison)
|
||||||
|
|
||||||
for i in range(len(snake.body)):
|
for i in range(len(snake.body)):
|
||||||
@ -104,10 +104,4 @@ def main() -> None:
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main_menu()
|
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)
|
|
||||||
Loading…
Reference in New Issue
Block a user