Skip to content

Commit 5e5b6d7

Browse files
Merge pull request steam-bell-92#784 from dhruvpatel16120/fix-snakegame
fix: make pygame dependency optional in Snake Game (steam-bell-92#635)
2 parents b49dd96 + db47c5a commit 5e5b6d7

2 files changed

Lines changed: 34 additions & 14 deletions

File tree

games/Snake-Game/Snake-Game.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import turtle
22
import random
33
import time
4-
import pygame
5-
64
# ================= SOUND =================
5+
try:
6+
import pygame
7+
pygame.mixer.init()
8+
eat_sound = pygame.mixer.Sound("sounds/eat.wav")
9+
gameover_sound = pygame.mixer.Sound("sounds/gameover.wav")
10+
pygame_installed = True
11+
except ImportError:
12+
pygame_installed = False
13+
print("⚠️ Warning: pygame module not found. Game will run without sound effects.")
714

8-
pygame.mixer.init()
9-
eat_sound = pygame.mixer.Sound("sounds/eat.wav")
10-
gameover_sound = pygame.mixer.Sound("sounds/gameover.wav")
1115

1216
# ================= SCREEN SETUP =================
1317

@@ -196,7 +200,8 @@ def move():
196200
def reset_game():
197201
global score, level, speed
198202

199-
gameover_sound.play()
203+
if pygame_installed:
204+
gameover_sound.play()
200205

201206
time.sleep(1)
202207

@@ -234,7 +239,8 @@ def reset_game():
234239
# Food collision
235240
if head.distance(food) < GRID_SIZE:
236241

237-
eat_sound.play()
242+
if pygame_installed:
243+
eat_sound.play()
238244

239245
score += current_food["points"]
240246

games/Snake-Game/main.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
import pygame
21
import turtle
32
import time
43
from constants import SCREEN_WIDTH, SCREEN_HEIGHT, COLLISION_DISTANCE, SLEEP_TIME
54
from snake import Snake
65
from food import Food
76
from scoreboard import Scoreboard
87

8+
try:
9+
import pygame
10+
pygame_installed = True
11+
except ImportError:
12+
pygame_installed = False
13+
print("⚠️ Warning: pygame module not found. Game will run without sound effects.")
914

1015
class SnakeGame:
1116
def __init__(self):
@@ -29,9 +34,15 @@ def __init__(self):
2934
self.scoreboard = Scoreboard()
3035

3136
# Sound setup
32-
pygame.mixer.init()
33-
self.eat_sound = pygame.mixer.Sound("sounds/Apple_Eating.mp3")
34-
self.gameover_sound = pygame.mixer.Sound("sounds/Game_over.mp3")
37+
self.pygame_installed = pygame_installed
38+
if self.pygame_installed:
39+
try:
40+
pygame.mixer.init()
41+
self.eat_sound = pygame.mixer.Sound("sounds/Apple_Eating.mp3")
42+
self.gameover_sound = pygame.mixer.Sound("sounds/Game_over.mp3")
43+
except Exception as e:
44+
print(f"⚠️ Warning: Could not initialize pygame mixer: {e}")
45+
self.pygame_installed = False
3546

3647
# Keyboard bindings
3748
self.screen.listen()
@@ -83,7 +94,8 @@ def run(self):
8394

8495
# Boundary collision
8596
if self.snake.check_boundary_collision():
86-
self.gameover_sound.play()
97+
if self.pygame_installed:
98+
self.gameover_sound.play()
8799
self.snake.reset()
88100
self.scoreboard.reset()
89101
self.delay = SLEEP_TIME
@@ -95,7 +107,8 @@ def run(self):
95107
self.snake.add_part()
96108

97109
self.scoreboard.increase()
98-
self.eat_sound.play()
110+
if self.pygame_installed:
111+
self.eat_sound.play()
99112

100113
# Level system
101114
if self.scoreboard.score % 5 == 0:
@@ -113,7 +126,8 @@ def run(self):
113126

114127
# Self collision
115128
if self.snake.check_self_collision():
116-
self.gameover_sound.play()
129+
if self.pygame_installed:
130+
self.gameover_sound.play()
117131
self.snake.reset()
118132
self.scoreboard.reset()
119133
self.delay = SLEEP_TIME

0 commit comments

Comments
 (0)