Add music and effects
This commit is contained in:
71
snake.py
71
snake.py
@@ -2,14 +2,19 @@ import curses
|
||||
import random
|
||||
import time
|
||||
from typing import Any
|
||||
import pygame
|
||||
import multiprocessing
|
||||
|
||||
|
||||
class Game:
|
||||
|
||||
def __init__(self, stdscr):
|
||||
def __init__(self, stdscr, queue):
|
||||
# Get curses standard screen
|
||||
self.stdscr = stdscr
|
||||
|
||||
# Multiprocessing Queue
|
||||
self.queue = queue
|
||||
|
||||
# Defaults
|
||||
self.snake_length = 3
|
||||
self.snake_speed = 7
|
||||
@@ -50,6 +55,9 @@ class Game:
|
||||
# Get terminal (screen) height and width
|
||||
self.screen_height, self.screen_width = self.stdscr.getmaxyx()
|
||||
|
||||
# Start Music
|
||||
self.queue.put('music.start')
|
||||
|
||||
# Show selcome page
|
||||
self.load_welcome()
|
||||
|
||||
@@ -178,12 +186,14 @@ class Game:
|
||||
new_head[1] in [0, self.screen_width - 1] or
|
||||
new_head in snake):
|
||||
# Crashed into something, show quit screen
|
||||
self.queue.put('effect.wall')
|
||||
break
|
||||
|
||||
snake.insert(0, new_head)
|
||||
|
||||
# Check if snake ate food
|
||||
if snake[0] == food:
|
||||
self.queue.put('effect.eat')
|
||||
score += 1
|
||||
food = get_new_food()
|
||||
else:
|
||||
@@ -219,7 +229,7 @@ class Game:
|
||||
return
|
||||
elif key == ord('q'):
|
||||
# Game over, exit app!
|
||||
exit()
|
||||
self.exit_game()
|
||||
|
||||
|
||||
def print_center(self, value, y_offset = 0, color = 3):
|
||||
@@ -308,12 +318,63 @@ class Game:
|
||||
def refresh_screen(self):
|
||||
self.stdscr.refresh()
|
||||
|
||||
def exit_game(self):
|
||||
self.queue.put('quit')
|
||||
exit()
|
||||
|
||||
|
||||
def main(stdscr):
|
||||
game = Game(stdscr)
|
||||
|
||||
def main(stdscr, queue):
|
||||
game = Game(stdscr, queue)
|
||||
game.load()
|
||||
|
||||
|
||||
def sound_process(sound_queue):
|
||||
# Initialize pygame mixer in the new process
|
||||
pygame.mixer.init()
|
||||
|
||||
effects = {
|
||||
'eat': pygame.mixer.Sound('effects/bing.mp3'),
|
||||
'wall': pygame.mixer.Sound('effects/cut.mp3'),
|
||||
'self': pygame.mixer.Sound('effects/cut.mp3'),
|
||||
}
|
||||
|
||||
# Load sounds/music (adjust file paths as needed)
|
||||
# Use .wav or .ogg for better compatibility
|
||||
#effect = pygame.mixer.Sound('effects/bing.mp3')
|
||||
#music_file = 'music/Guitar_Sound.mp3'
|
||||
music_file = 'music/s.mp3'
|
||||
|
||||
while True:
|
||||
if not sound_queue.empty():
|
||||
command = sound_queue.get()
|
||||
if command == 'effect.eat':
|
||||
effects['eat'].play()
|
||||
if command == 'effect.wall':
|
||||
effects['wall'].play()
|
||||
if command == 'effect.self':
|
||||
effects['self'].play()
|
||||
elif command == 'music.start':
|
||||
pygame.mixer.music.load(music_file)
|
||||
pygame.mixer.music.set_volume(0.25)
|
||||
pygame.mixer.music.play(-1) # Play music indefinitely
|
||||
elif command == 'music.stop':
|
||||
pygame.mixer.music.stop()
|
||||
elif command == 'quit':
|
||||
break
|
||||
time.sleep(0.1) # Small delay to prevent high CPU usage
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
game = curses.wrapper(main)
|
||||
|
||||
# Create a queue for communication
|
||||
queue = multiprocessing.Queue()
|
||||
|
||||
# Start the sound process
|
||||
p = multiprocessing.Process(target=sound_process, args=(queue,))
|
||||
p.start()
|
||||
|
||||
# Start curses game loop
|
||||
game = curses.wrapper(main, queue)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user