Lecture Materials

Questions & Answers


Q: Waow! Animation!!

A1:  It's really neat!


Q: When I try to follow the code I get an error saying NameError: name 'Canvas' is not defined. Do I need to import something other than tkinter?

A1:  You'd need to have the line: from graphics import Canvas at the top of your program


Q: What is the difference between Canvas and SimpleImage.blank?

A1:  Canvas is something you can draw shapes, like rectangles and circles, on. SimpleImage is a picture of individual pixels.


Q: Were canvas height and square size defined before main?

A1:  Yes!


Q: What is defined in the variable SQUARE_SIZE?

A1:  It's just a constant that is the size of the square we want to draw.


Q: Can't we just specify canvas.move(rect, <some number other than 1>, 0)?

A1:  That would make the square "jump" that many pixels in one step, not do a smooth animation.


Q: what specifically does canvas.update() do?

A1:  It makes Python redraw everything on the canvas. It doesn't necessarily do updates automatically otherwise.


Q: How does the computer know how long 1/50th of a second is?

A1:  It has an internal clock.


Q: does it make a difference if it sleeps before updating the canvas?

A1:  Yes! Otherwise it doesn’t look like animation. It will just happen instantly.


Q: what is the purpose of the canvas.update() function

A1:  It makes Python redraw everything on the canvas. It doesn't necessarily do updates automatically otherwise.


Q: Stylewise, should we put 1/50 inside a constant?

A1:  Yes.

A2:  Yes! Great idea!


Q: What does canvas.mainloop() do?

A1:  It get Python's drawing/canvas system set up to draw. If you didn't have it, you wouldn't see any of the graphics.


Q: What does canvas.update() do again?

A1:  It makes Python redraw everything on the canvas. It doesn't necessarily do updates automatically otherwise.


Q: without canvas.update() would we only see the final product of the square at location ‘x’ rather than seeing the actual motion of the square

A1:  Yes. You wouldn't see all the intermediate updates to the canvas.


Q: why is while not instead of just while?

A1:  We want to continue doing something as long as the rect is not past the middle of the canvas.


Q: Is there a reference handout for animations?

A1:  No, but there is a section problem on animations this week which will be good practice!


Q: should CANVAS_WIDTH/2 be integer division?

A1:  Generally, it's a good idea to do pixel math with integer division, but not strictly required.


Q: Why do some variables have methods associated with them while others only seem to have attributes (e.g. pixel.red v/s canvas.get_left_x())?

A1:  Good question! You will learn more about this later in the quarter, but it has to do with how the designers designed that variable type.


Q: Why do you stop returning false?

A1:  You want the square to step when it reaches the middle of the screen.


Q: When you increase the pixels that the function moves by, why does the rectangle appear distorted?

A1:  It's just the way it comed through Zoom video.


Q: Can you also make it move faster by decreasing the pause time?

A1:  Yes.


Q: If there is no reference sheet for animations, where should we go to find commands?

A1:  You can check out lecture code and also the section problem this week!

A2:  There's also the lecture slides that are posted.


Q: what exactly is a method (in comparison to say an int or str)

A1:  "method" is just another name for "function". Sometmes, functions that are associated with objects (like the Canvas) are referred to as "methods"


Q: what are some condition where we use "While True"?

A1:  When we want to repeat something forever. Or if we have some condition in the middle of the loop that we want to check to "break" out of the loop.


Q: What does TODO mean in this context?

A1:  It means "To Do", as in the code needs to be written still.


Q: is there a way to import hand-drawn pictures onto canvas?

A1:  tkinter allows you to include images into a canvas, but we won't be talking about that in class. You can look for it online.


Q: we can think of while true here as sort of the same as while front is clear in karel right?

A1:  While true is just used to keep repeating something forever.


Q: why is time.sleep 1/50. with a period in the end?

A1:  The 50. makes Python treat the 50 as a real number (float)


Q: so canvas.move has the variable you want to move, the change in x, and the change in y? techinaclly the x and y changes could be looked at like a slope

A1:  Yes!


Q: how did chris comment out all the lines in one click?

A1:  Or ctrl-/ on PCs

A2:  Highlight all of your code and hit cmd+/


Q: What is the shortcut for creating a vertical line of # to make code into a comment?

A1:  Or ctrl-/ on PCs

A2:  Highlight all of your code and hit cmd+/


Q: Will we have an animation handout?

A1:  No, but there is a problem in section this week where you work through an example! And the lecture code and slides are posted on the course website.

A2:  There isn't a separate handout, but the class slides are posted for reference.


Q: Why doesn’t this program include canvas.mainloop ()

A1:  It should. It's just off screen right now.


