For this assignment, I decided to play with JupyterLab. This is a newer and more future-proof version of Jupyter Notebook. After installing Python3 with the help of this resource, I added JupyterLab with pip using this installation guide.
Within the IDE, you can write Markdown text and code. For personal reference: a refresher of the Markdown syntax can be found here.
To open JupyterLab, type jupyter-lab in Terminal.
In the example below, results are succesfully plotted in JupyterLab. Initially I received an error because matplotlib had not been installed on my machine.
#
# image.py
# Neil Gershenfeld 2/3/11
# image plot example
#
import matplotlib.pyplot as plt
from matplotlib import cm
from numpy import *
l = 15.5
x = arange(-l,l,0.2)
y = arange(-l,l,0.2)
(x,y) = meshgrid(x,y)
r = sqrt(x**2+y**2)
z = sin(r)/r
plt.imshow(z,extent=[-l,l,-l,l],cmap=cm.gray)
plt.show()
Next up: the bouncing ball. This is my first time coding in Jupyter and with Python. Therefore, I consulted the O'Reilly book Learning Python, the Python documentation library and the JupyterLab documentation. Following this example I created a bouncing ball using Python's turtle graphics.
import turtle
# Set key parameters
gravity = -0.005 # pixels/(time of iteration)^2
y_velocity = 1 # pixels/(time of iteration)
x_velocity = 0.25 # pixels/(time of iteration)
energy_loss = 0.95
width = 600
height = 600
# Set window and ball
window = turtle.Screen()
window.setup(width, height)
window.tracer(0)
ball = turtle.Turtle()
ball.penup()
ball.color("#F08080")
ball.shape("circle")
# Main loop
while True:
# Move ball
ball.sety(ball.ycor() + y_velocity)
ball.setx(ball.xcor() + x_velocity)
# Acceleration due to gravity
y_velocity += gravity
# Bounce off the ground
if ball.ycor() < -height / 2:
y_velocity = -y_velocity * energy_loss
# Set ball to ground level to avoid it getting "stuck"
ball.sety(-height / 2)
# Bounce off the walls (left and right)
if ball.xcor() > width / 2 or ball.xcor() < -width / 2:
x_velocity = -x_velocity
window.update()
# output render in new window
In my JupyterLab environment, the output is not rendered below the code, because a new window is opened. To solve this, here's a video of the bouncing ball.
After closing the pop-up screen and running the code again, I receive an error and Python crashes. A similar issue is discussed on Stackoverflow.
The issue seems to be in the while loop, that keeps on running after the window is closed. This can be resolved by adding a if not and return statement. To continue prototyping without the error, the window can be closed with Control + Q.
Next up: let's add more bouncing balls.
import turtle
# Set key parameters
gravity = -0.005 # pixels/(time of iteration)^2
y_velocity = 1 # pixels/(time of iteration)
x_velocity = 0.25 # pixels/(time of iteration)
energy_loss = 0.95
width = 600
height = 600
# Set window and ball
window = turtle.Screen()
window.setup(width, height)
window.tracer(0)
ball = turtle.Turtle()
ball2 = turtle.Turtle() # Adding a second ball
ball.penup()
ball.color("#F08080")
ball.shape("circle")
ball2.penup() # Formatting a second ball
ball2.color("#F08000")
ball2.shape("circle")
# Main loop
while True:
# Move ball
ball.sety(ball.ycor() + y_velocity)
ball.setx(ball.xcor() + x_velocity)
ball2.sety(ball2.ycor() + (y_velocity+0.5)) # It has a higher velocity
ball2.setx(ball2.xcor() + (x_velocity+0.5))
# Acceleration due to gravity
y_velocity += gravity
# Bounce off the ground
if ball.ycor() < -height / 2:
y_velocity = -y_velocity * energy_loss
# Set ball to ground level to avoid it getting "stuck"
ball.sety(-height / 2)
# Bounce off the walls (left and right)
if ball.xcor() > width / 2 or ball.xcor() < -width / 2:
x_velocity = -x_velocity
window.update()
# output render in new window
The second ball with a higher velocity appears on the canvas, but flies out of the window as soon as it can ;-)