http://dl.dropbox.com/u/30557697/Raspbe ... gSnake.zip
See I don't know where the newDirection = self.direction is being set, there are a lot of self.directions' in the code if you take a look at it, some have a parameter within the class of where the rest of the methods are being set, like "direction = Direction.right", so this is where I am confused.
Code: Select all
def update(self): '''Update each snake part''' '''Set the new direction to our new direction''' newDirection = self.direction '''Create variables called oldDirection and newPart that will be used in the loop below''' oldDirection = None newPart = None if self.extending: '''If we are extending, set lastPart to the latest item in the list of parts''' lastPart = self.parts[-1] '''Set new part to a new instance of snake part with the, location and direction of the last part''' newPart = SnakePart(self.screenRect, lastPart.rect.x, lastPart.rect.y, lastPart.direction) '''We need to go through each part with a while loop''' '''Create a variable to keep track of what part we are on''' count = 0 '''Get the total number of parts''' totalParts = len(self.parts) '''Create a variable as a stopping condition... will be set if one of the parts hits the side''' updateResult = True while (count < totalParts) and (updateResult == True): '''Set the oldDirection to the parts current direction and change the parts direction to the new direction''' oldDirection = self.parts[count].direction self.parts[count].changeDirection(newDirection) '''Run the parts update function, if a part touched the edge it will have returned false so get the result''' updateResult = self.parts[count].update() '''Set the new direction to old direction for the next part''' newDirection = oldDirection '''Increment the count''' count += 1 if self.extending: '''If we are extending then run the extend function''' self.extend(newPart) def changeDirection(self, direction): '''Changes the snakes direction''' '''Variable to know if we want to direction or not''' change = True '''If our current direction is opposite to the one we chose, set change to false''' if (self.direction == Direction.right) and (direction == Direction.left): change = False elif (self.direction == Direction.left) and (direction == Direction.right): change = False elif (self.direction == Direction.down) and (direction == Direction.up): change = False elif (self.direction == Direction.up) and (direction == Direction.down): change = False if change == True: '''Set our direction to the new direction''' self.direction = direction
Any help would be appreciated. Thanks!