Game Development 1-2: Working with Sprites
Skills:
AI Pair Programming50%
Key Takeaways
Works with sprites to create objects that can move around the screen using Pygame
Full Transcript
hi everyone welcome to the second video in the series game development with py game please make sure that you've done lesson one already because in that lesson we explained the basics of py game how it works and we built a template for py game a skeleton of the code that we're going to need in every pame project we do in this lesson we're going to be talking about Sprites which are how you make objects that move around the screen okay to begin with I have my pame template which we made in lesson one and if you remember that all it does when we run it is create a nice blank black window that does nothing but we can close it and that's all there is to it so for this lesson we want to talk about Sprites and a Sprite is a computer Graphics term for just an object on the screen that can move around that's all that really means you played Mario everything you see on the screen is a Sprite Mario is a Sprite the coins are Sprites the mushrooms are Sprites so on the Sprites are super useful in gaming because you often want to have a lot of different objects moving around on the screen and pame has a lot of tools built into it that makes it very easy to work with Sprites now before we start making a Sprite let's talk real quick about the update and draw portions of our game Loop now the update section is where we're going to tell each Sprite or figure out what each Sprite needs to do does it need to move does it need to animate what needs to change about it and then obviously in the draw section we needed to tell the program to draw that Sprite onto the screen you can imagine if you start having a whole lot of Sprites on the screen that can start to get kind of messy your update section is going to be really long with drawing every Sprite and your draw section VI going to be really long with drawing every Sprite so to solve that problem pame has something called a Sprite group and a group is just a collection of Sprites so if you have a group it can make your update and draw section A lot easier so let's go ahead and make a group so before the game Loop starts we're going to make a group called all Sprites and it's just going to be a Sprite group okay and so this command creates a new group that's empty and we'll name it all Sprites because we're going to put every Sprite we make into that group and the reason is that we can now come down here to our update and all our update has to say is all Sprites do update and then whatever each Sprites rules are for how it updates each Sprite will update like it's supposed to and then in our draw section we can also say all Sprites do draw and then the draw command just needs you to tell it what place to draw it and we only have one place the screen so now it will draw every Sprite that's in the Sprite group so now as we make Sprites as long as we make sure to put them into the group and I'll show you how to do that then we won't have to add anything to our update or our draw section it'll automatically happen and that makes life a lot easier okay now I don't want to add any more code to my template here because again we want to use this as the skeleton and as the beginning for any new pame projects we do so I want it to stay like this so that when I start working on game number two or game number three I can come in here and I can um and I can just use this as the starting point so I'm going to make a new so I'm going to make a new file and I'm just going to take all of this and copy and paste it into there okay and I'm going to save this and I'm just going to call it Sprite example okay because that's what we're going to do right now is figure out how to make Sprites work okay so let's start creating a Sprite now Sprite is going to be an object use class and then we can name it so let's just name this uh the player because eventually this will be the player of the game um and it is a pame Sprite object so py game already has a Sprite a basic Sprite framework set up so we're going to make make player be a Sprite so this will be the Sprite for the player okay now just like when we did objects before um the first thing you need in your OB in your object is your init function right which is the the code that will get run whenever a new object of this type gets made so whenever we start and create the player object uh what code do we want to have happen well there's a few things that we're going to put in here but the first one is that's important is you have to do this is you have to for py game Run the init of its Sprite and this is kind of an ugly command uh but it's required if you don't include this line the Sprite won't function properly uh but then we can start talking about what we need now every Sprite can have all sorts of different properties for whatever you want depending on what you want it to do but there are a couple that are required every Sprite has to have an IM a self do image that's going to be what the Sprite looks like and everyone has to have a self Dot wct and that rect is the rectangle that encloses the Sprite and that's going to be really useful for moving the Sprite around figuring out where it is on the screen and seeing if that Sprite ran into anything else so we'll talk about that in a second let's start with the image so the image of this Sprite uh in this case we're just going to make it be a green rectangle okay to start with so we're just going to make a py game. surface that's a thing that you can draw on in pame um and it's just going to be let's say 50 pixels by 50 pixels okay and then I'm going to say self. image. fill green remember I have green defined up here in my colors so I just want to fill that little surface with just some green so we have a green block now let's talk about the wct property of the Sprite rect stands for rectangle and every object on the screen has a rectangle around it that helps the computer keep track of where it is how big it is and so on for example these Sprites have rect re angles around them they Define how wide and how tall the Sprite object is and also allow you to tell where it is or where you want it to be for example if we had our player Sprite and it had a rectangle around it in pame you have all these different locations on the rect for how you can specify where you want the rectangle to be for example if we wanted our player to stand on the ground we have some coordinate for where the ground is we could say put the Y of the Sprite at the ground minus 50 because our Sprite is 50 pixels tall but it's even easier in P game because you have that bottom handle and you could say put the bottom of the rectangle at the ground level and our player would be standing on the ground so rects are really useful in P game and you'll see as we start using them how useful they are and creating one is actually really easy too we just want to take the image and run the git rect command on it and it just looks at that image figures out what the rectangle needs to be and then we could tell let's have the player start in the center of the screen okay so let's set the self. re. center remember Center was the one of those handles I showed you in that picture let let's set the center of the rectangle to width over two oops and height over two and that will put it at the center okay so for now we're done with the basic properties of our player Sprite so now we want to get it to show up on the screen right because we've decid defined this class player but we haven't actually created one of these objects so let's go down here for our game Loop and let's make a player object appear and remember what I said about adding it to the group right so we want to take that all Sprites group and we want to add the player and if we do that we will be good because the player will get updated which doesn't do anything yet and the player will get drawn which means we can see it so let's run our program and see what happens there we go I have a green Block in the center of my window now our window is kind of small and since the next step is we want to make the player start moving around I'm going to go over here and I'm going to [Music] change this to a nice big size so that I have a large window to appear in and if I run that now I've got a nice big window for my little green square to move around in okay let's finish up this lesson by learning how to make the Sprite move around the screen so remember down in our update section we're telling all the Sprites to do whatever their update rule is so all we need to do is on our player Sprite add an update so let's call this update and all we're going to tell this Sprite to do is move the x + 5 so every time the game Loop repeats and does the update section the rectangle will move five pixels to the right and that's all we have to do now let's just make sure we don't have it run off the screen because that's kind of annoying and that'll show you how we use those different handles on the rectangle so if the rectangle's left side is greater than the width meaning it's gone off the side then we're going to change the re's right to be equal to zero and that'll make it look like it goes off the screen and comes back on all right so we'll stop there and in the next lesson we'll talk about some more things we can do with Sprites
Original Description
This is video #2 in the Pygame series. Try to watch them in order, as each video will be building on the one before.
In this video we'll explore how to use Sprites to create objects that can move around the screen.
Link to the Pygame Template file:
https://github.com/kidscancode/pygame_tutorials/blob/master/pygame%20template.py
Support me on Patreon: http://patreon.com/kidscancode
Links:
* Installing Python: http://kidscancode.org/python-install.html
* Installing Pygame: http://kidscancode.org/blog/2015/09/pygame_install/
* Setting up Atom: https://www.youtube.com/watch?v=uve1tjVIQ6c&ab_channel=KidsCanCode
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from KidsCanCode · KidsCanCode · 18 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
▶
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Learning to Code with Python: Lesson 1.1 - What is Programming?
KidsCanCode
Learning to Code with Python: Lesson 1.2 - Drawing with Turtles
KidsCanCode
Learning to Code with Python: Lesson 1.3 - Variables
KidsCanCode
Learning to Code with Python: Lesson 1.4 - Loops (and more turtles!)
KidsCanCode
Learning to Code with Python: Lesson 1.5 - Saving and Running Programs
KidsCanCode
Learning to Code with Python: Lesson 1.6 - Functions
KidsCanCode
Learning to Code with Python: Lesson 1.7 - Input and Conditional Statements
KidsCanCode
Learning to Code with Python: Lesson 1.8 - Number Guessing Game
KidsCanCode
KidsCanCode - Patreon Intro Video
KidsCanCode
Learning to Code with Python: Lesson 1.9 - Rock Paper Scissors Game
KidsCanCode
Learning to Code with Python: Lesson 1.10 - Secret Codes
KidsCanCode
Learning to Code with Python: Lesson 2.1 Creating Computer Graphics
KidsCanCode
Learning to Code with Python: Lesson 2.2 Simple Animation
KidsCanCode
Learning to Code with Python: Lesson 2.3: Animating More Objects
KidsCanCode
Learning to Code with Python: Lesson 2.4: More Fun with Animation
KidsCanCode
Extra: Setting up the Atom Editor for Python
KidsCanCode
Game Development 1-1: Getting Started with Pygame
KidsCanCode
Game Development 1-2: Working with Sprites
KidsCanCode
Game Development 1-3: More About Sprites
KidsCanCode
Pygame Shmup Part 1: Player Sprite and Controls
KidsCanCode
Pygame Shmup Part 2: Enemy Sprites
KidsCanCode
Pygame Shmup Part 3: Collisions (and Bullets!)
KidsCanCode
Pygame Shmup Part 4: Adding Graphics
KidsCanCode
Pygame Shmup Part 5: Improved Collisions
KidsCanCode
Pygame Shmup Part 6: Sprite Animation
KidsCanCode
Pygame Shmup Part 7: Score (and Drawing Text)
KidsCanCode
Pygame Shmup Part 8: Sound and Music
KidsCanCode
Pygame Shmup Part 9: Shields
KidsCanCode
Pygame Shmup Part 10: Explosions
KidsCanCode
Pygame Shmup Part 11: Player Lives
KidsCanCode
Pygame Shmup Part 12: Powerups
KidsCanCode
Pygame Shmup Part 13: Powerups (part 2)
KidsCanCode
Pygame Shmup Part 14: Game Over Screen
KidsCanCode
Pygame Platformer Part 1: Setting Up
KidsCanCode
Pygame Platformer Part 2: Player Movement
KidsCanCode
Pygame Platformer Part 3: Gravity and Platforms
KidsCanCode
Pygame Platformer Part 4: Jumping
KidsCanCode
Pygame Platformer Part 5: Scrolling the Window
KidsCanCode
Pygame Platformer Part 6: Game Over
KidsCanCode
Pygame Platformer Part 7: Splash & End Screens
KidsCanCode
Pygame Platformer Part 8: Saving High Score
KidsCanCode
Pygame Platformer Part 9: Using Spritesheets
KidsCanCode
Pygame Platformer Part 10: Character Animation (part 1)
KidsCanCode
Pygame Platformer Part 11: Character Animation (part 2)
KidsCanCode
Pygame Platformer Part 12: Platform Graphics
KidsCanCode
Pygame Platformer Part 13: Improved Jumping
KidsCanCode
Pygame Platformer Part 14: Sound and Music
KidsCanCode
Pygame Platformer Part 15: Powerups
KidsCanCode
Pygame Platformer Part 16: Enemies
KidsCanCode
Pygame Platformer Part 17: Using Collision Masks
KidsCanCode
Pygame Platformer Part 18: Scrolling Background
KidsCanCode
Pygame Platformer Part 19: Wrapping Up
KidsCanCode
Gamedev In-depth Topics: 4-way vs. 8-way Movement
KidsCanCode
Gamedev In-depth Topics: Time-based vs. Frame-based Movement
KidsCanCode
Gamedev In-depth Topics: Non-integer Movement
KidsCanCode
Tile-based game Part 1: Setting up
KidsCanCode
Tile-based game Part 2: Collisions and Tilemap
KidsCanCode
Tile-based game Part 3: Smooth Movement
KidsCanCode
Tile-based game Part 4: Scrolling Map / Camera
KidsCanCode
Tile-based game Part 5: Player Graphics
KidsCanCode
More on: AI Pair Programming
View skill →Related Reads
📰
📰
📰
📰
The AI Tool I Built for Myself Ended Up Becoming the Product Clients Asked About the Most
Medium · Programming
Can AI Help Plan Your Next Holiday?
Medium · AI
Beyond ChatGPT: How I Curated a Toolkit to Build an AI-Powered Workflow (And Why I Built a Tool to…
Medium · AI
Beyond ChatGPT: How I Curated a Toolkit to Build an AI-Powered Workflow (And Why I Built a Tool to…
Medium · Startup
🎓
Tutor Explanation
DeepCamp AI