Learn Python with Talk Python's 270 hours of courses

#106: Invent your own computer games with Python Transcript

Recorded on Thursday, Mar 23, 2017.

00:00 Michael Kennedy: Al Sweigart (@alsweigart) is back on Talk Python This time we're inventing our own computer games. Has anyone ever asked you for a resource to get started on how to learn programming or how to learn Python? Well one excellent and fun option is Al's book just updated called "Invent Your Own Computer Games with Python" and we'll be digging into it this episode. It's Talk Python To Me Episode 106, recorded March 20th, 2017. Welcome to Talk Python To Me a weekly podcast on Python the language the libraries, the ecosystem, and the personalities, This is your host Michael Kennedy. Follow me on Twitter where I'm @mkennedy Keep up with a show and listen to past episodes at talkpython.fm and follow the show on Twitter via @talkpython. This episode is brought to you by Intel and GetStream, be sure to check out what they're offering during each of their segments and helps support the show. Hey everyone before we get to Al I want to share three pieces of news with you. First two, I've launched two new courses this week. The first one I partnered with Dan Vader and it's called managing Python Dependencies. This is a deep dive into how you need to manage your dependencies in Python understanding pip understanding requirements and actually finding out what makes a good dependency in the first place. How do you search through all the possible packages on and other locations and determine which dependency you should even take so you'll learn all of that and more. You'll go from basically writing scripts and little utilities to building proper applications by mastering dependency management Another new course that I wrote is Using and Mastering Cookie Cutter. If you want to help users fall into the pit of success when they get started with your project or your library having a Cookie Cutter template that sets everything up just right for them is a really good option. So if you want to learn about Cookie Cutter, how to consume it as a user, how to create Cookie Cutter templates as maybe a project lead or something like that and even modify and contribute back or just customize for your own purposes, the Cookie Cutter project then you should check out using a mastering Cookie Cutter and the slide on piece of news is we're now offering course bundles if you come to training.talkpython.fm, you can buy an entire year's worth of content so make this year the year you grow as a developer and get a year's worth of Python training guaranteed. Now let's talk to Al and check in on his cat Al, welcome to Talk Python.

02:55 Al Sweigart: Well it's great to be here.

02:58 Michael Kennedy: Yeah it's great to have you back it has been a long time since we talked about your huge cat.

03:01 Al Sweigart: oh yeah yes she is slightly less huge these days. I put her on a diet.

03:06 Michael Kennedy: Your cat's on a diet how awesome.

03:07 Al Sweigart: Yeah she's well still kind of a bowling ball but she's doing she's doing great.

03:14 Michael Kennedy: I know you were on show I think it was shown 19 and understand that from memory but certainly around that era but I suspect many people will see now don't remember the story so maybe let's talk about how you got into programming and how you got into Python and to just start from there.

03:29 Al Sweigart: Yeah I always say that I hate telling people how I got started into programming because I was one of those kids that learned how to program in about the third or fourth grade just programming in basic making little computer games and I hate telling people this because it sort of reinforces that idea that unless you were programming you know from when you were a toddler or something like that that you'll never catch up and and there's too much to learn and you're too old to start learning to program. Which is really nonsense because in my case I think everything that I learned about coding from like third grade up to graduating high school when I was like one of those computer kids. I'm pretty sure anybody with you know the internet and Google and stack overflow can learn everything I learned in like maybe a dozen weekends.

04:14 Michael Kennedy: Yeah maybe just YouTube alone would take care of it.

04:17 Al Sweigart: I mean learning to program is so much easier today than it was 10 years 20 years ago. Whenever I say like oh I've been programming since I was in the third grade that's fairly meaningless these days. And so yeah I don't want to discourage anybody from from learning that and also whenever anybody says I've been programming since I was you know ten years old or something like that... I keep thinking well yeah but it was probably pretty lousy programs that you were making I just think I was.

04:43 Michael Kennedy: Yeah you know what's really interesting about when you state experience in those terms is you could have been working for two years but on like a massive wide range of technologies and really throwing yourself into it. Or you could have been programming for 20 years writing basically the same code and I put my bet on that too your person being able to do do more in the industry having that experience than if you keep doing the same thing. So there's quite a lense you have to put on I've been doing it for 20 years or years or whatever.

05:14 Al Sweigart: Yes definitely and the one constant in this industry is that you're always going to have to be learning new things pretty much non-stop so the feeling that you're really dumb and you know nothing is never going to really go away so just get used to it I guess.

05:28 Michael Kennedy: That was that was my Saturday morning. Saturday afternoon I felt a little bit better about life but it's Saturday morning as I how gosh surely by kid I read this word is he killing me so yeah so never-ending thing which I think is what's wonderful about the programming industry but if that's not your thing then you're not going to love it are you?

05:47 Al Sweigart: Yeah I mean certainly a lot of wheels get reinvented, but I think that there is measurable progress being made I mean you can just do so much more with software libraries and tools today and yeah so I very much encourage people to continue to or just new pick up learning how to program or maybe if you tried picking it up like a decade ago and you thought it wasn't for you just give it another shot.

06:10 Michael Kennedy: Absolutely you know one of the things I think is interesting is you talked about how the tools weren't really that great to write software when you got started and I suspect I'm just assuming but the scope of the things that you created was pretty narrowly focused right you weren't like creating YouTube or Dropbox, right?

06:29 Al Sweigart: Right.

06:29 Michael Kennedy: I mean it feels like on one hand the programming tools and languages have got so much better and yet there's more programmers and we still work hard like why do you think that is?

