Restarting the Bounce game (revisited)

04 Mar, 2018

Robin T writes (excerpted):

I am currently at chapter 14, i already made my first game called "Bounce". When the game is over, i want the user to push a button or a mouse click to restart the game. But i don't know how to do this.

Someone else asked a similar question a few years ago, so here's my post from back then, with some ideas about adding a restart button: "Restarting the game". In terms of creating a function to restart the game, you might want to use the canvas coords function to move the ball and paddle back to their starting position, and then set the values of all the other variables to the same as they started as well (for example, setting the hit_bottom variable to False; if you've added a score, setting the score back to zero; and so on). coords takes 5 parameters: the id of the object to move, the x and y position and the width and height of the object. So moving both objects back to their starting position might look something like this:

self.canvas.coords(self.paddle.id, 200, 300, 300, 310)
self.canvas.coords(self.ball.id, 245, 100, 260, 115)

Hopefully that helps.


Problem with paddle-ball animation

19 Feb, 2018

Hayden B writes:

I am trying to code the bouncing red ball in Chapter 13 but the animation is not working. I have checked the code, and it makes sense, but I am getting these errors when I run:

RESTART: C:\Users\rf\AppData\Local\Programs\Python\Python36-32\Files .py\paddleball.py Traceback (most recent call last):   File "C:\Users\rf\AppData\Local\Programs\Python\Python36-32\Files .py\paddleball.py", line 33, in <module>     ball.draw()   File "C:\Users\rf\AppData\Local\Programs\Python\Python36-32\Files .py\paddleball.py", line 15, in draw     self.canvas.move(self.id, self.x, self.y)   File "C:\Users\rf\AppData\Local\Programs\Python\Python36-32\lib\tkinter_init_.py", line 2585, in move     self.tk.call((self.w, 'move') + args)_ _tkinter.TclError: invalid command name ".!canvas"

...and the red ball gets "stuck" at the top "jiggling" but not bouncing or returning down as it is supposed to. I cannot figure out what the problem is. Can you help?

Your code is almost right, there's just one thing you've mistyped. Here's a snippet from my code:

canvas = Canvas(tk, width=500, height=400, bd=0, highlightthickness=0)
canvas.pack()
tk.update()

And here's your code:

canvas = Canvas(tk, width=500, height=400, bd=0, highlightthickness=0)
canvas.pack()
tk.update

Check the difference in the last line. Why do the missing brackets cause such a problem? The reason is that Python can't calculate the height of the canvas (400 pixels) until you call pack() and then update(). You are calling tk.update which is effectively just dumping information about the function, rather than calling the function itself. Here's what happens if I try that code:

>>> tk.update
<bound method Misc.update of <tkinter.Tk object .>>

And, because you aren't actually calling the update function, this line...

        self.canvas_height = self.canvas.winfo_height()

...actually results in canvas_height being 1 pixel, rather than 400. As a consequence, your ball then gets stuck jiggling at the top of the screen because both these if-statements end up being true:

        if pos[1] <= 0:
             self.y = 1
        if pos[3] >= self.canvas_height:
             self.y = -1

(so pos[1] ends up being less than or equal to 0, and pos[3] ends up being >= to the canvas_height)

By the way, you can ignore the error message -- that has nothing to do with your code. When you close the game window, your program is still trying to call the move function and so you get an error message because the canvas no longer exists (effectively).

Hope that helps.


Trouble with turtles

11 Feb, 2018

Theodore M writes:

Hello. Working in python with my daughter, but stuck on this error:

Python 3.6.4 (v3.6.4:d48ecebad5, Dec 18 2017, 21:07:28) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "copyright", "credits" or "license()" for more information. >>> import turtle Traceback (most recent call last): File "", line 1, in import turtle File "/Users/mac/Documents/turtle.py", line 2, in t=turtle.Pen() AttributeError: module 'turtle' has no attribute 'Pen'

