Ejemplo n.º 1
0
	def play(self):
		"""Play the game."""
		previousDirection = None
		listener = KeyListener()

		# Start the key listener
		listener.start()
		# Get the command for clearing the screen (dependent on OS)
		clearScreen = "cls" if platform.system() == "Windows" else "clear"
		# Place the first food
		self.placeFood()

		# Main game loop
		while not self.gameOver() and not listener.quit:
			# Display the game
			os.system(clearScreen)
			self.display(listener.paused)

			# Delay movement for the specified time
			time.sleep(self.speed)

			# Update the current direction
			currentDirection = listener.direction

			# Snake cannot immediately reverse direction
			if currentDirection == Direction.opposite(previousDirection):
				currentDirection = previousDirection

			# Move the snake if not paused
			if not listener.paused:
				self.move(currentDirection)

			# Store previous direction
			previousDirection = currentDirection

		# End the key listener
		listener.end()

		# Pause for 1 second
		time.sleep(1)