From 4dc9ec41d583a66766c696ebab29529b0865d20b Mon Sep 17 00:00:00 2001 From: Antoine Stevan Date: Mon, 5 Jul 2021 11:50:20 +0200 Subject: [PATCH] Snake's swiftness + better game borders handling. --- src/Apple.py | 2 +- src/Snake.py | 28 +++++++++++++++++++--------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/Apple.py b/src/Apple.py index 20e6b54..be487bc 100644 --- a/src/Apple.py +++ b/src/Apple.py @@ -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): diff --git a/src/Snake.py b/src/Snake.py index f37b5a7..ef0850a 100644 --- a/src/Snake.py +++ b/src/Snake.py @@ -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. @@ -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. @@ -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, @@ -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: @@ -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. """