Q: what does the get_top_y function do exactly?

A1:  It gets the top y coordinate of the ball


Q: So if you return an inequality it will return true if the inequality is true?

A1:  The inequality gets evaluated when you are doing the return, so a True or False gets returned


Q: How did Chris make the ball have that shape? It’s not an oval?

A1:  It is an oval (circle), but it looks funky in the Zoom video.


Q: if i got in the lower 25% percentile for the diagnostic, is it still possible to get an A in the class

A1:  It is. You just need do well in the other parts of the class. Feel free to come to office hours to talk more about that.


Q: Would the program have any issues if it hit a corner?

A1:  No, it'll be fine, as Chris just talked about.


Q: So the only functions about checking x and y we have are get_left_x and get_top_y? Everything else is a mathematical variaton of these two?

A1:  Yes


Q: These are a LOT of new function names to remember!

A1:  We post the lecture code so you don’t have to memorize any of them!


Q: Do you know if it’s possible to create a spring on a canvas?

A1:  You could draw one with curves in theory.


Q: why does the ball look like it forms a corner in the direction it's heading or is it just my screen? can you make it so its always a perfect circle

A1:  That's an artifact of the rendering an how quickly the updates are happening.


Q: do we have to return canvas ever then? thanks!

A1:  Changes you make to a canvas in a function persiste after the function is done. You can return a canvas if you need to for some reason, but your changes will persist even if you don't.


Q: are canvases mutable?

A1:  Yes.


Q: If I create a spring out of a series of curves, is there a way to make that a single object/shape?

A1:  You an create a "widget", which is like a compound object. But we won't be talking about that in class.


Q: how come the ball has a pointy side now?

A1:  That's an artifact of the rendering and how quickly the updates are happening


Q: where can we check what colors are available in tkinter? is there a way to do it via a command?

A1:  You can google this. Not sure if there is a command.

A2:  You can find them online, such as: http://www.science.smith.edu/dftwiki/index.php/Color_Charts_for_TKinter


Q: Can canvas register when you click your mouse?

A1:  Yes. If you look in the graphics.py file in the sample code from class today, you'll see there are functions for getting mouse events.


Q: is a shape immutable or mutable type object?

A1:  shapes are created on a Canvas and the Canvas is mutable.


Q: where can we find the mid-quarter evaluation to fill out?

A1:  There is an announcement on the course website!


Q: What are good ways to study for diagnostics/timed exams? I can do the assignments well, but I have a hard time under pressure

A1:  Do section problems for practice. Also revisting sandcastle problems from assignments is helpful. You just want to practice solving problems.

A2:  Try to time yourself on section problems. Give yourself only 10 minutes to complete one of them for example.


Q: How does the ball bounce for different x and y values when it hits the paddle rather than just alternate between x/-x and y/-y?

A1:  ah good question. I just had it always bounce up. You could write something even cooler :D


Q: how to acquire great passion & humors like Chris and Mehran do?

A1:  Be playful! Enjoy and relax into the goodness that is learning :)

A2:  Surgery. But it's worth it! :-)


Q: animation is so much fun!! can you show how you made the bat move?

A1:  all the code is availible on the website! You can download it and give it a go!


Q: so we can’t access the bottom or right coordinates of a shape?

A1:  Not with a specific function, but you can do a little math in addition to using these functions to get the value you want!


Q: Are there any practice exams/questions besides the section handouts that we can use to study for diagnostics?

A1:  We will post another practice diagnostic when it gets closer to the second diagnostic.


Q: why does the ball deform when it hits walls?

A1:  That's an artifact of the rendering and how quickly the updates are happening


Q: is there a canvas.get_mouse_y()

A1:  yes!!!


Q: Where can we get more practice with 2D lists from last friday’s lecture?

A1:  live answered


Q: random question, but how do computers interpret high level languages? like how does that even work if computers only know numbers

A1:  live answered


Q: what percentile on the diagnostic should we aim for if we want to get an A in the class

A1:  live answered


Q: what do you program every day?

A1:  live answered


Q: I find myself knowing how to complete problems with a lot of time (like assignments) but for some reason I have a super hard time thinking about diagnostic problems to complete them in time. Any tips for preparing?

A1:  Practice is helpful. The more you practice programming, the faster and more facile you get with it.

A2:  This is a normal. I still feel time pressured in all of my programming classes. I just try to practice problems in a time pressured environment.


Q: What does it take to make Juliette weep after seeing the code?

A1:  live answered


Q: How to add text onto a canvas? Like if we want a score var?

A1:  You can add text objects to a canvas. Check out the graphics handout.


Q: How is FPS able to change during certain games

A1:  They have variable delays between frames.