User avatar
D.E.L.B.
Posts: 92
Joined: Sat Jun 09, 2012 7:16 pm
Location: Wales, UK
Contact: Website

Don't get this part of the code...

Fri Jul 20, 2012 2:37 pm

If you take a look at this code with the provided link below, then I will then pinpoint out what the problem is:
http://dl.dropbox.com/u/30557697/Raspbe ... gSnake.zip

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
	
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.

Any help would be appreciated. Thanks!

Gibble
Posts: 56
Joined: Wed May 09, 2012 9:52 am

Re: Don't get this part of the code...

Fri Jul 20, 2012 4:57 pm

Hi, not a Python chap, but try this, to see if it helps break it down and makes sense...

Code: Select all

#!/usr/bin/env python

class Direction:
         right = 1
         left = 2
         up = 3
         down = 4

class Snake:
         def __init__(self):
                self.direction = Direction.right

mysnake = Snake()

print mysnake.direction

jardiamj
Posts: 18
Joined: Thu Jan 19, 2012 7:11 pm

Re: Don't get this part of the code...

Fri Jul 20, 2012 6:47 pm

First of all, you have a class called Direction that sets the values for Direction.right, Direction.left, etc. that are used all along the program.

Then I can see that are two classes that have a "direction" variable: SnakePart and Snake, both get initialize to Direction.right as you can see in the code:

Code: Select all

class SnakePart:
	'''This is a class one block of the snake'''
	def __init__(self, screenRect, x = 0, y = 0, direction = Direction.right):
		'''Set our direction to the one input'''
		self.direction = direction
The initialization of the SnakePart class gets "direction" passed as an argument with a default value of Direction.right and then it gets assigned to self.direction.

Code: Select all

class Snake:
	'''This class handles many parts of a snake'''
	
	def __init__(self, screenRect, x = 0, y = 0):
		'''Set our screen rectangle to the one being passd in'''
		self.screenRect = screenRect
		
		'''Set our default direction to right'''
		self.direction = Direction.right
When the Snake class get initialized it's self.direction gets set to Direction.right.
Both SnakePart and Snake have a "changeDirection(self, direction)" function that sets the direction to a direction passed to that function.

And the change of direction happens in the MainLoop when self.HandleEvents() is called:

Code: Select all

'''Handle all events'''
	   self.HandleEvents()

Code: Select all

	def HandleEvents(self):
		'''Handles all events for PiSnake'''
		for event in pygame.event.get():
			'''If there is a quit event in the collection of events'''
			if event.type == pygame.QUIT:
				'''We need to exit'''
				sys.exit()
			
			'''Do we have a keydown event?'''
			if event.type == KEYDOWN:
				'''If we do then check which one and set the snakes direction'''
				if event.key == K_RIGHT:
					self.snake.changeDirection(Direction.right)
				elif event.key == K_LEFT:
					self.snake.changeDirection(Direction.left)
				elif event.key == K_DOWN:
					self.snake.changeDirection(Direction.down)
				elif event.key == K_UP:
					self.snake.changeDirection(Direction.up)
				elif event.key == K_SPACE:
					'''If the user presses space, extend the snake'''
					self.snake.extending = True					
This HandleEvents class takes your key presses and passes the direction you press to the snake.changeDirection function, and that changes the direction of the snake.

I hope this helps, it don't really know the Object Oriented terminology very well (I don't really care so much about terminology any way), but I hope you understand my explanation.

User avatar
D.E.L.B.
Posts: 92
Joined: Sat Jun 09, 2012 7:16 pm
Location: Wales, UK
Contact: Website

Re: Don't get this part of the code...

Sat Jul 21, 2012 7:03 pm

Just as I expected.

Thank you very much.

jardiamj
Posts: 18
Joined: Thu Jan 19, 2012 7:11 pm

Re: Don't get this part of the code...

Sat Jul 21, 2012 7:42 pm

You are very welcome!
I'm glad I could help.

Return to “Python”