Don't do this at work
This is a modal window.
The media could not be loaded, either because the server or network failed or because the format is not supported.
Formal Metadata
Title |
| |
Title of Series | ||
Number of Parts | 118 | |
Author | ||
License | CC Attribution - NonCommercial - ShareAlike 3.0 Unported: You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal and non-commercial purpose as long as the work is attributed to the author in the manner specified by the author or licensor and the work or content is shared also in adapted form only under the conditions of this | |
Identifiers | 10.5446/44789 (DOI) | |
Publisher | ||
Release Date | ||
Language |
Content Metadata
Subject Area | ||
Genre | ||
Abstract |
| |
Keywords |
EuroPython 201982 / 118
2
7
8
15
20
30
33
36
39
40
45
49
51
55
57
58
63
64
66
68
69
71
74
77
78
80
82
96
98
105
107
108
110
113
115
00:00
Self-organizationMultiplication signVideoconferencingLevel (video gaming)Lecture/Conference
00:41
SoftwareProduct (business)Data managementData miningComputer programmingSoftware developerOrder (biology)Materialization (paranormal)Computer animationLecture/Conference
01:44
SoftwareData managementProduct (business)Software developerWave packetLine (geometry)Vapor barrierType theoryProjective planeIntegrated development environmentCodeComputer animation
02:22
SoftwareData managementProduct (business)Multiplication signProjective planeShared memoryFamilyGame theoryCodeComputer animation
02:59
Turtle graphicsTurtle graphicsModule (mathematics)Game theoryTotal S.A.Integrated development environmentLecture/ConferenceComputer animation
03:34
Turtle graphicsGame theoryClique-widthTouchscreenTurtle graphicsControl flowPrimitive (album)Clique-widthGraph coloringModule (mathematics)DistanceLoop (music)Transport Layer SecurityBitLine (geometry)Game controllerPoint (geometry)Game theoryDirection (geometry)TouchscreenNegative numberConstraint (mathematics)RoutingWindowComputer animation
06:37
Turtle graphicsTouchscreenRange (statistics)Gastropod shellComputer fileTransport Layer SecurityRange (statistics)Turtle graphicsLine (geometry)Functional (mathematics)Graph coloringModule (mathematics)Cellular automatonMatching (graph theory)Multiplication signTotal S.A.Reading (process)Scaling (geometry)PiRight angleForcing (mathematics)Sound effectMaxima and minimaTerm (mathematics)RippingDot productPoint (geometry)1 (number)Lecture/Conference
09:01
Turtle graphicsClique-widthRange (statistics)Game theoryComputer fileTouchscreenGastropod shellTime domainStatement (computer science)Electronic program guideShape (magazine)Event-driven programmingLine (geometry)Positional notationComputer-generated imageryTransport Layer SecurityRandom numberCirclePosition operatorTurtle graphicsTransport Layer SecurityKeyboard shortcutFunctional (mathematics)MathematicsBitRandom number generationModule (mathematics)Game theoryGame controllerSystem callPosition operatorMotion captureComputer fileLoop (music)AuthorizationMultiplication signLine (geometry)Medical imagingRight angleMereologyPoint (geometry)RandomizationShape (magazine)MiniDiscLogical constantSet (mathematics)Event horizonArtificial lifeOptical disc driveNumberTheory of everythingWordGradientLambda calculusSign (mathematics)Cartesian coordinate systemSocial classDirection (geometry)Process (computing)CircleOpen setSpeech synthesisLecture/ConferenceComputer animation
16:28
Food energyTurtle graphicsBeat (acoustics)Lecture/Conference
17:07
Transport Layer SecurityPositional notationTurtle graphicsComputer-generated imageryShape (magazine)Turtle graphicsCircleResultantFunctional (mathematics)Parameter (computer programming)Shape (magazine)Graph coloringStructural loadBitNumberMultiplication signModule (mathematics)Maxima and minimaComputer fileRight angleMedical imagingMiniDiscGame theoryComputer animation
18:50
Random numberTransport Layer SecurityPositional notationGame theoryTurtle graphicsTouchscreenMaxima and minimaMessage passingComputer fontComputer-generated imageryShape (magazine)Turtle graphicsSign (mathematics)Functional (mathematics)NumberCurveKey (cryptography)Multiplication signCodeMotion captureInsertion lossCore dumpKeyboard shortcutRight anglePlanningPosition operatorGame theoryDefault (computer science)Graph coloringMathematicsCondition numberCASE <Informatik>BitMessage passingComputer fileTransport Layer SecurityComputer fontString (computer science)1 (number)Negative numberGame controllerSystem callNeuroinformatikBoundary value problemMaxima and minimaGoodness of fitModule (mathematics)Function (mathematics)Cheat <Computerspiel>Lecture/Conference
24:40
Transport Layer SecurityTurtle graphicsComputer-generated imageryShape (magazine)Line (geometry)Random numberMaxima and minimaMessage passingTouchscreenPersonal area networkLinear subspaceClique-widthExecution unitRange (statistics)NumberGame theoryReading (process)String (computer science)Variable (mathematics)GeometryMedical imagingLoop (music)Line (geometry)Goodness of fitStatement (computer science)Turtle graphicsFunctional (mathematics)CodeLogical constantMultiplication signProgram slicingMessage passingEquivalence relationMathematicsLecture/ConferenceComputer animation
28:21
Turtle graphicsComputer-generated imageryMultiplication signPoint (geometry)String (computer science)Software developerRight angle2 (number)Heegaard splittingPairwise comparisonLine (geometry)CodeMedical imagingLecture/ConferenceComputer animation
29:05
Transport Layer SecurityTurtle graphicsComputer-generated imageryLine (geometry)Sound effectObject (grammar)Type theoryTurtle graphicsAttribute grammarFunctional (mathematics)Lecture/ConferenceComputer animation
29:50
Endliche ModelltheorieCodeObject (grammar)Attribute grammarException handlingDependent and independent variablesPower (physics)Lecture/Conference
30:27
Positional notationMessage passingComputer fontMaxima and minimaStatement (computer science)Position operatorCodeLine (geometry)Multiplication signIterationSource codeFunctional (mathematics)Poisson-KlammerGodRight angleThermische ZustandsgleichungElement (mathematics)Computer animation
33:13
Standard deviationLibrary (computing)Directed setLogical constantString (computer science)Telephone number mappingKeyboard shortcutElectronic mailing listCodeRight angleFunctional (mathematics)Library (computing)Direction (geometry)Module (mathematics)Turtle graphicsSlide ruleMultiplicationMedical imagingCASE <Informatik>Multiplication signoutputTerm (mathematics)Lecture/ConferenceComputer animation
35:49
Game theorySoftwareVirtual machineObject (grammar)Tablet computerRight angleLecture/ConferenceMeeting/Interview
36:44
Condition numberGame theoryComputer networkLoop (music)Event horizonTransport Layer SecuritySoftware developerNetwork socketLoop (music)Condition numberComputer programmingDifferent (Kate Ryan album)Level (video gaming)CodeConcurrency (computer science)Game theoryoutputDependent and independent variablesWater vaporEvent horizonData analysisSoftwareKeyboard shortcutEndliche ModelltheorieWeightAttractorMereologyCommunications protocolCoroutineBoss CorporationDistanceComputer animationLecture/ConferenceMeeting/Interview
39:06
MereologyRevision controlMessage passingLine (geometry)Module (mathematics)Game theoryPoint (geometry)CodeString (computer science)Formal languageFood energyVariable (mathematics)Multiplication signLevel (video gaming)Lambda calculusBitHacker (term)Chemical equationSinc functionPattern languageMaterialization (paranormal)Library (computing)MathematicsLoop (music)Condition numberCodeFunctional (mathematics)Computer fileGoodness of fitKey (cryptography)Musical ensembleMetropolitan area networkComputer animationLecture/Conference
Transcript: English(auto-generated)
00:03
Let me try to extend that thank you to a bunch of other people. Like all the volunteers, all the organizers. This is a huge endeavor, so thank you to them. The sponsors, thank you very much. Please, if you can, next time, more money.
00:21
And the often forgotten technical people like Matteo out there that are doing sound, video, stage lighting and stage support. Let's have a quick hand for all these people. Thank you.
00:41
So, hello. My name is Thiago. I come from Lisbon in sunny Portugal, which I invite you to visit. You will be welcome. And this story starts back in January when a very dear friend of mine came to me knowing that I do these kinds of things professionally.
01:05
And he asked me, Thiago, would you organize an introduction to programming with Python Workshop for my oldest kid, along with a few of his friends from school? Well, these kids are about 12 years old.
01:22
And of course I said yes, knowing two things from the start. The first one is that working with kids is a very serious business. So an appropriately serious amount of preparation was in order. And the other thing was that I wouldn't be able to reuse none of the materials, tools, exercises, whatever,
01:44
that I normally use when I train professional developers. And here we go. So instead I need to go for a much more informal approach, like a learn-by-doing approach in a friendly, fun, if somewhat chaotic environment where kids would be really engaged into the thing
02:03
without, you know, kind of barriers. So we would go for a learn-by-doing in lots of mini-code-along sessions where I would explain maybe a simple concept, type a few lines of Python, maybe one, maybe two, and they'd tap along and run it. And we'd do that repeatedly until we reached like a mini project
02:20
that was fun and they could then explore. And then we did that three or four times, and I wanted the last project to be something they would be really proud of. They would take home and share with their friends and family, you know, and it would need to be a game. Now, we're having a problem with projection.
02:40
May I plug and plug? Do you need anything, Matteo? Are we okay? Okay. I'll move on. I'll move on. You won't be able to see the code if it's not projecting, but I'll keep talking. Don't worry. So we needed... Thank you. So given that we were starting from the Turtle module,
03:02
I wanted to see if we could build a game with the Turtle module. Okay, and if you don't know the Turtle module, let me show it to you, hopefully. I'll try that. I'll try that. So the Turtle module implements, as the name implies, Turtle Graphics. And Turtle Graphics are a thing that back from the 1960s
03:21
where you get a canvas, okay, and a turtle. This is from a programming environment called Logo back then. And you get a canvas and a turtle, and essentially you can say things like, Turtle, go forward a certain amount. And you see, the turtle goes forward, okay? And then you can say,
03:42
Well, dear turtle, can you please turn left? And it does turn left. Do we need to make a break to fix this? No problem. There's no problem at all. We can try and see what's going on. So there's this certain amount of primitives that we can tell the turtle to do, like you see.
04:03
Okay, it's stable now, so it's just a glitch. So now we turn the turtle, we say go forward, and now it goes forward in a different direction. It keeps drawing a line, so it's pretty intuitive. It behaves pretty much like a pencil on a sheet of paper. And pretty much like a pencil, you can say, Well, turtle, go up, okay? It should be fly, because the turtle should fly for kids,
04:21
but it's forward, no problem. I mean, if we now say go forward, now it goes forward but doesn't draw the line, so it nicely animates. This could be useful for a game, I said. Oh, this can be useful. And then there's other fun things that we can do. We can tell it, go forward a negative amount of distance, whatever, and it goes backward, which is kind of intuitive, maybe.
04:42
And more, if we bring it down again, such as it goes back to drawing lines as it moves, we can also tell the turtle to go to a specific point in the canvas, and it will go there directly, in a straight line, regardless of being facing whatever it is. So it's now kind of facing up, but you tell it to go to the origin, which is the point where it started,
05:03
in the center of the canvas, and there it goes. It draws a straight line, okay? So the question is, could we build a game with this? And again, the answer is yes. And let me show you, okay? What do I want to do? I want to start with something like, what, Python 3.7, maybe?
05:21
Shall we? Let's do that. It's here, game.py. And we will make it executable. And don't worry about this. You really don't care about this. What you care is this. So we'll import you to the module, okay? And we'll run it like this. And if you run this, this main loop thing here,
05:42
it's just like keeping the canvas window open, okay? So let's pimp it up just a bit. And we can pimp it up by setting up a screen thing that's here, okay? And we say start x600 to shift the canvas more towards the right-hand side of the screen. And we'd like to work with the known width and height,
06:01
so 640 should be enough for anyone. Mr. Bill Gates has said that, so let's go with that. And let's give it a nice orange background color. And if you haven't made any horrible mistake, we now have a nice orange canvas. So what kind of game do we build with an orange canvas? Well, given that Minecraft and Fortnite were taken,
06:22
okay, so we went a different route. And in our game, there will be two characters, a player, player control character, aptly named player, and a beast. So it's the player and the beast. And these characters move around in the grid, somehow constrained in the grid. And the purpose is you control the player, you need to capture the beast,
06:41
the beast runs away randomly, okay? Pretty simple, much better than all those other fancy stuff. So we will start by drawing a grid to help us out with this. So bear with me for a minute. This will define the grid size, which is the grid size, each cell size. The grid span, which is how many cells we will have from the center of the canvas.
07:02
And we'll use this one, grid max, which will be used all around. So this would be grid size times, grid grid, no, grid size, okay, go. And grid max, grid span. And we'll draw the grid using a line function that draws arbitrary lines. So let's go here and here. We know these, we can say, total up, and then we go.
07:22
So we'll go to the starting point, like x1, y1, and then we duplicate these ones, say go down, and now go to the end point, okay? So this would be 2 and 2. And now we can iterate over the range for i in range of minus grid span to grid span, okay?
07:43
Plus one, because there's always this plus one, isn't there? Scaled i equals i times grid size. And now we'll draw a line from minus grid max, that's the leftmost edge, let's say of the grid, scaled i, and to grid max, scaled i.
08:01
If we run this, and if I haven't made any horrible mistakes, there you go, the total slowly trying to draw grid. So let's speed it up, okay? Let's speed it up. Hang on, hang on, we're just starting, okay? Let's keep having some fun. So we can say something that's not very intuitive at all, if you look at the dots, but the speeds, they tell you in the dots,
08:20
it's from one to ten, ten being the fastest, but if you tell it zero, it's even fastest, okay? So you go zero. You can hide the total, and you can tell it, give it a nice, you know, dark orange background color, because to match the nice orange color for the line, let's make it thicker, you know, for effects, whatever.
08:41
And by the way, let's swap these coordinates here, okay? And we get the vertical lines as well, and we get a grid, a much finer looking grid. Right, so this is a grid. We need the players, the characters, okay? And one very interesting thing in the total module is that we can have as many turtles as we like, okay?
09:00
We don't get just one. So how do we create a turtle, like this? Let's see it. You go player equals turtle, that's a module, and there's a turtle class, and that's it. And you get a new turtle, and you can have as many as you like, and there's your turtle. Not a very engaging character in the game, okay? You're a 12-year-old, you want to see something more engaging.
09:22
So there's yet another thing that the turtle module can do, which is very cool, okay? If I can properly copy this line, of course. So that would be, you can register a shape and give it a file, okay? Have on disk. That would be player GIF. I'll tell you about the file in a moment.
09:40
And then once you do that, you can tell the player turtle to use that shape. Once you do this, you get this guy. And now the kid's eyes go, oh, I like this. I know how to move this guy. It's move and go to and forward, okay? So let me tell you about these files in a quick minute. So I brought along these two files here,
10:01
which I obtained from OpenGameArt.org, okay? Go there, lots of cool stuff, thanks to the authors. And the other thing we want to do, of course, is to control this player character with a keyboard. So how can we do that? Well, here's another trick that the turtle module has for us. So that's a two-step process, okay? We say something like this.
10:23
On key, func, and then up. And the idea is the listen function tells the main loop, okay, when it enters the main loop, to keep listening for keyboard events. And this other call, on key, tells the main loop to call the function func
10:42
every time the up key on my keyboard is pressed, okay? So we will need four of these because we want to control the player in all directions, down, left, left, and right. Right, right. And as for func, let's go a bit funky, okay, here. I'll go with a lambda. Don't worry too much for now.
11:01
I'm just being generic here, okay? And when we move up and down, x doesn't change. When we move left and right, y doesn't change. When we move up, y is positive. Down is y is negative. Left, x is negative. And right, x is positive. Now if we just need to create the move player function,
11:23
don't you like the justs that we keep introducing in our speech? Just, just. It seems as if it's obvious. It's not, okay? Especially when working with beginners. So we will create the move player like this. So it takes a delta x and a delta y. So we'll try to move the player relatively to where it is. So if we knew where the player is, we don't, okay?
11:43
We could do something like player go to, remember this one, wherever you are, plus the delta x, x times the grid size, right? And wherever you are on the y-axis plus the delta y times the grid size. And if we knew the player position, if only we knew the player position,
12:01
okay, that's it, okay? If only we knew it. And you go like this. And now if I hit my keyboard, oh, there it goes, okay? It's drawing the line because, guess what? That's what turtles do here, okay? But we know how to fix that, okay? Because we have the player fly or up method. If we do this, then it moves nicely, okay?
12:22
It can go crazy, but we don't really care, okay? It moves. What's missing? Well, the other character, the beast, right? So we might feel tempted to like copy and paste these. We won't do that. You don't copy and paste code, don't. I'll do a few of that, but we'll get to that. So we'll instead create an actor function
12:42
that will create an actor or a character for us, okay? And the way to do that could be, one possible way would be, let's say, this image, okay? An image. Then we'll return this thing that we're creating here. I don't like that. It's called player because it doesn't make much sense any longer. So let's call it T. And see that player is actor.
13:00
Player give, okay? And this should work, okay? And now we can just copy it over and say beast, and we have player and the beast. And there's the beast, okay? It's sitting there still, so let's make it move around. So we could create maybe a move beast function, okay? Let's see, find a move beast function here
13:20
that will move it kind of randomly. And for minus grid span to blah, blah, blah, you know, this is the boring part. Sorry for that, but there's no fun without pinging, I guess. So we can say beast. Now go to guru, okay? I'm kind of Japanese-ish today. X times grid size. And Y times grid size, I guess.
13:43
And now this function just moves the beast at some point in random, the grid. We'll call it right before starting here. And the other thing we're going to do is we'll need to import the random module, right? Because we're using the random function. So there it goes, okay? So it's there. Let's start it again, let's see where it goes, okay?
14:02
Okay, it goes there. Now the idea is that we'll move the player, and as we move, we need to see, did we capture the beast, yes or no? And when we'll capture the beast, the beast will do like a, let's call it a small dance, as if it's stunned by being caught, then it runs away again. So the way to do that is, let's say every time we move the player,
14:22
we will attempt a capture, say capture, capture, okay? And what's that? So attempting a capture might be something like, well, if the player position is equal to the beast position, right? Then beast do a dance. Doing a dance might be drawing a circle, which is yet another method of turtles that,
14:41
you know, appropriately named, draws a circle, but, say this ray, it won't draw it, just do a little dance. We'll reduce the steps to make it faster, and then you say, move beats, so go away from wherever you are, and let's see how it goes. If I save this and run it. So, dance goes away, cool, okay.
15:01
Dance goes away. Dance goes away, okay, cool. But it's really not challenging, because as we move, the beast is just sitting there, so it needs to be a bit more challenging. So maybe, whenever we move, there's some odds that the beast will try to trick us, okay? So it might say something like,
15:20
well, else, if we didn't capture, okay, so maybe if random.random, this will give us a random number between zero and one, okay? We call it lower than move beast odds, we'll just move the beast, okay? So the bigger this number is, the bigger the odds are the beast moving around and tricking us.
15:41
So we'll set this, call it the game difficulty constant here, and we'll say, well, let's make it one in five, 0.2. So let's see how it goes. So, dangerous, this is random in a live coding scenario, so it might never happen, but it did, you see. I'll just move around and see if the beast, okay, the beast, it's very dumb sometimes,
16:01
so if you really want to capture it, you'll see there's some artificial intelligence here. The random number knows, I know that. I've been playing this and the kids know too. So if you take a look at this, okay, so the game is already cool, okay, you can play it around, but there's a very serious problem in this game, and that's it never ends.
16:21
And with kids, you need games to end because at some point we'll be telling kids, come over, it's dinner time, and they say, hey dad, let me just finish the game. It doesn't finish, so it must finish, okay? So we'll introduce the concept of energy or karma, which will again be visually represented by yet another turtle, of course,
16:42
and we'll have like a turtle center on top of the grid, and the turtle will move slowly towards the left as the player moves, okay, and if it ever reaches the leftmost edge, the player loses. And on the other hand, whenever the player captures the beast, the karma will move like a chunk towards the right, and if it ever reaches the rightmost edge,
17:02
the player wins, okay? So let's build this step by step. So we need a new turtle, a new karma turtle. So let's go there. This is the time for copy and pasting, right? We can copy and paste this. Karma, but oh, come on, I don't have a karma.gif image on disk, so what am I to do? Well, it turns out that if you look at our actor function,
17:23
it creates a turtle, registers a shape, loads it from disk, and then tells the turtle to use it, okay? It turns out there are a few built-in shapes in the turtle module that don't require registering, okay? And one of those is called circle, which we'll use here, which will fail to register
17:40
because register will try to open a file on disk. So we'll say, well, if the image ends with, you know, .gif, then register it. Otherwise, assume it's a built-in shape and just go ahead and do it. And there's our karma turtle. It's not at the right place, and it's kind of dark. Nobody wants a dark karma, right? So let's make it shiny a bit.
18:02
So one thing I'd like to do is something like, maybe color equals gold. I don't know. And for that, we'll add a keyword argument here, defaulting to black so that I don't need to go back and change all the other code and say this is the turtle I created locally
18:20
and say color, color, okay? And this should work. And then we'll tell the karma turtle to go to zero, that's horizontal center, and then gridmax, which is the top of the grid, and just a little bit further up in that thing. Whenever you need a little bit further up in a crazy number, you go at 42 and nothing can go wrong. So there you go.
18:40
There's our karma turtle, okay? And now it needs to move, depending on the result of the game. And if you remember, even though it's a circle, it's facing towards the right-hand side, okay? So we'll use that forward positive number, forward negative, as a trick to move it left or right so that moving the karma turtle is just a matter of saying,
19:02
karma, go forward, this number, okay? So to do that, we'll create an update karma function somewhere here, let's say, well, well, well, well, maybe here, not here, Tiano, maybe here. So let's call update karma, it takes a delta karma, which would be positive or negative, depending, and we'll say go forward, delta karma, that's it, okay?
19:22
And now, in our attempt to capture, we say, well, this is a capture case, so we'll say update karma with a karma value of a capture, and we'll set this variable to a positive value. And then, this is the case where we moved, but didn't capture, so we'll go there and say, karma move, and we'll set these two here, maybe.
19:44
Let's say this one is 100, and maybe this one, this one I know it needs to be negative, so let's go minus 20, say, and we'll get back to our code and let's run it and see if it moves. Okay, slowly towards the left, right? Slowly towards the left, and now it should jump
20:02
towards the right-hand side. Okay, this is working, again, you get the idea. All we need to do now is to find our boundary conditions, detect them, and tell game over, you won, game over, you lost. And we do that by checking the karma position, the horizontal position, after every move.
20:21
So we'll grab the karma x and karma y, we already know these, karma y equals karma position, but we really don't care about the y, so let's go like this, okay? That's a throwaway variable, and we say, well, if karma, where's my keyboard? Karma x is greater than or equal to grid max,
20:40
then let's say in game, we'll give it, this is winning, so let's say, yay, victory. Congratulations, you won. And otherwise, we can say, well, if it's negative, if it's below negative grid max, so this is certainly a defeat, so let's go, ah, defeat, sorry for that, try again, where end game, where the end game function is a function that takes a message
21:01
and uses the right method of turtles that just displays text strings besides the turtles. We don't have much control, but it's good enough. We have control over the alignment, the horizontal alignment, which we'll try to center on the turtle. We will override default font, because the default font is actually too tiny
21:21
for this kind of game, and I thought, why not go with Helvetica for two reasons, okay? So it's a hallmark of Swiss design and it seemed appropriate, and you really can't go wrong. Nobody was ever fired by using Helvetica, so don't be creative with your fonts. Use Helvetica and you'll be fine, okay?
21:41
So if you, I don't want to go off rail, but I love typography, so don't use other things. Don't be creative. So this should work, okay? So just write a message, and we're going to test this by cheating a bit, so, because we don't have that much time, so we're just going to say, well, let's win this really quickly. We'll always win really quickly.
22:01
And here we go. So we move, yeah, we're about to win anyway. I don't really care. I go up and down, I don't care. I win, victory, great. So it seems to work. The text is in black. I'd rather have it in white, but if you recall, the turtle's default color is black, so I'll change it to white. And there's another thing, and that's a much more serious thing, because if I head up, you see,
22:22
the game is still on, okay? And why is that? Look at our code. So we said in-game. What does in-game do? We thought in-game meant everything's done, but all we did in in-game, sorry for that, is we wrote a text message. But originally, when we set up the keyboard things,
22:40
we told listen, and whenever this key is pressed, do this and this, call the move. It's still doing it. It's still doing that. We haven't told it, stop doing that. So ideally, we would do turtle and listen, so forget about the keyboard, just be still. We can't do that. The turtle module doesn't have that, so we'll hack it away in a very ugly approach,
23:01
but it works, so I don't care. Up, down, left, left and right. And I'll tell you a bit about why I think this is really bad later, but if we have the time, okay? On key, we'll go the on key again, but now we'll say, well, for these keys, use the none function, so it means forget about
23:23
anything you knew about this key, okay? And we want the player turtle to be white. You remember that? And this should be pretty fast and cool like this. So let's see, okay, down. Oh, I want to win really quickly. Yes, we're in cheat mode, because we are the coasters.
23:41
We can do anything, right? It's very powerful. Victory, good. And now I'll bang on my keyboard to see if it works. Okay, I'm banging. This is one of those Apple computers, but one of those oldest that used to come up with real keyboards, okay? The new ones seem kind of flaky. That's not the case. So Tim, please fix the keyboards, okay? Thank you.
24:00
This is working, and what I'm going to do is cheat the other way around to check that my losing condition is being properly checked. And we're nearly done, okay? So let's go minus, what, minus 82? Why not? Let's make it go faster. Go, go faster. Faster for kids is great. So don't capture the beast.
24:20
We want to lose this really quickly, and defeat, and bang on our keyboard. So the game is nearly done, okay? This was my original plan for the game, but the kids came and said, yes, but we need the score, okay? We need the motive to come back to the game and do better. That makes a lot of sense, okay? So they came up with this very simple and effective idea.
24:43
And what they came up with is, well, if we counted the number of steps that the player does along the game, and you just need to print them at the end. So if you win, you want to win with as few steps as possible, okay? That's reasonable. It's a better score. And if you lose, I don't know.
25:01
Ask the kids, okay? So there's a score. Let's implement that really quickly. And the way to do that is, or one way to do that, because there's no single way to do things. We'll do it like this. Well, player steps equals zero. Start counting steps at zero, okay? When things start, and then every time the player moves, this is the function. We will just increment it.
25:22
Plus one, okay? And then, if you remember, there's a method here in game that writes a message with the you won, you lost. We'll just copy and paste that and use the karma turtle to write a different message. We'll use a funky F string. F strings are so cool. Don't you like them? Player steps. We'll make it slightly smaller,
25:42
and this should be it. Let's see. Three statements of Python. We've implemented a score. Isn't Python cool? Three steps, good. So let's cheat the other way around, okay? Let's instead remove the cheat and go for a real quick run of the game to get a feel, okay? And I won't go until the end
26:00
because this is one of those games that may never end so dangerous for the dinner thing with the kids, but it seems to work, okay? It's fun, engaging enough, and I won't play it anymore. I won't bore you because if you play it, you'll have fun, but just watching it is boring, okay? So let's step back for a minute. What do we have here?
26:21
So we have 100 lines of Python code created pretty much in a make it up as you go along way, okay? Yeah, I don't care. Just create a function. Oh, that's not cool. And what I'd like to do now is do a quick review of this code. I'll highlight a few things. I don't have time to go over all of it, of course.
26:40
Highlighting maybe good ideas, bad ideas. Would you do this at work? Yes or no, okay? So let's get here. For example, these variables or constants, these are a very good practice. The character movements and the grid drawing, they're all based on these constants. So if we need to change something, let's say if I go five here, five.
27:02
Can I go five, please? Five, yeah, there you go, five. Then it also works, okay? So this is clearly something that you would do at work. It's a good practice, okay, to have common constants or variables. And same can be said for these. That's the game difficulty, okay? That's a good thing. Then I'm going to take a look at these.
27:22
This is kind of a mess. So this loop draws the grid, but it's difficult to read. I wouldn't want something like this in my serious, sustainable code. Maybe it's the variable names I selected, I don't know, but I don't like this very much. It's hard to read. It's lots of geometry. Bah, I hate it.
27:41
But it works, there you go. And then there's this line. This line deserves a mention because it could have been written as if image of minus 40 or blah, blah, blah, equals two. These are semantically equivalent. And I claim that the first one that I used is much better for two reasons. First reason, readability.
28:02
Read that says, if image ends with that, so it's clear for you to your brain. It's direct to your brain. This string ends with that string. Whereas in the other one, whether you're a beginner or an experienced professional, something like this is going on more consciously or subconsciously. What's that? Oh, that's the colon thing.
28:21
The colon thing is a slice. It's from there to there. What's the there on the right? Oh, there's nothing there on the right hand side. So that's still the end of the string. Okay, what about minus four? So that's four from the end. Does it include the fourth from the end or is it from the fifth or from the month? It's a mess, okay? This happens in a split second for advanced developers
28:40
and it takes a long time for beginners. It's not readable. And then there's this. If you change at some point your mind, you want to use JPEG images, okay, then this line of code still works and the other one doesn't any longer because image of minus four till the end is four characters long and dot JPEG is five characters long
29:01
so you need to change in two places. You need to change your code in both sides of the comparison so it's clearly a worse solution, okay? So go with ends with and starts with. It's simpler. I don't know. Just use ends with and starts with, okay? Keep it simple. Now this deserves some mention too.
29:22
What's going on here? So we're calling the active function that creates a turtle ready to use, okay? So we're ready to play with it and then we're slapping a dot steps attribute there to count the steps. It's our solution to tracking the player's steps and it's effective, simple, okay? So let's review this.
29:41
So in general, everything in Python is an object and Python objects have a type and have attributes. Now, again, in general, any piece of code can create, destroy, rebind or reassign values to any attribute in any object.
30:02
There's no protections. There are some exceptions to this but this is the fundamental model of the way Python works. So in general, this is not really a good idea because we're poking around at objects that might behave differently, okay? So this isn't something you would do at work but it's possible and again with great power
30:20
comes great responsibility. So use these kinds of things wisely but I wouldn't want this in any serious sustainable code, okay? We would need to find different solutions to that. Now, I'll highlight just one more thing, two more things for the sake of time so that we can step back furthermore
30:40
and again this statement could have been written as comma position of zero or bracket zero and again I say that the one I used is better for two reasons. First, again, readability, okay? Maybe for some people that funky thing where the comma, comma and the score, blah, blah, blah is somewhat strange, okay? But you'll quickly get used to that.
31:03
What I claim is that this line has the comma position in parentheses and brackets and blah, okay? So it's slightly harder to read, okay? I'll grant you that might be a personal opinion but check this. When you look at this code, you wonder, well could I have used 42 instead of zero?
31:23
Or could I, maybe that comma position is a dictionary like thing, maybe could I have used key there? You don't know. The only way to answer these questions is to look at the function documentation, okay? Or God forbid, look at source code. You're worried about your update karma function,
31:42
you don't want to know about the others. Whereas here, okay, you know that that karma position thing I'll short it to right hand side is iterable, that's a funky thing, okay? And we won't go into that but think, think less like maybe it's a bunch of things, okay?
32:03
It's a bunch of things. And if you want to know more, we'll talk in the quarters. And it's iterable and it has two elements, okay? It's guaranteed to have that because there are two things here, comma separated, otherwise this code wouldn't work, okay?
32:21
And by the way, if we're using that underscore, that's a variable like any other but it's a way of saying we don't really care about what's here, okay? So we also know that we care about the first, okay? So it's clearly fist, no, first Tiago. So it's clear, other than readability,
32:41
the line that I used gives you three facts immediately and the other line leaves you with a few questions, okay? So again, I say this is better. There's a very interesting article by Trey Hunter, I think. Trey Hunter, he's a trainer too, I think. Go read it and come to me.
33:00
I can point you to that. It's very interesting on this idea, okay? And let's wrap up our code review to move on to some different thoughts. Now just go here. This is the most horrible two lines I've written here in my understanding and why? Because there's repetition. The up, down, left and right string constants
33:21
are repeated here and you see in this code that sets up the keyboard initially and that's really a no, no, no, no, no. So possible solutions would be to define constants with these values, wrap them in a list, in a dictionary, create an enum, something like that. This is really, you wouldn't do this at work. I would be angry or disappointed if someone said
33:42
Tiago just created the best looking code out there. Now I think we can improve that, okay? So let's forget about the code for a minute, okay? So this is the code. Now here are a few thoughts. Well the Python scenario is amazing. It has a bunch of functionality
34:02
and quite often it does surprise you, okay? It has its smelling corners, it does, but if you dig deeper into a given module, you're bound to be surprised and you're bound to find useful functions, tidbits, examples, okay?
34:20
That was the case with the turtle module. I didn't know you could do this with a turtle module. Keyboard input, multiple turtles, GIF images. I can do a bunch, okay? I can nearly serve a breakfast with a turtle module. I don't know. So there have been recent discussions on the standard library and they're very relevant, I think.
34:41
But the fact of the matter is that the standard library is bound to change in the long term, okay? But in the near term, it will stay pretty much as it is and knowing it and using it is a very valuable skill. Don't rush into it. It takes time, but it pays back.
35:00
So I learn a lot. Every time I go into the docs, go docs, python.org, then library reference. That's it. You go there, there's a bunch of cool things. And then, of course, there's this, you know, I'll read my own slides. Don't do this. Direct in your face, Python is effective and fun, and it is.
35:21
And maybe the reason why so many of us are here today, because one day we said, oh, this is Python. Let me try this. Mm, funny. Oh, cool. Oh, I like this. I want more. And then 15 years later, here you are. I don't know if you relate to this, but this is what happened to me. And having fun is obviously the best way to learn new things. And the kids did learn a few things.
35:43
Well, the workshop was back in March, and certainly three months after the fact, or four, whatever, they probably don't know much Python now. But there's one thing they learned. They will never forget. And that is that their gaming consoles, their tablets, their electronic devices,
36:02
they're all programmable. They have seen the light in a way. They know there's a way to make the machine do something they want. So, maybe in 5, 10, 20, 40 years time, they're faced with some kind of software that just doesn't feel right for some reason.
36:22
They know, I know how to fix it. I'll go pick up back on that path. And this is a very powerful thing to know, that you can't really unlearn. And this was my main objective with the kids. These machines, you are the master, so you can make them do whatever you want. So, if you want, go do that.
36:45
Now, what if you're a professional seasoned developer? Four years experience, you've done it all. You've never done it all. That's my opinion. There's always something else to do. Now, would you do this at work? Not something like this. Exactly this.
37:02
This silly game. Would you do it? Well, could you learn something from it? Say, extending it to network-based gameplay. Would you learn socket programming, IP addressing, IPv6, by the way? DNS, I don't know, Bluetooth?
37:21
Would you learn that there's no way, no technology, no protocol, no whatever, that guarantees that when you send a message out through the network, there's no guarantee that you'll ever get back a response. So you need to use timers and timeouts and things. Would you learn something from it? Would that compel you to write a low-level event loop,
37:44
managing network traffic, keyboard input, internal animation, and sound, by the way, why not? And would that help you understand different models of concurrent programming with coroutines and learning about the async and await keywords that are so mysterious to many?
38:01
Would that be helpful in something else that you do? Would you learn about race conditions? This game here has race conditions. You might say, oh, I don't care about race conditions because I do data analysis. And you know, you don't need network code to have race conditions. You don't need to have event-driven code to have race conditions. All you need to have is concurrency,
38:20
and concurrency is everywhere, more and more. But tuning your mind into grasping what the race condition is and kind of smelling it from the distance and knowing how to avoid and improve your code to avoid those because they are very difficult to diagnose and fix. But if your mind is kind of tuned into that,
38:40
you've learned a very valuable skill, okay? And so on and so forth, okay? So I clearly say, yes, you should do this at work. Go ahead, tell your boss, yeah, what are you doing? I'm doing a silly game. Are you crazy? No, I'm not crazy. I'm learning the best way possible because I'm having a bunch of fun. But don't just do this at work, okay?
39:03
You balance it out, okay? Now, the parting thought is this. It came to me as I evolved and parted away from the workshop. As that learning and having fun,
39:20
hopefully learning while having fun, okay, is probably the best way to grow into better versions of ourselves. And I think in the end that's what it's all about. I mean, I've had a bunch of fun since January. Thinking, discussion, discussing, working long nights.
39:41
It was a lot of work, but a bunch of fun for, why did I do it? For love, I don't know, for passion, for friendship, for kids. But I learned a lot too, okay? I'm still figuring out all that I learned from this experience. And I humbly hope that all of this, what shall I call it?
40:03
This is not to talk, this is some silliness, some fun. That it might motivate you and inspire you to think. Maybe to put away some time, some energy or karma, if you will, and dedicate it to learning and having fun.
40:23
Whichever is silly, however silly, whatever, who are they to say, oh, this is silly? You do whatever you like. So, to dedicate some time and energy to having fun and learning. And if you need to choose, if you can't have both, okay, go for fun, okay?
40:42
If you can't have learning and fun, go for fun. So, this is it. Thank you very much. I hope you go out and have a lot of fun. Thank you.
41:09
Thank you, Thiago, for this very entertaining talk. We have a few minutes for questions. If you want, there's... Yes, I'm happy to address anything about the code, the kids, the method, and catch me out there.
41:22
I'll be happy to discuss all of this at any time, okay? Go ahead. Thank you. I really love the message that you have about providing kids the ability to think, yeah, I can tweak this in the future. I really don't know how it works, but I know that it can be fixed. So, my question is, how would you address it to adults
41:41
if you had to do something like this, like 20-somethings or 30-somethings? That's a great question. I haven't thought about that much, but this is a thought that crossed my mind a lot of times. Working with kids is a serious business, and you heard me say that, because kids are very wide open,
42:02
so you can pretty much kind of drive them the way you want, okay? So, that's a huge responsibility, okay? So, they're more malleable, so to speak, okay? That's why I meant that kids, working with kids is very serious, okay? Because you can't, like, mold them some way. I don't know how we do it with adults,
42:22
because as we grow up, we tend to become stiffer. But in the end, it's about showing and not telling. If you show people something that resonates with them, they will resonate with that and go from there. So, from an educational standpoint, I would say that it's about finding what resonates with your target audience.
42:44
And certainly, what resonates with kids is a game like this, and if I went to this with an adult, this would probably be interesting, it wouldn't resonate. I'd say that that's the key idea. But I'm happy to discuss this more.
43:01
Okay, it works. Thank you very much, it was great, it was very important, I think. But I have a neat pick. Go ahead, go ahead. This pattern where you use a single underscore for a variable that you don't use. Have you ever had to use GetText?
43:22
GetText, yes. Sure. Yeah, GetText is a module in the standard library that helps you internationalize or translate your code to different languages. And it does a hack. It uses this underscore variable that becomes a function.
43:40
So you convert all your strings that you say to the user, say hello, user, and then you wrap it up with underscore and then parentheses. And then in another file, you say, well, hello, user is, hola, uclizador, hola, suario, blah, blah, blah, and all the languages. And then it does that for you. So, thank you, Radomir, that's a great point.
44:03
As like a counterpoint, okay, this code that we've seen is much more advanced than what we did with the kids. We didn't do any kind of lambdas. We didn't do loops. They went like draw a line, draw a line, draw a line, draw a line, draw a line, draw a line. So we went for the basics. Thank you, that's a great nitpick.
44:21
And I'd like to know how to solve that. I don't know. I would like to. That's why I'm asking. I have no idea. I never thought about it. I never thought about it. Maybe you go with a double underscore variable. I don't know. Good idea. Thanks, Radomir. So, it's gone red. Yeah, I'm sorry.
44:41
Okay. Let's talk out there, okay? Yeah, you can. A very short question. How long did it take for the kids to reach this level of. Okay, we did eight hours, okay, two hours, two hours, two hours, two hours across two days. So two hours in the morning, and we went slowly, okay, from.
45:01
I'm happy to share you the materials. I have all the materials, all the step-by-step lessons, okay? But when they got here, they were comfortable enough to vary some, change some conditions, play around, okay? But this was like a bit too sophisticated for what they were comfortable with. So it's kind of a balance between having fun, being proud, look what I did, but I don't completely understand it, okay?
45:25
Okay. Thank you. Thank you. Bye-bye.