06:41 Al Sweigart: Well I think it's just the scope of things that we build are so much greater. For the last couple of weeks I've been learning bootstrap and this is a JavaScript and CSS framework for basically creating modern looking websites. You know how websites usually have like the giant image on there here a banner and them again and then they have like another row with like three little images in a row and then they have like another row and you can sort of tell like oh this is sort of a website created with Bootstrap. But the reason that all these websites look that way is because Bootstrap makes it really easy to make this reactive website that looks great on your desktop but also great on your mobile phone and it's doing a lot of really complicated busy work behind the scenes to make that happen. But using Bootstrap on the other hand is really I mean I'm really impressed by how by how simple and easy it is to pick it to pick up and start using and meanwhile it's doing something that if I were to try to do it on my own would just be weeks and weeks and weeks per web page or per site or anything lik.e

07:44 Michael Kennedy: You have to learn about reset CSS's about media queries about polyfills for java..., it's just like oh like all that stuff is gone right but now we can focus on building something the design is kind of solved a little bit I mean still a challenge but...

07:58 Al Sweigart: Right so we would look beyond those problems it say okay now what is the actual problem we're trying to solve right now.

08:03 Michael Kennedy: I totally agree that this is there's in my opinion there's no danger that we are going immediately out of a job.

08:10 Al Sweigart: No, definitely. I remember it like I think sometime in the early 2000's I was in a coffee shop and I was overhearing somebody talking with some software developer and they asked him like I don't know like do we need more software like when do it like will we always need more software and I kind of thought it's sort of like well I don't know will we need more novels or will we need like more movies or you know like we already have this detective story I think that entire genre has been filled by this one book. So yeah I mean in a sense you know we're always going to be creating new word processing applications or new instant messaging applications but there's a lot of of stuff and I think we just sort of take it for granted and forget that it's there in the same way that fish just forget that water is wet because we get so used to it. But I mean like all these modules like jQuery is what made JavaScript programming so much easier and even things like Python is just making programming in general so much easier compared to working with C++ or older languages a lot of busy work that you would have to do as a programmer writing code in assembly is just taken care of for you so you can concentrate on on what makes your application different.

09:22 Michael Kennedy: Yeah come to think of it I haven't allocated or deleted any memory in quite a while. Also so people may remember from episode 19 pretty sure that's right but why don't you tell everyone what you do day to day these days like what do you do for a job in addition to writing books about inventing games?

09:39 Al Sweigart: So I wrote those books and then started self-publishing them while I had a full-time job as a software developer and about three years ago, yeah I think it was 2004, was when I took the plunge. And I was in the middle of writing automated boring stuff and I was fairly certain that it was going to be past deadline on that and I was just everything was was working out well for me at that point where I thought like okay I can leave this job and continue writing full-time and I think Automate The... came out later that year or in sometime 2005 I believe, but yeah it worked out that I could finish this book and if it was really successful maybe I could continue writing more books and if it wasn't then I was in a position where you know just being in San Francisco as a software developer it wouldn't be that difficult for me to get a new job. I'm always blown away by how successful Automate The Boring Stuff has been.

10:36 Michael Kennedy: Yeah that's a great book and that was the topic of our first episode that we did together somebody would recommended I reach out to you because they thought your book was so cool and so maybe tell people you know what's the elevator pitch on Automate The Boring Stuff?

10:48 Al Sweigart: So Automate The Boring Stuff is a book that teaches non-programmers and office workers how to code and also how to do really practical tasks. So the one story that I I think of is I had a friend who worked in an office and part of his job was just opening up PDF files and then finding this one line in each of these PDF reports and copying and pasting it to an Excel spreadsheet and then just opening the next PDF and just doing this for four or five hours at a time. And it's something that he taught himself a little bit of Python coding and grabbed a whole bunch of modules like he doesn't know the PDF file format which is insanely complex but he can learn how to use this PDF module for Python to grab this text and then update an Excel spreadsheet and it took several hours to write this this small script, but running it now in you know takes a few seconds to do all that work that normally would take him hours and hours. And I realize that you know a lot of people are saying oh everyone should learn how to code and I sort of thought well what exactly could you do if you weren't a software engineer but you know you do work with a computer every day what are the things that you would want to do to using programming to help just free yourself from a lot of tedious tasks. And so that's sort of the first half of Automate The Boring Stuff with Python is a general Python and programming tutorial for complete beginners in the second half just covers a whole bunch of useful modules that the Python community has created.

12:15 Michael Kennedy: And just little little tasks that you could automate in your life that maybe you have some out right. So I think that's a great idea and I when I hear people say that we need to teach everyone to code or coding as a core skill like math and reading I do feel that that's true but I don't feel like we need 10 times the programmers I feel like instead like having some sort of programming skill amplifies whatever you actually are good at right. So if you are if you are a biologist you're going to be a really effective biologist if you can automate things right. If you can use a little Python to like not do stuff by hand and collect a bunch of data and whatnot same for finance same for whatever right.

12:59 Al Sweigart: Oh yeah. And today most people who want to gain more technical skills are pretty much doing programming except they're doing it with Excel. Essentially just like creating really complicated spreadsheets to manage large amounts of data and then using these complex formulas. And you know that's a great thing and but also a terrible thing because oh my god that code. But you know actually sitting down to learn a programming language like Python makes you know doing these tasks so much easier for them and also a you know produces very readable code and it's possible to share this, these small scripts with other people. And also I guess the same is done with Outlook mail rules like I remember at my at the last job I had you know I would get about 150 or 200 automated emails for different things every day and it would be completely unmanageable except I just setup you know a dozen or so Outlook rules where you know if I get an email from this email address with this in the title like search for this substring filter it to this folder and just doing that over and over again well I mean that's essentially the nature of programming you're trying to program this logical intelligence to do all this tedious work for you.

14:10 Michael Kennedy: Yeah I think that's a great example for people are getting started right? I think I have a ton of Gmail rules as well. Very nice. So maybe we should start by start getting into your book. So you have a number of books and one of them you just released the fourth edition and it's called "Invent Your Own Computer Games with Python" right?

