diff --git a/background_sound.mp3 b/background_sound.mp3 new file mode 100644 index 0000000..b439e25 Binary files /dev/null and b/background_sound.mp3 differ diff --git a/death_sound.mp3 b/death_sound.mp3 new file mode 100644 index 0000000..dcfee83 Binary files /dev/null and b/death_sound.mp3 differ diff --git a/eat_sound.mp3 b/eat_sound.mp3 new file mode 100644 index 0000000..2643783 Binary files /dev/null and b/eat_sound.mp3 differ diff --git a/main.py b/main.py index ec0760e..3925b1f 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,6 @@ from tkinter import * import random +import pygame GAME_WIDTH = 700 GAME_HEIGHT = 700 @@ -9,6 +10,7 @@ SNAKE_COLOR = "#00FF00" FOOD_COLOR = "#FF0000" BACKGROUND_COLOR = "#000000" +game_in_progress = False welcome_screen_text = "Snake Game!\nMade By: RejectModders\nMade it for fun.\n\n" \ "Instructions:\n" \ @@ -16,14 +18,26 @@ "Eat the red food to grow and earn points.\n" \ "Avoid running into the edges of the game window\n" \ "and colliding with yourself.\n\n" \ - "Reminder... this does NOT support WASD. Only arrow keys." + "Reminder... this also supports WASD keys!" + +pygame.mixer.init() +eat_sound = pygame.mixer.Sound('eat_sound.mp3') +death_sound = pygame.mixer.Sound('death_sound.mp3') +background_sound = pygame.mixer.Sound('background_sound.mp3') + +play_eat_sound = True +play_death_sound = True +play_background_sound = True +background_volume = 0.05 +death_volume = 0.05 +eat_volume = 0.05 + class Snake: def __init__(self): self.body_size = BODY_PARTS self.coordinates = [] self.squares = [] - for i in range(0, BODY_PARTS): self.coordinates.append([0, 0]) @@ -62,6 +76,9 @@ def next_turn(snake, food): label.config(text="Score:{}".format(score)) canvas.delete("food") food = Food() + if play_eat_sound: + eat_sound.play() + eat_sound.set_volume(eat_volume) else: del snake.coordinates[-1] canvas.delete(snake.squares[-1]) @@ -74,13 +91,24 @@ def next_turn(snake, food): def change_direction(event): global direction - if event.keysym.lower() in ['left', 'right', 'up', 'down']: - new_direction = event.keysym.lower() - if (new_direction == 'left' and direction != 'right') or \ - (new_direction == 'right' and direction != 'left') or \ - (new_direction == 'up' and direction != 'down') or \ - (new_direction == 'down' and direction != 'up'): - direction = new_direction + key = event.keysym.lower() + + if key in ['left', 'a']: + new_direction = 'left' + elif key in ['right', 'd']: + new_direction = 'right' + elif key in ['up', 'w']: + new_direction = 'up' + elif key in ['down', 's']: + new_direction = 'down' + else: + return + + if (new_direction == 'left' and direction != 'right') or \ + (new_direction == 'right' and direction != 'left') or \ + (new_direction == 'up' and direction != 'down') or \ + (new_direction == 'down' and direction != 'up'): + direction = new_direction def check_collisions(snake): x, y = snake.coordinates[0] @@ -96,23 +124,29 @@ def check_collisions(snake): return False def game_over(): - canvas.delete(ALL) - canvas.create_text(canvas.winfo_width() / 2, canvas.winfo_height() / 2, - font=('consolas', 70), text="GAME OVER", fill="red", tag="gameover") - canvas.create_text(canvas.winfo_width() / 2, canvas.winfo_height() / 1.5, - font=('consolas', 13), text="Press the 'SPACE' key to restart", fill="white", tag="restart") - window.bind('', restart_game) - -def restart_game(event): - global snake, food, score, direction - window.unbind('') + global game_in_progress + if not game_in_progress: + if play_death_sound: + death_sound.play() + death_sound.set_volume(death_volume) + game_in_progress = True + canvas.delete(ALL) + canvas.create_text(canvas.winfo_width() / 2, canvas.winfo_height() / 2, + font=('consolas', 70), text="GAME OVER", fill="red", tag="gameover") + canvas.create_text(canvas.winfo_width() / 2, canvas.winfo_height() / 1.5, + font=('consolas', 13), text="Press the 'SPACE' key to restart", fill="white", tag="restart") + window.bind('', lambda event: restart_game()) + +def restart_game(): + global game_in_progress, snake, food, score, direction + game_in_progress = False + window.unbind('') score = 0 label.config(text="Score:{}".format(score)) direction = 'down' snake = Snake() food = Food() window.after(1500, lambda: next_turn(snake, food)) - window.bind('', change_direction) canvas.delete("gameover") canvas.delete("restart") @@ -132,6 +166,41 @@ def start_game(event): window.after(1500, lambda: next_turn(snake, food)) window.bind('', change_direction) +def toggle_eat_sound(): + global play_eat_sound + play_eat_sound = not play_eat_sound + +def toggle_death_sound(): + global play_death_sound + play_death_sound = not play_death_sound + +def toggle_background_sound(): + global play_background_sound, background_volume + play_background_sound = not play_background_sound + if play_background_sound: + background_sound.play(loops=-1) + background_sound.set_volume(background_volume) + else: + background_sound.stop() + +def update_eat_volume(value): + global eat_volume + eat_volume = float(value) + if play_eat_sound: + eat_sound.set_volume(eat_volume) + +def update_death_volume(value): + global death_volume + death_volume = float(value) + if play_death_sound: + death_sound.set_volume(death_volume) + +def update_background_volume(value): + global background_volume + background_volume = float(value) + if play_background_sound: + background_sound.set_volume(background_volume) + window = Tk() window.title("Snake game") window.resizable(False, False) @@ -140,10 +209,30 @@ def start_game(event): direction = 'down' label = Label(window, text="Score:{}".format(score), font=('consolas', 40)) -label.pack() +label.grid(row=0, column=0, columnspan=2) + +background_sound_button = Button(window, text="Toggle Background Sound", command=toggle_background_sound) +background_sound_button.grid(row=1, column=0) + +death_sound_button = Button(window, text="Toggle Death Sound", command=toggle_death_sound) +death_sound_button.grid(row=2, column=0) +eat_sound_button = Button(window, text="Toggle Eat Sound", command=toggle_eat_sound) +eat_sound_button.grid(row=3, column=0) + +background_volume_slider = Scale(window, from_=0, to=1, resolution=0.01, orient=HORIZONTAL, label="Background Volume", command=update_background_volume) +background_volume_slider.set(background_volume) +background_volume_slider.grid(row=1, column=1) + +death_volume_slider = Scale(window, from_=0, to=1, resolution=0.01, orient=HORIZONTAL, label="Death Volume", command=update_death_volume) +death_volume_slider.set(death_volume) +death_volume_slider.grid(row=2, column=1) + +eat_volume_slider = Scale(window, from_=0, to=1, resolution=0.01, orient=HORIZONTAL, label="Eat Volume", command=update_eat_volume) +eat_volume_slider.set(eat_volume) +eat_volume_slider.grid(row=3, column=1) canvas = Canvas(window, bg=BACKGROUND_COLOR, height=GAME_HEIGHT, width=GAME_WIDTH) -canvas.pack() +canvas.grid(row=4, column=0, columnspan=2) window.update() @@ -157,6 +246,8 @@ def start_game(event): window.geometry(f"{window_width}x{window_height}+{x}+{y}") +toggle_background_sound() + welcome_screen() window.bind('', start_game) diff --git a/snakegame.exe b/snakegame.exe index 72e143f..76f10b3 100644 Binary files a/snakegame.exe and b/snakegame.exe differ