Python for fun 4


Today we work on animation with hundreds of triangles, balls, squares and vickies (you can call the shapes anything you like).   Shapes are defined by coordinates (x,y).   A huge improvement over last time is that we use a loop to add shape to lists of shapes.
More

Python for fun 3: bouncy ball animation

We are working on animations this week.  While terminologies like coordinates may sound scary, but kids are very fast at learning patterns and understand what these concepts are if we just take breaks and explain a little bit.

import random
from tkinter import *
import time

v = Tk()
canvas = Canvas(v,width=500,height=400)
v.title(“Victoria’s bouncy ball animation”)
canvas.pack()

ball= canvas.create_oval(10,10,60,60, fill=”blue”)
for i in range(400):
    canvas.move(ball,1,1)
    v.update()
time.sleep(0.01)

canvas.mainloop() #this is for the canvas to stay put

#The following animation is much more interesting than the one above because we are making 5 bouncy balls with different colors and speed (you can make as many as you like). But the first animation serves as a good beginning step.   Note that the way we make balls in this section of code is not very smart because we have to repeat chunks of code 5 times.  We will learn the smart way after we have mastered the dumb method. 

import random
from tkinter import * 
import time

WIDTH =600 #you set up size of picture frame
HEIGHT=400

v = Tk()
canvas = Canvas(v,width=WIDTH,height=HEIGHT)
v.title(“Victoria’s animation”)
canvas.pack()

ball= canvas.create_oval(10,10,40,40, fill=”red”)
xspeed= 4
yspeed= 5

while True: #means keep moving and don’t stop until you make it stop
    canvas.move(ball,xspeed,yspeed)
    position = canvas.coords(ball) #coordinates of the ball
    if position[3]>= HEIGHT or position[1]<=0:
        yspeed=-yspeed  #These two “if” statements are to make the ball bounce off the frame 
    if position[2]>= WIDTH or position[0]<=0: #In Python, the first number in a list is indexed as 0
        xspeed=-xspeed
    v.update()
    time.sleep(0.01)

canvas.mainloop() #this is for the canvas to stay put

#5 bouncy balls (but you can make as many as you like)

import random
from tkinter import *
import time

WIDTH =900 #you set up size of picture frame
HEIGHT=800

v = Tk()
canvas = Canvas(v,width=WIDTH,height=HEIGHT)
v.title(“Victoria’s animation”)
canvas.pack()

ball= canvas.create_oval(10,10,40,40, fill=”red”)
xspeed= 10
yspeed= 10

ball2= canvas.create_oval(10,10,40,40, fill=”blue”)
xspeed2= 5
yspeed2= 5

ball3= canvas.create_oval(10,10,40,40, fill=”yellow”)
xspeed3= 7
yspeed3= 3
ball4= canvas.create_oval(10,10,40,40, fill=”orange”)
xspeed4= 11
yspeed4= 6

ball5= canvas.create_oval(10,10,40,40, fill=”green”)
xspeed5= 3
yspeed5= 7

while True: #means keep moving and don’t stop until you make it stop
#ball1
canvas.move(ball,xspeed,yspeed)
position = canvas.coords(ball) #coordinates of the ball
if position[3]>= HEIGHT or position[1]<=0:
    yspeed=-yspeed
if position[2]>= WIDTH or position[0]<=0: #In Python, the first number in a list is indexed as 0
    xspeed=-xspeed
#ball2
canvas.move(ball2,xspeed2,yspeed2)
position = canvas.coords(ball2) #coordinates of the ball
if position[3]>= HEIGHT or position[1]<=0:
    yspeed2=-yspeed2
if position[2]>= WIDTH or position[0]<=0: #In Python, the first number in a list is indexed as 0
    xspeed2=-xspeed2
#ball3
canvas.move(ball3,xspeed3,yspeed3)
position = canvas.coords(ball3) #coordinates of the ball
if position[3]>= HEIGHT or position[1]<=0:
    yspeed3=-yspeed3
if position[2]>= WIDTH or position[0]<=0: #In Python, the first number in a list is indexed as 0
    xspeed3=-xspeed3
#ball4
canvas.move(ball4,xspeed4,yspeed4)
position = canvas.coords(ball4) #coordinates of the ball
if position[3]>= HEIGHT or position[1]<=0:
    yspeed4=-yspeed4
if position[2]>= WIDTH or position[0]<=0: #In Python, the first number in a list is indexed as 0
    xspeed4=-xspeed4
#ball5
canvas.move(ball5,xspeed5,yspeed5)
position = canvas.coords(ball5) #coordinates of the ball
if position[3]>= HEIGHT or position[1]<=0:
    yspeed5=-yspeed5
if position[2]>= WIDTH or position[0]<=0: #In Python, the first number in a list is indexed as 0
    xspeed5=-xspeed5

v.update()
time.sleep(0.001)

canvas.mainloop() #this is for the canvas to stay put

#We define a class of item call Ball that have some ways of doing things; Then we create 200 red bouncy balls.

import random
from tkinter import *
import time

WIDTH =900 #you set up size of picture frame
HEIGHT=800

v = Tk()
canvas = Canvas(v,width=WIDTH,height=HEIGHT)
v.title(“Victoria’s bouncy balls”)
canvas.pack()

class Ball:
def __init__(self,color,size):
self.shape = canvas.create_oval(10,10, size,size,fill=color)
self.xspeed = random.randrange(1,15)
self.yspeed = random.randrange(1,15)

def move(self):
canvas.move(self.shape, self.xspeed, self.yspeed)
position = canvas.coords(self.shape) #coordinates of the ball
if position[3]>= HEIGHT or position[1]<=0:
self.yspeed=-self.yspeed
if position[2]>= WIDTH or position[0]<=0:
self.xspeed =-self.xspeed

balls= []
for i in range(200):
balls.append(Ball(“red”,30))

while True:
for ball in balls:
ball.move()

v.update()
time.sleep(0.001)

canvas.mainloop() #this is for the canvas to stay put