14:30 Al Sweigart: Yes. Yeah it has an absurdly long title but it was the first book that I ever wrote and put book in quotes because it was sort of just a series of web pages that I had and I just started putting chapter numbers on them eventually and then created a self-published book for it. So I felt like I don't know it's sort of like "Invent Your Own Computer Games with Python" and I didn't realize like you know I was like oh I'm going to make this a commercial book maybe I should come up with a snappier title but it became popular and so it that was sort of the title I was stuck with.

15:05 Michael Kennedy: No, I think it's a good title it's pretty descriptive it's not too long. So I know not everybody gets into programming because they want to create computer games. I don't feel like I personally got into programming to create computer games when I was a kid. More i think it came out it a little bit later so I came at it from a he had these research problems and my PhD program I gotta work on so let's try to solve those or even in undergrad but a lot of people do want to build games especially younger people right so so what's the audience look like for your your book?

15:38 Al Sweigart: So the current audience it's actually quite a wide range of readers but the intended audience is roughly maybe as young as nine years old but sort of I always pictured "Invent Your Own Computer Games with Python" as the book that I wish I had read or I had when I was young because I remember there is like only very few books on programming in Basic that I had access to and there were so many different versions of Basic but you know some of the programs didn't work and I really didn't have there was no real internet to google for answers so i wanted to just get a broad range of different types of games so that's why the book ended up with over 20 chapters of games in it.

16:20 Michael Kennedy: Yeah I think that's really cool.

16:22 Al Sweigart: Yeah it was just I wanted a book that went from knowing absolutely nothing to being able to make some like really like clever little games that wouldn't take up that much time you know wouldn't take like a week to write. Games are a great sort of gateway into the world of programming like I really didn't have that many ambitions of becoming a professional game developer, but learning how to program games just because I was really into Nintendo as a kid became this it was the main motivating force for me to get into programming and then later on become a software developer.

16:54 Michael Kennedy: Yeah it's cool and each little chapter is pretty small and everyone has a different game or most of them have different games where there's kind of a quick win. And I think when you're learning programming that's so important because you can spend all your time going here's how you declare variables this is what a type is this thing is an integer that thing is a string.

17:12 Al Sweigart: Yes.

17:14 Michael Kennedy: This is a loop this is s function use the dev for but you thinks but like oh my gosh like why do I care about this anymore? I thought this would be fun but it's not fun anymore right? So how do you solve that? How did you address that at least in your your book?

17:26 Al Sweigart: So in my book and I really am copying what a lot of like Byte Magazine from the 80s which would have just listings of source codes of source code for a small game that you would just copy from the magazine there are also a lot of--

17:40 Michael Kennedy: Maybe even type it in?

17:41 Al Sweigart: Right yeah yeah exactly and I believe a Atariarchive.net or Atariarchive.com is a website that has a lot of these old basic programming books that you can view online now. And I thought it was just great because none of the programming books that taught me like these are variables these are loops and everything like I had the reference manual but that didn't show me anything I couldn't make heads or tails of it but I found one book that basically taught by showing the complete source code to a small game and then just sort of explaining how it works. And as I was writing Invent Your Own Computer Games i realized with Python if anything this is way easier than with basic because the code is much more direct and much more simple than having to you know. So there's still a lot of hoops that you would have to jump through through basic even though basic was revolutionary for its time as being a simple programming language.

18:31 Michael Kennedy: Still it didn't have much of a base library that you could leverage there's a lot of stuff you had to do yourself right. Nice so you start on this thing by explaining a couple of pieces like what is the repl what is the interactive shell how do you write programs, what is a variable it's interesting when I started doing training for software developers I always my first concern was I'd end up in a classroom with super smart people and they would ask me all these hard questions and I would not know the answer. And I was look stupid or they would feel like they didn't get their money's worth or whatever. What I found was those people are super easy to talk to. Because you are speaking the same language but when you are speaking to somebody's just getting into programming like many of the people reading your book, that is a much harder conversation in my mind. And I think you did a pretty good job but like even talking about what is a variable and how does code execute I think it's easy to just go well it just does, you just run it right? Whatever, like was it easy for you to explain that stuff in a way that you felt conveyed it to newcomers?

19:43 Al Sweigart: I mean I think my main approach that was just for that is I would write out sort of a map of all the topics I wanted to cover and then just start drawing lines between them saying like in order to understand this they have to understand this other concept and in order to understand that so... I would sort of get this dependency graph of concepts and a lot of times I really am just sort of tempted to just say like well it just works like this, but the great thing about Pythons interactive shell with the repl is you kind of can get away with saying well this is just how it works by showing an example in the interactive shell like Oh type this in and that spits this out. And it's surprising how just having this sort of visible example that they can see and then you can share like oh you can type like a 2+2 and then you can also type 2+2+2 and then just sort of explain that like well two is just a value like this is kind of like math problems in your math class except if you think of these numbers as values and those plus signs as operators you can see that this is an expression which just combines those and evaluates down down to like a single value like six. But I really lean heavily on the interactive shell for just demonstrating this because the last thing I want to do is just sort of talk about these concepts instead of actually showing you know what the computer does when you type in these instructions.

21:03 Michael Kennedy: Yeah that's the best way right? You have to just do the thing if you want to learn programming. It's kind of unique in that sense. I guess math if you're trying to learn advanced math you can't just read a bunch of math and be able to solve problems. Yu probably got to just do it as well but it's certainly one of the disciplines or you have to sit down and do the thing in experience so the repls seems like a great way to do that.

21:26 Al Sweigart: yeah I mean the great thing about computers as they do exactly what you tell them to and that's also one of the worst things about them.

21:32 Michael Kennedy: One of the most frustrating things ever.

