Python Game Development | Tony Staunton | Skillshare
Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
55 Lessons (4h 34m)
    • 1. Introduction

      0:58
    • 2. How to install homebrew and pygame

      3:43
    • 3. Basic game code structure

      5:46
    • 4. How to change pygame background color

      2:14
    • 5. Creating a settings class for our game

      5:09
    • 6. Creating the ship class

      6:33
    • 7. Drawing the ship to screen

      2:58
    • 8. Refactoring part 1

      3:50
    • 9. Refactoring part 2

      3:03
    • 10. Moving our ship to the right

      4:10
    • 11. Constant movement

      5:44
    • 12. Moving to the left

      2:39
    • 13. Making our ship go faster

      4:30
    • 14. Stopping our ship at screens edge

      3:13
    • 15. Bullet settings

      1:36
    • 16. Bullet class

      9:31
    • 17. Bullets group

      2:45
    • 18. Refactoring check events

      6:01
    • 19. Firing bullets

      5:58
    • 20. Deleting bullets

      4:54
    • 21. Update bullets function

      3:32
    • 22. Creating the alien class

      6:05
    • 23. Creating an instance of the alien

      1:47
    • 24. Making our alien appear on screen

      1:35
    • 25. Creating row of aliens

      6:19
    • 26. Creating the fleet

      7:38
    • 27. Refactoring create alien

      5:55
    • 28. Adding rows of aliens

      8:53
    • 29. Moving aliens right

      3:26
    • 30. Has an alien hit screens edge

      4:49
    • 31. Changing fleet direction

      6:24
    • 32. Shooting down aliens

      4:10
    • 33. Repopulatng the alien fleet

      3:32
    • 34. Changing alien and bullet speed

      0:36
    • 35. Detecting alien ship collisions

      4:00
    • 36. Responding to alien ship collisions

      12:53
    • 37. Reacting when an alien reaches the bottom of the screen

      3:58
    • 38. Game over

      2:26
    • 39. When different parts of the game should run

      1:27
    • 40. Create the button class

      10:53
    • 41. Creating a play button

      4:38
    • 42. Making the play button work

      5:00
    • 43. Resetting our game

      6:05
    • 44. Deactivating the play button

      1:28
    • 45. Hiding the mouse

      2:01
    • 46. Refactoring bullets

      4:11
    • 47. Leveling up

      6:23
    • 48. Resetting the speed

      1:26
    • 49. Creating a scoreboard

      12:25
    • 50. Adding points to aliens

      4:50
    • 51. Increasing the points score

      5:43
    • 52. Rounding the score

      2:10
    • 53. Adding a high score

      8:49
    • 54. Displaying our level

      10:29
    • 55. Adding lives

      12:51
15 students are watching this class

About This Class

Are you ready to save the earth from invaders from the planet python? If so, then you can take your Python skills from basic to advanced by creating a fully featured and professional Space Invaders clone called Invaders from Python.

This course is the follow-up to my hugely successful Python 3: A Beginners Quick Start Guide to Python and will take you the student from knowing the basics of the Python programming language and walk you through creating your very own Python game.

Transcripts

