Skip to content

Commit

Permalink
Snake's swiftness + better game borders handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
amtoine committed Jul 5, 2021
1 parent edc951d commit 4dc9ec4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Apple.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def spawn(self, y, x, h, w):
w : int
the width of the game area.
"""
self.pos = (np.random.randint(y+1, y + h - 1), np.random.randint(x+1, x + w - 1))
self.pos = (np.random.randint(y, y + h), np.random.randint(x, x + w))
log(self.pos)

def show(self, stdscr):
Expand Down
28 changes: 19 additions & 9 deletions src/Snake.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Snake:
SELF_BITE = -1
OUTSIDE = -2

def __init__(self):
def __init__(self, swiftness):
# the body and the assets (resp. the positions of the parts and the assets used for rendering)
self.body, self.assets = None, None
# a cache of the scene used in the game.
Expand All @@ -34,6 +34,11 @@ def __init__(self):
# a list containing parts to add to the snake, allows better animation.
self.trail = []

# the swiftness of the snake, inversely proportional to the rate at which the snake moves.
# the number of life steps that the snake experienced.
self.swiftness = swiftness
self.life_step = None

def spawn(self, y, x, h, w, init_length=3):
"""
Spawns the apple inside the game area.
Expand Down Expand Up @@ -74,6 +79,9 @@ def spawn(self, y, x, h, w, init_length=3):
# the assets are simply the head plus the right amount of body parts.
self.assets = ['@'] + ['|' if self.dir in [curses.KEY_UP, curses.KEY_DOWN] else '-'] * len(tmp)

# reset the number of life steps.
self.life_step = 0

def change_direction(self, new_dir):
"""
Changes the direction of the snake if it is different and compatible with current one,
Expand Down Expand Up @@ -143,14 +151,16 @@ def move(self):
-------
None
"""
# pop the head.
self.body.pop()
self.assets.pop()
self.life_step += 1
if self.life_step % self.swiftness == 0:
# pop the head.
self.body.pop()
self.assets.pop()

# compute and insert the new head and the neck asset.
new_head = self.body[0][0] + _directions[self.dir][0], self.body[0][1] + _directions[self.dir][1]
self.body.insert(0, new_head)
self.assets.insert(1, self.neck_asset)
# compute and insert the new head and the neck asset.
new_head = self.body[0][0] + _directions[self.dir][0], self.body[0][1] + _directions[self.dir][1]
self.body.insert(0, new_head)
self.assets.insert(1, self.neck_asset)

# complete the snake with its trail.
if self.trail:
Expand Down Expand Up @@ -186,7 +196,7 @@ def get_head(self):

def inside(self, y, x, h, w):
""" Checks whether the head of the snake is strictly inside the scene. """
return (y < self.body[0][0] < y + h - 1) and (x < self.body[0][1] < x + w - 1)
return (y <= self.body[0][0] < y + h) and (x <= self.body[0][1] < x + w)

def self_intersect(self):
""" Returns a boolean telling if the snake bit its own tail. """
Expand Down

0 comments on commit 4dc9ec4

Please sign in to comment.