21:34 Al Sweigart: It is much nicer to learn how to program you know with the computer right there in front of you telling you when you get things wrong. As opposed to I don't know if you just checked out a calculus book from the library and tried to learn calculus. I mean you have this sort of inert book sitting in front of you and maybe you're doing the math right maybe not it's hard to tell because you know maybe you have just the answers in the back of the book for the questions in the math book but you don't have whereas the Python interactive shell it will always be a little able to tell you like oh this evaluates to that or this raises such and such exception.

22:07 Michael Kennedy: Yeah exactly the only one that's hard to get it to give you an answer is an infinite loop.

22:10 Al Sweigart: Oh yeah well you know you just have to wait a while.

22:12 Michael Kennedy: Exactly I'm just going to wait for a little more time. So one of the things that you used in in the book was IDLE the graphical tool that comes with Python. Why did you pick that over some other tool like I know the book was written a little while ago so maybe those were some of the better UI tools or what was the was the story there?

22:34 Al Sweigart: Oh, I would say that so IDLE has I would say two things going for it I mean it is sort of like a very rough IDE and it's more text editor than IDE but the great thing about it is that it's really simple you can just fire it up and it has the interactive shell in one window and it has a file editor in another window and you can just press F5 to run your program and it's really easy to get started with... And then the second thing is also that it comes with Python there's nothing extra that you have to install you don't have to configure any environment variables you don't have to navigate your filesystem to find the Python.exe or that the Python interpreter. And this is actually a pretty big deal especially if you've ever had to teach Python or teach programming in a classroom setting. Where now you have 15, 20 laptops that you have to set up and maybe you don't have all the permissions because the school district or the IT department decided to lock down all the permissions on things. It just saved me a whole lot of steps where I wouldn't have to talk about like here's how to install Notepad++ if you're on Windows but if you're on Mac then here's how to install PyCharm and here's how to set it up and now all these other steps and I really feel like IDLE is one of the reasons why Python I mean there's lots of reasons why Python is so popular but IDLE is certainly one of them. It's it makes Python a language that's easy to get started with and the more of that you have for any language the more adoption you're going to see and the bigger of a community that you're going to create.

24:07 Michael Kennedy: Yeah I see. So it's in your start menu it's in your applications folder or whatever right. Once you run the path is their right? Not like if its environment environment variables not set up or whatever okay. That's a pretty good reason actually. We all love Python for its tremendous productivity benefits, but giving the best performance take some work. What if you could get out of the box easy access to high-performance Python? Intel Distribution for Python Developers delivers just that. Get close to 100 times better performance for certain functions when using numpy, SciPy, scikit-learn linked with the optimized native libraries like Intel Math Kernel Library. Access efficient multi threading and Python projects like Numba and Cython. Try the Intel Distribution for Python and experience performance today at talkpython.fm/intel. Profile your Python and native C++ applications for performance hot spots with Intel VTune Amplifier. With Intel it's all about performance. Do you think that Python would be better off if it had something like Swift playground? Do you think that matters?

25:35 Al Sweigart: No like a like what?

25:36 Michael Kennedy: Like Swift playground. Are you familiar with those?

25:38 Al Sweigart: Oh no.

25:39 Michael Kennedy: So Swift playground basically it's kind of like a rebel but on the right hand side you can have like graphs and little animations.

25:46 Al Sweigart: Oh, okay.

25:47 Michael Kennedy: And you've got the code like on the left and on the right the output of that code going down together.

25:54 Al Sweigart: I think like iPython notebook is sort of set up similar to that like it can have non-text output.

26:00 Michael Kennedy: Yeah iPythons the closest I can think to it but you know iPython is it's not something that's super easy for beginners to set up like tell somebody like you have Python now, get iPython running on Windows.

26:12 Al Sweigart: Right.

26:14 Michael Kennedy: Well the problem is I can't find vc VARs dot bat or whatever that thing is. Okay forget it, I didn't, I just I feel like there's IDLE and if like IDLE was taken to the next level it would look a lot like Swift's playground and I'm wondering if something easy and built in and there was really really slick where's it feels like it's effective and it's nice but it's not really slick these days.

26:40 Al Sweigart: Yeah well I mean IDLE is definitely sort of showing its age but in a certain sense it is fairly timeless where it just has the its simplicity means it can get away which is having the interactive shell and the file editor and it's perfect to work with. It is also I guess somewhat controversial to just sort of add an application with your programming language but I'm really or even something like a GUI tool kit like TKinter that was added to the Python standard library out I don't really know of any other programming languages that that sort of do that with their standard library but I'm certainly glad that that happened that that ended up happening.

27:17 Michael Kennedy: Yeah it's a play nice for your game right?

27:19 Al Sweigart: Yeah for your gamebook.

27:20 Michael Kennedy: Nice so you want to touch on some of the games that that you take people through I mean obviously you have the requisite guess a number hi-low game to start.

27:29 Al Sweigart: Yes I mean that's something I've seen over and over and over again even when I was back first writing this book I felt like okay guess the number is perfect it has loops and if-else and variables and it's also just kind of like a fun little game where you know you can say like Oh ten well my number is higher than that and so you can sort of do this dictionary style binary search sorry.

27:53 Michael Kennedy: Yeah exactly yeah you know you're unwittingly like the optimal strategy as a binary search for humans. I do feel like that's one of the games that is like on the per line of code like is the most dense in like fun and joy right it's like well I like 8-9 lines of code probably?

28:10 Al Sweigart: Oh yeah.

28:11 Michael Kennedy: And people can actually get some anticipation and something out of it which is pretty cool.

28:15 Al Sweigart: Yeah and so the book that's the first program in the book and it continues on with there's like a little joke telling program. I mean so there's not all the chapters have game programs some of them are just sort of like fun little programs to run. There's this joke-telling program which is really there just to get familiar with how to output text in certain ways and format the text. And then next is a game called Dragonrealm which sounds really fancy but actually if anything it's even simpler than the guess that number game.

