def __init__(self, colour=None, size=None, position=None): pygame.sprite.Sprite.__init__(self) # Make sure snake valid parameters if colour == None: colour = Snake._DEFAULT_COLOUR if size == None: size = Snake._DEFAULT_SIZE #if size[0] != size[1]: # raise Exception('Invalid tile size. Width and height must be equal.') if position == None: position = Snake._DEFAULT_POSITION self.color = colour self.size = size #self.head = Snake._SnakeHead(colour, size, position) self.segments.append(Snake._SnakeSegment(colour, size, position)) self.head = self.segments[0] #self.tail = Snake._SnakeTail() self.curseTail = 0 self.direction = Snake.SnakeMove.UP print "TODO: make this use self.adjust_tail_size()" for x in range(1, Config.INITIAL_LENGTH): # Initial Length tailposition = [(position[0] - x*size[0]), position[1]] # self.tail.add_tail_section(colour, size, tailposition) self.segments.append(Snake._SnakeSegment(HelperFunctions.random_rgb(), size, tailposition))
def adjust_tail_size(self, number): size = self.size[0] current_direction = self.direction if number > 0: for count in range(number): # ### TODO - randomly generate from the colour of the food eaten if self.curseTail > 0: colour = Config.BACKGROUND_COLOUR else: colour = HelperFunctions.random_rgb() # Randomise colour of new tail section lastindex = len(self.segments) - 1 X = self.segments[lastindex].rect.topleft[0] Y = self.segments[lastindex].rect.topleft[1] # New tail section position if current_direction == Snake.SnakeMove.RIGHT: X = X - size + (count*size) elif current_direction == Snake.SnakeMove.LEFT: X = X + size + (count*size) elif current_direction == Snake.SnakeMove.UP: Y = Y - size + (count*size) elif current_direction == Snake.SnakeMove.DOWN: Y = Y + size + (count*size) self.segments.append(Snake._SnakeSegment(colour, self.size, [X, Y])) else: for count in range(abs(number)): # leave at least the head if(len(self.segments) > 1): del self.segments[-1]
def curse_tail(self): self.randomize_snake_colour() self.curseTail = Config.FOOD_CURSE_TIME_TO_WEAR_OFF * Config.FPS for (idx, s) in enumerate(self.segments): if idx > 0: s.set_colour(Config.BACKGROUND_COLOUR) else: s.set_colour(HelperFunctions.random_rgb())
def randomize_snake_colour(self): for s in self.segments: s.set_colour(HelperFunctions.random_rgb())