1. Introduction: heightened newbies. It's time to step up and take your skills to the next level. You already know how great pipe is. That's why you're here. But how great can you be? Take this course and find out. You've already learned the boring stuff, the strings, integers. Dictionaries now learn to fund stuff and create a game. This course has over 50 lectures on his over four hours long. By the time you're finished, you'll be well on your way to saving the galaxy from another pouring wire loop on learning how the blast aliens to smithereens. After you've completed this course, you will have a solid understanding of piping game development and be ready to create many more programs on gains so lamp iten and Save the World. 2. How to install homebrew and pygame: Hi, everybody and welcome. Now, in this lecture, we're gonna be looking at installing pie game on a Mac. So how do you know if you already have pie game installed or if you don't have pie game installed on your Mac, which can work with your piping treat programs? Let's open up our terminal. So here we go. On inner terminal, we enter fightin tree. So this course is all about using pi country. So we're gonna type in piping tree Now you get the tree Chevron icon here, so you know, you're in the pipe and show on tight. Been in port. Play a game now, as you can see, I gotta clean return statement. They're ready for my next command. If when you type in import pie game, you get an error on the next line. A trace back error with and you do not have pie game installed on this lecture will show you have to install it so you can get a successful import pie game statement going. So the first thing you need to do is pop open your browser. Here we are here and go to www dot through don't s age. There we go now. Installed Homebrew. So again, this lectures for the Mac. So you take this entire line here, copied this entire line here, command and see Pop back in your terminal exit out of your piping shell and then paste in that full line. There you go. There. Now, I already have pie game installed, but I run it here anyway, so you can see this. You get the script when installed. The following libraries, the following dependency. So you press enter to get it going. Enter and doing your password on the download and installation will begin. Now, I'm gonna pass this video on when the installation is complete. Will return. See you in a couple of minutes. Okay, so Homebrew has installed and we're ready now to begin installing the libraries needed to run various games and entering pie game within Peyton. So the next command we need to enter is brute Install. Hey, H g STL on the score. Image space STL! Underscore TTF press enter and up on this. Commander installed the library. Needed to run our game. Okay, so I've already installed them prior to this lecture sport. You'll get a big long list of installations. Now, if you want to enable sound, then you need to additional libraries. And to do that, type in brute installed. STL mixer, Port Mitty. And again, the insulation would take a couple of minutes. Okay, Welcome back. Now, finally, we want to install pie game with Pip, so you type in pip tree in stone User. Hey, H G plus http. Colon forwards Last bit bucket dot org forward slash pi game forward slash pi game and again hit. Enter Now we'll head off now. Did a bit book a website and install pie game. And again I already have pie game installed. But for you this will probably take a couple of minutes. Now, still in terminal again, we go into a piping shell piping tree shell on import pie game and you should see now and nice clean return statement, which means that you have pie game installed. Then that's it. You're done. You're ready for the next stage of this project, which is beginning error game. Thanks for listening. And I'll see you in the next lecture 3. Basic game code structure: Hi, everybody, and welcome back. Now in this lecture, we're gonna look at the basic structure of a game written for pie game. So let's kick off and get started. So our first line we import, that's why, yes. Next import pie game. So if you haven't installed play game, go back to the previous lecture on installing Homebrew and Pie Game on. Get it installed there. Don't worry too much about following about understanding what's going on right at this moment because we're going to step back. True this code and true this Elektra. Now in a few moments next we create our function run game. When we got into comment initialized game and create a screen object. What is the screen object? But we'll explain that now in a few minutes. So pie game that i n i t so initializing pie game now screen equals pi game. Dark display that set mode 1200 by 800. That's a screen size now pie game start display dot said caption invaders from fightin. So that's the name of our game. And that's gonna be the caption at the top of our screen invaders. Frump Iten. Now what we want to do is we want to. Yeah, I forgot to close off a bracket there, so just go back and do that now. Next, we want to start the main loop for the game, so we created flags a while. True, not a comment. Watch for Keyboard. Most events because there's gonna be a keyboard and mouse control game, so keep an eye for them here. So four event in pie game dot event not get if events. So, if the event that were tight keeping an eye out for If the event is off the type pie game don't quit, then that's why s that exit. Okay, so somebody presses the X on the top corner of our display screen exit the game next. I didn't comment. Make the most recently drawn screen visible pie game dot display flip. And then finally, we have a function call run game brackets. OK, so now let's step through this code. So it lines one entry we import es y yes and pie game modules. We'll use assist module. Yes. Well, yes. Module to exit game. When a player quits the pike, A module contains the functionality needed to make the game. The actual game starts at the Function Room. Game online. Five here that line seven to part the line pie game that i n I t. Initialized the background settings for a game. So at Line seven here we initialize the background statics at Line eight we called Pie Game . That display got set mode here, Line eight to create a display window called Screen. The argument 1200 comma 800 is a to pull, which defines the dimensions of the game window. You can adjust these values depending on the size of your screen, the screen object we've just created. It's called a surface in Pie Game. A surface is part of the screen where we display a game element. Later, when we create aliens, there will be a surface or a ship that would be a surface at Line 12. We create a while loop, which contains the game line tartine we created while Loop, which contains the game inside the While loop. We have a for Loop, which watches for events. On event is an action that the user performs when playing the game, such as moving the mouse or pressing a key. This four Loop is also called on event Loop online. 16 were used. Pie game Done event dot Get to access events detected by pie. Game on the keyboard and mouse event will cause the four loop to run. Currently in this loop if a player clicks the game window Close button the pie came. That quick event is attacking on. We call s y s not exit to exit the game at Line 21. We were called Pie Game That display that flip was told pie game to make the most recently drawn screen visible. When we start to have moving elements within a game, this call will constantly update or display to show the new positions of elements and hide the old ones. Our last line of code called run game initialized the game and starts the main loop. Now what happens if we run this code? Well, drumroll. Let's see. There we go. We get our game screen. So there it is there. It's nothing at the moment, but a black screen with clothes and shrink buttons. So let's close out there. And that's our quit event. Thanks for listening. And I'll see you in the next lecture. 4. How to change pygame background color: Hi, everybody. And welcome back now, the last lecture when we ran our code as we have to code here in front of us. When we ran this, we got our game screen, which was a nice, dull, boring black. So let's change the background color of our screen from black toe a nice, bright blue kind of Skyy color. So I still have the same code open from the last lecture. And just after a caption invaded from pie towns here online. Nine, I'm gonna add in another couple lines. So we started off with a comment set the background color, and I spell it the American way for American friends. So And what we added now is BG underscore color equals brackets. I'm gonna just paste in here now, color that. I looked up earlier on 135206 to 50 and then just spaced out at for clarity. So in pie game, colors are formed from the red, green, blue or G B index. So any call you want Just look up that on the internet on, find the color on You can change in the color here Now. Next, what we need to do is we need to fill the screen with the background color using screen filled, which needs only one argument. Background color. So we scroll down here beneath RSVPs exit. I'm just out in a couple of spaces. Add in a new comment. Three. Draw the screen during each pass. True, the loop. When we got it now screen dot fill. I just had only one argument. BG color. There we go Now Let's just quickly recap on what we've done at Line 12. Here we create a background color and stored in BG color. This color needs to be specified only once, so we created before entering the main wire loop. Then here at Line 23 we filled the screen with the background color using screen Dot Felt, which needs only one argument. BG color. So that's one are cold now and see what our new color it looks like. There we go. A nice sky blue, an awful lot better looking than the black from the previous lecture. Thanks for listening, and I'll see you in the next lecture 5. Creating a settings class for our game: hi, everybody, and welcome back now, most likely in our game. Every time we add new functionality, we're going to need to add new settings as well. So instead of writing standing straighter code, let's write a module called Settings that contains a class called settings to store all the settings of our game in one place. So as you can see here, I've create a new file called Setting Stop eY and that is stored in the main file with my main piping game code. Now now, when we modify, our game will simply change in values and settings that p y instead of setting true all of our files. So let's start to fill settings up. Ey, now that's credit class cold settings. Given a description, class to store. Are games settings simple enough? Next, the F on their innit function self. And what is this going to do? Well, it's going to initialize our game settings on screen settings. Perfect. Closed off or comment. Now what do we put in here? Okay, let's go. Self thought screen with equals. 1200 south dot screen height equals 600. Now I'm just changing that from 800 to 600 which was in our first lecture because my screens Liberte smaller here. I'm on my Mac laptop, but you can adjust the settings for whatever size your own screen is at home and then self dot BG underscore color equals 135 comma, 206 comma 2 50 There we go. So that's our settings class know what we want to do is we want to go back into remain file , which is invaders from Pike Tree. And I'm naming these want to trade because these air cold files that I'm gonna provide with each lecture so you can just call this file invaded frump iten. But I have to name one to treat the corresponded or lectures. So back in here now what we need to do is at line five here we import settings into remain file. So from settings So from the file called settings import settings, so import the class settings. There we go. Next. What we need to do is we need to create an instance of settings and stores. So here on their pie game that i n i t. I'm going to create an instance of her settings ongoing a shorthand the name of this game. So invaders from piping so I n f o m p y short for invaded from fightin. So invaded frump iten underscore settings equals settings. There we go. No, while we can do here now is we can start to remove and replace some code. So let's just have a look. We start off here with our screen width and height, which are no longer needed in this form. What we can put in here is invaders. Frump iten underscore settings that screen with come up invaded from piping settings. Thought screen height. Perfect. Next, what we want to do is we want to remove this piece of code here. We just set the background color because we're going to set that now in our setting so we can take that out completely. That's just removed a couple of lines that we had it in for that. There we go. And down here where we have a screen, Phil, we just change the background color here to invaders from piping settings that BG color. So we're importing arguments here from our settings class. Okay, there we go. Now let's run this and see what happens on fingers crossed. There we go. Perfect. What we've done now is our program still runs prayerfully. It gives us that blue screen, not the blue screen of death. Thank God for any Windows that users out there. I don't know if that's still a thing or not, but what we've done is now. We created a class called Settings, which is here. We then imported that class into remain program file here from settings import. Then, using our settings, we were able to remove some code and adding some code. So over time you're going to see how that's going to make our code a lot simpler and not a lot easier to write new code to on a lot easier to debug. Thanks for listening, and I'll see you in the next lecture. 6. Creating the ship class: Hi, everybody. And welcome back. Now, we're making very good progress towards our invaders from pipe game on towards having a completed and fully functioning video game where we're going to move on to next is we're going to start adding a ship to our screen. So the first thing we need to do is at the image and use a pie. Came method be Let s p L i t to draw the image. So what I need you to do now is I need you to create a new folder called Images in your main project folder on Save the Ship Docked BMP image there on the ship image is in the resource is section off this lecture. So now we have our ship image. We need to display it. Let's create a new module called ship, which will contain a ship class. So, as you can see here, I have a new file here called Shipped Up Ey. And we're going to right now our ship class, so let's get going. Ship brackets, cola d e f i n i t next and in there and i n i t two parameters self on screen now. Don't worry we're going to step to It is code line by line in a few minutes. Colon on our Collins. Our description is initialized our ship on set. It's starting position. So you know exactly what we're going to be doing with this class. Now we have self dot screen, equal screen. Perfect. Now another Commons. Load the ship image. There we go. So self image equals pi game that image that load now. Where to restore the image where we saved it in images. Forwards Last ship Shimp ship got B M P. There we go. That's where we saved our image at the start of this lecture. Next itself does or D C t wrecked for rectangle self. That image, I thought, I guess wrecked self dot Screen for E. C. T equals screen, not death or you see t brackets, No next piece of code. We start with a comment on what we want to do with this next piece of code is we want to start each new ship at the bottom center of the screen Every time a ship dies or we restart the game. Whatever might be the ship restarts in the middle bottom of our screen the same position each time, so self dot or D c t dot c anti or X So center X equals self thought screen or e c t dot center x Next line is the bottom. So the line we just wrote we've sent it our ship in the middle of the screen. Now we want to center the ship at the bottom of the screen. Self dot wreck Stop bottom. You call itself thought. Screen score E c t that bottom. There we go. No, What's next? Well, as we said at the beginning, after lecture, we're gonna use what's called B l i t tope items b l I t function so b l I t m e gonna give it one parameter self. Then where we gonna step through all this cold now in a couple of minutes? Description. Throw the ship at its current location. There we go. One line of code for this self dot Screen that b l I t south dot image comma south dot for e c t. There we go. Okay, so let's step through this code. So here at line tree Thean admitted off ship takes two prominent self on screen to load a ship image we call the pie Game down here at Line eight Pie Game that image dot load this function returns of surface representing a ship, which we saw, which we store in south dot image After the images loaded, we use get underscore or E C T. To access the surface wrecked attribute. Pie game lets you treat elements like rectangles or E. C. T. Rex, even if they're not exactly shaped like rectangles. And this makes our lives a lot easier, because rectangles are simple shapes toe work with. When using direct object, we can use the X and Y coordinates off the top bottom left, right and on right edges off the rectangle as well as the center. We can set any of thes values to determine the current position off direct reposition airship at the bottom center over screen. To do this, we forced or two screens wrecked in screen in self dot screen Underscore wrecked at Line 10 Here. We then make the value off self dot wreck dot center X, which is the X cordant off the ship center, much the center X attributes of the screens wrecked at Line 13 here. Next at Line 14 we make the value ourself dot wreck Doc Bottom. The white cordoned off the ships bottom, equal to the value of the screens. Wrecked bottom attributes. Pie Game will use these attributes to position the ship so it's centered horizontally and aligned with the bottom of the screen. Now, with no code to run at the moment because of this code won't produce anything just yet. But in the next lecture, we're going to look at drawing the ship to screen, which is a couple lines of code in our main invaded from Biden program. Thanks for listening, and I'll see you in the next lecture. 7. Drawing the ship to screen: Hi, everybody. And welcome back now in this lecture were back in their original invaded frump iten code. So you can see I have it open here and hasn't changed into last time. We used it on where we're going to do now. This lecture is we're going to import on used the ship class that we wrote in the last lecture to put our ship on screen. So let's start off here at the end of import settings. Where you going to say from ship import ship? So that makes sense. Ship. There we go. Next. What we want to do is after our invaded from piping title, we want to just add in a couple of spaces had in a comment and say, Create a ship. There we go. Ship equals ship brackets screen. Okay, Perfect. Now we scroll down a different just a where we treat redraw the screen during each passed through the loop here and what we want to say next is you want to say ship, Doc B L I t m e brackets. OK, there we go. Now, that's just explain what we did. First outlined six. We import the ship class from the fire ship. So from the module ship down here, then of line 16 we then make an instance of ship. We create our ship before the main loop so that we don't make a new instance off the ship on each passed through the loop, So would line 28 down here we call shipped out B l t B l I t m e After failing the background, this is so that the ship appears on top off the background and not beneath it. Okay, let's cross our fingers. Run this code and fever ship will appear on screen. No, we got an error. So let's have a look at what that error is for. This error is because in my ship class here, I very foolishly at the top left out import pie game. And I left that out from the last lecture. So jump back into fire now added in Save it. Let's go back in here Now on drone this file. But again, we get another error. No tragedy. Tragedy. Her ship is sinking rapidly. So what is the error here? Ship object has no attributes. B l I t and meets. Let's just jump back. Interest your into our ship file. As we can see here, our indentation is off by one. So that's just correct us. There we go. Save it now. Can we save the sinking ship? Let's have a look. Hey, Excellent. Excellent. So after those two corrections or couple of corrections honor code, we can see her now are beautiful blue back background screen on our ship image. Now, in the next lecture, gonna look at cleaning up and re factoring some of her code, and then we'll get into some exciting stuff I'm piloting or moving the ship. Thanks for listening. And I'll see you in the next lecture. 8. Refactoring part 1: Hi, everybody. And welcome back. Now, in this lecture on the one to follow, we're gonna talk about re factoring now. Don't worry. Because sometimes when I hear the word re factoring related to my code, I often think of homework. But we're gonna make these lectures nice and short and snappy. So hopefully, if you're taking this course, you know what re factoring is It helps you simplify your code. So we're going to create a new module called Game Functions With Was store a number of functions that makes invaded frump iten work. So let's start by moving the code that manages events to a separate function called check events. By doing this, we will simplify our run game on the Isolate the event management loop. So first we will create a new file called Game Functions. Duck P White, which I have up here, gain functions, not p. White. So it's empty at the moment on. What we'll do is we'll start off by importing like we did before. Import s Why, yes. Next we'll import play a game. Okay, Now, that's followed by our actual function so d e f check events under no prominence to pass to to check events function yet. Okay, so watch a description here. We want to respond. Noah, respond to key presses. I'm most events. Okay, finish off a description on what we want. Well, for event. So this is very similar to this code back here. In fact, it's identical four event and pie game. We're moving this piece of code here into a game functions code, but we're gonna type it all out because it's good for a muscle memory for event in pie game dot event don't get and what we want to do. Well, if we get an event, if an event, the type is play game that quit, then pretty simple, quit or exit. Okay, so it's all we need to do their that saved that. Never lets go back into our invaded from fightin program here. I will modify it so it imports the pie game function modules. And to do that, we start off here. Just, you know, just below the line from ship import ship on, we say import game functions as the alias GF. Okay. So hope You know what in aliases. If not, go back and check out my area of course, piping tree at beginners. Quick start guide. But if you're taking this course now, which is for intermediate pipe News is or people who just want to jump right in, then you should know that GF is an alias Short hand for game functions. So if that piece done there on what we want to do here now is we want to remove this loop this event loop here, which watches for a keyboard and mouse events because we put it in our game functions. So we just wanted to leak that. And what we want to say here is GF So game functions dust and what was the name to function ? We gave it. Check events, check events. Here we go. So Polian check events, okay? And that's a nice bit of re factoring there on our invaded from pipe Main program by creating our game function. So let's run this code and see what we get to make sure everything is still working. There we go blue screen with our ship down the bottom middle, so that's perfect. So that's re factoring by putting check events into our Game functions program in the next lecture will look at re factoring the update screen function. Thanks for listening. And I'll see you in the next lecture 9. Refactoring part 2: Hi, everybody and welcome back. Continue on with a re factoring. We're going to look at re factoring the update screen function. That's all of this code down here so you can see it there. So let's move the code for updating the screen to a separate function called updates Green in our game function stop ey program, which we created in the last lecture here. This will help before to simplify, ruin, gain this function here with all the code in it. So the first thing we need to do is in game functions. Let's create a new function on that's called DF update under sore screen. And what promise does not need what it has the invaders from from P y. So if you remember, we short handed this so invaded from P y on the score setting. So that was all the settings screen on a ship. Okay, perfect as always, Lets out in their description, updates the images on the screen on flip to the new screen. Good stuff. Next, we have a comment. Actually, don't put in that coming because I want a copy and paste it from our invaders from piping program. So here we are here on. What we want to do is we want to take this code into a game. Underscore functions up. Ey code. So we copy that. Sorry, we cut that hair. It's gone on. We pace it in here. Okay, let's saved us. That's jump back in here on what we say here is now We've already imported game functions from the last lecture Import game functions as GF here and we gave an alias. So here we say GF Just watch your indentation when you're copying and pasting GF update screen In what promises did we give it while we said invaders from piping settings screen ship So nothing too much to remember there. Then we close it off with run game Okay, that saved up and see how it runs. Ah, and ever why an indent error by looks of it. So let's see where that Indian air comes in to money in dense here. And like I said, when you're copying and pasting, you have to really careful on your indentation. Okay, there we go. Let's say that again. Go back into our main program and run perfect. So now, instead of working inside the run game function, Which is this here? We're working in that a lot. We can do most our work in the module game functions which we created over the last two electors. When we when we started out with this section, we started invaders from piping in a single file. We didn't introduce the game functions module right away. This approach gives you an idea of a realistic development process. You started writing your code as simply as possible and re factory as your project becomes more complex on your confident of its working parts. See you in the next section where we're going to learn how the pilot, your spaceship. Thanks for listening, and I'll see you soon. 10. Moving our ship to the right: Hi, everybody. And welcome back in this lecture, we're going to give her ship the ability to move left and right. So, as you can see here in my lecture screen, I've got our last lecture open. So invaders from piping code on my game functions program still open. So if you don't have those two programs open, go ahead and open them now. So, as I just said in this lecture, we're going to give our ship the ability to move left and right on to do this. We need to write code that responds when we pressed the right or left arrow keys on our keyboard whenever play a press The key that key presses registered in pie game as an event . Each event is captured by the pie game dot event, Doc, get method. And we've seen that before when we were using it to capture, to quit event. This means that inner check events function. We need to specify what kind of events to check for. So where is your check events function? Well, that's here in our game functions program. So check events from there it is there each each key press is registered as a key down event when a key down event is detected. We need to check whether the key that was pressed is warned. That triggers a certain event. For example, if the right arrow key is pressed, we increased the ships or e c t dot center X value to move the ship, right? So let's have a look at how we would do that now. So in our game functions that P y program our module, I should say We want to check for a right arrow key down press on. How exactly do we do that? Well, let's add in some code into our check event function. So we have here the first piece of code here captures to quit event. So let's add in a new one. So l if so else if event that type is a pie game that key down. So it's a key down event. We have a place about that right key down. And then, if the event that key is the right key, so pie game dot k for key, underscore right all and cups. Same is like we had quit here. It's a pie game. So then what we want to do? Just trolling a comment here. Moved a ship to the right. Okay. Ship dot Poor e c t dot center. So rectangle center, X plus one. So, in comment by one. Okay, I think that code makes sense. So here in the forest, part of our check of and function, we check for the quit event. So is a user exiting the program in the piece of cold we just written now else, if we detect a key down event and that key down event is the right hand key, then we move our ship one to the right. There we go. Simple enough. Then we need to modify our main program invited from fightin. So let me just say invaders from piping. So let me just say this a move in here on what we need to do is we need to give our check. Events need to pass the argument ship. Who's want to check now if the venue events related to the ship? Now let's ruin a code and see if we can move our ship to the right and error. Let's have a look, but one was given. Okay, let's see here now. What This is Taipower. Check events take zero position argument, but one was given. Okay, so check events. So where is our check event is in our game function. So let's have a look here. The problem solved. We have our in here. We have our check events ship. So we have the argument here. But what I neglected to do in my game function was check events on specified a parameter ship. So there we go. Let's have a look now. Saved us. Let's run our main program. And there we go. So the moment we've been waiting for let's have a look on. There it is moving. So if I repeatedly pressed and depressed the right arrow key, my ship moves to the right. Okay. Perfect. So far, so good. In the next lecture will look a continuous movement on moving the ship to the left. Thanks for listening. And I'll see you in the next lecture. 11. Constant movement: Hi, everybody. And welcome back. Now in this lecture we're going to look at continues movement. So as we saw on the left in the last lecture, when we pressed and depressed the right arrow key, our ship moved to the right. But what about if we just have a look? Held her finger down on the key? Wouldn't it be great if our ship just kept moving? Well, dream no more, because in this lecture, we're going to do it. We'll give our game to build to detect a pie game dot key up event so we'll know when the Arrow Key is released. Next will use a key down and keep events together with a flag called moving underscore. Right to implement continuous motion. When the ship is not moving, the moving underscore right flag will be false. When the right arrow key is pressed, will set the flag. The true on when it's released will set of the faults again. The ship class controls all attributes of the ship, so we're given an attribute called moving underscore, right on an update method to check the status of the moving right flag. So let's jump into our ship dot p y program. Okay, here we are in our shipped out P y program, which we created a Iran in the lesson and the course I should say so what we want to do here is if you go down the line 16 on my window and at the end of this line of code here, just enter in a couple of spaces. There we go on. What we want to do is just a comment on this is going to be our movement. Flag on Dakoda follows that is itself dot moving underscore, right equals faults. So that means the ship is not moving next. What we want to do is couple of spaces and we want to say D S o a new function update self on what this is going to do. This function is going to do update the ships position based on the movement flags or the flag changes, dysfunction will kick in. There we go and the cold that looks like this if self stop moving, right, So if it's moving right itself the or e c t dot C and center X plus are equal toe one so increment by one perfect So let's save that. Let's now jump into a game functions program here on what we want to do is we want to beneath our key right here instead of saying increment by one because we've just done that now in our ships in our shipped up ey program, we need to remove that. And what we need to say is ship dot moving underscore right? Equals true, because that would be the key event. So change our flag in her ship that p y program. And now, as we said at the beginning of this lecture, when the when the right arrow key is released, the ship should stop. So let's put that in and we're checking for event a key up event. So Elliff event that type is pie game dot key up. So a key up event keep up. So if the system detects, if our game detects a key up event, so if event that type Sorry, Doc Key is part of game. That key underscore. Right? So key up, right? Key up ship that moving right equals false. So stop moving to ship if we detect a key up event of the right key. Okay? So let's save that. Okay, so let's just have a quick flick True here of what we've just done. So back in the ship that P. Y. Program here at line 19 we add a self taught, moving underscore, right attributes, and we set up the faults, then outlined 21. Here we had an update function, which moves the ship to the right. If the flag is true, so it lying tartine, we modify how to game responds when a player presses the right arrow key. Instead of changing the ship's position directly, we instead set moving underscore right to true. Then, at Line 15 we add a new LF block, which responds to a key up event. When a player releases the right arrow key, we set moving right to false. Finally, we modified a wide loop in our invaders from Platon main program so that it calls the ship's update method on. To do that, we simply insert the line at Line 21 here so it becomes lying 22 and we just put in ship that update brackets, there we go. Now check for any updates of our ship. Okay, let's run this and see what we guess. So let's hold down the right arrow cape. And there you see it. Continuous movement. I just have my finger on the right arrow key. And when I release it, the ship stops. Absolutely perfect. Okay, that's this lecture done in the next lecture. We're looking moving to ship the other way to the left. Thanks for listening. And I'll see you in the next lecture. 12. Moving to the left: Hi, everybody. And welcome back in this brief lecture, we're going to look at moving our ship to the left. Let's jump straight in on. We're in our ship P Y program. So as we did in the last lecture, we have our movement flag here. And what we want to say is self dot moving underscore left equals false. This is gonna work the same way as are moving right down here in our Dev update function. We add in a new if statement if self dot moving left self dot records a rectangle dot center X increment by one Sorry, decreased by one and getting carried away myself here decreased by one. Okay, let's save that. Let's jump into a game function stop ey program And we want to do here at line Turkey on my screen is we want to check for a key left event. So Elliff event that key pie game. So you should be well used to this by now. That key underscore left. So it's a so if pie game detects a left key press ship that moving left equals true so very much the same as the last lecture. We're just adding it in now for the left key on down below In the next LF block aleph event dot key is pie game that spell that right, Tony? Pie game dot Okay, Underscore Left, then ship that moving left equals faults. So stop the ship. There we go. Okay, So let's say if the US back into remain program and that's run thus Okay, so there's our main screen, our main game screens. Let's just check it Still moves to the right and continuously move to right, Mikey, when my fingers held down on the right key on now it moves to the left. So we have a full range of motion for our ship. It's ready to tackle the invaded frump. Iten. Okay, that's this brief lecture. Thanks for listening. And I'll see you in the next lecture. 13. Making our ship go faster: Hi, everybody, and welcome back now this lesson. We're going to look out adjusting our ship's speed. So at the moment, it's safe to say that our spaceship is not the fastest in the fleet. But we can change this. Currently, our ship moves at one picks of perky press. Let's upgrader ship's engines using a ship underscore speed attribute in the settings class . So here's how we would do that. As you can see here now I have my settings, my ship on my invaders from fightin main program open. So just in my settings program, I add in the following code. So comment, ship settings and self dot ship underscore speed. He called 1.5. Let's save that. Now. We've just set the initial value off the ship Speed toe 1.5. When we want to move, the ship will adjust its position by 1.5 pixels instead of one like previous, because we have set the ship's speed. The 1.5 we need to make a couple of changes because wrecked or e. C. T. Attributes stuff such as Center X store only entered your values, so we need to make some modifications to our ship program. So let's jump now into her shipped up ey program on make some changes. So we start off here in it function. What we want to put in here is we have to initialize are setting. So if you remember, we short handed invaders from fightin underscore settings, There we go. That is the first thing done. Next we have to initialize are settings. So self dot conveyed from piping settings equals invaded from piping settings. Simple enough there, on down here, just above our movement flag. We want to put in a new comment. What we're saying here is we're storing a decimal soaring a decimal value for the ships center, not code Looks like this. Self dot center equals float. Self slept itself 0.4 e c t that center x. Okay, there we go. Now. Next, What we need to do is down here in our update method. Sorry in our update function. So if self taught, moving right, self doubt rec center X. Let's just change this line here. Take out this piece of code when we have so if self doubt, moving right. Self dot center plus equals self fit to get in at what we're doing. Self dot Invaded from piping settings. That ship underscore speed. So now, instead of increasing by one were increasing by the ship speed. We sat in our settings that p wife filed a few minutes ago. When we do the same here now, on the left I'm just gonna take this here. Copyists inserted here on one small change instead of a plus. It's minus. And then finally, we want to add in one little piece of code here when adding a comment, and we want to say update direct objects or rectangular object from self dot center. There we go. And what is that code while itself dot or e c t that CNT your center X equals self dot center. Okay, so let's save this command. That s that's jumping to remain invaded from piping program. And what we need to do here is we need the past are invaded. Sump iten underscore setting as an argument when we create an instance of our ship. So here we have invaders from piping settings Comma that saved us. Let's run it and see what we guess. Okay, there's a main screen, and as you can see that those move a lot faster, done previous settings, and you can change. Have a mess around with it. But we're gonna be changing it as the game levels up. Tour of the selector. Siri's. Okay, so that's the selector. Finished. Thanks for listening. And I'll see you in the next lecture. 14. Stopping our ship at screens edge: Hi, everybody, and welcome back, let me ask you a quick question. A bit of a history question. Does anyone remember the eighties Atari game just basically, as a player? You sat on top of an ostrich or what looked like an ostrich, and he ran and other players with the idea of being to knock the other player off their ostrich I had on the Atari 600 XL, but it was also available on the Atari 2600 and probably a few other K consults. One of the features of the game was You build him to ruin off off the left hand side of the screen on Appear back on the right hand side, a bit like a loop, and he could run out the right hand scar outside on appear on the left hand side, and you could catch out your enemy that way. This was actually both in the game, which the design has turned into a feature. At the moment we have a similar book in our game. If our ship disappears, all provided a right or left hand screen. That's it. It's gone, which is obviously a problem. So have a look here. Here's my ship near the edge of the right hand screen on if I keep moving to the right. Ah, that's it. It's gone. It's not coming back. So let's fix this now by doing the following Open up. You're shipped up ey program and go to line 28. Okay, so if south up moving underscore Right colon is what we have there at the moment. What you want to do is remove the cola from the end of dust and add in the following, if self doubt moving. Underscore Right on self stop wrecked dot right less than the less than symbol self. It's left itself dot screen. Underscore, right underscore erect dot right cola. Okay, and I moved down to Lyon Authority, remove the colon just after left there and add in on a self dot wrecked, not left greater than zero cola. No, this code checks the position of the ship before changing the value of self dot Center the code self don't wreck that right returns the X coordinate value off the right edge of the ship's wreck. If this value is less than the value returned by self dot screen underscore wreck that right then the ship hasn't reached the right edge of the screen. The same goes for the left side if the value off the left side of the ref is greater done. Zero. The ship hasn't reached the left edge of the screen. That's run a program now and see how that goes command and be there. We go and let's move across to the right edge of the screen and let's see what happens. And hopefully what we want to happen is the ship just stopped dead right on the very edge of our right hand screen here. So we're almost there now, So let's have a look. Fingers crossed. Boom! There we go for a secondary, Actually didn't think stopping, but the hair we go, it stops. And if we were to move over the left hand side, it would do the same. So give that a go Now. Thanks for listening. And I'll see you in the next lecture. 15. Bullet settings: Hi, everybody, and welcome back. Now we're moving into a new section now, and that section is all about adding the ability to shoot bullets from our ship So bullets will be fired when a player presses on releases the space bar key. So the first thing we need to do is update our setting stuff p y. Program so you can see I've got my settings program open here on. What we'll do is we'll include some new values which will need for our new bullet class was recreate in the next lecture. So at the end of our file here, just hit down and just hit enter a couple times and move some spaces in on. Let's add in a comment bullet settings. There we go. And what settings do we need while we need self that bullet underscore speed equals one. No prizes to guess what that does. So self dot bullet with actually no prizes to guess what any of these do because they're also self explanatory. Self dot bullet underscore Height equals 15 and self. That bullet color equals 60 comma, 60 comma 60. There we go now. These settings create dark grey bullets in the next lecture will look at how we use these in creating our apology class. Thanks for listening. And I'll see you in the next lecture. 16. Bullet class: Hi, everybody. And welcome back in this lecture, we're going to be creating the bullet class, and it's quite a bit of get cold to get through. So let's get started. We kick off with import If I was on the rights Grain import pie game. There we go. Next from pie game. That's bright import Sprite. So we get the water. Sprite is now in just a few minutes. No, and it's after taking off the Oprah case there. So be careful of that Sprite and my AutoCorrect sprite. Now we have our class bullets. Sprite Colin out in her comments class to manage bullets fired from our spaceship. Okay, so far, so good. Close off for comment. Next, we create a function on its air in it function, and we initialize self in from P y settings. If you remember, we short handed this in the previous lecture. So invaded from Pete Frump Iten Underscore settings screen on a ship. Okay, there we go. Put in our colon on what's a description for this one? Create a flawless object of the ships position. So revenue ship has to be on our screen. Created politics, bullet object from it no super And you might remember this from when the electric early running the course. I'll get back to it now. Just a moment. Explain what it is. Super bullet. Self duck self s E l f closer brackets, Doctor. I n I t underscore underscore i n I t brackets and then self that screen equals screen. Now what we want to do next is I didn't comment on We want to say here, create a bullet for e c t. A bullet wrecked at position zero comma zero on. We'll explain. Why now in just a moment. Then after that, Dan set the correct position. Okay, so what we have now is let me just take up that space itself that wrecked or e c t equals pi game that capital or E c t. Zero comments. Zero comment and faded from piping settings dot So that's what we did in the last lecture. Bullet underscore with Come up invaded from fighting settings that bullet height. Okay, next self dot Wrecked or E c. T. That center X equals ship that wrecked or e c. T. Thought center picks self thought or you see t the top equals ship stopped. 40 c t duct up. Don't worry. We're gonna come back to this now, just a few minutes and walk. Truitt. So I'm gonna comment. Store the bullets position as a decimal value. So self dot Why? When a y coordinate equals float self dot wrecked dot Why? And then finally, let's set the color so self dark color he goats invaded from piping settings That bullet underscore color on the speed. Let's not forget the speed self doc. Speed equals invaded from fightin that bullet underscore speed. Okay, so setting some of setting stay that we did in the last lecture. So the bullet class inherits from Sprite, which we import from the pie game. That's bright module. When we use Sprite, weaken group related elements in our game and acting all the group elements at once, such as our bullets, The credible of instance. We use our i n i t function, which needs the invaders from piping settings, screen and ship instances on we called Super to inherit properly from Sprite. So and moving down truer code here itself that wrecked pie game at line 13 On my screen we create the bullets wrecked attribute the bullet is not based on an image, so we have the bill erect from scratch using the pie game dot wrecked class. This class requires the X and Y coordinates up the tough leapt off the top left corner off direct on the width and height of direct. We initialize direct at 00 but we're moving to the crack location in the next two lines because the bullets position is dependent on the ship's position. We get the width and height of the bullet from the values stored in invaders from fightin underscore settings. And remember we short handed that I end from P Y underscore settings. Then that Line 14 we set the bullets center X to be the same as the ships wrecked that center x the bullet short of marriage from the top of the ship. So we set the top off the bullet direct to match the top of the ships wrecked. Make it look like the bullets fired from the ship. Then it line 18. We store a decimal value for the bullet Wyke Ward so we can make fine adjustments. That bullet speed and then finally at Line 20 restored a bullet color and speed in self dark color and self doubt speed. Now there's one other part that we need to add to our class bullets, and that is a new function. D E f update itself. Colon on description here is move the bullet up the screen closer description. Okay, we want to start off with a comment update two decimal position off the boat. There we go. So here we have self Why minus are equal. So decrease self dot speed. That's followed by another comment. Hope take direct or E c. T position south dot wrecked that why equals itself dot y. And then finally, one more function. Draw the bullets, so draw sorry d e f got ahead of myself there. Draw, underscore bullet, self, colon And again, our description of dysfunction is drawer the bullet to the screen. So no prizes for getting what this does. Play a game doc. Draw dot Wrecked self dot screen, comma, self dot, color, comma, self dot, wrecked or e. C. T. So the update mattered. Manages the bullets position. When the bottle is fired, it moves up the screen, which corresponds with decreasing. Why cordant value So the update the position. We subtract the amount stored in self dot speed from south dot y. We then use the value of self dot. Why to set the value off self doubt. Rectal. Why the speed attributes allows us to increase the speed of the bullet as the game progresses or is needed to refine the games. Behavior. Once fired a bullet X coordinate value never changes, so Onley traveled vertically and in a straight line, which is exactly what we want. When you want to draw bullet, we'll call draw bullet. The draw dot wrecked function fills the part of the screen, defined by the bullets wrecked with the color stored and self dot color. So that's it for this lecture. So plenty of code there, so make sure you could get a grip of it on any questions. Please do not hesitate to contact me. Thank you. And I'll see you in the next lecture. 17. Bullets group: Hi guys. Welcome back. So now that we've created a bullet settings on a bullet class, we can write the code to fire a bullet each time play oppressive to space bar. But Forest will create a group in our main invaded frump Iten file on this group of store all the life bullets so we can manage the bullets that I have already been fired by a user . This group will be an instance of the class high game. That's bright dot group, which behaves like a list with some extra functionality That's helpful when we build games will use this group to draw bullets to the screen on each pass through the main loop on to update each bullets position. So that should give you an indication of where we're gonna be working in this file. Most of our code is going to go into our wire loop. So here I am, in my invaded from pipe and file, I'm a kickoff just beneath import pie. Game on. We have from pie game. That's bright Import group with a capital G. OK, there we go. Now we make our way down here on where we have line 18 on my screen created ship. And then we have the line of code that creator ship we add in a new comment. And that comment is going to say, Make a group to store bulletin. Simple enough on the line of code. Beneath that is bullets equals group capital G brackets. There we go Now scroll down for a while loop. So to start, make the start for a main loop for the game, which is here on in the first line here GF dot check events after the end of this line just after ship common bullets. Then we have ship update. When we add in a new line bullets that update brackets and then below that we have GF that update on the score screen and after ship, just like we did two lines above. Add in bullets. There we go. You can see the end of her file their run game. So we import group from play game. That's bright. We make an instance of group and call it bullets. This group is created outside of the while loop so we don't create a new group of bullets each time. Delight the loop cycles. True with then passed bullets to check events and updates Green. We'll need to work with bullets and check events. When the space buyers pressed, I will need to update the bullets that are being drawn to the screen in update screen. When we call update on a group, the group automatically called Update for each sprite in the group. The Line Bullets that update called Polyta Update for each bullet replace in the group bullets. Thanks for listening again. There's plenty of code in this lecture. So if any questions, please do not hesitate to contact me on. I'll see you in the next lecture where we look at firing the bullets. Thank you. 18. Refactoring check events: Hi, everybody. And welcome back. Now, as you can see here, I'm in my game functions that p y program on. Before we go any further, we're just gonna add some re factoring to this program because, as we move true are invaded from piping game, our game functions program. It's just going to become bigger and bigger, so that's clean it up a little bit now on Let's Start would adding, in a new function that checks for key down events. So at the very top here we have imports this import P guy pie game that's out in a couple of spaces on, let's say D E f check underscore key down underscore events. Now you're gonna recognize some of this code in this function that is from down here, and that's okay. We're not gonna copy and paste in this in this lecture because we're gonna practice our muscle memory. The more we type in the same things, a little bit of repetition when coding is a good thing that help hammer home these points. So key events. And then we have event common ship. There we go, colon. So when is this going to do? That's out in our description. Respond two key presses. There we go. Next, if event dot key. So if the key event is what's going to be if it's a pie game that capital K underscore. Right? So if the right key event colon ship dot moving right equals true. So you can see where the duplications coming up here. But don't worry down here. We're gonna clean up now in just a few minutes, then elliff event dot key. Is it a game that key that case are left. So if it's left key event colon ship dot moving underscored left equals true. Okay, there we go. Now. Next, another function. So the forest function was key down events. Now we're gonna go D e f check for Can you guess it? Key up events. There we go. And same as before. In here. We have event on. We have ship colon. Next, a description. Respond to keep up event not sorry. Respond to keep up releases because it's not really invented to release. There we go spawned the key up releases. So someone letting the key go and the key pops back if event that key. So if event that key is my game dot k. Underscore right? Colon ship Thought moving. So if the key is if the right key has being released, key equals false. The ship is not moving. We wanted to stop out. If so else. If event dot key is pie game dot k underscore left ship that moving left. But in the comment shift dot Moving left equals fault so soon as a player releases their key. The movement of the ship should be false. Shouldn't be moving a tall now down here, our check events function so respond to keep press and mount event most events. That's fine. So four event and pie game that event dot get That's fine. If event type is if the event type is quit exit, that's perfect. Next Ellis event are type his pie game, the key down. Perfect. Now what we want to do is we want to change this line here and actually want to take out are moving. So we want to change these two lines here. So Alice event are type is pie game that key down? We want to say here check. So check underscore key down events. There we go on, then in brackets event, comma ship close brackets. Okay, on next. But we want to do here where l if is we want to take her. LF they're out or back one. Sorry. So, Ellis event dot key. So l if event that type So we're just replicating what we have above it. Event that type pie game key up. There we go. Key up, Colon. What do you want to do? What we want to do. Like we did above. We want to just watch your inundation indentation here because we're editing something's key. Ope events, event ship, close brackets. There we go. Now, we can remove all this here because we put this into two separate functions. Open. Both. Now, let's just take that out. There we go. And that's a big cleaner. So we just made two new functions. Check key down events here on Chek Key ope events here. Each needs an event parameter and a ship parameter. The bodies of these two functions are copied from check events and we've replaced the old cold will cause the new functions. The Czech events function is simpler. Now, with this cleaner code structure which will make it easier to develop for two responses to player input later on in a game. So let's just say that. Perfect. Okay, Thanks for listening. And I see you in the next lecture. 19. Firing bullets: Hi, everybody. And welcome back. Now in this lecture, it's kind of crescendo. Everything is coming to a four were coming to a head. We're gonna look at firing bullets on. What we need to do is we need to stay in the game function, stop p Y program. So here we are here. It's still open from the last lecture and we need to modify our check key down events to fire a bullet whenever the space bar is pressed. Now you might be thinking, What about the key up events? But we don't need to modify it out because nothing happens when the key when the space bar is released. We also need to modify update screen to make sure each bullet is redrawing the screen before we call Flip. So here, now at a relevant change that we need to make the game functions at our first function here , D e f Chechi down events we need to add in after event. We need to add in invaders from piping setting. So I m from p y settings comma screen come up and then after ship comma bullets. There we go beneath the LF here, so beneath the key Left l If we need to add in and Notre Elliff so just remote your indentation. E l i f So Elliff event. So we're just watching for another key event, which is the space Backe Elliff event. The key is pie game, so there's nothing really new here. We're just rehashing what we've done over the last few lectures dot k underscore space. So if the event is a space bar and it's adding a comment, create a new bullets on attitude, a bullet group. There we go. Now, how do we create a new bullets? Well, we say new underscore bullets equals bullets, Invaders from piping settings because we have it there. Screen on ship. Okay, there we go. Then we followed out with bullets. That ad brackets new underscore bullets. There we go. Now moving forward it down to her Check events function where we have ship. We need to add in invaded from piping settings comma screen, come up ship which we have comma bullets. There we go. Then where we have our key down. So LF event that type key down Chechi down events. So we have event we need to add in some more here. We need to say Invaders from piping settings, comma screen, com a ship come up bullets. Okay, then we follow that down when we moved down to our updates. Green function. So we have our invaders from plight and settings. We have a screen, we have our ship and we need to add in bullets. There we go. Now it down here. But we need to do is comment. Redraw all bullets behind the ship on aliens, when we do add in, aliens wouldn't look very good if the bullets were appearing on top of her aliens. So four bullets in bullets that sprites, brackets colon bullet dot Draw Hell, if I spout that right D or a w draw underscore bullet brackets and then mover indentation back one ship dot pl i TME brackets. Now just scroll down a little bit here and take your ship dot Be Anthee Emmy function out of here. Sorry. Method out of here. There we go on. Let's save that now. The group bullets is passed a check He down events When the player press the space bar And we did that at the very start here we create a new bullet a bullet. Instance that we name new bullet additive group bullets using the admitted the code bullets start add brackets, new bullet stores the new bullet in the group bullets. We need to add bullets as a parameter in the definition of check events. And we need the past bullets as an argument in the call to check key down events as well. Who gives a put bullets prominent to update screen, which we did down here on this draws the bullets to the screen. The bullets start sprites method returns a list of all sprites in the group bullets to draw all fired bullets to the screen. We looked through the sprite and bullets and call draw bullet on each one. Now we jump into our main invaders from piping program here on Brunnis Okay, there's our ships. So far so good. Let's press the space bar. Uh, let's see, every way we get that down, the bottom bullet is not to find OK, so game functions. Bullet is not to find at line 13. Uh, that is because at the very top, we just need to add in from because he didn't call in our bullets from bullets. So from our file bullets import polish. So how can we expected? Have all the bullet parameters when we haven't told it to import it? Now let's jump back into a main program here on Let's run it again. And there we go Bullets, lo and behold, bullets. But at the moment they're blanks because we have no aliens to shoot and they are horrific, Lee slow. But we're looking, speeding them up in a later lecture. Thanks for listening, and I'll see you in the next lecture. 20. Deleting bullets: Hi, everybody. And welcome back. Now, we're gonna kick off this lecture a bit philosophically on. Let me ask you this question. If a tree falls in the woods and no one's there to hear it, fall doesn't make a sound. Well, as you ponder this, ponder this next question. At the moment, with our game, we fire off a number of bullets. What happens to those bullets when they go off screen? So when they moved to the top of our game screen and then disappear, do they just keep going on and on and on in a loop, forever or duty actually disappear? What happens to them? Well, unlike our tree question with our bullets, they will go on and on forever, and they will continue to take up memory and eventually slower game on their entire computer system to a halt. So we need to remove the bullets that have left our game screen on. How do we do that? Well, we could do it very simply with just a few lines of code. And as you can see, I'm in my main program here, Invaders from fightin So down here online. 28 of my screen just beneath bullets. That update brackets I'm gonna add in a couple of spaces. So there we go on going to kick off wit. I comment that comment is going to say Remove bullets. It's that have gone off game screen. OK, simple enough. So what do we do? What we say Four bullets in bullets. We're not going to remove the bullet from our bullet group or a bullets list. It's a group, but acts very much like a list. What we're gonna do is we're gonna make a copy. Okay, so four bullet in bullets. Copy If Pulis Don't worry, we'll step over this code now in a few minutes. If bullet dot or e c t dot Bottom is less down or equal to zero. Then bullets stop removed bullet so removed a bullet from bullets. Okay, then. Just one little thing here and we're gonna talk about this now in a moment. Print the length of what's inside bullets. Okay, so print what's in the group bullets and then just below this Now you can see that we have the line GF that update screen which we don't have to do anything with. I just insert that new code. Both that line just that's just to show you where you insert it. Okay, so let's ruin our game. No, just actually, just before we ruin our game, you can see down the bottom of my sublime set up that this is where my terrible happened. So when I run a game, or when I put a file, this is where I see the output. So at the moment that zero, which means there's no bullets, there's no program running around things like that. So don't worry about that. I just cleared out history. Now there we go. Now let me run my game there. We go away for the pop up. Okay, so now you can see over here in the bottom, left hand side of the screen. My terrible is at zero. So there are no bullets on the screen, so let's fire a bullet. There we go. Changes the one that's for another two. Another tree. So what? A print statement here. Print length of bullets. It shows how many bullets are on the screen at any given time. So in our group of bullets at a moment or a tree never before now there are five. And if we just wait a few more seconds because our bullet firing is horrific, Lee, slow. Don't worry. We'll correct that later. Now you can see the first bullet here is about to move off the top of the screen on when that does watch the output here in my terminal gun. Four tree to almost two. There we go to the two bullets left on the screen. So that's what our print length bullets does. What it does is it shows us that our bullets are actually being moved removed from our system. I'm from our game memory. So there we go. And now it's back to zero. Okay, that's killer game. Let's quit our game. There we go. You can see my output here. Finished the game. Finished. We just watch it for a minute and 10 seconds. Now we don't move. Need this here. So I'm gonna comment this out. There we go because it takes an awful lot of system memory to write to the terminal. Okay, Now, just a step back through our cold here for a moment. As I said at the beginning, we shouldn't remove items from a list or a group within a four loop. So we have to loop over a copy of the group, which we did here by creating a copy dot copy. We used a copy matter to set up the four loop, which enable just a modified bullets inside the loop. We check each bullet to see weather has disappeared off the screen and we do it out here. And if it has Sorry we remove it from bullets. We demonstrated a prince table to show how many bullets currently exist in the game and verify that they're being delete. Okay, that's it for this lecture. Hope that made sense. Thanks for listening. And I'll see you in the next lecture. 21. Update bullets function: Hi, everybody. And welcome back and this lecture, we're gonna look at creating an update Bullets function. So we want to keep our main invaded frump Iten program as clean as possible. Like we've done through all the past lectures on to keep that going. What we want to do now is we want to move this code here that removes the bullets into our game functions program. So let's look at doing that now. Okay, So I have here my game functions program. Let's just go all the way down to the bottom. Let's add in now. A couple of new functions, so D e f updates bullets underscore bullets. This takes one argument or one parameter Sorry, Bullets. There we go. And what's the description for this? Well, update position of bullets on remove old bullets. Okay, there we go. Just really cleaning up what we do in the last couple of lectures. Okay, so first updates, bullets, positions, Rio, that's not done. And then a single line of code here, bullets, bullets, that update brackets. Next. What we want to do is we want to get rid of the bullets that have left our game screen like we did in the last lecture. So remove bullets that have left our game screen. There we go. And we want to take this from our main program. So here we go. Here we are here. So let's just take all this out Commanding X on the Mac. We can remove these spaces. There we go. Come back here. Just commanded V. And I'm going to remove the couple of duplicate lines here is removed us. There we go and save. Okay. Perfect. So the code for update bullet is cut and pasted from remained invaded from piping program on back in there. Invaded from pipe Main program. We want to add in a line here, so we already have bullets update, and we just need to change this. And we need to say, because we're pulling it in there from the games function module game functions. Dust up days underscore bullets, but bullets update underscore bullets. Sorry. Bullets yet bullets. B u l l e t s bullets. Ok, there we go. So let's say it does. Let's just make sure that ruin our game again. And just before we ruin a game, I'm just gonna tell you our inner settings. I have changed our bullet speed from 1 to 5 because the speed was absolutely driving me mad . And for demo purposes and on video. It's not really great to have to wait 20 a 30 seconds for the bullet to move up the screen , so I've changed that. So let's go back in here and that runner game. No error. Let's have a look. Okay, so four bullets and bullets are copy, so it is an indentation. Errors. Let's have a look. Yeah, because the copy and paste never does it. Right. So you have to be very, very careful that I've made that mistake in a couple times. So let's run it again. Saved that. Run it again. There we go. Fire off a couple of bullets so you can see already the speed there is much, much quicker, which makes for a much better demo, and it even looks better. There we go. So at that speed, I don't see many alien invaders from pipe and standing much of a chance. Okay, that's it for this lecture tank for listening. And I'll see you in the next 22. Creating the alien class: Hi, everybody. And welcome back. Now we're entering a new phase for game development, and that is placing an alien under screen on the way we do. This is very much like what we did when we placed a ship on the screen. Each aliens behavior is controlled by a class called Alien on, Well structured this class like we did the ship class. I've added the alien bitten up image to the resource of sex of this lecture. So go there now and check it out. Make sure when you're writing a program to save the alien image in the images folder just like we did with the ship image. Okay, so let's get straight into it. We're going to kick off by creating our alien class. So as you can see here, the top of the screen, I create a file called alien dot p Y on. We start this program off, we start this file off with import pie game next from pie game. That's bright import Sprite with a couple of s. There we go, and it's made my s smaller, but we're just correct that net. Okay, Perfect. Next class. Alien sprite. Okay. And that's to find this class a class to represent a single alien. Don't worry, he wont be he or she won't be single for too long. We will be adding a whole fleet of aliens very shortly. Okay, Lets out in the first function, so D e f underscore. Underscore i an I t on the score. Self invaders from piping p y underscore settings. Common screen. There we go. Colon. Okay, again, let's give this a description. Initialize the alien. Andi. It's starting position. Okay, Perfect. It's closed. It's out. No Super alien comma Self bracket dot underscore underscore I and I t brackets. Next self dot screen equals screen Self Dot Invaded from fightin settings equals invaded from piping settings. Now let's just troubling a comment here. Load the alien image on set. It's wrecked. There we go. So self dot image equals pi game dot Image that load. Now. What are we loading? Well, we're loading our alien bit map, and like I said, you get that in the resource section of this lecture. So images forward slash alien stop BMP. There we go. Make sure again you say this image in your aliens in your images folder self doubt, wrecked equals self dot Image Don't get underscore. Erect brackets. Perfect. No, no. To comment. Start each new alien near the top left of the screen. Okay, south dot wrecked dot exe equals self dot Wrecked thought with. There we go. Next self dot Wrecked. I thought Why? So you can see here were out again are X and y coordinates self dot wrecked dot height. Okay, Next. No comment Story. The aliens. Not sure how you would pronounce that exact position. Okay, Itself that X equals float self The wrecked that X Now, finally, one more function D e f B L I t a m e self Colin at the description. Draw the alien its current position. There we go. Perfect. No self dot screen dot B l I t self dot image self dot Wrecked. Ok, there we go. So most of this class is like the ship class, except for the placement of the alien. We initially place each alien near the top left corner off the screen, adding a space to the left of it That's equal to the aliens with on a space above it equal to its height. Okay, I hope all that made sense as always. If not, please just drop being dropped me a question in the Q and A section off this lecture on. I'll see you in the next lecture will be looking at creating an instance over alien. Thank for listening on. I'll see you in the next lecture. 23. Creating an instance of the alien: Hi, everybody. And welcome back now, this lovely, that lecture we're gonna look at creating an instance off our alien. So as you can see here on my screen, I've got my invaders frump Iten ship docked p Y file open. So go ahead now and open that on. What we're going to do is we're just gonna add in a few lines here to crater instance for the aid. So let's start off just here below ship. So online, eighth of my file. So now we add in re enter and we go to line nine. So from alien Import Alien there we go. Perfect. And then we want to scroll down a little bit here under our first function run game. And we want to add in a comment we want to say, Make on aliens. Perfect on the line. We want to add in here is alien equals alien. But a couple of a this time invaded from piping settings Comment screen. There we go. And then finally we wanna add in a line Here is on my file. It's lying turkey tree on. What we want to say is g f dot update underscore screen invaders from piping settings comments screen, comma ship, comma, alien, comma bullets. There we go. OK, that's it for this lecture on. That's how we create an instance of our alien hope that made sense on I'll see you in the next lecture. 24. Making our alien appear on screen: Hi, everybody. And welcome back. Now, in this lecture, which is beautifully short again, we're going to look at making our 80 and appear on screen on to do this. I've opened up my game functions that p Y file on. We're going to look at calling our B l i t m e method. I'm going to call that method in our update screen function. So let's have a look care. So here we are. Scroll down. Lying 35 in my file and just after ship, I want to put in alien comment There we go on after ship dot B l i t me brackets. I want to put in alien that b l I t m e brackets. There we go. OK, let's save that command and s. And now I'm going to jump into my invaded from pipe and file, which is the main file we've bean adding to all along on gun control and being run this I'm There we go an ugly looking, four legged, four eyed alien spider, a racket kind of thing which comes from the land off piping. So there we go. And as I scrolled across here to our alien and I fire a few bullets. Which does that. I shouldn't do that because I have the command button held down. There we go. Perfect. And as you can see now are bullets go under our alien thick passed under alien. But we look at how our aliens will explode or die in a later lecture. Our next lecture is going to be building our alien fleet. Thanks for listening, and I'll see you in the next lecture. 25. Creating row of aliens: Hi, guys. Welcome back there with this lecture and the next several lectures, we're going to start building our alien fleet. And to do that, we need obviously to draw our alien fleet to screen. First, we need to figure out how many aliens conferred across your screen on how many rows of aliens conf it down. Our screen would first figure out the horizontal space in between aliens and create a row. Them will determine the vertical spacing and create our entire fleet. So how do we determine how many aliens fit in a row while the figure it is out? Let's look at how much horizontal space we have to screen with stored in our invaders from piping settings file. But we need an empty marriage and on either side of the screen, we'll make this margin the with of one of our aliens because we have to margins. The available space for aliens is a screen with minus two alien wits. So what would that look like? Well, let's just write out a quick piece of code here, and it would look something like this. Available space underscore Space X equals invaders frump iten underscore settings dot screen with. Let me move my mouth so you can see my line out. There we go. Minus brackets two times the alien with Okay. So as we said, our margins are going to be minus twice the size off One alien. Now, we also need to set the spacing between our aliens. I will make this space the size of one alien with the space needed to display one alien is twice its width. One week for the alien on one week for the empty space to its right. To find a number of aliens that fit across your screen, we divide the available space by two times the width of an alien that would look something like this. You have to move my bracket there. Hang on. Now, let's see. So are a number of aliens. X equals available space X on the score. X divided by that some of two times Alien. With there we go. So will include these calculations when we create the fleet. Now let's look at how we create our first role of aliens. So to create a row first create an empty group called Aliens and we create that in our invaded from piping up ey program Now your program might be something different. You can see mind is underscore ship here, but it doesn't matter. Invaders from fightin is well being using since the beginning of these lectures. So let's have a local where we put that in. Let's scroll down here and just beneath our first function run game, Let's add in something here. So let's add in a comment on let's say, make a ship a group of bullets on a group of aliens. There we go. Now, what's going in here? Well, we're going to take our ship line here, lying 25 on my screen. Just let me cut that Paced out in There we go. Next, we're going to take our bullets group from lying 30 on my screen is that we cut that cut paste and then we're going to take our alien here. We're gonna cook that again, and we're gonna pace it in here. Lying 22. I'm just gonna remove what we have here. We're going to say, instead of alien, it's going to be aliens equals group brackets on. We could just remove the Commons here Now There we go. Perfect next, we're going to create a fleet of aliens. So comment create a fleet off aliens. There we go on. What is that gonna have while it's gonna have GF dot Create underscore. Fleet invaded from piping settings. Comma screen comma alien aliens. There we go. And then finally, right down the bottom here where we have outlined 24 on my screen GF update screen. We have alien, but we want to say aliens. There we go. Now we can say that Perfect. Now, because we're no longer creating aliens directly in invaded from piping dot p y. We don't need to import alien class into this file, so we create an empty group to hold all of the aliens here at Line 22. We Dan called a new function. Great Fleet here at Line 25 opens with a comment on the cold is at line 26. We were right. This function create fleet very shortly and pass it to our invaders from piping settings. The screen object on the empty group of aliens. Next, we modified to call to update screen to give it access to the group of aliens. Now we also need to modify our updates. Green function in game functions that jump on over to our game functions File. Let's have a look here. So, as I said, update screen. So here's our update screen here on what we need update here. But we need to say, instead of alien, we need, say, aliens done. And then we have alien dot bleidt me brackets. But we need to say, now, aliens, let me just removed at peace here aliens dot draw screen. Okay, perfect. Saved up. Now, when we call the draw on a group pie game automatically draws each element in the group at the position defined by its wrecked attribute. In this case, aliens start draw screen draws each alien in the group to the screen. In the next lecture, we look at creating the fleet. Thanks for listening and, as always, have any questions, please do not hesitate to contact me 26. Creating the fleet: Hi, guys. Welcome back. Now this lecture, we're gonna look at creating our fleet, so we're gonna build on what we did in the last lecture. I'm going to put our fleet on screen, so let's jump right in. So what we're gonna do now is we're going to create a new function called Create Fleet, which replaced at the end of our game function. Stop P Y files. You can see here I have game functions open on Our new function is going to go at the bottom here. But first, we also need to import aiding class and make sure you add an import statement at the top of this file. So just believed are from bullet import statement. We say from Etienne Import alien. There we go. And that should be a capital any. Not a couple L A l i e. And there we go. Import alien. Now let's go all the way down to the end for file. And that's likely change on the dare. Let me just make that a couple ate. There we go now. As I said, let's go all the way down to the Enver file and let's kick this off So we start with, as always when credit new function D e f create underscore fleet There we go on in here. We have invaded from piping settings. Come up screen comma aliens. That was our bracket Colon. No, Let's give this a description. Create a fleet of aliens. Here we go on just because I'm funny. Let's just make that see here a couple c Perfect. Now that's out in another comment on Let's Say, Create on Alien and find the number of aliens in a row. So the calculations that we kind of roughly sketched out in the last lecture are going to come back and benefit us now in this lecture. Next, let's add in another comment I'm going to say spacing between do that aliens is equal to one alien with my spelling has left me down this morning. There we go. Okay, so let's kick this self. So what we do? Where? For us. We start with alien equals alien invaders frump iten settings screen. Okay, I'm going to step through this code, as always, at the end of this lecture. So alien with underscore with equals alien. That's correct that with there we go next available space on the score. X equals invaded from piping settings dot screen with minus two times alien With there we go. Next, we have number of aliens. Underscore X equals. And so we're going to turn this into an integer available space X divided by open brackets . Two times Alien With There we go Close bracket Close bracket Now another comment. Great first row of aliens. There we go. So four alien number in range So you can see here love the stuff from area run in the course coming back to help us on to be re used now in a fully functioning program. Or what will be a fully functioning program. So in range number. Underscore Aliens X colon. There's another comment. Are you going to say here, create an alien and place it in the road? Here we go. Okay, so alien equals alien. Couple a days time invaded from fighting settings Comments screen. There we go. Next, eight in the X equals alien with plus two multiplied by Aidan with times alien number. Next alien got wrecked. Don't X equals alien that x And then finally aliens dot ad alien. So at alien, there we go. Okay, Now let's just briefly stepped through this and see what we have. So we've already talked to most of this cold and we discussed somebody in the last lecture on. What we need to do now is no the aliens with on height in order to place the aliens. So we created Alien here on alien equals alien invaders from piping settings. Line 66 on my screen. And we do this before we perform the calculations. This alien won't be part for a fleet, so we don't add it to the group aliens. Then at the next line alien underscore with we get the aliens wit from its wrecked attributes and stored its value an alien underscore with So we don't have to keep working with direct attributes at the next line available. Underscore space on the score X. We calculate the horizontal space available for aliens on the number of aliens that can fit into that space at the next line number. Underscore aliens underscore X. The only change here from original form is is that we're using into to ensure we end up with an integer number of aliens because we don't want to create partial aliens on the range function needs. An integer thean function drops the decimal part of a number effectively rounding down. This is helpful because you'd rather have a little extra space in a row than an overly crowded role. We then set up a loop that count from zero to the number of aliens and we need to make. So here's our loop. Starting here in the main body of the loop, we create a new alien and then said It's X coordinate value to place it in the road. And we do that here. Alien equals aliens. Each aiding is pushed to the right one alien wit from the left Margin. Next we multiply the alien with by two to account for the space each alien takes up, including the empty space to its right. On. We multiply this amount by the aliens position in the road. Then we are each new alien to the group aliens. Let's see what happens when we run it. Hopefully, all our code is rice on. We get a nice line of aliens, so let's have a look. There we go. First time lucky, unlike some of our other ones. So what we have. There is five aliens. Very nice on excellent progress so far. So the fourth row, as you can see, is offset. That left, which is actually good for us. Because in our game play, we want the fleet to move to the right until it hits the edge of the screen, then dropped down a bit, moved left and so on, so forth. In the next lecture, we look at doing a little bit of re factoring with a crate Fleet function on, move on and create more and more aliens. Thanks for listening. And I'll see you in the next lecture. 27. Refactoring create alien: Hi, guys. Welcome back. Now, as you can see here in front of us, I've got my game functions up, ey file open and we're gonna do a little bit of re factoring. So before we move on to the next next few lectures, which we'll be building out our alien fleet, we're just going to clean up this fight a little bit because we're gonna be using it later on. So, as you might remember from earlier run the course re factoring is taking the time to clean a bar code. So let's do it up now. So what we're going to do is we're going to clean up the function, create fleet. We're gonna create another couple of functions from that. So don't worry about the function. Create fleet for the moment. Will come back that now in the second. So let's just start off D E f get number aliens. Underscore X get number. What number? Aliens on the score X and in here. We're gonna give it the parameters. Invaded from piping settings on alien with alien number. No alien with. There we go. Colon description. So what are we doing? We're determined the number of aliens that fit in a row. How many aliens will fit in a row? It's kind. Like that joke. How many elephants can you fit into a mini? I don't know the answer to that joke, actually. So what we want to do is we want to take a couple of lines from appear. We want to say available space. So we want to take this line. Let's just cut that and that's just paste it in here. There we go. There's that line in. Next. We want to take this line. Line 69 on my screen. Number of aliens. That's just cut that again and paste out here. There we go. We want to finish this off of the line. Return number of Aliens X. There we go. Next, we want to create new function again. And what we want to say here is D E f create alien. And the problem is, we want to give it our invaded from piping screen aliens. Alien number. There we go. Colon, on its description description is create an alien and place it. I'm just gonna say in a row, not dear. Oh, because we have several roads. So in a row. Okay, Now what goes in here? Well, forest, the alien equals alien. Here, Line 74. We take that. So Commander Necks paced out in new line. Now it is a new piece of code. So alien underscore with equals alien dot wrecked dot with there we go. And next we have alien dot Exe equals alien wits and line 75 here that we don't in our previous lecture just cut that. Pace it here. Next alien dot Wrecked thes two lines here, the line 76 77. Just cut them on a paste. Now, finally in a crate fleet up here. We want to clean this up a little bit because we removed some of our cold from it, and we place it down here to new functions. So what we gonna be left with Where we have our create fleet evaded from pipe and setting states, the same alien stays the same so we can leave our description in. So what we want to do here is take out this comment. The second comment here removed that zap that's gone. We leave in alien equals alien brackets invade from fighting settings and screen we leave that in on we take out alien with on what we say Here is number of aliens Underscore X equals get number of aliens. Brackets invaded from fightin settings. Comma alien thought, wrecked thought wit. There we go now four drawn down where we have our commentator created. Fourth row of aliens. Leave that as it is and we add a new line here. Just remove this comment so we leave our four loop in place. Four alien number in range. Leave that there. And what we say beneath that in the loop is create alien invaded from piping settings. Common screen, comma aliens, comma alien number. There we go. That's close. Office and brackets. So what we do now here is we just remove these spaces. Just take that out. There we go. Actually, I don't leave a space in here at the Ford just reliving the clarity. And I just take out this space here at Lyon 65 on my screen. Ok, there we go. Let's just quickly recap what we've done. Just a couple of space. That there while we do. Okay, So the body off get number of aliens here that line 72 on my screen. Here we go is exactly the same as a wise and create fleet the body of create alien here at Line 78 on my screen. Let's give that a bit of a highlight is also one change from our previous create fleet function, except that we used the alien that was just created to get the alien wit. We Denver plates a cold for determined the horizontal space and with a call to get underscoring number of underscore aliens underscore X and removed the line, referring to Alien with because that's now handle inside. Create alien itself. We don't call create alien perfect. So this reef act make it easier to add new rose and creating entire fleet in the next few lectures. So that's that one in the next lecture will get back to adding roles of aliens tour games. Thanks for listening, and I'll see you in the next lecture 28. Adding rows of aliens: Hi, everybody, and welcome back. Now, in this lecture, we're gonna look at adding rows of aliens to the alien fleet. So the finish offer fleet. We need to determine the number of rows that fit on the screen and then repeat the loop for creating the aliens in one row that number of times to determine the number of rows. We find the available vertical space by subtracting the alien hide from the top to ship height from the bottom and to alien heights from the bottom of the screen. This will result in some empty space above the ship, so the player has some time to start shooting aliens at beginning of each level. Each row needs empty space. Blow it, which will make equal to the height of one area. To find a number of rows. We divide the available space by two times the height of an alien. And as always, if these calculations were off, we'll see issues right away when we were on our game, and we can then fix it as we go. So how do we begin this? Well, let's start in our game functions program here on. Let's create a new function. So let's just down here. Let's just space in a couple of lines here on Let's a d efs or a new function get underscore. Number of Rose. There we go. And in that report are invaded from piping. Settings are good, are reliable settings file. Then we have a new prominent ship. Height. There we go. Comma on. We have alien height. Okay, brackets, colon. So our description determine number of rows that of rose of aliens not fit on the screen? No. So what? We started what? We started available space. Let me just change the extra white ass are available space underscore why equals brackets invaded from piping settings dot screen height screen underscore heights minus brackets, tree times the alien height. There we go. And then we have here minus ship height, close brackets. And on the next line, we have the number of rows number underscore, rows equals and Dodger. That's gonna be an interview. Obviously available space available. Underscore space. Underscore Why? Divided by brackets two times alien height. There we go. Close brackets, close brackets. Next line return number of rows. There we go. Now what we want to do is in their crate alien function here just above what we've just done. We have invaded from fighting settings. We have our screen. We have our aliens. We have alien number. And what we need you to add in here now is row number, row, underscore number. There we go. That's done On now. Just beneath this line here, Row line 80 Tree on my screen alien dot Exe new space here. So this becomes I know our second last line and dysfunction. Alien dot wrecked dot Why equals alien does wrecked that height plus two times alien direct that height, times row underscore number. There we go. Okay, So we just saved up for a moment and now lets you scroll up to our create fleet DF correct fleet. And so what we have here is invaded from fighting settings. We have our screen, We have our aliens, but we need to add in ship That's put in ship here. Perfect comma aliens? No. After the alien with here A and direct out with here and just before our four loop. So we go number of rows. The number rose equals get number of rows. Brackets invaded from Peiding settings, ship dot wreck that height, comma alien that wreck that height. There we go. Now what we just want to do now is whether for loop just adding a comment of over four. Loop on What we're going to say here is create the fleet of aliens. Small changes within their loops. What we want to say is four row number so four row number in range number Underscore Road. So we just take out underscore Alien Underscore X That's a rose. And then finally, we want to make one more adjustment here in a crate. Alien invasion, Pipkins screen aliens Alien number. And then on the end of this, we tack on row number. There we go. Okay, Perfect. That's just saved that again. So what have you done? Well, the calculate the number of rows we could fit on the screen we write are available. Underscore space. Underscore why a number underscore Row calculations into the function Get number Rose, which is similar to get number of aliens. Underscore X to create multiple rows were used to nested loops one outer and one inner loop , and thats done here. The inner loop creates the aliens in one role on the outer loop count from zero to the number of rows we want Fighting will use the cold from making a single row and repeated number. Underscore roll times the definition of crate underscore alien here, Line 80 on my screen needs a parameter to hold the roll number within. Create Underscore Alien We change and aliens white quartered value when it's not in the fourth row on we do this by starting with one aliens height to create empty space at the top of the screen. Each row start to alien height above the last row, so we multiply the alien height by two on, then by the row number, the first roll number zero. So the vertical placement off the first row is unchanged. Alsop's controls are placed further down the screen definition of create on score. Fleet also has a new parameter for the ship object, which means we need to include is the ship argument in the car to create fleet in their invaders? Ramp Iten file, which is this file here, and what we need to do is where we have created fleet of aliens. Here we have create fleet invade from fighting settings, screen and we just need to add in ship here. Let's do that ship come up. Let's say it does never We wonder Program. We should see a fleet of aliens. Let's have a look. Okay, we get never and let's see what that error is. I've left out underscore here in my four loops. Let's do that. Let's run it again And there we go a second row of aliens out of your fleet. Now what you can do if you want is you can start the mess around with these numbers here in your game functions. So for argument's sake, if I change to tow one here for alien height, that means out squeeze maw ratings into the screen. So let's just say, if that's on, run it again. And there you go. You can see more much, many more aliens on the screen and the tree rows of aliens under screen. So what you can do is you can mess around with these calculations and find the settings that it right for your screen. I'm just going to leave it to their for the moment because we're creating nice, simple game and we just want to zap away a few a day and see how that works. So there we go. Now, just before we go, I noticed one error in my game function screen. On that is I left out of line here in between line 70 and 72. Line 71 48 and underscoring number in range. Brackets number underscore and underscore X. So we get an error and I can't figure out why. Go back to your game functions program and add that in now. Sorry about that. So that's adding aliens to our fleet. So, as you can see there now, we've you know, we've now got two lines or depending on the number of size your screen. You can mess around with all these figures here, all these calculations here, and try and add more aliens or less aliens to your game. Thanks for listening. And I'll see you in the next lecture. 29. Moving aliens right: Hi, everybody. And welcome back. Now, in this lecture, we're gonna look at moving the aliens to the right hand side of your screen. And to do that, we'll use an update method in our alien duck P Y file, which we'll call for each aid in the group of aliens. So what defiance that I have open here, which you might want to get. Opener settings up. Ey! Alien up. Ey invaded from fightin on game functions up. Ey, So kick this off. A comment on that is alien settings. There we go on. Like we just said, we want to add in a speed so self dot alien underscore speed factor. Okay, there we go. And let's just set up the one initially. Okay, Safe. There we go. Next. We want to use this setting to implement the update in an alien that p white file. So at the end of her alien that p wife I'll scroll down on, we'll create a new function. And that will be t f update self. It's the only prominent colon and then comments. So description moved the aliens right there we go. OK with the comment in, we say self that X plus equals self itself completely misspelled itself that invaders from pipe and settings that alien on the score speed when the score factor so setting from the last file and then finally in dysfunction, we say itself direct that x equals self dot Exe. Okay, so he saved us. Next we go to our invaded from pipe and file and down here in line 32 of my screen. So just after our GF dot update bullets line here we say GF that update underscore aliens, brackets, aliens. Okay, there we go. And we saved that. What we want to do is we want to update our game functions with our new function up the update, underscore aliens, and we do that at the end of this file. So scrolled all the way down to the end. And we say, D f sorry. Just give that line D E f updates aliens. I should've put in an underscore their update. Aliens, brackets, aliens, Colon, After our brackets, my class I let me down today. I need new glasses. I think update the position off all aliens. There we go. Close that description on what we say here we just say simply aliens that update brackets. Okay, let's say that I mean, go back into invaded from pipe main file When we run this. Now we should see. Are aliens moved to the right? Let's hope there we go. Moving away to the right. Now we set our speed, our speed factor, toe one at the beginning of this lecture so we could go back and increase that. But we'll look at that a little bit later on as we move through different levels of our game. Okay, that's it. Thanks for listening. And then we're last on in our next lecture. We look at how to detect when aliens have hit the side of our screens. Thanks. And see you in the next lecture. 30. Has an alien hit screens edge: Hi, everybody. And welcome back. Well, now that we have our alien fleet moving to the right, we need to detect when our fleet hits the edge of the screen on when it does to move our fleet down onto the left. So here's how we begin to start that process. Open up our setting. Stop ey file on here. Down beneath the common alien settings out in the line. Self start fleet underscored Drop, underscore speed. Let's make that equal to 10. There we go next, just out in a couple spaces and we want out in a new comment on We want to say Fleet underscore direction of one Represents right on minus one. Represents left. OK, there we go. Perfect on beneath that. Now let's say self. Starfleet direction equals one, so you can tell there. Now that's moving the fleet that it right, so the settings fleet underscore. Drop, underscore speed control. Tell quickly to fleet drops down the screen. It's helpful to separate this speed from the aliens horizontal speed so you can adjust the to speed independently, which we have now done to implement the settings free direction. We could use a text value such as left or right, but we'd end up with, if else statements testing for the fleet direction instead. Because we only have two directions to deal with. We will use values one on minus one and switch between them each time the fleet changes direction. Now let's look us how we check to see whether and Aidan has hit the screens edge. Okay, so let's saver file their on. Let's move into our alien dot p y program. So here, just in between our second last and last functions, let's add in a couple spaces on. Let's create a new function on dysfunctions called D E F. Check underscore edges. There we go on. This is gonna have one parameter itself. Okay, so next let's put in our description return. True, it's alien is asked the edge off the screen. There we go. Okay, So what's next? So we have our screen underscore Wrecked equals self dot Screen. Don't get underscore. Wrecked and close it off. Ended with brackets. There we go. So if self the direct stuff right is greater. Diner equal to screen. Underscore wrecked out. Right? What do we dio? So if our screen position on the right is great on the edge of the screen. What we do. Well, we se return true. Okay, out If self dot wrecked that left his last time equal to zero colon What we say we say return true now and down below dysfunction. We want to make some changes to our update function. So here's what we do we say instead of moving aliens to the right now, do we have to Billy to move them left when he hit the edge of the right hand side the screen. So move the aliens right or left. There we go. Perfect on in the second line here. We want to say self dot Exe. But what we want to do is we want to put a bracket around here because we have small calculation to do self doubt, invade from piping underscore settings, that alien speed factor Space times self. You slept self dot invaders from piping settings. That fleet underscored direction. Okay, so I think we can all see what's going on here. But we'll run truth now in just a second. Okay, so let's save that. So when we call the new method, check edges the alien is at the right edge. If the right attributes of a direct is greater than or equal to the right attributes of the screens wrecked, it's at the left edge. If it's left, value is less than or equal to zero. We modified the method update to allow the motion to move left or right by multiplying the alien speed factor by the value of fleet direction. If fleet direction is one, the value of alien speed factor will be added to the aliens. Current position. Moving the alien to the right. If fleet underscore direction is minus one, the value will be subtracted from the eighties. Position moving the alien to the left. So that's it for this lecture. On the next, we'll look at how we drop the fleet and changes direction. Thanks for listening, and I'll see you in next lecture. 31. Changing fleet direction: Hi, guys. Welcome back. Now, in this lecture, we're gonna look at dropping the fleet and changing its direction. So in an alien reaches the edge, the entire fleet needs to drop down and changes direction. So what we need to do is jump into our game functions. Harpy wife. I'll make her changes there because that's where we check to see if the any aliens are at the left or the right edge of our screens will make this happen by writing the functions. Check on the score fleet, underscore edges and change. Underscore fleet on the score direction on. Well, then modify our update alien function, which I have here on the screen. It's our last function here, so let's get started and kick off because there are a couple of new function that we have to right here. So we'll start off here just above our update. Aliens function on what we say is D E f check underscore fleet. Underscore edges. Okay. And then we give it the parameters. Invaders from piping settings on aliens. Here we go. I was our brackets. Colon. So description respond. If any aliens have reached the edge of the screen There we go. Okay, so four alien. So we're doing a for loop for Alien in 18. So for any alien in aliens dot sprites brackets colon, if alien dot check underscore edges. Okay, Brackets, colon, than what we want to do is we want to change underscore fleet on the score direction. There we go. Brackets invaded from piping settings, common aliens and then break. So break out up with school stuff Now, in a few minutes. Okay? Our next function D e f change fleet direction invaded from piping settings. Common aliens, brackets coding. Okay, under description. Drop de fleet and change the fleets direction. Okay, coz, offer a description on what we say here. We would say four alien in aliens dot sprites. So similar to above brackets colon alien dot or E C T. So the alien wrecked dot y plus equals invaders from plate and settings dot fleet underscored drop, underscore speed. Okay. And then we have just backspace one invaders from pipe and settings. That fleet underscored direction times equals minus one. So, as we said in the last lecture, that will move left. Okay, now for it. Down in our update Aliens. What we need to do is we need to add in another primary haired beginning. So invaders from piping settings. There we go. Come up. So we're going to stay here now is check if the fleet has reached the edge and then updates the position of all the aliens. OK, it's what we do is now we're gonna add in a new line check fleet edges, brackets, invaders from piping settings, comma aliens. There we go. Brackets now believe the last line as it is Alias out update brackets. So in Czech underscore fleet underscore edges. So that was the forest function Just wrote here online 96. On my screen We leave, we looped through the fleet and call check edges on each alien. If check on the score, edges returns true. We know 1 18 is at an edge on the whole fleet needs to change direction. So we call change, underscore fleet, underscore direction on break out of the loop and change. Underscore fleet, underscore direction. Our next function here, line one. Otri On my screen we looked through all the aliens and drop each one using the setting fleet. Underscore, Drop, underscore, speed. Then we change the value our fleet direction by multiplying its current value by minus one . We've modified the function Update aliens to determine whether any aliens are at an edge by calling check, underscore fleet, underscore A it's. And then finally, dysfunction needs to have our invaders from piping settings parameter so including arrogant for invaded from piping settings in the called update aliens. Now what we need to do is jump into invaded from pipe and file our main file on where we have here now in 29 on my screen. While true, underneath bullets, what we have is update underscore aliens we need to add in our invaded from fightin parameter space that out because of the pick it up automatically for me. There we go. Come on. Okay. Lets saved us. Now, if we run our game, our fleet should change direction. So let's take a look. Fingers crossed, everybody. Oh, error. So let's have a look. It's left. This one really is gonna follow me to the grave. I have misspelled slept on wherever I misspell that There it is. There, in my settings file direction equals one I've put in slept on there it is as you can see Right there. It's left, Self. There we go. Saved us. There is one place in my main file. Okay, lets fingers crossed. Fingers crossed. Let's see. Let's see. Let's see from Wow. Okay, Very good. Very good. We can look at speeding it up later on, but as you can see now, our fleet is moving down. Changing direction on hitting the engine screen. There we go again. Okay. Very, very exciting. Okay, guys, Thanks for listening on. I'll see you in the next lecture. 32. Shooting down aliens: Hi guys. Welcome back. Now. I think it's fair to say we've come a long a very long way. We've built a ship, we've built a fleet, and we have a lot of functionality on response in the game. But now it's time to start shooting down the aliens. At the moment when bullets reached the aliens, they just simply passed beneath them. And this is because we're not checking for water called collisions in game programming. Collisions happen when game elements overlap. So such as our bullets hitting are aliens. To make the polit shoot down, aliens will use the mattered Sprite that group collide to look for collisions between members of two groups. So obviously, when we're detecting bullet collisions, we want to know right away when a bullet hits an alien so we can make an alien disappear as soon as it's hit. To do this, we look for collisions immediately after updating a bullets position. Despite that group, Collide Method compares each bullets wrecked with each aliens wrecked and returns. The dictionary contained the bullet on aliens that have collided. Each key in the dictionary is a bullet, and a corresponding value is the alien that was hit. So let's look at the cold. Now. We need to use the check for collisions in the update bullets function. So head on over into games function here, which I have open on. Let's go to our update Bullets function. Let's have a look. There we go. Line 51 of my screen DF update bullets. Okay, so we need to put in now is a new parameter aliens Comet. Perfect. So we want to add in a couple new comments, so we have update at the bullet positions. So we want to say is comment check for any Well, it's that have hit aliens have have hit aliens. Perfect. Next Notre comment, we want to say, get rid of the bullet and the alien. Okay, there we go. That's what you need to add in. But for us, we have collisions. C o l l i n s collisions at sugar CEO l l I s i o n s collisions equals pi game. That's bright dot group collide bullets. Aliens. True, true. Okay, there we go. On we remove her last line here. Okay, So new line, we added loops through each bullet in the group bullets and then loops. True, each alien in the group Aliens whenever directs off a bulletin Alien Overlap group, collide as a key value parroted dictionary. It returns to to True arguments tell pie game weather to delete the bullet and the aliens that have collided. We passed the argument aliens in the call to update bullets. Next we move into our invaders from piping program. Here we go on, we say here where we have GF dot update bullets, we say Aliens, bullets. There we go now a letter on our program. First, let's save our game functions program save. Let's run a program and see what we get up. I know what's wrong here. I should not have removed and I don't know why, I said, but I should not have removed this line here. Saved us. Sorry about that. Getting ahead of myself. That's runner game again. Let's see what happens. Link Xilinx Inc. Here we go. Excellent. Let's wait for a few more aliens for a period. There we go. Bum bum. Okay, we can now move our ship on. Shoot down Another alien from from, um I'm moving out of the way. My bullets, But Yep. There you go. Ok, excellent. So now we're able to change the direction of our fleet and shoot down aliens in the next lecture, we're going to look at re populating our fleet on a few other functions. Thanks for listening. And I'll see you in the next lecture. 33. Repopulatng the alien fleet: Hi, guys. Welcome back now, as we just saw in the last lecture were able to shoot down aliens. But what happens when we shoot down all the aliens and none are left? Well, what we want to appear is a new fleet. So to make a new fleet of aliens appear after Fleet has been destroyed, we first need to check and see whether the group aliens is empty. If it is, we call the crate Fleet mattered. We perform this check and update bullets because that's where individual aliens are destroyed. Let's jump on over into a game, functions that p y file and let's go to our update bullet function. So here we are here, okay. And what we want to change now is let's have a look. So at line 56 just entering a new line on what we say here is if so, if the length of our aliens So having a bracket is the wrong way around, there we go. So if the length of our aliens is zero, okay, what we do? Well, let's add in a comment, destroy existing bullets and create a new fleet. Okay. Sounds as simple enough. So then we have our bullets. Start updates. Let's leave that there for the moment. And let's just go put in a new space here on let's say bullets not empty. Okay, brackets. Now let's Aldean create fleet. There we go to populate Crate Fleet Invaders from plight and settings brackets A common screen. There we go. Common ship. Come on, Aliens. Okay, there we go. No, let's say that. So we started off and we check whether the group aliens is empty. So that's here. If the group aliens zero, if it is, we get rid of any existing bullets by using the empty method which removes all the remaining sprites from a group we don't call create fleet, which fills the screen with aliens again. And finally, what we need to do is the definition of update bullets. We need to add in some new parameters on we add in, conveyed from piping settings, common screen on aliens or dare. So common aliens come a ship. Then we finish off with the bullets, which is already there. So now what we need to do is update our call update bullets in our invaded from piping fire . So let's jump on over here and where we have update bullets. So we're in that. The last four in the last lecture. Here we are here. Update bullets. We need to say space invaders from piping settings. Comma screen, comma ship. There we go. Come on. Okay, let's say it does. And let's jump on into our game. Well, so let's see what this error is. Okay. It's not Era has to do with the order of parameters here in my line. 51 update bullets function. So go ahead now. And just if you haven't already reorganized the order of your parameters here to match what I haven't screen. Okay, so with that, don't let's run our program again, OK, here's our program. Now, I'm gonna pause this on, remove most of the aliens under screen, but one and then see what happens there back on screen now. And as you can see, I only have two aliens left. So hopefully when I destroy these two aliens are fleet should re populate. So let's give it a go. There goes a long 18 into berm Free fleet re populate accident. That's exactly what we wanted. Okay. And the next re election would look at making the game Livermore difficult by speeding up our bullets. Thanks for listening. And I see you in the next lecture. 34. Changing alien and bullet speed: Hi, guys. Welcome back. Now, in this very short lecture, I'm not gonna ruin any game. And I'm just gonna jump into my settings file here. Settings, not p y. And as you can see here online 15. On my screen, we have the bullet speed eight and we have the alien speed. I've changed that from 1 to 2. So what I suggest you do now, is it just these settings to a setting that you're comfortable with so that the game is a little more challenging the aliens of moving faster and the bullets are moving faster or slower Whichever way you want. You can even try and increase the drop down speed to make the game that little bit more challenging for yourself. So go ahead and mess around with those settings now, and I'll see you in the next lecture. Thank you. 35. Detecting alien ship collisions: Hi, guys. Welcome back now. Anybody who is familiar with the original version of Space Invaders will know that Wendy aliens reached the bottom of screen. Hit the ship. The game is over. So let's try and replicate that feature now within our program. So when a player doesn't shoot down enough of the ship's enough of the alien ship quickly enough, we'll have the aliens destroy the ship if and when they hit it on at the same time will limit the number of ships a player can use and will destroy the ship when an alien reaches the bottom of the screen will then end the game when the player has used up all their ships . So the first thing we need to do is detect alien ship collisions. We'll check for alien ship collisions immediately after updating the position of each A. So how would we do that while we jump into a game Functions file on we go down to our update aliens function, which should be our last function. So here it is, here, on my screen started line 117 on. What we do is we just add in a couple spaces here and we added a comment, and we say, Look for alien ship collisions. Commissions will be good exposed again. Commissions every time a ship donating collect crash. But that's not what this game is about. Maybe in the next game. Okay, so look, look for alien ship collisions. So if pie game that's bright, that's bright collide, honey. Then we're looking for a ship on aliens. There we go. Colon print. So we're going to put up a message to say that a ship is being hit your ship ship. Your ship has bean it. Okay, there we go. That's just saved up. So the method Sprite collide. Anne takes two arguments s Sprite and a group this method Look for any member of the group that's collided with spray on stops Looking true, the group assumes it finds one member that is collide with the Sprite. Here it looked through the group Aliens on returns the first alien defiance that has collided with ship If no collisions occur. Sprite collide. Annie returns known on the If block at line here one to treat won't execute it finds an alien that's collided with ship. It returns that the alien on the If block executes on a temperance, your ship is being hit. So now what we need to do is we need to pass to our update. Aliens method. So jumping to remain file here. Invaders from fightin on in our GF dot update alien. So here we are here. We need to add in after settings. We need adding ship. There we go. So now when we run a game, whenever an alien collides with a ship, we should see the message. Your ship has been hit. Let's save this. Okay, so let's run game now and see what happens. No, we got an error when we get a never two position. Arrogance, poetry were given okay to jump back into a game functions here. We have invaded from Peiding settings. I should have put in ship, which I Ferguson even though I mentioned it. Ship. Ok, listen, let's say that that's jump across here. Run a game. I know there ever. Okay, hang on. Let's see here. What's going on going on is I put the ship in the wrong function, so that Israel schoolboy error stuff So sorry about that. You probably haven't made that mistake so Let me just say that, Jim, back into a game. Third time. Lucky boom. OK, so as you can see, in the last lecture, you update the speed of my aliens and my bullets. So there you go. So what I'm gonna do now is passes for a moment. And when we come back, the aliens were just about be able to touch the ship, and we'll see what happens. I'll see you in a second, guys. Welcome back. So, as you can see here now are aliens are just about to collide with a ship, and we need to see what happens when this happens. So let's have a look. Looks like we have one more loop to do. There we go. OK, so you can see in my terminal here, just coming in the corner of my eye. It is just for testing part with your ship has being hit. So there we go. We look at removing the ship in a later lecture, but for now we know that the collisions are working. Thanks for listening. And I see you in the next lecture 36. Responding to alien ship collisions: Hi, guys. Welcome back. Well, now that we have are aliens on our ship colliding. We need to figure out what happens after the collision. Now, instead of destroying the ship instance and creating a new one, we'll count how many times a ship is being hit by tracking statistics for the game. So that's right. A new class game starts to track game statistics and save it as game on the store starts up . Ey. So as you can see here, I've got a new fire creative game on the store. Starts that p y on. Let's kick off our new class. So what do we say when we say class game Stutts and in brackets on Colon? Oh, Colon, Not quotes. So let's see what we want to say here. We want to say track stats for invaders from fightin. Okay, let's close that off. Next. What we want. Okay, well, we want a new function, so d e f underscore. Underscore i n I t self comma invaders from piping settings. There we go. OK, Colon, what we have in here What we want to say here is initialized settings. Sorry. Initialized starts. Okay. Next self dot invaders from piping settings equals invaders from fighting settings. Okay. And then self dot Reset going to score stats. Make sure I spell itself right, cause you've been with me for the last few lectures. You'll notice that when it comes to that word, I have particular trouble. Okay, So new function DF reset. Stutts Yeah. Start is not being very helpful to me either. Much like self. Okay, colon initialize stats that can change during the game. Okay, its closest off on let's say, self dot ships underscore left. So how many ships are remaining? Equals? Self dot Invaded from plate and settings dot ship underscore limit. Okay, there we go. Now let's just saved us so we'll make one game starts instance for the entire time our game is running, But we need to reset some statistics each time to play or start a new game on to do this will initialize most of statistics in the method Reset stats instead of directly in the inner method. We'll call this method from in it so that the stats are set properly when the game starts. Instance is for us created. We'll also be able to call resets. That's any time to play or start a new game. Right now, we have only one statistic on that is ships underscore left, the funny of which will change throughout the game so the number of ships to player starts with stored in our settings. File. Let's jump in there now and have a look. So here we go Ship settings Line 11 on my screen so that's just odd. In self dot ship, underscore Limit equals tree so we're given tree lives. Essentially. Let's save that now. We also make need to make a few changes in a main part. File invaded from fightin, and we need to create an instance off game stats. So where do we do that? We'll hear online. Seven. Will rehear of from settings, import settings, we say from game underscore Stutts, which is our file that we just created up here. Game underscore Stutts import game steps. So what are we importing? We were importing from this file. This class game starts. There we go Now. What we also need to do is in a room game function here, just beneath it. Invaded frump Iten. Add in to comment on the code. Let's see comment. So what should we say? What we say Create an instance that sometimes difficult type and talk at the same time. So, craven instance, to store game Stutts There we go on what we want to say here is stats equals game starts, dairy go invaders from plate and settings. Okay, But we also want to do now is and our loop are while true loop here in our update aliens. So update aliens we haven't heard from piling on. What we need to say here now is stats. Come it, then. We have ship dominate the ad in screen, which I have Corinne screen S C or e and screen ship aliens on. We need to say bullets at the end, so let's see. B u l l e t s bullets. There we go. Okay. So what are we doing so far? Well, here we've imported the new game. Stats class on. We don't not here with this line from game starts. Import game starts and we make it starts. Instance here. Where am I? Here lying 21. There we go. And then at the start, screen and ship arguments to to call update aliens and We did that down here. Line 38. Okay, We'll use these arguments, attract the number of ships the player has left and to build a new fleet. When an alien hits the ship, when an alien hits the ship, we subtract one from number of ship left. We don't destroy all existing aliens and bullets and create a new fleet. We don't also reposition the ship in the middle of the screen. Let's put most of this code in the function ship hit. So that's going to game functions. Here we are, game functions. So lying to just beneath import S Y s, we say from time import sleep. Now, what does this do? Well, we need to pull us again for moments or that the player can notice the collision and regroup before a new fleet appears. So that would come back to this now, in a few minutes. So where do we put in our new function? Well, let's have a look beneath here. Import alien. Let's say D f ship underscore hit brackets, invaders from piping settings. Comma starts, comma screen. Come a ship. Aliens, bullets. There we go. Now, Colin, on what we put in Well, let's say respond to a ship being hit by an alien. OK, there we go. Close that off. Next line. Add in a common here just to tell you what we're gonna do. So decry meant ships underscore. Hips were dancing program. Maybe ships left underscore left. And how do we do that when we say stats dot Ship left minus one. There we go. Now what's next? Well sailed in the same function we say. So I re adding a comment on we say empty the list of aliens on bullets. So we're clearing the bullet from the screen on the aliens. Ah, b u l l e t s helps to say it out loud sometimes. Aliens not empty. Here we go on, then bullets. You guess it bullets dot nt There we go. Ok, what's next? Now that a comment create a new fleet on center the ship. So it's just like a game resetting. So create fleets. There we go. Invaders from piping settings. Common screen. Come a ship. Come on, Aliens. Okay? Nothing Every ordinary. Their ship that center underscore ship. Okay, now common to pause so you can go to your thoughts. Sleep How long do you want to sleep for? Let's say half a 2nd 0.5 There we go. No. In our update, aliens function, which is where it gets all the way down the bottom update. Aliens, We have to update the parameters here. So we have invaded from fighting settings. Then we have Stutts. Then we have screen. Then we have ship alien. I need add in bullets. There we go. OK, Perfect. No. With interplay game here, we take out our print statement cause we know our collisions are working. What we say is ship hit. There we go. Ship hits what we want to do on the ship hits invaders and Peiding settings. Stats were calling all these Stutts screen ship aliens bullets. There we go. Bullets. There we go. Okay, so what if we just don't? OK, what's going back up to the top? And this is a pretty big file. So we started off here, would import into sleep function from the time module to policy game. The new function ship hit, which is here I'm not going to do is actually just while we're here, I'm gonna clip this out of here. I'm gonna run all the way down the bottom, and I'm just gonna throw it in here, okay? There, Regardless because I'll be talking about the alien update. Aliens function as well. Okay, so a new function ship hit coordinates the response. When the ship is hit by an alien inside chip hit, the number of ships left is reduced by one, after which we empty the aliens and bullets here. So that's pretty self explanatory. We've been following a lot. Next we create a new fleet on Santa to ship, and that's here so you can see create fleet on censorship. Finally, we pause after the update of being made to all the game elements. But before any changes have been granted screen so the player can see that the ship is being hit, the screen will fleas, more screen will freeze momentarily on. The player will see that the aliens has hit the ship when the sleep function ends too cold to move on to the updates green function, which had drawn a new fleet to the screen. We'll also update the definition of update aliens to clue the parameters Stats screen on bullets, so capacities values into the call ship hit. Now what we need to do is create a new method called center ship and added to the end of ship dot p y. So let me just open that. Okay, so here it is here now. So let's just go down to the bottom and let's just say D F center, underscore ship, okay? And we initialize out with itself. We give that the parameter self. No, what we're going to say, I want to say, center to ship on the screen. Nothing out of the blue there. So self dot center equals south dot screen underscore wrecked dot center X. There we go. So let's say that the censorship we set the value of the ship's center attribute to match the center of the screen, which you get through the screen wrecked attribute. Let me just save our game functions file here. Then we're going to invade from pipe and let's run us well, we get a era because I have two periods in here. Every go, Let's make sense. That would be never All right. Jump back in here. Run us. There we go. I'm gonna pause it again. I'm gonna going to do is Come back to you when the aliens are about to hit the ship. I'll see you in a second. Welcome back. So as you confuse, see here I've zapped a few aliens and the aliens air right down the bottom of the screen Now, So let's see what happens when an alien actually hits the ship. So he's running away from that alien there on the left. But let's see now, what happens when he hits an alien? Uh, not gonna. You won't catch up, so I'm gonna have to wait a second after the agents to come back to me. Here we go. Let's see. But the game posits center ships and a fleet re populates. That's exactly what we wanted. Okay, that's perfect. So we're getting more more towards a complete game that resets when you reach the end. When he hit kill all the aliens or when a nail in reach of department of screen and the next set of lectures, we'll look at what we do when the actual alien hits the bottom of the screen. Thanks for listening. And I see you in the next lecture 37. Reacting when an alien reaches the bottom of the screen: Hi, guys. Welcome back. Now this lecture, we're gonna look at what happens when an alien reaches the bottom of the screen to an alien regions bottom of the screen. We're gonna respond in the same way that we do. When an alien hits the ship, we're gonna add a new function, performed this check and call it from update aliens. So let's have a look here now where we start. But I'm in my game functions program I want I want to do is here just beneath the function , creating the last lecture ship hit. I want to say D e f check underscore aliens bottom. So we sound like a doctor. Checked the aliens bottom. So invaders from fightin Comment, Stats, comment Screen Comma ship Comma Aliens come up. Bullets. There we go. No! What's our description? Where we're going to say check if aliens have reached the bottom of the screen the crane s c or e and screen. There we go. No! Okay, so screen underscore Wrecked equals scream. Don't get underscore wrecked brackets. Next we save four alien in aliens dot sprites brackets colon. It's alien dot Wrecked that bottom is great. Lander equals the screen. Correct, Doc Bottom What we want to dio when adding a comment freak dis the same as if this ship got hit. There we go. Ship hit brackets, Invaders from quite and settings. Comments Stutts common screen, Common ship. Come on, aliens, come up. Bullets. There we go Now and then we add in our break right here. There we go. Now we have our next function, which is update eighteens and we're gonna add into this function. So where do we add in? We're gonna add in after our ship collisions. I never should have done Dash. What you gonna do? We're gonna add a comment here, and we're going to say, Look for alien. Look for aliens hitting the bottom of the screen. Okay, so check aliens, bottom. There it is. There. Invaders from fightin steps, Screen ship aliens, bullets. There we go. OK, so the function Check on the score. Aliens underscore bottom check to see whether any aliens have reached the bottom of the screen and alien reaches the bottom of the screen. When it's wrecked up, bottom value is greater than or equal to the screens. Wrecked up bottom attributes. If an alien reaches the bottom we call ship hit. If one alien hits the bottom, there's no need to check the rest. So we break out of the loop after calling ship Underscore Hit, We call check. Underscore Aliens underscore bottom after updating the positions off all the aliens on after looking for the alien ship collisions. And now a new fleet will appear every time the ship is hit by an alien or an alien reaches the bottom of the screen. That's it for the moment. Thanks for listening. And I see you in the next lecture. 38. Game over: Hi guys. Welcome back. Now we're getting very close to what feels like a completed game. But at the moment when our alien ships hit the bottom of the screen or when they hit her actual ship, the game never ends. The value of ships underscore left just grows increasingly negative. So let's had no a game active flag as an attributes game starts to end the game when the player runs out of ships. So as you can see here, I've got my games that file open I'm just gonna do here now is I'm gonna first. I didn't comment and says Start invaders frump Iten in an active state There we go on. I'm gonna follow that up itself. Duck game underscore Active equals True. So you might remember our flags from area running the course. But that's what we have here now. True flag. Now we had a code to our ship underscore hit method that sets game underscore active defaults if the player has used up all their ships. So let's save that on. Let's jump now into a game functions file. Here we are game functions on where we have our ship hit. Here We go here, line 1 to 2 on my screen. So we have our stats here on what we want to say is if steps thus ships left is greater than zero. Let the rest of the function run. So where we have sleep what we say now is else actually, I need increment All this here bump It's not a top. That's a time Top top Top Top Top tab top. It's up now. What we want to say is else Stutts that game underscore active equals bolts. So basically of our ships, if we've run out of ships of which we have tree, then the game is over, So that saved us. So most of the ship underscore hit function where we are here is unchanged. We've moved all of the existing all of the existing code into an If block which test to make sure that the player has at least one ship remaining. If so, we create a new fleet, pause and move on. If the play has no ships left, we set the game at the flag to false OK, that's it for this lecture tank for listening on. I'll see you in the next 39. When different parts of the game should run: Hi, guys. Welcome back. Now, this beautifully short lecture, we're just gonna identify some parts of the game that should always run. So in their invaded from pay, inner invaded from piping game, we need to identify the parents of the game that should always run on the parts of the game . That should run only when the game is active. So as you can see here, I have my my main invaders from pipe and file open on. What I'm just going to do is in my true loop here in my wire loop here. I'm just gonna hit enter a couple of times after the Forest Line. And I'm just going to say if Stutts does game active, so I think you can guess what that means. And then we have the rest of our code here. So let me just take out that line here on. Let me just IND end the rest. There we go now. So what have we just done there? Will in the main loop, we always need to call check events here at line 34. Even if the game is inactive, for example, we still need to know if the user presses cute to quit the game or click the button to close the window. We always continue updating the screen so we can make changes to the screen while waiting to see whether the player chooses start a new game. Breast of the Function calls only the happen when the game is active, because when the game is inactive, we don't need to update the position of game elements. So now when you play invasion, the game should freeze when you've used up all of your ships. So go check it out and let me know how you get on. And, as always, thanks for listening. If any questions let me know and I'll see you in the next lecture. 40. Create the button class: Hi, everybody. And welcome back now. True, the previous lectures, We've really built up a solid play ball game. So what we're gonna do now over the next few electors, it's add some nice tohave to our invaders from piping game on. What that is going to be is a score play button scoreboard play button. The number of ships we have remaining, I suppose, a number of lives we have remaining on things like that, items that will make the game that a little bit more playable and that little bit more user friendly. So let's get started with adding a play button. So right now our game begins, assumes we run our invade from piping file. So let's start the game in an active state and then prompted Player the click of play button to begin. So to do this, we go to a game stats file and enter the following. So here we are in game Stutts. So here let's just say Come in, start the game in on inactive state. Okay, there we go. And I've spelt inactive Wrong. Just put in the tea there. There we go. OK, so what we say self dot game underscore active equals faults. Okay? No, the game should start in in an inactive state with no way for the player to start until we make a play button. So let's get into that. Now You save this file so we're going to first create a button class. So because Pie Game doesn't have a building method for making buttons, we'll write a potent class to create a filled rectangle with a label. We can then use this code to make any button in a game. Here's the forest part of the button class on. We'll save it as a buttoned up ey. So that's quite a new file on, Let's Say, file savers button P White. There we go. OK, so what do we start with? Import pie Game dot fund? Okay, No explanation needed. There. Class button that button brackets cola. Next D e f. Underscore. Underscore i n I t self underscore Invaders from piping settings. There we go. Comma screen comma MSG. I get that now in a few minutes. Cola. So let's just described this initialized button attributes. There we go. Nice. Okay, so self dot screen equals screen Next self dot screen underscore wrecked equals screen. Don't get underscore wrecked. And don't worry, we're gonna go through this, not a few minutes. So next set the dimensions on properties off the button. Okay, so what will that involve? Well, itself dot with comma self dot height equals 200 com a 50. So we're setting the wit, obviously. On the height. Self Doc button underscore. Color equals brackets. Zero 255 Comment. Zero closer brackets Next self duck text test text underscore. Color equals 255 255 255 Obviously can change any days if you want. You have a particular color favorite color in mind. Self. That fund equals pi game play a game dot front does. That's why s fo nt none comment 48. Okay, Still more attributes now a couple of lines. Notre Comment Build the buttons correct object and center it. Okay, self dot Wrecked equals pi game That pie game the wrecked zero comma zero Come up self dot with comma self dot height. Okay, Next line itself dot wrecked dot center equals itself dot screen underscore wrecked dot center Now cut the lines. No comment. The button message needs to be prepped on Lee once. Okay, South Dock Prep. Underscore message brackets Message. Okay, so let's see what we're doing here. Just going to say this now. Forced reimport the Pie game dot fund module, which let's play a game. Brenda. Text of the screen. Then the enough method takes a prominent self. The invaders from piping settings on screen Objects on Message, which contains attacks with a button at Line 12. We set the button dimensions and then reset button color to color. The buttons. Wrecked object. Bright green on the onset. Tax on a score color to render the text and white. We didn't prepare a fund attribute for rendering text. The known argument tells Pie Game to use. The default fund on 48 determines the size of text to send to the button on the screen. We create a wreck for the button on set. It's center attribute to match that of the screen pie. Game works with text by rendering the string you want to display as an image we call prep underscore message. The handle this rendering. No, look, Let's look at the code for a message so out in a couple of lines and say D E F prep underscore MSG brackets and SG Colin. So what's this going to say? Well, we're going to say Turn our message into a rendered image and center de text. There we go. So what's involved in that? Well, let's go Self dark message. The self tough message underscore Image equals self dot front render brackets and SG comma True. Come up itself The text underscore color comma self that button underscore color. Okay, new line itself. That message left M s g na self thought message msg underscore image underscore Wrecked equals itself dot Message msg for short image, not get underscore Wrecked brackets New line self dot message underscore image underscore wrecked that center source Entering it now equals self dot Wrecked underscore center Okay, so the prep underscore message method needs a self parameter on the text we rendered as an image the cult of fun thought render turns the tech stored and message into an image which we dense door in message underscore image. The fun thought render method also takes a boolean value to turn initializing honor off What is initializing Well, initializing makes the edges off the text smoother. The remaining arguments are the specified fun color and background color we set initializing the true and said the background to the same color as the button. We don't center the text image on the button by creating erect from the image and setting it center. Attribute to match that of the button. Finally, we know create a draw but mattered that we can call to display the button on screen on How do we do that? Well, a couple of lines DF draw underscore button self brackets spate up cola. Now it's troubling a comment, and so we know what we're doing. Drew a blank button and then draw message self dot screen dot fill south dot button color comma self dot wrecked close brackets. Then we have a new line self dot screen that b l I t self dot m s g underscore image comma self dot message underscore image underscore wrecked closer brackets. So what does that do? Well, we called screened up filled to draw the rectangle portion of the button. Then we call screen dot bleidt B L I T. To draw the text image to the screen, passing at an image and then direct object Associated with the image. This completes the button class. Now in the next lecture will look at drawing the bottom to the screen because I think we've done enough now for this lecture. So time for listening on. I'll see you in the next lecture. 41. Creating a play button: Hi, guys. Welcome back. We're continuing straight on from the last lecture on. We're going to look up now, drawing or button to the screen. So we use the button class we just created to create a play button on because we need only one play. Button will create the button directly in their invaders. Frump iten file here. So let's jump into this file on where do we start? We start at the very top on beneath Game Stutts, where the game starts its here. So let's say we're going to say from button import button. There we go. Okay, Next that's added here. Invaded Frump Iten. Next comment. Make it play button. There we go. So what we say what we say Play underscore Button equals button brackets, Invaders from plate and settings. Common screen. Come up. What are we going to say? What we want to say? Play. No, you can have it, Stark, Begin. Whatever you wanted to say. There we go and close our brackets. Okay, so next down below. Here, now in a wire loop where we have updates Screen here. Line 44 on my screen. So we have invaded frump Iten, We have screen. We have ship on in between ship and screen. We want to say Stutts, here we go. Come up And then after bullets, we want to say, Play button. There we go. Let's save that for a moment. So we import button. I'm Creighton instance called Play button and then we passed Play button toe update screen So the putting appears when the screen updates. Next we modify update screen so the play button appears only when the game is inactive. Now we have some more changes to make, so let's jump into a game functions file. Here we go on. What we need to do is we need to go into our updates screen. So where is our update screen? Let me have a look here. This fun is huge. Updates green. Here we go. So we need to make some changes here. So we have invaded on piping settings. We have screen. We need to add in Stutts. Come up then. We have ship alien bullets. I will need to add Ian Khama Play button underscore Play button. There we go. Okay, So for two down here, let's add in after aliens dot Draw that screen so adding a couple of spaces And what we want to say is Comet, draw the play button. If the game is in active, why inactive? I would see now in a few minutes. So if not Stutts Dust Game underscore Active colon. Play button dot drawer Underscore button brackets. There we go. Okay, lets saved us. So to make the play, but invisible above all our order elements on the screen we draw after all the other game elements. Now let's run her game and see if we can see a play button. So let's have a look back into our file here. That's fingers cross and let's hope for the best. No typos, no nothing. A typo! Typo, Typo, where is this type of? I know where it is, where we would just wear. So let's see invaders frump! Iten, I guys, I'm back now. It took me a good while to find his error. So in my game stats file, I left in these two lines, which I should not have done, but I simply should have done is not write the code that I did because I am getting caught up in my own notes and I sort of just changes defaults. There we go. Save that on then, in my invaders from pipe Main file, I forgot to end and update screen. So once that's done and then I run my game, I have my play button. So tell me that does not not cool. And I'm pressing the buttons here. I can't do anything because the game is in an inactive state. But in the next lecture, we'll look at making this play button active and then our game of common life. So thanks for listening. Apologies for the error on what I'm gonna do is in the resource of section off this particular lecture I'm gonna add in all the cold files because, to be fair, we've put in an awful lot of cold enough a lot of files into this lecture. So it's handy toe have the hard code files, for your resource is so thanks for listening. And I'll see you soon 42. Making the play button work: Hi guys. Welcome back. Now, just on the last lecture, we underplay button, but at the moment or play. But it doesn't do anything. We can't actually start a game anymore. So to start a game, we need to add the following code or game functions file, which will monitor and mouse event over the button. So we need to jump into a game functions file here on just at our check events. Here we go. Check events, invasion piping settings, screen need to have stutts. We have ship, but before shipping how we insert play button Because that's the most over event we're gonna watch for. Okay, so that Stop it there. Now, what we need to do is we need to add in a new l. If statement So just here. Let's say Aleph event stop type is a pie game. That mouse button down. Okay, so that's what we're watching for. We're watching for a click of the mouse button down, So mouse underscore X com a mouse underscore. Why equals pi game. That mouse don't get p. O s underscore P o s brackets. Okay. And then finally in this line, we want to say check underscore play Underscore BUTTON STUTTS COMMENT Play on the score button COMMENT Mouse underscore X com A mouse underscore why there we go now. Next. What we want to do is we want to add a new function, owns a D F check play on the score, play underscore button. Okay and then open up brackets. I'm up parameters. We want to give it. We want to say stats, stats come up. Play underscore button Common mess, X comma mouse. Why there we go closer brackets Colin. That's out in their description, and it simply says, Start a new game when the play button is clicked. Here we go and what we do. So if play underscore button dot wrecked dot collide points. Most ex comment Mouse White colon stats Does game underscore? Active equals True. Okay, there we go. So we update the definition of check events to accept the stats on the play button parameters. Will you start to access the game at the flag and play button to check whether the play button has been clicked? Pie game can detect a mouse button down event when the player clicks anywhere on the screen , but we want to restrict our game to respond to most clicks only on the play button. To accomplish this, we use pie game that most that get underscore P. O S, which returns a tube, will contain the X and Y coordinates off the most coarser when the moat, when the most button is clicked, we send these values to the function Check Play button, which uses which uses Collide Point to see if the point of the mouse click overlaps the region defined by the play buttons wrecked. If so, we set game after the true and the game begins. Now what we also need to do is jump back into our invaders from fightin Main file here on. We need to update our call to check events on past two. Additional arguments starts on play. But so down here, while true check events screen. Let's add in here after screen stats, comma, play button, underscore button. There we go and then ship on bullets. Okay, so let's say that and thats resume our game again. Command and be on. Hopefully when I click this, nothing happens. Well, that's really good. Let me have a look here for a moment on the play button didn't work that time because instead of saying game active in my new check underscore play on the score button function , I said Gain. So game saved us from back into my file here, my invaders from fighting file command and be play There we go. Beautiful. Our play button now works. So I leave that to you to test in the next lecture. Will have a look of resetting the game every time the game ends. Thanks for listening, and I'll see you in the next lecture. 43. Resetting our game: Hi, guys. Welcome back now, as you can see here on the screen from our last lecture to play button works, and it works prayerfully when the game runs for the very first time. But the game at the moment does not reset after game has ended. You see here I'm hitting play and nothing is really happening. So what we need to do now is change around a few bits of code to reset our game in its entirety every time to gay men's. So let's have a look of doing that. Now let's jump into our game functions file. Here we go on, Let's go to the function we create in their last lecture check play button. I don't need to add in some more information. Some more parameters here. So let's start off with invaders from piping settings. There we go. Comment screen starts play button. We want to add in here. Play button with a want ad in ship. Come a aliens. Come a bullets? Yeah, Mouse X maps. Why? Ok, there we go. So, like I said, the start of the lecture, we have to reset everything. So what does that mean? Right where we can leave our if statement in place here at Line 46. Let's leave. Done as it is. No, let's insert a new line. Would a comment on Let's say, Reset the game. Stutts. There we go. So let's change this now to stats dot Reset underscore stats, brackets. Okay. And the next line and is the line of just deleted. But anyway, no harm starts dot Game act of equals True. Okay, now what's next? Well, Dan, what we want to do is we want to empty the list of aliens and bullets. Here we go. And how do we do that? Well, we say aliens does empty brackets, bullets that empty brackets. Next, What you want to do? We want to create a new fleet on center the ship. So, as you can see here, what we're doing is resetting everything back to kind of zero back to the very start of the game. So create fleet invaders from fighting settings. Come a screen common ship, common aliens, and that's it cause their brackets. And then the next line ship dot center underscore ship brackets. Okay, just to recap, we update our check on the score play underscore important function, so it has access to our invaders from piping settings are starts our ship are aliens under bullets. It needs these objects to reset the settings that have changed during the game and to refresh the visual element of the game. So it looks like it's a new game starting here. At the beginning, we reset the game. Statistics would give the player tree new ships. Then we set the game active the true, so the game will begin as soon as the cold and dysfunction finishes running. We also entered the aliens and bullet groups and create a new fleet and censorship. The definition of check event ultimate to be modified now, as does the call to check on the score, Play on the score button. So still in our game functions file. Let's go to our check events, which is here on where we out of the line in her last lecture at Line 42. Here, check on the score and score play button. We say space invaders from fighting settings. Common screen. Then we have comma stutts play button. Add in the ship. Come up, aliens come up bullets. There we go. Then finish with Mouse X Men's white on the definition of check events needs the aliens parameter, which will pass to check play button. We then update to call the check play button so it passes the appropriate arguments on Let's have a look at us Invaded from piping main file here. Actually save our game functions. File and range from piping. Here we have our check events, so he has settings. We have screen. We have stats. We have play button. We have ship now we need the audience. Aliens comment, bullets. Okay, so let's have a look now. Let's save that. Let's build our game. We get an error, Okay? Why do we get in there? Check events is giving us an error. So let's have a look at that. I'm getting an error because back in my game functions file in check events for some strange reason, I left out aliens, so I'm not exactly what it this morning. So apologies for dust. So we put in aliens, common bullets, comma bullets there. Let's save that. Let's run a game again and see what we get. There we go. So I hit play. What I'm gonna do is I'm going toe. Let this game run past the video and come back when the aliens, I just about the hit the ship and we'll see what happens. A game should hopefully reset. OK, guys, So welcome back. So as you can see, your aliens are moving down the screen and we're honored turd life Here are turret ship. So let's see what happens now when the aliens hit the ship, which is gonna happen now very quickly, but the game reset. There we go. OK, that's good enough for this lecture on What you might notice is that the aliens are moving across the screen and down to screen very fast on what I've done is I jumped into my settings file and just for the purposes of testing, I've increased the alien settings speed settings here, So increase the speed factor and increase the drop speed. Because when we're testing, we don't wanna be waiting for all the aliens to follow down the screen. We wanted to see them fall straight down, do our testing on move on, and we can adjust these speed factors the speed settings when we're finished our game development. So that's it for the moment. Thanks for listening. And I'll see you in the next lecture 44. Deactivating the play button: Hi guys. Welcome back now as we know where Play Button is working away and that's gonna see her aliens is still moving out of quite a quite a decent speed. But one thing that's happening is when we click on the screen, our game is resetting no matter where we clicked. So what we need to do is we need to deactivate the play button so that when we click on the screen, nothing happens. So let's jump into a cold and see how we do that. Now let's go to a game function stuff p y file on in every function. Check on the score. Play on the score button. Let's add in the following button. Underscore. Clicked equals play on the score button. Direct Dark Allied Point Mouse X comma mouse white What happened there at Mouse X Comment Mouse way. There we go. Next. We want to modify our if statement just below here. And what we want to say is if button underscore clicked and not stutts dot game underscore active colon. Okay, so what are we doing here? Well, the flag button on the score clicked stores a true or false value on the game of restart only of players clicked on the game is not currently active, so go ahead now start a new game session and test this behavior and let me know whether or not it works in the comment section. Thanks for listening, and I'll see you in the next lecture. 45. Hiding the mouse: Hi, guys. Welcome back. Now here's something that may or may not be annoying you. When we start a game, we can still see our mouths courses. So that might not annoy some people. Doesn't really annoy me, but let's see how we can hide a mouse coarser when the game is active. That's goes a game now, and let's jump on over into our game functions. File. Here we are. No beneath where we added this line of code. Inner last lecture. If Putin collector if statement here, let's add in a line here. Let's add in a comment that's adding a comment. Here we go. Hide the mess. There we go. So pie game dot mouse Ah, moist mouse that set underscore visible False. Okay, what is wrong with my spelling today? I have no idea. Policies for the poor spelling. Okay, so passing faults to set underscore visible towns Pie game to hide the chorus or when the mouse is over, the game window will make the car. So reappear once the gay men so the player can click play to begin a new game on. Here's the cold To do that. Let's find our function ship underscore hit contact. Remember where he put that? Now this file. It's so big Ship hit all the way down the end. Yep, there we go. No, where we have our else. So if on else else false out in a new line pie game, don't mess dot Set visible. True. There we go. Let's saved us. Let's jump back into remain fire. Let's start a new game. There we go. Let's hit play and a mouse is gone. I move my mouse around and actually we look at the bottom of the screen here you can see my mouse, but when I move it on to my game screen, it's gone. That's perfect. Excellent. There we go, working away perfectly. OK, so that's different at this lecture. Thanks for listening, and I'll see you in the next 46. Refactoring bullets: Hi, everybody. And welcome back. Now, before we go any forwarder and the next few lectures where we'll be making changing to our game functions again and our update bullets and are setting to level up the game and increased bullet speeds increase alien speeds and things like this. We're gonna do a little bit of your favorite on my favorite pastime, which is re factoring on the reason re factoring is because in a game functions here we have our update bullets function, which is pretty big at the moment, and it's only going to get bigger. So we're just gonna clean it up a little bit to remove collisions on everything to do with alien in public collisions from dysfunction and give it its own. So let's have a look here. So from Line 93 on my screen from the comment, removed bullets highlight everything up to the function, comment or the function description on Just call it. Don't believe it. Just cut it can so we can paste. Then later. Okay, so there we go. Now what? We want to do it out in a couple of lines on just in line with a four loop to start for four loop here. We want to say check. Underscore bullets underscore alien underscore collisions. I have two hours in there for alien alien collisions. There we go. On in here. We want to give it invaded from fighting settings. Screen ship aliens, Comer bullets. Okay, there we go. Next we want to create our new function. D E f on. It's exactly what we just gave it a moment ago. Or exactly what? We just entered in a moment ago. Check underscore. Bullets on the score. Alien underscore collisions. There we go. We're going to give us invaders from fighting settings. Comment Screen, Common ship. Come on, aliens, come up Bullets. Now we have to give this a description of what we're going to say. Respond. Two bullet alien collisions. There we go. Close that off. Okay. And what we want in here for us, we want in a new comment, and we want to say, Remove any bullets on aliens that have collided. There we go now that it's pays back into code that we caught a few moments ago and just remove these tree comment lines here, get rid of us. So collisions equals pi game. That's bright. That group collide. Yep. Bullets, Aliens. True. True. If length of aliens equals zero. Yep. Destroyed bullets. Great fleet. And then what we can do is we can remove this bullet Start update here. Okay? And I just gonna remove some of the blank space here. There we go. Looks good on. Let me just check this here. Now, before we go on, ruin it. Untested Update. Bullets setting screen ship. Yep. That's good for bullet in bullets Cup? Yep. Bullet check. Underscore. Bullet. Yeah. Okay, All that looks good. So let's save that. And let's just do a quick test and ruin of our game. Oh, and I know why. DOT is just give me one minute because I'm ahead of myself on my notes here. And what happens when we run our game? That's a look. This happens. Bullets come out, but they don't move. And why is that? Well, I tell you exactly why that is because I left out a line of code here which I've left out before, so it's a bit silly. There we go. Bullet stop. Update saved us. Pervaded from plate and ship. Run it again. Play didn't ended ended that bullets loads of bullets. Okay, Perfect. That's a re factoring. So now we're going to jump into actually leveling up per game as we clear the screen off aliens. So thanks for listening. And I'll see you in the next lecture. 47. Leveling up: Hi, guys. Welcome back. Know anybody familiar with the old arcade games will no doubt want you kill all the aliens on the screen or once you defeat the evil giant ape that you move up a level in the game and that's we're going to look at next. In the next few lectures, we're gonna look at leveling up the game, and as we do, the aliens will get faster under drop speed will get quicker. So let's look at how we do that before it's we need to modify to speed settings. And to do that, let's jump into our setting. Stop, P WiFi. Here we are here. We're going to change a few things around here, so let's have a look at what we do. So we're going to go down to the very end for file here. I remember going to say is comment now and then we're gonna add in how quickly the game speeds up. There we go. Okay, So what we're going to see is self dot speed up underscore scale. So obviously that makes sense. We're gonna have a scale of how the game speeds up, so we're gonna settle at 1.5. Now, if this is too quick, we just come back and drop this down. It's too slow. Will come in on wheel, higher it up, and then next self dot initialize underscored dynamic underscore settings. There we go. Close it off in some brackets. That's what we do here is first. We had to speed up on the score scale, setting the control, how quickly the game speeds up. After that, we call initialized, underscore dynamic, underscore settings to initialize the values for attributes that need to change throughout the course of the game. So let's add that function in now. And what we're gonna do is never once a d f initialized i n i t. I eyes that initialized dynamic settings and give that one parameter self. There we go. No, What's the description of this initialize settings that change during the game? Okay, so what settings changed during the game? What we're gonna do is we're gonna copy some of them from here and put them down below. It's let's close off our comment now. So the 1st 1 is the ship's speed. Swears that there is deadline 12 on my screen ship speeds. Let's cut that. Let's out that in here. Okay, So ship's speed next is the bullet speed. Where is that? Here it is. Here, Line 16. Let's take death. Perfect. Next. Alien speed. Where is alien speed? There we go. Line 22. Here we go. Take that. Put that in here. Okay. Next. When I added a comment, we want to say fleet direction. Not in the space there, because all my comment up spaces after the hashtag street direction. Actually, I'm gonna take that from here, so I don't even need to type it out. What we just do is we take these two lines here and we caught them on. We paced him in here. There we go. Okay, so this method initialized underscore dynamic underscore settings sets initial values for the ship. The bullet and the alien speeds will increase thes speeds as a player progresses through the game on, reset them each time to play or start a new game. We include fleet underscore direction in this new method. So the aliens always moved to the right at beginning of a new game To increase the speed of the ships, the bullets and the aliens, each time to play a reached a new level will use increase, underscore speed. So that's Craig New function here. The F increase underscore speed a result brackets cola increase that increase speed settings. Perfect. So what we want to do? Well, let's have a look itself dot ship underscore speed Times equals self dot, speed up scale so we can leave that there. It's not stuff itself that bullet we have bullet speed. Times equals self, not speed up scale. Same is above. And then finally, itself that alien speed factor times equals self dot Speed up we're doing is here. We have the speed settings here, and we're adding them into a new function so that every time we move up a level, the speed increases to increase the speed of these game elements, we multiply each speed setting by the value of speed upscale. We increase the Games temple by calling increased speed and check underscore bullet underscore alien underscore collisions when the last alien in a fleet has been shut down before creating a new fleet. So how do we do that? Let's jump into a game functions file here on where we have checked bullet ailing collisions, which is here just below this line. Let's have a look. We're going to add in, destroy existing bullets, come at speed up and create a new fleet of to comment. And there is So let's just take one out. There we go. So, after bullets, empty invaders from piping settings not increase. So we're calling the function from our invaders from fightin setting file increase underscore speed. Here we go, Brackets. Okay, let's save that. The changing the value off the speed settings ship underscore speed underscore factor on a and underscore speed underscore factor on bullet underscore. Speed is enough to speed up the entire game because they had a real elements that move. Okay, so that's it for that. That this lecture, I think we've enough cold in there to get us going with Open the temple of the game as a player moved through the different levels and the next lecture will look a resetting the speed every time a new game has started. So thanks for listening. And I'll see you in the next lecture 48. Resetting the speed: Hi, everybody. And welcome back now, hopefully have tested your game and everything is working. Okay, if not, let me know in the Q and a section of this lecture. But what will be happening now is that every time you clear the screen of the aliens, the game restarts or resets to a new level on the aliens in the bullets get faster and you ship gets faster. So let me know how that goes. No, this is just a short lecture to reset the speed of our game. So we need to return any change settings toe to their initial values each time the player starts a new game. Or if we didn't, each new game would start with the increased speed settings of the previous game. So to do that, jump into our game functions file and go to her check play button. So that's finer check play button. And just beneath our if statement here, Line 44 on my screen, we're gonna add in a comment a new line. So the comment is reset the game settings, and then the code is invaders from piping settings that initialize dynamic settings going to score dynamic settings brackets. There we go. So every time now you play invaders from fightin and you clear the screen of aliens, the game will start on a new level, meaning that the aliens, the bullets on the ship will move faster. So hopefully it's a little more challenging on. A little more exciting. Thanks for listening, and I'll see you in the next lecture. 49. Creating a scoreboard: Hi, guys. Welcome back. Now, in this lecture in the next several actors, we're gonna be looking at implementing a scoring system within our game. So we'll track the game. Score would have an indicator showing the level and what I want to show how Maney lives or how many ships we have left in the game. So the scores a game statistics. So we'll add a new attribute to our game stats file. So, as you can see here, I have my game stats file open. So that's just have a look here, So reset starts here. So what we want to do is set the score initially to zero. So self dot score equals zero. There we go. So what's next? Well, we have to display to score. And how do we do that? We're gonna have to create a new file, so let me just do it up now. New file. Let me save it. Save as and what? We're gonna call this with this. The scoreboard, The P y. There we go. No. So what's this going to do without a forest in port? Play a game? There we go. Next. That's created class scoreboard brackets Colon. Now what's the description of this class? A class to store and show scoring info? Here we go. Next. D E f. Underscore. Underscore i n I t underscore Self invaders from piping settings Screen on dstets. There we go. So initialize score attributes on close offer description there. So let's start off with a screen. So self dot screen equals screen. Self dot Screen underscore wrecked equals screen dot Get underscore wrecked. We have brackets there to close that off. Self invaders from piping settings equals invaders from piping settings. There we go. I'm self taught. Stats equals stats. Okay. Next, we have to set a fund. So fund four front settings for score. There we go s c o r. Eat. There we go. Okay, So self dot text underscore color equals 30. Common 30 comma 30. Hope you can understand my trees with my Irish accent. Self got fund equals pi game not fund the same fund of user button. That's why S s y s f o N t none on 48. So the same text size are fun size as our Putin as well. No one more comment here. Prepare the initial score image self dot primp p or PP on the score score. Underscore Score could get complicated. Score So because scoreboard writes text of the screen, we can we begin by importing the pie game that fund module. Then we give underscore in it are in it mended the parameters invaders, frump, iten settings, screen and stats so we can report the values were tracking. Then we set a text color on Dunstan. She ate a fund object the turn, the text to be displayed into an image we call a prep score, which we define here. So we're going to do it up now. The D e f Crip underscore score brackets initialized at our side. We give that the private herself. There we go. Let's give this a description. Turn to score into a rendered image. There we go. Next score underscore S t. Or remember, that is strength equals SD or self dot Stutts that score. Close your brackets. Next we have self that score on the score. Image equals self dot front that render score underscore S t or comma True. Come up self The text underscore color come up self I thought Invaded from piping settings dot BG Underscore color. Okay, close offer brackets there, which has done for us with the auto complete. Next, we had, in a comment a couple of blank lines in a common to display to score in the top rice of the screen. There we go. Self not score. Underscore Wrecked equals self dot Score underscore image. Don't get correct brackets. Next self dot score That right equals self dot screen underscore Wrecked dot right minus 20 self dot score underscore wrecked dot Top equals 20. Okay, so can probably guess what we're doing here. But if not, we'll just go. True. So in prep Underscore score, we first turned in America Value stats dot score into a string and then passed this string to render. This creates the image to display the score. Carry on screen. We passed the screen's background color to render as well as a text color well position to score in the upper right corner of the screen and have it expanded left as a score increases and the width of the number grows to make sure that the score always lines up with the right side of the screen, we create erect called score underscore, wrecked and said It's right Edge 20 pixels from the right edge of the screen with them. Place the top edge, 20 pixels down from the top of the screen and then finally, to create a show score method to display the rendered score image. So next what we need to do is we need to create our function or a method for show scores with D E F Show Underscore Score Self. Give this a description. Draw to score to the screen. Okay, itself dot screen b l I t. Our old friend self dot score underscore Image comma self dot Score Underscore Wrecked. There we go. OK, so this method draws to score image to the screen at the location that we previously specified so to display to score will create a scoreboard Instance in our invaders from fightin main file So that's jumping there now And just beneath line eight here on my screen . So game stats of from game stats were going to say from scoreboard import scoreboard There we go. No, it's taken away. My capital s which it would have done Scoreboard. There we go. No, what we want to say in a wrong game functions are in the running game method here, just over stats line when a modified his comment and create a scoreboard. There we go, so we have game starts. Leave that line in place. So let's say scoreboard for short. So SB equals se S B for short scoreboard invaders from fighting settings. Common screen comet steps. There we go. Now what we also want to do is we want to go down here to our while. Loop on in our updates screen line so g f that update underscore screen we have invade from fighting settings. Screen Stutts SB. We import the new scoreboard class on making instance called SB after creating two starts. Instance. We then passed SB to update underscore screen so to score can be drawn to the screen to display. To score, we need to modify our update screen like this. So let's into our game functions file again. Let me just say that game functions. Let's go to update screen. And as you guessed, we have stats. Now we have tohave scoreboard coming. There we go. We also need to do is just here Line 74 on my screen, both to come and draw the play button. If the game is inactive, we want to add in a comment of her own and we're another comment. Sorry, it is our own coming draw to score info. Got a Korean folk? Not a 1,000,000 miles off. Try to score. Draw to score for info. SB that show Underscore score brackets. Okay, Saved us. So he had SPD list of promise not to find update screen and call show scored just before the play button is drawn. Now, when we were in a game, we should see our score at zero in the top right of your screen. So let's take a look command and be Oh, ever and see why we haven't error here. Lying 20 Tree or scoreboard. The P y file. Let's have a look here at line 23. Here we are because I have score on the score string dot Which should be a comment. So that saved us. Let's go back into remain file runners. Oh, another one. No attributes Score. OK, so we have a spelling mistake. Summers let me just policies for a moment. Okay? I'm going to assume this is our error here. Lying 27 just as our error report showed I had done instead of underscore, right, Too many scores, too many underscores that saved that. Let's run it. There we go and for some reason that it's in the top left. But I'll have a look at that. That's a book, all part of coding. I'll have a look at that now and come back to you, so let's see what that is. Okay, so this was an interesting book. So down here in my method prep score, you can see I have self doubt. Score, underscore. Right? But actually, it should be wrecked that right? So an interesting book in that it didn't cause Inara that stopped the game it simply kept are score board on the left hand side of the screen. So let's run our game now again. And there's our score over here in the right hand side, set to zero and actually might notice as well that the score zero is libre cleaner than it was early. Iran. Here we go. That's that bug fixed. Okay, so that's it for this lecture in the next lecture will look at updating our score as we shoot down Iranians. Thanks for listening. And I'll see you in the next lecture. 50. Adding points to aliens: Hi, guys. Welcome back. Now I feel like this lecture is a bit of a watershed, a bit of a turning point, a bit of a momentous lecture, because in this lecture we're going to be able to assign a score every time we shoot down an alien. And that was really the point of these old fashioned single player arcade games. Who got the highest score? To write a live score to the screen? We need to update the value off stats that score and then called Prep on the score. Score the update to score image. But for us, let's determine how many points a player gets each time to shoot down. Alien. So let's jump into our settings out P y. File. Here we go on in our method initialized dynamic settings. Let's art in the following line are the following lines. I should say scoring Okay, so self got alien. Underscore points. So how many points we get? Let's give them Let's give them 100 points equals 100. OK, there we go. Let's save that for the moment. Will increase the point value for each alien as the game progresses, but we need to make sure it at this point value is reset each time a new game starts. So we set the value initialized dynamic settings. Now, what we need to do is it is update the score each time and aliens shutdown on. We do that in our game functions file. So here we are in our game functions. File on inter matted check. Underscore bullet underscore. Alien collisions. So where is that? Here we go. On my screen. Right there on it. There we go. Check. Underscore bullet underscore. Alien underscore collisions. And that's lying. 99 on my screen here. You can see it here on what we need to update here. Well, we need to add in some details into the parameters that are accepted. So we have our settings. We have our screen. We have our ship and what we need to add it now, after screen in between ship is Stutts. There we go. Starts on scoreboard. SB for scoreboard. Come on. Don't forget the closing. Common there. Then we have ship aliens, bullets. Perfect. Next. What we want to say is here. So we have responded. Bullet alien collisions. Then we have our collisions and we have our true so after True, That's crate a new if statement. And let's say if collisions starts dot score plus equals plus equals invaded from plate and settings that alien under score points. Okay, then SB does prep underscore score brackets. Okay, so you update the death definition of check on the score Bullet underscore alien underscore collisions to include the stats on SB parameters so we can update the score on the scoreboard. When a bullet hits an alien pie game returns a collision dictionary. We check right addicted. We check whether the dictionary exists. And if it does, the aliens value is added to the score. We then called prep underscore score to create a new image for the updated score. Now, what we need to do is modify update bullets to make sure the appropriate arguments are passed between functions. So still in our game functions file Goto update Bullets. Here we are just a both my previous funk just above the previous function of my my screen and we have settings screen. Then we add in starts just like we did a few minutes ago. Kama SB comma OK, there we go. From what we need to say now here is leaving the update bullet leaving this here. So where we have check Bullet underscore Alien underscore collisions Invasion Fightin screen That's odd in Stutts sp There we go on that saved us. So the definition of update bullets needs the additional parameters. Statin SB. They call to check on the score. Bullet underscore. Ailing collisions needs to include the stats and SP arguments as well. We also need to modify it to call to update bullets in the main wire loops. Let's jump into remain File here on update Volunteer Line 44 On my screen update. Bullets very from fightin settings Screen. Stutts Kama SB There we go. OK, so now when we play our game, we should be able to start gathering points on beating people. So let's have a look at that. Let me save that there and let's run. Here we go. So play 100 points. Excellent. 200 300 400 on 500. So I think you get the gist there. So that's it for this lecture. Now we are scoring and racking up points on we are advancing up levels, so that really is progress. Okay, Thanks for listening. And I'll see you in the next lecture 51. Increasing the points score: Hi, guys. Welcome back. Now, in this lecture we're looking at, we're going to look at how to increase point values. But before we do that, we need to address kind of book that was built in ourselves into a game on. That is, as our game stands now, at the moment, our code or are bullets Could missile millions, for example, of two bullets collide with the aliens during the same pastoral loop? The player will receive points only for one of the aliens killed. So let's fix this and refined away that alien bullet collisions are detected. So in our check, underscore bullet on the score. Alien collisions. Anne Polyta, Clyde. When alien becomes a key in the collisions dictionary, the values associate it with each bullet Is a list of aliens it has Clyde with. We looked through the collisions dictionary to make sure we award points for each alien hit . Said jump into a game functions file on where we have checked bullet underscore ailing collisions. Line 99 on my screen here. Just move it to the top. There we go. So if collisions So what we want to say I'm gonna change that from an if to four aliens in collisions. Dot values brackets colon and we want to say stats, start score plus equals invaded from piping settings that alien points times, Ellie and aliens. So if the Collisions dictionary has been to find we looped through all values in the Collisions dictionary, remember that each value is a list of aliens hit by a single bullet. We multiply the value of each alien by the number of aliens and each list and at this amount to the current score. OK, so that's that done. Now we're gonna look at increasing point values. So because the game gets more difficult each time a player reaches a new level aliens and later leveled, she'll be worth more points to. To implement this functionality will add cold to increase the point value in the game. Speed increases. Let's go to our setting. Stop ey file on that line 22 here on the comment How quickly the game speeds up. First we have self that speed upscale equals 1.5. I'm gonna reduce that down a little bit. 1.2 actually, cause when I was testing, it is going a little fast next lot. Let's add in a new comment how quickly the alien point value increases. Okay, so there we go. I want to be going out. What code? We give it here. What was a self dot score? Underscore Scale equals 1.5. So it's going up by times and 1/2. Okay. And we leave our line in their self doubt. Initialized underscore dynamic settings. And where we have down here are last method Line 41 of my screen method increased speed. So increase speed settings. Andi alien point values. Okay. And what we want to add in here is last line self, not alien underscore points equals and it's an integer self. That alien underscore Points times self dot score on the score scale. Okay, there we go. And I've left out of assets that core. But let's say score. Otherwise we won't get anything. Okay, so we define a rate at which points increase, which we'll call score scale. Let's go back up here Syria score scale. And just above that, we have our speed upscale. So every time we changed levels, the speed of our aliens increases to make the game that little bit more challenging. But in order to see a notable difference in scoring, we need to change the alien point value by a larger amount. So we given that 1.5 now, when we increase the speed of the game, we also increase the point value of each hit. We use the in function to increase the point value by whole integers, and that's down here. Line 47. Okay, so that's run our game now and see what we get after you jump into my main file. Okay, So I'm gonna pause the game and come back when we're about the level up and see that the scores change. I'll see you back in a minute. Hey, guys, Welcome back. So as you can see her now, I've got to aliens left. So let and I have 1800 points on the board. So when I shoot down these two aliens, I should have 2000 points on the board on instead of our points going up by 100 each time. That value should increase by times and 1/2. So let's have a look. Let's kill these two aliens. Okay? A new level on Let's start shooting aliens there we go times and 1/2 each aiding is now worth 150 points on. As you can see, they're moving fast, but not so far started. It makes the game unplayable. Which a kink with my last setting. 1.5 under alien speed It waas now my tree and 1/2 1000 points. So let's have a look here. Now let's zap all these guys. There we go. They've been zapped and let's move across. Here we go. Now let's start shooting these guys. There we go. Bum bum, bum a bum! There we go. Let's get him up. Mr. Is going to be just ahead of me now each time. Come on, come on, Come on, Come on. He's getting forward and further away from a look at that. Now he's gonna bounce back that send up some bullets. Room. There we go. Now, 5000 on each point is going up looking out on. The aliens are moving quicker. Looking at our points Are rocket skyrocketing up There we go. Let's have a look here now 9 50 Our plants are worth 300 each because we've being going up and up and up Okay, that is perfect. So as you can see now, here are points. Our current scores 10,848. So we're gonna take a look at that in the next lecture. Okay, Thanks for listening. And I'll see you in the next lecture. 52. Rounding the score: Hi, guys. Welcome back. So as you can see here now, I'm in my scoreboard R p y file to this citizen. Nice assured that lecture just around out to score because we had seen in the last lecture their score was something like 10,848 points. So most arcade style games, if you remember, are kind of multiples of 10. So let's follow that rule whenever scoring on round up our score when possible. So I'm in my scoreboard Harpy wife ill and in my method prep underscore score dot self or self and brackets not not self. So beneath for beneath the description turn to score into rendered image We're just gonna add in a line And what we're going to say is round it underscore Score equals int round brackets self dot Stutts not score comment minus one. Okay. And close off our brackets with two closing brackets There on in the next line. We have score on the score. String equals on. What we want to change this to is we're gonna put in here what's called a regular expression on what we want to say here is equal. So then we have open up double quotes. Currently. Bracket colon comma closer curly bracket clothes are double quotes. Does format. Let's remove the remainder off the line. There we go for my brackets. Rounded score. Okay, when we close off the brackets there on the left, the last line there we can leave and play self doubt. Score on the score image. So the round function normally rounds a decimal number to a set number of decimal places. Given as a second argument, however, if you pass a negative number as the second argument round will round the value to the nearest 10 100 1000 and so on. So let's say that I'm not going to run the game. Now. I leave that to you to run and test for yourselves. So thanks for listening. And I'll see you in the next lecture. 53. Adding a high score : Hi guys. Welcome back. So I think you can see now that we have a very, very complete and almost complete game, I think we might say, But there's something still missing. There's a couple things still missing, but the first thing that we're going to look at is I suppose, what every player really wants to know. And it's what kept me and my friends interested in these kind of games. When we were using an Atari 2600 we were kids, and that is the high score. So who has the high score? So let's look at that now and adding in a high score to our game, let's jump into a game stats file and our first method DF in it. That's odd in their last line comment, High score should never be reset. Okay, so self dot high score Hi underscore score. Be glad when this is finished because I don't have to say underscore score anymore equals zero. And let's say if that so because the high score should never be reset, we initialize high score in our method in it Method rod land in reset starts. Now we'll modify scoreboard to display the high score. So let's jump into our scoreboard. Let's start with the innit method. So here at Line 18 prepared the initial score images. Let's add in under that forced line itself dot prep Underscore. Hi, underscore Score. OK, brackets. There we go. Now the high school will be displayed separately from the score, so we need a new method. Prep Underscore High underscore score to prepare it the high score image. So let's create now the high score. The prep underscore high on the score score method. So still no scoreboard lets out this in last or go to the last line. And then let's go. New method D E F. Prep underscore. High on the score. Score. Okay, brackets, self close brackets colon. So that's out in description. Turn the high score into a rendered image. Okay, close off that description. Next high underscore. Score equals and round brackets. Self Does stutts dot high underscore. Score, comma minus one. And why are we doing that? What? We're doing that to round out the number the high score like we did in the last lecture. Okay, Next, we have high on the score, score on the score. Strength equals double quotes. Currently. Bracket Colon comma Closed Pretty brackets closed over quote dot format brackets high underscore score. Next line. Self dot high score Underscore Image equals self dot fund stuff Render hi underscore String underscore score underscore String Comma True Comma Self Doc text underscore Color Comma Self that invaded from piping settings dot BG Underscore color close brackets. Next, we out in a new comment center. The high score at the top off the screen. Okay, there we go. So how do you do that when we say self dot Hi Underscore Scored that wrecked underscore wrecked Sorry, Made that error when we're doing Our scoreboard in a previous lecture equals self dot hi underscore. Score on the score image dot Get on the score wrecked brackets. Next self not high. Underscore. Score on a score wrecked dot center X equals self dot screen. Underscore wrecked dot center X. Okay, and in the last line here, self dot high underscore score wrecked or is my wreck? There it is. There. Docked up equals self dot score underscore. Wrecked stuffed up. Okay, let's save that. So we round a high score to the nearest hand on format it with comments. We then generate an image from the high score scented a high score wreck horizontally on, said its top attribute to match the top of the score image to show underscore score meant that no draws the current score at the top right on the high score on the top center of the screen. So what we need to do now is modify our show score. So where we have to first line their self doubt screened up BLT, we need add a new line self not screen. Not be lt b l I t self not high score. Hi Underscore Score on the score Image comma self dot Hi. Hi. Underscore Score wrecked close brackets. Okay, that saved us. So to check for high scores will write a new function check underscore high underscore score in our game functions, not p y file. Let's go to our game functions people I fun scroll all the way down to the end on let's say d e f don't forget to invent d e f check. Underscore high underscore score brackets. What Prime Minister? We give that when we give that stutts SB. Okay, Colon, what's the description? Check to see if there's if these. If there's a new high score. OK, don't that's like period and then close off that description? So how do we do that? We say, If stats dot score is greater than stats does high score. So this should be pretty at common to you now are pretty readable to you now. So if our stats dot scores were first score is greater than our high underscore score. Well, then stutts dot high score equals stutts dot score and then finally SB dot prep one to score high on the score score. OK, so there we go. That saved us. So the function check underscore High on the score score takes two parameters starts and SB . It uses Stutts to check the current score on the high score, and it needs SB to modify the high score image when necessary. We then check the current score against the high score. If the current score is greater, we update the value of high score and called prep. Underscore high underscore score To update the image of the high score we need to call check Underscore High on the score score each time and aliens hit after updating to score and check. Underscore Bullet on the score. Alien underscore Collision started a mouthful, so let's go to that now let's go up here. There were a check bullet. Alien collisions. So where we have SB prep score There we are there. Line 106 on my screen. Intend one back and say Check underscore. High score. There it is, My auto complete brackets. That's Kama SB. Okay, so the first time we play our game now our score will be the high score. So will be displayed is both the current and high score. But when we start the second game are high. Score should appear in the middle of our current score. OK, so let's give that a go. Then we saved this on Let's run it. So here we are in. So as you can see, I have zero high score on zero actual score. So let's play. Let's shoot down a few game. Let's shoot down a few aliens bump, bump and we go. So high score is does now. Every time we have a new game, we'll have a high score. Excellent. So it really is starting to look like a proper old fashioned arcade style video game, which are all the rage now, as I'm sure you know. OK, so thanks for listening, and I'll see you in the next lecture. 54. Displaying our level: Hi guys. Welcome back. Now we almost have a complete game on. I think we can see riel progress. Well, obviously we can see real progress because the game is pretty solid now. So what's the next thing that our game should display or should do? Well, how about showing us what level we're on? So the display to players 11 game before us needed attribute in game starts representing the current levels. Let's jump on into a game start, stop, ey file on. Let's have a look in here. So we're coming out of in here, okay? We have our high score zero that we added Previously on we have our score zero. So stats that can change during the game. Okay, so let's add in. Obviously, this is a stop that will change during the game as we level up. So let's add in self dot level equals one, because we're always gonna start at level one. Let's save that so they have to score board. Displayed a current level, will call a new method prep level from our innit method, says jump into a scoreboard on in her in of Method at the top. Let's have self the prep prop prep A prep on the score level. Sorry, I'm getting excited because this is a good one. Toe have and prep level. Okay, let me save Dutch Now. We need to create the money we need to create the matter now. Prep levels. Let's doing that down here. Don't forget in Denver. One back. DF prep Underscore level brackets colon. So what are we going to say here in our description? Turned the level into a rendered image? There we go. Self dot Level on the score. Image equals self dot front render S t or for string self dot Stutts don't level there we go close off that bracket there, put in a comma, then say true comma self, not text underscore Color comma self dot invaders from piping settings dot BG underscore color. There we go. Close off a bracket on new line, actually, two new lines now. Another comment What we want to say here, position the level low to score. OK, so it's just going to sit or hover right below our score on the right hand side of the screen. And how do we do that? We say self dot level on the score wrecked equals self dot level on the score image, not get wrecked. Brackets self dot level underscore. Wrecked stuff right equals self, not score wrecked. Underscore. Wrecked stuff, right and then self dot level underscore. Wrecked docked up equals self dot Score underscore Wrecked stop bottom plus 10. Okay, so what does that do? Well, are new method prep level. Create an image from the value score from the value story and stats that level and sets t images right Attribute to match the scores right, Attribute it then sets the top after it 10 pixels beneath the bottom of the score image to leave space between the score and the level and we ultimate update our show score. So we saved out. Let's go to our show score. So here it is. Here it lying. Turney tree on my screen d e f show score. So what we want to say here we want to say self dot screen that b l I t self dot level underscore image comma. Uh, I am a g e comma image. Comment itself dot level wrecked. Underscore wrecked. Okay, so we can closed out with a bracket and save it this adds a line to draw the level image to the screen. Will increments stats that level and update the level image and check on the score. Bullet on the score. Alien underscore collisions. And, as always, that is an absolute mouthful. So let's jump into our game functions. Here we are in line 99. Check on the score. Bullet alien collisions. Okay, so here we are, online 109 So what this comment here tells is we're going up a level you re just gonna update is coming to say 50 alien fleet is destroyed. Move up one level. Okay, so that makes more sense. Another game has evolved slightly than we have. Bullets are empty, invade from fighting. Setting speed. Okay, Now we're gonna do is just adding a couple spaces. Their new comment increase level. There we go. What we want to say here where we say stats don't level plus equals plus equals one. There always starting up one, then scoreboard dot Prep Underscored level. Close it out with some brackets. There we go out into space. Just separate the last line. So if a fleet is destroyed, we incremental value off starts that level and called prep underscore level To make sure the new level is displayed correctly to make sure the scoring a level images are updated properly at the start for a new game. Trigger reset when the play button is clicked on. How do we do that? Well, let's go to our play button method here. We aren't lying 41 of my screen check play button. So we have invaded from fightin screen stats. Now we need to add in scoreboard Come it, then we have play button ship, aliens, bullets, most ex most. Why, that's perfect. And where we have stats that game underscore active equals. True. Let's add in a new comment. Make sure the indentation is in line preset. The scoreboard underscore. No underscore here, but this is a common images. I'm so used to saying, Score on the score. Underscore images. There we go and what you want to say. SB dot prep Underscore score brackets SB dot prep Underscore high underscore score. Ghana. Such underscore score is such a hard thing to say sometimes. SB Duck prep underscored level. Okay, that's dust. The definitions of check underscore play underscore Button needs TSB object to reset the scoreboard images we call prep Underscore Score prep on the score. High score and prep underscore level after resetting the relevant game settings. Now we pass SB from check events, so check underscore. Play underscore Putin has access to the scoreboard. Object. Let's go to our check events method here. Yo, check events. Stutts SB come up. There we go, then. Here, where we have Elliff. Event type equals most buttoned down. So that's our last Elliff statement Here lying 37. We want to update our check on the score. Play underscore button line here Settings screen Stutts SB coming. Say that the definition of check on the score events needs SP is a prominent So to call the check on the score play on the scoreboard and can include SB as an argument. Finally, we update the call to check events in our main invaders from pipe and file. Here, Check events. Here we go. Here, Line 40. Check events screen Stutts As you guess It s be coming that saved us. Okay, so let's ruin our game and see what level were on. Ah, error. Okay, being a few lectures since we have had an error. So let's see what this one is. For the moment, this was very simple. The new method that we wrote in our scoreboard file DF prep On the score level. I never gave it a parameter self. So that saved us back into invades from fighting. Let's run again. Another one. Let's have a look. Let's have a look guy. So we found the two errors that we were getting there on here they are here. So for us on in scoreboard, self doubt crept up level where we entered down in. Just take it from beneath the stats line here. So take it from Line 12 on my screen Commander next and put it into line 21. There we go. Say, if that's let me remove one. The white lines there. Perfect on in game functions where we hade sp dot prep underscore level. I haven't put in any brackets, so just make sure you have brackets in there as well. Now, just a little side note. You might be wondering why I don't run the game for us to make sure that everything works correctly. Well, as we can see in most lectures, nine out of 10 lectures. They run perfectly fine. But what I want to show you, the users are you DeLay, Earner as well is a student is that errors do happen. So don't just stop. Because when I was first learning the code, I would just stop coding if I got a bog. Think there's something wrong and maybe never come back in a problem. But some problem can take seconds, minutes or hours, or even days to figure out some bugs. So I just want to show you that books do happen, that you have to go through each line of code, sometimes each file to find what it is or what went wrong on. Then try and try again. So in this instance, now I found the two problems I save my file on. Let's go in and run it. There we go play, and, as you can see here on the right hand side, I'm on level one. So let's see by play, let's have a look of killing all the aliens. So our policies and then come back and make sure that we're on level two. Okay, guys. So here we are back on. We're about to go to level to. Hopefully, we're about to go to level two. So let's have a look. Have a one level too. Absolutely brilliant. Okay, so they re gold games running that just a little bit faster on our level now. Level to our points. Have a point value have also increased. So there we go. So thanks for listening. And I'll see you in the next lecture. 55. Adding lives: Hi, guys. Welcome back and welcome to what is going to be our last lecture off this lecture. Siri's discourse. Now we've come an awfully long way, and we have a very solid game. And we've used from our basic places fightin understanding to create this amazing game with the fundament of which you can use to create your own game or your own programs in time and with practice. So what we're going to do now in this lecture is we're going to display the number of lives or the number of ships that we have left. So the kickoff open up your ship docked P Y file and up the top where we have import pie game, we're going to say from pie game, that's bright import Sprite. There we go. Now what we want to do is we want to just add in some details to our ship class where we have initialize our ship on said it's starting position. We want to add in super bracket ship with a capital s self bracket. Does underscore underscore. Hi. And I t brackets. There we go. So here we imports bright and make sure ship in Harris from Sprite on called Super at the beginning of in It. Next, we need to modify scoreboard to create a group of ships we could display. And here's the import statement. So that saved us. Let's go to our scoreboard file on up the top import pie game. Let's just say as well new line that you're going to length from Hi game that's bright. Import in in Port Group, New line from ship import ship with a capital s. There we go. No, Where we have self taught prep underscore level, which is here. We wrote that in the last level in the last lecture. Sorry. Get my lectures on my levels Confused on lower spores As you progress through your lectures It is like moving up a level prep. Everything gets harder. Underscore ships, brackets. There we go. So because we're making group of ships, we import the group and ship classes we called We called prep Underscore Ships After the call to prep on the score level on Here's our prep underscore ships method so still in her scoreboard file all the way down to the bottom. Don't forget to invent back won t e f prep underscore ships brackets on. Let's give itself. There we go. So what's this going to say? Show how many lives we have left? Uh, space. There we go. First up on close out the description. Self taught ships equals group brackets. New line four ship underscore number in range. Brackets self dot Stutts. I thought ships underscore left. Okay, colon ship equals ship BRAC itself that invaded from fightin settings have stopped saying settings. The last few lecture you might notice because I think you're familiar with data that phrase by now. Come up self dot screen. There we go. Make sure bracket are closed off. Ship got wrecked dot Exe equals 10 plus ship underscoring number times ship das recht dot with Okay, New line ship dot wrecked dot y equals 10 self. I thought ships that ad brackets ship. There we go. Okay. So the prep ships mattered. Create an empty group. Self dot ships toe hold the ship's instances. The film this group at Lou Bronze once for every ship the player has left inside the loop, we create a new ship and set each ships x coordinate values. So the ships appear next to each other with a 10 picks of marriage in on the left hand side of the group of ships. We set the white quart and Value 10 picks it down from the top of the screen, so the ships lined up with the score image. Finally, we add each new ship to the group ships. Now we need to draw the ships to the screen. So still in our scoreboard file where we have show score. So I'm scrolling. Appear to line 36 where we have the method show score. OK, comment. Draw ships gift at a Capital D drawn ships. There we go. Self dot ships that draw south dot screen closer brackets. Two spaceships on the screen we call Draw on the group and play Game draws each ship to show the player how many ships they have to start with. We call prep underscore ships when a new game starts. We do this and check on the score play button in game functions, so jump into a game Functions file here were here last one on my list on Let's go to our check on the score play button in our method. Check on the score. Play on the score button. We do the following that line 59 beneath SP Dark Prep On the score level, we had SB dust Prep underscore ships Brackets don't leave out the brackets like they did in a previous lecture. We also call prep Underscore ships when a ship is hit to update the display of ship images when the player loses a ship. So still no game functions filed, make sure it's saved where we have our method Update aliens Here we go. Second last method In my file we have invaded from fightin Stutts Screen SP comma, then here at Line 210. If pie game that's bright. We have ship on the score. Hit invaders from Biden Stutts SB There we go, then further down where we have to comment where it started to come. Look for alien ship collisions. We have check on the score aliens bottom and we also add in SB after stats as b comma. Say that again. No, where we have our ship hit method. There we go just slightly above on my screen line 1 70 Treat We have Stutts Kama SB coming . Then we want to add in a piece here So where are Deke? Remain? Ships on the score. Left line 1 78 Let's out in a couple of blank lines there and let's on in a comment Update ! Scoreboard, SB the prep. Underscore Ships brackets. Okay, so we first at the prominent SB to the definition of update aliens. We then passed SB to ship. Underscore. Hit on Chek Underscore. Aliens underscore bottom. So each has access to the scoreboard. Object, Then if you update the definition of ship underscore hit to include SB. We called Prep on the score ships after decrease in the value of ships left. So the correct number of ships is displayed each time a ship is destroyed, there's a call the ship hit and check on the score. Aliens bottom! So update that function as well. So that scroll down here. Check on the score. 18 bottom Stutts Kama SB coming. There we go and then forward it down. We have ship hit, invaded from fightin starts SB There we go. Saved us. Now check on the score. Aliens on the score bottom except SP is a prominent when we add an SP argument in the call to ship Underscore hit. Finally we passed SB in the call to update aliens in our main invaders from pipe and file. So update aliens here it line 45. Screen ship very from fighting settings starts SB coming. Okay, so that's runner game and see what we get. We get an error, and it wouldn't be right to have our very last lecture without an error. Just for good time's sake. So, what have you got? Scoreboard line 70 in prep ships. Scoreboard line 70 in prep ships. Okay, we'll hear it. Line 70. Our ship, because of my older cracked was a small was a lower case s or just corrected that. Now that's running again on another error. So let's see something. What are Sprite? Sprite up. Ey in Sprite. Okay, so what? Hillary in scoreboard prep ship. Okay, guys, but we needed to do waas inner ship toppy wife I'll where we have class ship. I neglect the Odeon Sprite here. There we go. Now let's run a game again. Save that Ruin the game. Okay, ships at the top, Let it plate. And there we are, pumping Boom! We have tree lives. So let's lose a life. So when I'm gonna do is pause this. Let the aliens dropped down to the bottom of the screen on, then come back and see if one of our lives decreases when we get hit by some aliens. Okay, so let's see you in a minute. Are aliens hitter ship on. What happened? Was the game immediately exited on? Brought up this error. So let's see what this error is. So we're back. And as you can see, the game is working away beautifully. And I'm on level eight under aliens of falling down pretty quick. I resolved the error we just had, and now I'm going to let the ship get hit. I'll show you to fix it out, Eric. Just now. In a moment. There we go. And as you can see in the top left hand corner, I am now down one ship. That's exactly what we wanted on. Now it's working away perfectly. So we have now implemented a live system. Lives L I V E s not live as in live TV or live radio whenever you might want to call it. So now, as you can see are aliens are absolutely flying down the screen. I'm on level nine bump. There we go, Level 10. And when I'm gonna do here now is I'm gonna play out this game because what I'm gonna ask you to do is see what level using the same settings that I have here you can get without losing all your life. So I'm on level 10. You can see my scored every goal. 11 life, one life left. So let's have a look. Yeah. I don't think this was gonna go to out for me, to be honest. No, I'm dead. I'm dead. There are one. Life is my last life. Level 10 this is about around is about around. There we go. Here we go. Level 11 still have no lives left. Let's have a look. I'd like to see if you can do any better with your game, So let's see here. There we go. Level 12. High score. Go on. Go on. Go on. Go on, Go on. Never hurting. And they're really moving now. Come on, Come on, Come on. Boom! Level 14 of nearly half a 1,000,000 points. Push! My God, they're moving two left. Oh, come on, I got you. There we go. My best score. 1,767,550 on level 15 with tree lives used. Okay, guys, that's it. We have created a game from scratch and piping. We started off with absolutely nothing on. We finished with a very playable a very professional on a very well finished and bug free game. On what? The book that in the last time when we run the game, we had a book. What? That WAAS And in our game functions file check Aliens bottom update. Aliens. I just need to rearrange so invader from fightin screens that SB varies from fightin screen starts SB and also in our calls here. So just go ahead and rearrange them and make sure that in ship hit as well, they're in the same order, your prominence or in the same order. So that's it, guys, Thanks for listening. As always, reverting questions. Please just let me know in the Q and A on I look forward to receiving your game files and see how far you can get toe what level you can get and what score. Thanks for listening on. I'll see you again soon.