28:45 Michael Kennedy: Is that like a mud--

28:45 Al Sweigart: Oh well I mean it would look like it but really it comes down to pick Cave 1 or Cave 2 and then it tells you if a dragon has eaten you or not...

28:54 Michael Kennedy: Yeah it's like one step in a mud.

28:57 Al Sweigart: Yeah it's a coin flip but it has like a really nice user experience tighter and story behind that kind of like a lot of modern games where as long as you have a great story we don't tell you don't care if the game itself is kind of dull.

29:12 Michael Kennedy: Yeah exactly. Yeah I like the retro retro game experience where they specifically sort of water down the graphics and make it about the the game experience and the story and it's kind of nice not to have like always the most shiny shadowy reflecting on water graphic engine with nothing else to it, you know?

29:32 Al Sweigart: I definitely grew up with mud sending sort of text adventures and that's style of of games and Invent Your Own Computer Games with Python similarly has just text based programs for I think the the first major chunk of the book and the last four chapters starts covering pygame and 2D graphics but the great thing about having text only for just you know having keyboard input from the user and then all the output is just print statements is that you can always tell from the output that you get from the program where that came from in the source code. So you can sort of like draw line like okay this text here that says the dragon ate me comes from this line which is print the dragon ate you and so you can actually sort of see the flow control going through the program. Whereas you know a lot of I don't know if you like fire up unity 3D or something like that or just these really complicated game engines it's kind of hard to figure out what part of the code causes what to happen on the screen.

30:31 Michael Kennedy: Oh yeah those 3D versions get so intense. I remember I had done a 3D simulator in OpenGL and I couldn't see the thing I was trying to display on the screen I'm like I know it is in front of me why can't I see it and I had I had like back face culling turned on and I had gotten clockwise versus counterclockwise backwards alright. So it thought I was inside the thing and it just took it I kit was and it took me I remember hours of like why kid I see in oh it should be clockwise not like it doesn't want--

31:03 Al Sweigart: It only draws one side of the triangle that you 're looking at. From the other side it disappears...

31:10 Michael Kennedy: And that's exactly why am I looking at the inside and it's not the outlet of it yeah certainly I don't think that's a good place to start even though like people many people where I think i'm going to create Eve online i'm going to create some kind of unreal thing. It's effective but it's also not just so I I just got as much fun as it sounds like in the end in my estimation.

31:29 Al Sweigart: Yeah I mean actually so i'm working on a book right now for No Starch Press because I do some teaching classes with kids and they say like I want a program Minecraft. Minecraft is like the main games I hear over and over again I'm just like oh well that's going to be a lot of work but the next book that I'm writing which so far doesn't have a title done roughly calling it "Programming Robots in Minecraft" is a book that covers this one mod for Minecraft called computercraft that adds in these programmable robots into the game world. And so you can write these Lua cripts to program these robots and they're called Turtles just like the old logo turtles that do drawing on the screen so you can program these robots to just like move forward move turn left move forward again start mining in front of you and you can basically have these robots sort of do all of your mining or chopping down trees or tending to a farm or building for you I jokingly call this book Automate the Minecraft Stuff with Lua and so so I was hoping like to use this getting people getting kids who are really interested in Minecraft into programming once again using games as sort of a gateway into that the broader world. I'm just really sad that it's all in Lua instead of using Python but Lua's aws--

32:49 Michael Kennedy: Is there some sort of just sort of Python adapter you could do?

32:52 Al Sweigart: There is a Python module in fact I think No Starch Press has a book called a learned program with Minecraft that uses a Python module for Minecraft it's more like a sort of a 3D editor right using in the Minecraft world so you can specify like XYZ coordinates and then change that block to something else but it's great because now you can just sort of instantly build giant creations by writing code sort of like you know computer-generated art yeah you have these computer-generated blocks now.

33:24 Michael Kennedy: Okay, very cool have you seen the movie Indie Game? I-N-D-I-E, Indie Game.

33:30 Al Sweigart: No I haven't.

33:32 Michael Kennedy: There's Indie Game and then there's like a sequel and I think it's on Netflix and other places, but it follows these four or five groups of long who spend like a couple years doing just solo indie game development and either their great success or not so much at the end because if such antics yeah for for people who are interested in this kind of stuff that might be a good movie recommendation.

33:53 Al Sweigart: Oh yeah definitely I mean I keep thinking like I hear about Minecraft and Dwarf Fortress and a game that I recently got into Stardew Valley. And these are all created by sort of like solo or just like very small teams of people usually one person just over years and there were just these big labors of love that turned into this great thing. And I was thinking like oh man I could I could totally just spend the next four years of my life just sitting in a small room and I'll program something that's like a cross between Stardew Valley and Minecraft and I'd call it Starcraft.

34:24 Michael Kennedy: There's no trademarks or anything.

34:26 Al Sweigart: That would be great.

34:28 Michael Kennedy: Yeah I saw one of the guys that was on there he was featured on the Apple App store with his sequel game so he must have gone somewhere with that. That was cool.

34:37 Al Sweigart: Yeah that's great.

34:37 Michael Kennedy: So what other games do we have? We have Hangman of course.

34:40 Al Sweigart: Yes yes and this game actually sort of like the first major game in the book I've actually spread it across a couple chapters and I also talked about how you should write out flow charts for sort of designing what your pro, what you want your program to do and how you know it's a lot easier to just change a flowchart around than it is to actually change start changing code around if you later like come up with a better idea or something else like that so just doing a little design ahead of time just and thinking about code before you start writing it.

35:09 Michael Kennedy: Yeah you probably wouldn't do that too much as a professional developer these days, but certainly in the concept of you know what is the function again? When you're at that level it does become hard to manipulate the code and whatnot so having it drawn out is nice.

