Exercise 5 · Chapter: GravityOptional+50 XP
A Thrown Ball
Throw the ball sideways as well as dropping it.
Start at x of 2, y of 0, with dx of 2 and vy of 0. Each frame:
xmoves bydx— no gravity, sideways speed doesn't changeyandvydo exactly what they did before
The path you get is an arc: fast and flat at first, then curving down. That's a real thrown-ball path, and nobody wrote a curve. It's a straight line sideways and an accelerating fall, happening at once.
Each axis minding its own business, again. Same lesson as Chapter 3, and it keeps paying.
Expected output
4/1 6/3 8/6 10/10 12/13 14/10 16/8 18/7 20/7
Need a hint?
3 available · costs 5 XP each
python
def new_game():
return {"x": 2, "y": 0, "dx": 2, "vy": 0}
def update(state, keys):
state["vy"] = state["vy"] + 1
state["y"] = state["y"] + state["vy"]
if state["y"] >= HEIGHT - 1:
state["y"] = HEIGHT - 1
state["vy"] = -state["vy"] * 3 // 4
# The ball falls, but never travels sideways. Move x by dx.
return state
def draw(state):
grid = blank_grid()
grid[state["y"]][state["x"]] = "gold"
return grid