# Reverse Direction¶

Detect when we hit the right edge then reverse direction.

## Code¶

```import arcade

def __init__(self, width, height, title, bg_color):
super().__init__(width, height, title)
self.width = width
self.height = height
self.position = 0
self.velocity = 200

def on_draw(self):
y = self.height / 2

def update(self, delta_time):
self.position += self.velocity * delta_time

# Did the circle hit the right side of screen?
is_at_right = self.position > self.width - self.radius
if is_at_right:
self.velocity *= -1

def main():
game1 = MyGame(600, 600, 'Drawing Example', arcade.color.WHEAT)
game1.position = 10

if __name__ == '__main__':
main()
```

## Analysis¶

1. We put the starting position at 10.

2. We introduce concept of velocity in the constructor.

3. Velocity is two things:

• How fast we are moving
• Which direction we are moving
• A negative number means we are moving that fast, backwards
• Note: This number is a LOT bigger for Arcade 2.0
4. No changes to `on_draw`…shows the point that we change the data and let the drawing take care of itself.

5. Important changes in `update`

6. On each tick, move `self.position` (really, `center_x`) based on:

• Velocity
• `delta_time` meaning, how long since we were last called
• Makes sense
7. Introduce a conditional, aka the `if`

8. How do we know if we are at the right edge?

• The right edge is a certain number of pixels from the origin
• The number is…the window width
• We care about the edge of the circle, not the center
• So we have to use the radius
• If `center + radius` is at the edge, turn around
9. How do we turn around? Change the velocity to reverse.

## Exercises¶

1. Try making the circle bigger. Does the formula still work?
2. Same for the window width.

## Analysis¶

1. We could have skipped a separate line for `is_at_right` and just done that formula in the `if`. What are some reasons for doing it separately?