35:25 Al Sweigart: Yeah and also it's just a flowchart is just sort of a natural way of depicting the flow of execution in a through a program.

35:34 Michael Kennedy: Nice okay so tic-tac-toe does it play against you automatically how do you play tic-tac-toe?

35:41 Al Sweigart: So this is a tic-tac-toe game against the computer and the computer is actually somewhat intelligent. So it'll look if it can win the game by making a move it'll make that move. And then it'll check if you can make a move that will block the player from winning the game it'll make that move. And then I think the algorithm codes on where it just it'll take any corner space that's free and then any side space that's free or I think maybe it takes the center first. But I really wanted to introduce the idea of you can actually have you know again it's still just a bunch of function calls and if-else statements and sort of also the first time where I introduce a data structure we have a data structure to represent the tic-tac-toe board and this data structure is what's used by a bunch of the functions to sort of plan out what the AI will do I remember trying to program this as a kid in Basic by following some some source code that I was copying and I could never get it to work. I would just like try it and then like a couple weeks I you know forget about a couple weeks later would try it again and we just never actually work out and so when I was writing this originally for the I guess seven years ago I think was when I first started writing this book it was kind of like a miniature triumph that I got the tic-tac-toe program working. I have produced my masterpiece right here.

37:01 Michael Kennedy: Yeah that's awesome.

37:02 Al Sweigart: So yeah and the game continues on I have like a few other games there's like a deduction game called Bagels. It's also called Mastermind. Where it's sort of your it has three numbers and it tells you guess which numbers and it gives you clues where like if you've guessed one digit or if you've guessed a digit but it's in the wrong place that's sort of deduction game. And I also have a small chapter on the on a little encryption cipher program so that you can do the Caesar cipher which is where you shift the letter over by three characters so like in an A becomes a D and a B becomes an E and then you decrypt it by just shifting the letters back but this is just like a really simple sort of like encryption program that you could make in Python it's not necessarily a game I thought it would just be like a really fun thing.

37:55 Michael Kennedy: This episode is brought to you by GetStream. If you're building an application that has some form of activity stream like you might see in slack or Facebook or Instagram and others then you owe it to yourself to have a look at GetStream. They provide scalable reliable and personalizable hosted API feeds as a service. The feed is the most intensive component of these types of applications yet there's no need for you to reinvent the underlying feed technology when GetStream has the infrastructure and a Python API already in place. Go from zero to scalable feed in hours not weeks or months they even use advanced machine learning to serve up personalized results to each and every user. Stream powers the feeds for over 500 companies including Makerspace and Fabric with a total of seventy million end-users. Try the API yourself in a short five minute interactive tutorial at talkpython.fm/stream. Try it for yourself. It helps support the show.

38:52 Al Sweigart: Actually this chapter sort of inspired me to go on later and I wrote an entire book full of these in Hacking Secret Ciphers with Python.

39:01 Michael Kennedy: Nice yeah I think ciphers are another thing that are pretty fascinating to people. It's a parallel track to be an interested in games that these secrets and these secret ciphers are quite interesting. Encryption is so complicated these days that it's not fun to look at anymore but trying to solve--

39:17 Al Sweigart: Especially when one minor oversight means that your entire system can fall apart. Yes I so I wrote an entire book of bad encryption in a modern sense it's their terrible ciphers but these are all sort of like classical ciphers Caesar cipher literally goes back to the times of Caesar.

39:34 Michael Kennedy: Was Caesar actually using it?

39:35 Al Sweigart: Yes, so this was sort of the system where they would just shift letters over by three characters in the alphabet and that was how they would encrypt things. Of course back then just writing things down was effectively encrypting things because most people were illiterate, but you know just the idea of your just seemingly writing a bunch of nonsense on to writing that down and then that meant you know anybody who intercepts that message would just be completely confounded because these concepts of encryption and you know treating letters as sort of generic symbols that can be manipulated according to some algorithm that just wasn't really common all that common. So I mean the Caesar cipher was very effective.

40:15 Michael Kennedy: Yeah. I mean it just seems like so basic and it's you know encryption for like a serious empire right?

40:21 Al Sweigart: Yeah yeah I mean and so they're basically a few other different cipher schemes that that came up. But really I think it was the Visionaire cipher I'm never certain if I'm pronouncing that correctly and it's that really the Visionaire cipher is sort of like a the Caesar cipher except slightly more complicated and you have a different key for the first letter of you know if you have a key that's four letters long then you have one Caesar cipher key for the first letter of a message in the second letter you have a second key in the third letter e of a third key and forth Larry's of four key in the fifth letter you go back to the first key. And this was really kind of brain dead simple but it remained effectively unbreakable they called it the indecipherable cipher in French which I'm not even going to attempt to to pronounce. But I think Charles Babbage solved it but not published or was able to break these messages but never published is method behind it.

41:20 Michael Kennedy: That that's like back to the 1800s or something right?

41:22 Al Sweigart: Oh yeah yeah I think it came about in like the 16th century or so I want to say. But for you know a couple hundred years it was just unbreakable but you know these days like anybody with a laptop and a little bit of programming knowledge could write something that can just do all the number crunching that you have to do to come up with to be able to break these ciphers. So I wanted to sort of write an entire book on that and you know, and so all of these Python books you know going from Invent Your Own Computer Games with Python even the fourth edition and Automate The Boring Stuff and Hacking Secret Ciphers I release under a Creative Commons license so you can find them online at InventWithPython.com and they've been copied to other sites I'm sure I just really wanted to put this information out there and get it into the hands of as many people as possible.

42:09 Michael Kennedy: Yeah that's great and so people can go check it out they can basically read it online they're right or they can buy the tree version or maybe even right--

