Code Bullet: AI Learns to play Flappy Bird
In this AI video ...
Hey guys, welcome to today’s video. Today we’re going to do some flappy bird action, and I know I am hardly the first person to do this on YouTube, and that’s because it’s kind of pretty easy. So today, we’re going to be doing this as a one day challenge. I’ve got a day to make a fully functioning version of flappy bird and add the AI to learn how to play it. So the day before, I knew this was going to be a big day, and thus I set my alarm to wake me up right nearly 6 a.m. The next morning, I jumped out of bed, ready to begin my day, and only pressed the snooze button 12 times. So, yeah, it’s like 8 a.m. now. That could have gone better probably, but I’m ready now. I just need to check YouTube for a while. It is my job after all, and oh my god, it’s 10 o’clock. Okay, okay, deep breaths. Okay, let’s just get started. Okay, so let’s just start off playing some flappy bird for a while so you get a feel of what we’re doing, and as you can see, I’m really not fantastic at this game, but that is what my computer’s for, so let’s start coding. So to begin with, let’s just get a circle on the screen which is affected by gravity. It’s beautiful. Okay, so yeah, that’s great and all, but we need that bird to fly. So when I hit space far, we need to apply a force pushing the bird upwards. Let’s talk some adjustments, get the flap just right, but I’m happy enough with the movement for now. Okay, let’s start on the pipes. The pipes are just gonna be rectangles with move from the right side of the screen to the left. Not nice. You’d think by now that I’d be capable of drawing rectangles to a screen, but nope. You’d be wrong, you’d be wrong. Okay, there we go. Now we just need to add some collision detection. You’d default. Instead of dying, I just had to burr a teleport to the top of the screen when hit because I haven’t implemented the dying mechanism yet. So only having one pipe makes this game ridiculously easy. So we need to add a pipe on top to make pipe pairs. It’s required tweaking the collision system a bit, but it’s really not too bad. Here we go, very nice. Yeah, we’re getting close now although the game is still a little too easy, so I made a bunch of small changes to make it more difficult, including but not limited to. Once again, twerking the jumping mechanics, changing the thickness of the pipes, I made the player actually die when hitting the pipes and decreased the distance between pipe pairs. And here she, oh, and I added the ground. And here she is. Okay, so the game is functional now and I could just add the AI to this, but I wanna make it look actually like Flappy Bird. And we got time, right? What’s the time? Ah, shit, it’s 1pm. Oh, well, yeah, no, no, no, no, no, no. Yeah, we got time. We got time. So it is time to dust off my pixel art skills and let’s get drawing. First up, let’s draw the bird. Okay, now we’re gonna need to draw the pipes. I’m actually really happy with how the pipes look, but that’s mainly because I pretty much just copied the pipes in the game pixel for pixel. I told you, I had skills. Oh no, Evan, the pipe is too short. You ready for this? Boom, pipe for days, boy. Okay, now we need a background. And unfortunately, my screen capture software apparently went on lunch break during this part because it forgot to record most of it. But you get it, it’s sky clouds building grass. Yeah, so anyway, let’s shang the pics in. We got background, we got pipes, we got birdie. So yeah, I’m pretty happy with how everything looks. However, the bird looks really stiff and kind of weird. So let’s make the bird tell up when it’s flapping until down while it’s falling. Okay, beautiful. Now the game is done and what is the time? Ah, it’s 5pm. Okay, that’s okay, I didn’t think I was that slow at drawing but that’s okay. I did have a nap in the middle of drawings that probably didn’t help. But yeah, that’s okay. We’re just gonna need to add the AI quickly. Luckily, I’ve done this before. So I’ve coded a pretty good library which you can just shang in and change a few details. It’s not quite that easy but close enough. For the AI, I’m using an algorithm called NIT, which teaches newer networks to play the game through the process, similar to evolution in nature. Talk up the newer networks to the game. We need to specify what the players can see, what the players can do, and how to tell if a player’s doing well. So the players can see the distance to the next pipe, the bird’s vertical velocity, the vertical distance to the top pipe, and the vertical distance to the bottom pipe. The actions players can take is actually really simple. It can either jump or not jump. And for the fitness function, which tells us how well each player did so we can choose the best players to pass their genes onto the next generation, we can simply use how long each bird survives. So for bird survives longer, and it will do better. Simple stuff. With that done, it’s 9pm and I’m going to bed because I’ve got to get my 12 hours of beauty slate. So enjoy. Yeah, I was going to do a Christmas special episode, but I didn’t have the time. So here we go. Merry Christmas. Hey guys, guess what? It’s Christmas. I can’t be asking anybody to be, because nothing’s gifts are a thing, which I kind of hate because I had so much stress. Like, do I get my grandma in seventy seven hundred or at eighty seven hundred Kay,Cori Seven Process어? I mean, I can’t just ask her straight up if she was to overclock her rig, because that would spoil the surprise. It’s just a mess. So if you can’t decide what you’re getting someone for Christmas, then get them the greatest gift of all, knowledge. Spearn the love of math, science, and computer science. Do you love ones by gifting them brilliant? This really excites me, because it’s such a fun way to nurture curiosity, build confidence, in life. School, job interviews and their career. It’s really just a top tier gift. Bruins thought provoking content breaks up really complicated issues into bite-size, understandable chunks, which will lead your loved ones from curiosity to mastery. So go to brun.org slash code bullet and grab a gift subscription to help your loved ones finish their day a little smarter. Thanks for watching and yeah Merry Christmas. Thanks for great year, honestly, it’s been awesome. We just hit 500,000 subscribers, which is insane. I should probably do like a 500 subscribe a special 500,000 subscribe a special. Sorry, but I really can’t be bothered and I don’t know what I do. Maybe Q&A or something. But yeah, anyway, so thanks heaps. I’m actually going on break for like a week or two because Christmas and family and stuff. But I will also be spending some of that time learning about some better techniques to train AI. It’s like deep Q learning and PPO, which is what open AI used to beat Dota 2, which is some pretty next level stuff. So if anyone knows any good resources for learning those, hit me out on Twitter. But anyway, have a good Christmas. Evan out.