Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 69d655f

Browse files
committed
update documentation
1 parent d9cb648 commit 69d655f

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

projects/Snake Game/src/display.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44

55
class Display:
6+
"""Manages the display of the game."""
67
def __init__(self):
78
pygame.init()
89
self.width = GameSettings.WIDTH
@@ -13,6 +14,14 @@ def __init__(self):
1314
self.clock = pygame.time.Clock()
1415

1516
def update_ui(self, snake, food, score, high_score):
17+
"""Updates the UI with the current game state.
18+
19+
Args:
20+
snake (Snake): The snake object that contains the snake body (Snake.blocks).
21+
food (Point): The food object to be displayed.
22+
score (int): The current game score.
23+
high_score: The highest score achieved so far.
24+
"""
1625
self.window.fill(RgbColors.BLACK)
1726
self.draw_snake(snake)
1827
self.draw_food(food)
@@ -40,7 +49,7 @@ def draw_food(self, food):
4049
def draw_score(self, score):
4150
self.font = pygame.font.Font(None, 25)
4251
score_display = self.font.render(f"Score: {score}", True, RgbColors.WHITE)
43-
self.window.blit(score_display, [0, 0]) # score in top left window corner
52+
self.window.blit(score_display, [0, 0])
4453

4554
def render_game_over(self):
4655
self.font = pygame.font.Font(None, 48)

projects/Snake Game/src/game.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88

99
class Game:
10+
"""Manages the gameplay logic and its user interactions."""
1011
def __init__(self):
1112
self.display = Display()
1213
self.snake = Snake()
@@ -33,6 +34,11 @@ def game_loop(self):
3334
pygame.quit()
3435

3536
def is_collision(self):
37+
"""Checks if the snake has collided with the boundary or with itself.
38+
39+
Returns:
40+
bool: True if a collision is detected, False otherwise.
41+
"""
3642
# Snake hits boundary
3743
if (
3844
self.snake.head.x > self.display.width - self.snake.block_size
@@ -65,6 +71,7 @@ def get_user_input(self):
6571
self.snake.direction = Direction.DOWN
6672

6773
def play_step(self):
74+
"""Executes one step through the game."""
6875
self.get_user_input()
6976
self.snake.move(self.snake.direction)
7077
if self.snake.head == self.food:
@@ -79,6 +86,7 @@ def play_step(self):
7986
return game_over, self.score
8087

8188
def place_food(self):
89+
"""Randomly places the food on the screen."""
8290
x = random.randint(0, (
8391
self.display.width - GameSettings.BLOCK_SIZE) // GameSettings.BLOCK_SIZE) * GameSettings.BLOCK_SIZE
8492
y = random.randint(0, (
@@ -88,6 +96,7 @@ def place_food(self):
8896
self.place_food()
8997

9098
def play_again(self):
99+
"""Asks the user to play again or quit the game."""
91100
while True:
92101
for event in pygame.event.get():
93102
if event.type == pygame.QUIT:
@@ -99,12 +108,14 @@ def play_again(self):
99108
return True
100109

101110
def restart_game(self):
111+
"""Resets the state of the game."""
102112
self.snake = Snake()
103113
self.score = 0
104114
self.place_food()
105115
self.high_score = self.load_high_score()
106116

107117
def load_high_score(self):
118+
"""Loads the high score from a JSON file."""
108119
try:
109120
with open('high_score.json', 'r') as file:
110121
data = json.load(file)
@@ -113,6 +124,7 @@ def load_high_score(self):
113124
return 0
114125

115126
def update_high_score(self, new_score):
127+
"""Updates the high score in the JSON file if the new score is greater than the current high score."""
116128
high_score = self.load_high_score()
117129
if new_score > high_score:
118130
data = {"high_score": new_score}

projects/Snake Game/src/snake.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,28 @@
22

33

44
class Snake:
5+
"""Represents the snake in the game."""
56
def __init__(self, init_length=3):
7+
"""Initializes the snake.
8+
9+
Args:
10+
init_length (int): Length of the snake on initialization.
11+
"""
612
self.head = Point(GameSettings.WIDTH / 2, GameSettings.HEIGHT / 2)
713
self.block_size = GameSettings.BLOCK_SIZE
814
self.blocks = ([self.head] +
915
[Point(self.head.x - (i * self.block_size), self.head.y) for i in range(1, init_length)])
1016
self.direction = Direction.RIGHT
1117

1218
def move(self, direction):
19+
"""Moves the snake in the given direction.
20+
21+
Args:
22+
direction (Direction): The direction to move the snake.
23+
24+
Returns:
25+
Point: The new snake head position.
26+
"""
1327
x, y = self.head
1428
if direction == Direction.RIGHT:
1529
x += self.block_size
@@ -24,6 +38,11 @@ def move(self, direction):
2438
return self.head
2539

2640
def self_collision(self):
41+
"""Checks if the snake collides with itself.
42+
43+
Returns:
44+
bool: True if the snake collides with its body, False otherwise.
45+
"""
2746
if self.head in self.blocks[1:]:
2847
return True
2948
return False

0 commit comments

Comments
 (0)