42:16 Al Sweigart: Yeah and the Kindle version is slightly oh it's formatted a much much more nicely. I just sort of have these HTML versions that I sort of hastily exported from Word, but yeah so all the information is there even if you don't have the funds to pay for an actual like dead tree book. And I get a lot of emails from sort of students and other people who are you know really thankful that information is there that they can just read it off of their screen without having to buy a book yeah that's really cool.

42:46 Michael Kennedy: Do you know if your book is being used in any like classes like official classes?

42:50 Al Sweigart: I do get some emails from a few teachers who say they're they're using sort of like Invent With... Invent Your Own Computer Games with Python as part of a class I never really wrote it sort of to follow a curriculum or to be teachable you know in 45 minute or 90-minute class period. But you know I'm really glad that you know it has some examples that they can pull out especially being online where and under a Creative Commons license where they can easily just copy and paste sections they need and print it out as needed so I'm really glad that it's just produced some material that teachers can can make use of.

43:28 Michael Kennedy: Yeah that must be really cool to think that there's probably kids out there learning through your book.

43:33 Al Sweigart: And also really terrifying. Just in case I have any typos or or weird practices of mind I should have actually been clear on how this works.

43:42 Michael Kennedy: I don't actually do this for real people. So you kind of wrap up things by talking about Pygame and you have another book on Pygame itself right?

43:50 Al Sweigart: Right so Pygame is a 2d graphics library. Pygame's really a wrapper for another library called sdl that is very popular with programming 2d stuff with C++ but Pygame really makes creating sort of graphical games so much easier. You can just sort of point to a PNG image file on your hard drive and just display it in a window and then start moving around and it can support mouse input and also play media files you can play mp3s or wave audio. And I have I once wrote a blog post a long time ago about professional games that actually make use of Pygame I know that there's at least two or three of them that are on Steam the people named marketplace that were originally just created in play games so it is at my game is mostly seen as sort of a you know just sort of like a toy hobbyist library but you can actually make commercial games with it.

44:49 Michael Kennedy: So a good 2d scroll run might work well there.

44:54 Al Sweigart: Oh yeah definitely. Yeah so so at the end of Invent Your Own Computer Games with Python I had a few chapters devoted to sort of just a very light introduction to Pygame followed by one game programmed in programmed using Pygame. I felt like wow I have a lot of other ideas for small games I'd like to create so I thought like well guess I'm writing another book and yeah and so it was that was the second book that I wrote it was also a self-published and then Hacking Secret Ciphers was self-published and this is all you know just a spare time hobby of mine but it was sort of this was what built up my confidence into thinking like hey okay I have an idea for a book that could be really you know possibly really popular that I would want professionally published and that's sort of when I approached No Starch Press about publishing Automate The Boring Stuff with Python.

45:44 Michael Kennedy: That's really cool. Was it weird like when did you first start describing yourself as an author to other people? They go hey what do you do? It's like I'm programmer, leave me alone.

45:54 Al Sweigart: It's always been weird because I thought like well you know it's just a self-published book. Is what I would always tell people. I guess it was really sort of with you know Automate The Boring Stuff you know I would go to Python meetups and people would know who I am because of that book. Like I would go to PyCon on people would have heard of me and it's a strange feeling that never quite stopped being strange but you know it's it's also a great feeling.

46:18 Michael Kennedy: I remember I came over to see you at the No Starch Press booth last year at PyCon and that's how I found you. Hang around with the books.

46:25 Al Sweigart: I remember at that PyCon I had two people who wanted to take selfies with me and so I kind of felt like a rock star and it's you know I mean our industry talks a lot about impostor syndrome but I feel like I have enough experience under my belt right now that I still feel like an impostor but I'm pretty sure I'll get away with it for the rest of my life useful devour friend of yours that you're right yeah the betrayal danke like I am completely confident myself that no one will ever find out what a fraud programmer I really am.

47:02 Michael Kennedy: Nice so speaking of the trail are you what books are you working on next?

47:07 Al Sweigart: So yeah so I have the minecraft book that I'm working on. And I keep coming up with ideas for I don't know like Automate The Boring Stuff with JavaScript or maybe sort of a sequel to automate where you're using flask to create really small applications. So and again this would be sort of for office workers who have something that they you know some custom software that they would like to have written that's really small but they don't want to hire they don't like contract out a programmer to write it for them. I was thinking like well you know you could actually use flask and the browser for all the UI components and everything like that. I thought like okay that would be pretty good and then I just keep poking around with with other ideas so but for right now the the Minecraft book is the one I'm working on and I'm also creating a website that is set up so that readers of that Minecraft book can then go on and write their own programs for Minecraft robots and then go to this website to share their programs with a lot of people. I use this tool called Scratch from MIT in a lot of teaching to programming to kids. It's a great system. It has sort of this like snapped together block interface for creating games.

48:20 Michael Kennedy: I really like it I used it for my youngest daughter's hour of code I wanted an hour code in like elementary school for all the grades he's one great at a time and we used it and I was blown away at how could the kids could do stuff with it.

48:36 Al Sweigart: Oh yeah I mean the one thing that I've learned is that especially young kids really have problems typing or you know they're just like pecking at the keyboard one key at a time and it's really frustrating to type that slowly and then realize you left out a semicolon and now you get some weird error message and scratch fixes that with this snap together block system. Where it becomes literally impossible to make a syntax error in your code because the blocks just don't fit that way. I just you know the more I used it the more like clever design decisions I realize they had made behind the scenes and the other great thing about scratch is not only did they really nail down the editor. But the scratch runs in a web browser and also it has a website where you can share all of your programs with other people and also view the source code of other people's programs. So you know if you find some game that's really clever you know you have the motivation that you want to create something cool to show off to people and learn more programming to do that but also if you find a really cool game then you can just you know click on one button and I'll show you the source code behind that. And that's sort of something I'd been thinking about like okay I really want to create a website like that for this Minecraft robot programming book and also I've been sort of thinking like oh maybe I could create something like that for Pygame specific games as well.