It looks as if you might have created a file called turtle.py in your Documents directory (/Users/mac/Documents), and because you're running IDLE from the same directory (I assume), that file is being loaded before the actual turtle module. If you delete this file and try again, hopefully you'll have more success.


Becoming an expert?

12 Jan, 2018

Femi A writes:

I just got your book: Python for kids and I’m excited to start reading it. I am 36 years old and I would like to learn computer programming. I figure python would be a good place to start. Is it possible to become an expert at python in 6 months with intensive efforts? And if it is can you please let me know how to go about that, the steps to take etc...

I would say this isn't necessarily a Python, nor even a general computer programming question. It's more a question about how you define expertise, what that means from a learning perspective, and what your goals really are. If you define expertise in terms of Malcolm Gladwell's 10,000 hour rule and spend 4-5 hours a day at it, you'll be an "expert" in 6 years or so [1]. So if your goal is to call yourself an "expert", then that's probably your timeline.

If your goal is to develop some software, make a meaningful contribution to an open source project, or something along those lines, and are well motivated, it's certainly possible to do that, and more, with 6 months of dedicated effort. So, how to get to the point where you're at least confident in your coding? Start with a beginners book, funnel that knowledge in developing a few simple projects to build your experience; then perhaps look for a more advanced Python book (O'Reilly's Python Cookbook might be a good option), followed by some more advanced projects, again for the experience. Just remember, it's more important to be coding than reading about it.

Hope that helps.

[1]: Also see this Business Insider article for a counterpoint to the 10K hour principle.


Apple's Broken Parental Controls

12 Nov, 2017

What's a nice way to give a kid a limited amount of spending freedom, and letting them buy some things without being able to decimate your credit card? iTunes Voucher -- seems reasonable right?

So... redeeming voucher... Enter Apple ID password. Okay. Use camera to enter code. That's helpful. Please enter your credit card security code. Wait, what? Sigh. Fine.

Hand iPad back to kid. "Right, you can buy something now."

"How about this one?"

"Nooo that's too expensive. Find another one."

"What about this?"

"Yes okay, that works. You can click this Buy button here."

"Pa??" iPad thrust back into your hands.

Enter Apple ID password

Sigh. Okay. Why the heck did I bother to add a nice long, secure password for his account?

Ask for your parent's approval. Click Ask... time passes... Nothing happened. Huh? Click Buy again.

Enter Apple ID password.

WTH? Just did that! For. The. Same. Game.

Ask for your parent's approval (they can also authorise on this device). Yes, but that defeats the purpose. The whole point was to let the kid buy a few things himself. Click Ask.

A message arrives on my phone: Your iDevice-obsessed post-toddler wants to purchase lego-something-or-other-game. Are you sure he's not already spending too much time on devices already? Woohoo, finally got the request! Unlock phone. Wait, WTH! Where's the authorisation dialog gone? Settings > Parental controls? Nope nothing there. Search > Parental Controls. Nothing there either. Sigh. Back to the kid's device again. Click Buy... again.

Enter Apple ID password

Whatever. Stupid long passwords.

Ask for your parent's approval. Stab Ask. Kid now looking at you strangely because you're starting to mutter rude words under your breath.

A message arrives on my phone: Your iDevice-obsessed post-toddler wants to purchase lego-something-or-other-game. Yes I know. It's me not the kid -- how's a child supposed to figure this out? Touch the dialog correctly this time.

A message arrives on the kid's iPad: The bossy parental unit has authorised your purchase. Excellent. Hand back device to kid.

"Pa??" iPad thrust back into my hands.

What's the problem now? Oh, the buy button has turned into a get link. Only it doesn't look like a link at all. It's just the text GET. WTH? Where are the UI standards? Isn't this supposed to be a button??

Your purchase is downloading. Finally. Mutter mutter Apple mutter.

...5 mins passes...

"Pa?? I'm bored with this game. Can I try this one?"

"Fine, okay". Click Buy.

Enter Apple ID password

.

.

.

Angry, from Disney's Inside Out