Computer Programming Challenge - Physics Game
Not able to fully understand what I have to do to stop the loop from repeating, because break and continue do not work.... sorry this is my first time coding but no idea what to do to stop the codes
1. main.py
import ball
import graphics
output_width = 19
ball_position = 1
ball_speed = 5
for time_step in range(100):
# Show the ball's position at the current time step.
print(graphics.show_screen(ball_position, output_width))
ball_position = ball.move(ball_position, ball_speed)
2. ball.py
"""Simulates a ball's movement and collision with the edges of a screen."""
def move(position, speed):
"""Returns the ball's new position after one time step.
The ball moves in straight line at the given speed.
"""
return position + speed
def maybe_bounce(position, speed, right_wall):
"""Returns the ball's new speed, which stays the same unless the ball
bounces off of a wall.
"""
if position >= right_wall:
# Reverses direction and loses a bit of speed.
speed = speed * -0.75
return speed
3. graphics.py
"""Displays a ball's horizontal position on a screen."""
def show_screen(ball_position, screen_size):
"""Returns a graphical representation of the ball's position."""
position = round(ball_position)
# The position is offscreen so no ball is drawn.
if position < 0 or position > screen_size:
return "|" + (screen_size + 1) * " " + "|"
spaces_before_ball = position * " "
spaces_after_ball = (screen_size - position) * " "
return "|" + spaces_before_ball + "o" + spaces_after_ball + "|"
Please sign in to leave a comment.