49:53 Michael Kennedy: That'd be really fun yeah you could totally do that a little bit of docker a little bit of Github.

49:59 Al Sweigart: Yeah exactly that's why I'm teaching myself like bootstrap and a whole bunch of other web frameworks and things like that.

50:10 Michael Kennedy: Of course I like Bootstrap. I use it for all my sites is about this great alright well so you're working on a little book you've got the website coming like how do people keep up with this? Like how will they know when this comes out? Probably the best way is to probably follow me on Twitter actually but then also I have I'm restor... I had a blog on InventWithPython.com that I used to post little tutorials to and I'm ramping that back up again so it's if you just go to InventWithPython.com and then click on the link to the blog that's probably like the best way to keep up with what I'm doing. And I also have oh I also have AlSweigart.com which is a really plain looking website but I kept losing track of like oh yeah I created this Udemy course for Automate The Boring Stuff in fact I think i forgot to mention that Miss in this podcast yeah so why don't you tell people about it because you do have a pretty popular course on Ume right? around your book

51:10 Al Sweigart: Yeah so this was like shortly after Automate The Boring Stuff came out and I kept thinking of like how can I help promote this book and so I found Udemy.com. So there's a lot of these these websites online that have video courses but most of them you have to be affiliated with a university. I think like Coursera or EDX you have to be a professor essentially Udemy.com has courses that can be created by anybody. And so of course you know it does have sort of like a lot of really low quality courses on it but I felt like well okay I can try this out and so I started making screencast tutorials basically following along with Automate The Boring Stuff and and that course actually sort of is the other half of my income I mean it's sort of making as much as as Automate The Boring Stuff the book and I'm really just blown away by you know that's like well I can actually you know it wasn't entirely dumb to quit your job as a software developer and enter the lucrative world of you know books.

52:08 Michael Kennedy: Yeah I think that the the trick or the thing you got right is you've got the book you've got the website you've got the course it's like you sort of got a package around it it's not just like oh here's a book I did go for and like live in the world book. You've got the promotional angles and the different stuff going.

52:25 Al Sweigart: Yeah yeah yeah I try to just create a whole bunch of things that sort of support each other and you know and of course all of this is just only possible because so many people in the Python community are so excellent about spending their time and skills making Python even better and just building the community. It's something I really haven't seen anything close in other programming communities. So the Python and also PyCon the Python conference are just they just blow my mind it's it's really great.

52:55 Michael Kennedy: Yeah I definitely a second that and for people who don't have your PyCon ticket yet better hurry it's getting kind of... it's going to be sold out and you're going to be sad right. I have a booth there with the podcast booth and are you going to be there?

53:06 Al Sweigart: Oh yeah definitely.

53:07 Michael Kennedy: Nice. Yeah it's not that far from San Francisco to Portland right?

53:11 Al Sweigart: Oh yeah not at all in fact last year it was it was also in Portland they do it in the same city for two years before switching and I went up with a bunch of people on Amtrak. So there's a group of people starting in San Jose making their way up the coast taking a train to PyCon. So that was a lot of fun just hanging out with conference goers beforehand. I think it i think it was called like pycon on a train or something like that or Pythons on a train but yeah you can probably if you're interested in finding out about that you can probably google for that oh that's awesome.

53:44 Michael Kennedy: It's about a 15-minute drive from my house to PyCon this year so no trains for me. Yes but maybe maybe sometime soon I'll get a chance although probably not to Ohio next year. But definitely if you guys are interested and go to PyCon and want to do a selfie with Al get yourself there. Alright man that's awesome so it sounds like you have a lot of cool stuff brewing and this book is really interesting and thanks for chatting through it with me.

54:11 Al Sweigart: Yeah thanks for having me. You bet.

54:13 Michael Kennedy: Before I let you out here two more questions. We just passed over a hundred thousand packages on PyPI I what one one of those few would you like to recommend that people check out that we don't know about?

54:24 Al Sweigart: Well if I want to be totally selfish PyAutoGUI because it's a package that I wrote. It's a GUI automation package we're essentially can have your Python scripts control the keyboard and mouse so if you just have some task where you need to like click on something over and over again or just like automatically fill out a form and type something for you. You can use this to essentially sort of like simulate having an intern type and click for you. But I think really I'm really impressed by the requests module... so requests is a really simplfied...a really simple to use sort of version of what urllib in the Python standard library does for making HTTP requests except its request just makes it so easy to just download files or access websites and I'm just really impressed by it and so I highly recommend that.

55:14 Michael Kennedy: Yeah I definitely second. It's a very very nice one awesome.

55:16 Al Sweigart: And favorite editor I don't I mean these days I'm mostly using Sublime text so I've kind of gone the text editor route rather than having like a full-blown IDE so yeah I think I would go with Sublime Text but you know I guess IDLE technically counts in IDE so I would say my favorite ide is IDLE.

55:36 Michael Kennedy: All right on alright so it's been great to chat you with you about this final call to action what's people do to learn more about what you do?

55:43 Al Sweigart: okay oh well I guess like alsweigart.com has just links to everything I've done but really like as far as a call to action definitely get involved with with your local Python community. If you live in a large city there's bound to be like a Meetup.com group for Python or definitely think about heading out to PyCon even if you're still a beginner in Python or you know you're not yet a professional software developer it's such a great and welcoming community that I really recommend it for a lot of people.

56:15 Michael Kennedy: All right I totally agree. Great recommendation. All thanks for coming on the show. Chat with you later.

56:19 Al Sweigart: Yeah take care bye.

Back to show page
Talk Python's Mastodon Michael Kennedy's Mastodon