Skip to content

Commit a083758

Browse files
committed
bit comments
1 parent 5ee84ce commit a083758

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

astar.py

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55

66
class AStar:
7+
"""
8+
A star algorithm implementation
9+
f(n) = g(n) + h(n)
10+
"""
11+
712
def __init__(self):
813
self.paths = [
914
KEY_RIGHT,
@@ -21,9 +26,11 @@ def __init__(self):
2126
self.moves = 0
2227

2328
def collides(self, headPosition, snake):
29+
""" Check for body collision on the next step """
2430
return any([body.position == headPosition for body in snake.body[: -1]])
2531

2632
def getDistances(self, goal, current, snake):
33+
""" Finding distance for each path """
2734
distances = PriorityQueue()
2835
self.moves += 1
2936

@@ -58,6 +65,7 @@ def getDistances(self, goal, current, snake):
5865
return distances
5966

6067
def getKey(self, food, snake):
68+
""" Returns the next step """
6169
if snake.head.x == food.x and snake.head.y:
6270
self.moves = 0
6371
return snake.direction

components.py

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66

77
class Body(object):
8+
""" Each body has a x & y """
89
def __init__(self, x, y, char=BODY_CHAR):
910
self.x = x
1011
self.y = y
@@ -16,6 +17,7 @@ def position(self):
1617

1718

1819
class Food(object):
20+
""" Food with random location """
1921
def __init__(self, window, snake, char=FOOD_CHAR):
2022
self.x = randint(10, MAX_X - 10)
2123
self.y = randint(10, MAX_Y - 10)
@@ -38,6 +40,7 @@ def collides(self):
3840

3941

4042
class Snake(object):
43+
""" Snake with multiple body """
4144
def __init__(self, x, y, window):
4245
self.window = window
4346
self.body = list()

main.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77

88
class StdOutWrapper:
9+
""" Helper to print on terminal """
910
text = ""
1011

1112
def write(self, txt):
@@ -32,7 +33,7 @@ def startGame():
3233
window.border(0)
3334

3435
snake = Snake(SNAKE_X, SNAKE_Y, window)
35-
food = Food(window)
36+
food = Food(window, snake)
3637
astar = AStar()
3738

3839
while True:

0 commit comments

Comments
 (0)