Pygame or App Development using Kivy or XCode?

19 May, 2018

Sunil writes:

I would, first of all, like to thank you for the amazing book you have written "Python for Kids"
It's really helped my nephew learn Coding in a very exciting and fun way.
He is now very keen on continuing with Python. He is 8.
But at this point, I do not know what would be the next set of things to teach him. Should we proceed with learning Pygame or App Development using Kivy or XCode. I am a little confused on this matter.
I tried a bit of reading online, but it did not help my decision making.

What would you suggest is the best Python learning path for an 8-year-old after having followed your book.

I'm quite partial to the idea of Pygame as a natural progression from the games in the book. Adapting the Bounce or Stickman game to use Pygame, instead of tkinter, seems like a good way to get started, and then Pygame has support for audio (mixer) and joystick modules, so there's opportunities to enhance and extend the games to add new features. This would be building on learning from the book, which I think is a nice way to move forward. It's certainly going to be the easiest way to get some instant feedback as well (since it's not hard to get up-and-running), so thumb's up from that perspective.

I don't know a lot about Kivy - so I can't really comment on how suitable it would be for an 8 year old -- but even if it proved too difficult, if he's interested in mobile apps, then I don't think that time would be wasted.

XCode will probably mean learning a new language (depending on what your nephew wants to do with it). If iOS apps, then the options are Objective-C or Swift -- which is not to say you're entirely limited to those languages, but you're going to find the most community support with one of the primary languages. Of the two, Swift is the most approachable (IMO), but I still wouldn't call that easy going for a child to pick up.

So with very limited experience in the other two options, I guess my personal preference would be Pygame, then Kivy, finally XCode. Do let me know how you get on...

Reference links for other readers:


A question about tkinter update

09 May, 2018

Feng writes:

I bought a chinese version of your book. when i tried the paddle ball at this step: 'Bounce (example 4) - ball moving up and down' at the first part of the code, you add a line tk.update(). I cannot see why this is necessary, since there are no balls moving at all. Then, i comment it with a '#'.

Now, it comes with a problem, i failed to see the ball move down, it seems stuck at the top line of the canvas. I tried to print the variable 'pos', and it shows as follows:
------------------(some part of the outcome)
[255.0, 1.0, 270.0, 16.0]
[255.0, 0.0, 270.0, 15.0]
[255.0, -1.0, 270.0, 14.0]
how did this happen, why the first 'tk.update()' cannot be omitted?

The first call to tk.update() isn't strictly necessary - but what it does do is to force a "wait" until all the Tk user interface events have been processed.

When you call pack to tell your Tk components to resize and display themselves in the right place, you're actually sending a message to a whole separate process (this is the Tk event loop). Let's imagine it took 1 minute for each component to set itself up, and you had 10 components in the window. If you called pack it wouldn't take 10 minutes to complete that function call -- it would actually complete almost instantly. Behind the scenes however, the components are busy re-arranging themselves to fit in the window (and in this imaginary scenario, that process would then take 10 minutes to complete).

Try running this code (create a file and run it in IDLE):

from tkinter import *
tk = Tk()
canvas = Canvas(tk, width=500, height=400, bd=0, highlightthickness=0)
canvas.pack()
print(canvas.winfo_height())

You'll get 1. That's because the canvas hasn't finished rearranging itself by the time you call winfo_height. Now try typing the first 4 lines of the code into the IDLE shell -- wait a few seconds then enter the last line:

print(canvas.winfo_height())

Now you'll get 400 (i.e. 400 pixels high). Because you've given the canvas time to complete.

In summary, pack is saying "ok Tk components, re-arrange yourselves on the screen" and update is saying: "ok Tk, I want to wait for that to complete". Which is why, if you remove the update call, inside the __init__ function of the Ball class, it calculates a height of 1, and the code no longer works correctly.


Just in time?

01 Apr, 2018

Perhaps just in time for Google shutting down its goo.gl url shortener, here's a potential alternative: nimwhistle, an algorithmic URL shortener loosely based on the ideas in Whistle, but with a few minor modifications (URL patterns in the form of dir/YYYY/mm/dd/filename; 'favourite', fixed or non-standard URLs stored in a fixed-line-length file), and a CGI-based redirection service. 462KB compiled, Apache2 licensed. Not guaranteed for any purpose, because it was a hack-job/experiment, put together in a few hours.

Update: somewhat stupidly, forgot to mention, nimwhistle is already in use here (for example, https://jasonrbriggs.com/u/f1 redirects to Python for Kids)


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.