At present, most of the longitudinal car-following control algorithms based on model predictive control (MPC) do not consider the influence of the presence of the sloping road on the inter-vehicle distance, resulting in poor tracking capability under ramp conditions. In order to reduce the inter-vehicle distance error under ramp conditions and improve the tracking capability of longitudinal car-following control algorithm. The car-following control algorithm based on MPC considering uphill and downhill is proposed. This algorithm is based on the vehicle structure of fuel passenger cars, and adds a slope angle reconstruction module for implementing slope angle measurement and reducing the complexity of slope angle calculation based on the framework of conventional hierarchical control structure. Meanwhile, in the upper-level controller, the MPC algorithm is improved by categorically discussing the actual inter-vehicle distance on horizontal roads and slope roads, and by introducing offsets that consider the slope of the road into the formula for the desired inter-vehicle distance. In the lower-level controller, the presence of slope resistance and slope angle in the vehicle longitudinal dynamics equation are additionally considered to further improve the accuracy of the lower-level actuators in executing the control signals transmitted from the upper-level controller on the ramp. Finally, the tracking capability of the algorithm is verified by using Carsim & Matlab/Simulink co-simulation platform. The results show that under the control of the car-following control algorithm proposed in this paper, the number of road slope segments is reduced by 60%, and the inter-vehicle distance error is reduced by 15% and 3.1% in the car-following process of uphill and downhill, respectively, compared with the conventional car-following algorithm. The algorithm in this paper has better car-following accuracy on the road with changing slope.