Pygame Tutorial #6 - Enemies

Tech With Tim · Beginner ·🛠️ AI Tools & Apps ·8y ago

Key Takeaways

This video tutorial demonstrates how to create an enemy that moves along a one-dimensional path in a game using Pygame, covering topics such as enemy AI, pathfinding, and image loading. The tutorial provides a step-by-step guide on how to implement this functionality using Python 3 and the Pygame module.

Full Transcript

[Music] hey guys and welcome back to another YouTube video so in today's video I'm gonna be continuing with the six video in my Python programming series and I'm just gonna give you a quick sneak peek on what's gonna be happening in this tutorial pretty much we're just gonna be creating an enemy on the screen just like the description or the title says that's able to walk around like this now I know this seems simple for something that's probably gonna be about a 12 to 15 minute video but this is more complicated than it looks and obviously we have the animation going so everything looks good and he's changing velocities as he hits each point so we're gonna put him on a path and he's gonna be able to move that path so that could be the path could be here could be here to here it's variable meaning it can change so that's what this tutorial is gonna be make sure you stick around to the end if you want to learn how to do this and since we're using images I just want to direct our attention to the github here quickly so if you haven't seen already I have a github for this series where I post all the images and all the tutorial files I started at 3 just because the first two were pretty basic if you scroll all the way down to the bottom so what actually happened is you're gonna get to this page if you click on the link in the description click on game scroll all the way down to the bottom these are all the tutorial files if you want to download them if you want to look at them for reference and then all of the images are in here so if you actually want to download these files you click clone or download and click download zip file and it's gonna download all of it onto your computer from there you're gonna drag all of these images so like from the top here all the way down to r9e into your what do you call it into your game folder directory wherever you have your Python files stored and then you're gonna be able to do this tutorial so make sure you do that if you want to use these sprites if not then you're just gonna have to load your sprites in by yourself hopefully you can figure that out and pretty much what I've done is I've named for example we have 11 images for our enemy moving left and moving right so I've done like L 10 e L 11 each meaning left 11th image and all right so that's the github if you guys haven't been there go check it out if you need to and yeah whatever okay so let's get right into this tutorial so our game here remember from last time right now oops this is a different game that I'm showing what am i doing that for um sorry okay so we got into our tutorial here you remember from last time we have our little guy jumps up and down moves around the screen now we're gonna create the enemy so the first thing we need to do when we want to create the enemy is oops I've accidentally closed this game again is we need to create a class so that we can then work out of that class so I've already done this I was done class and then enemy and it inherits from object and then I've created two class attributes here don't have to worry about what that means just write variables that look like this in your class now I've already loaded up all the images so I have r1 e r2 e all the way up to or 11 E and L 11 e into their names here so walk right walk left and you just do that at the top your class and then after that you can define your initialization function like so now you don't have to understand exactly how these class attributes work just write them in like this and we'll get to them later so we're just going to create our initialization here so we need X Y width height and then we're gonna create another one here that's new it's gonna be called end and I'm gonna go over that in a second so do self taught x equals x self the why cuz why wow I can't type and we'll do so filled with equals width and so on until we finish all of the variables and then we'll add our constants and I myself thought and equals ends like that so now we're gonna add our self dot walk count this is what's gonna be responsible for counting our sprites and what image Ron and we need self dot Val and that's gonna be equal to we'll start with three so now we need to create our draw function our methods sorry we're gonna do define draw inside here we're gonna take self and window and then we're gonna write pass in here is we're gonna get back to and then we're gonna create our note another method called move and then here we're gonna do self and that's all we need just self in there alright so I know I've just whizzed through that it just because we've already done this a bunch of times so I figured we can go fast through this part what we're gonna do here is we're gonna create our enemy now our enemy's gonna move on a set path so like I showed you before it moves from left to right and then left and then right and pretty much moves between two coordinate points so ours is gonna move in one dimension meaning it's only gonna move across the x-axis now if you wanted to move this up or down you would just change the Y value rather than changing the x value okay whereas for us since we're moving X we're just gonna be create adding to the x value or subtracting to the x value depending on where our enemy is so I want to run this program already I have another file where it's already coded you can see once he gets to a coordinate point so about where my mouse is he turns around he changes directions now to change directions he needs to change velocities so to do that we're gonna be multiplying the velocity by negative one and so on and I'm gonna get into that now so this is gonna be our move method here now what this move method is gonna be responsible for is every time we try to draw the character we're gonna move we're gonna move the character so we're gonna do self-thought move in here so we don't forget every time we draw we're gonna first move and then we're gonna draw the character now I just need to add another variable appear it's gonna be so called self that path and it's going to equal self dot X and self dot end now this represents where we're starting and where we're ending and these are just the x coordinates so that we can keep track of them when I'm checking them down in the move method so we go into the move method here and we're gonna start by saying if our character has a velocity that's positive so pretty much if self-thought velocity is greater than zero that means we are moving right so we're going right we're adding to the velocity then what do we have to do well we have to check if he is about to move past the point that we wanted to stop at so we're if self that X is greater than or actually we're gonna check if it's less than so we know if it's less than and then self thought path and since we're moving to the right we know this coordinate is gonna be to the right of whatever what do you call it our character is moving up if it is less than self thought path and then from there we just do one like this because we're accessing this element here so add a little brain fart there - self thought velocity or plus actually so this way we can tell sorry I need to change this to over here plus self dot velocity so that we see that if we add to the velocity of this x-coordinate here if it's less than the coordinate that we can't go past then we will allow our character to move so then we will do these self thought X plus equals self dot velocity like that I hope that makes sense now if we are already past this coordinate so our X is greater than or we are about to be greater than then we need to change directions so to change directions like I mentioned before we're just going to multiply our velocity by negative one but therefore flipped us 180 degrees and moving us in the opposite direction then what we're gonna do is going to set our walk count back down to zero like that and that's all for that now let's create our else statement because if our velocity is negative so it's not greater than zero then we need to check if our self dot X it - be self double I'll see this time is greater than in this instance what's gonna be self dot path one like this they're not one zero straight so this is exactly where we started from we're moving negative so we want to make sure that we don't go past that if we're not moving past that again will change our X so your self thought X plus equals self novel now you might think well we should probably subtracting and removing left but our velocity is gonna be negative if we've changed directions move left so if your negative you're really just subtracting that number so that's fine and then same thing in the else statement here you're just gonna copy exactly what we have here oops don't know why I deleted it like that so we have self dot Val equals self sub L times negative one again changing the directions to negatives make a positive we're gonna be moving right again and self to walk count equals zero we can get rid of this pass and that's all we need for our move method like that all right so now after we've done that we're gonna move back into our draw here and this is where we're gonna determine whether we're drawing an image to the left or whether we're drawing an image to the right and how that animation is going to work similar to what we've done in the player class up here you're just going to be a little bit different so if you remember in our player class so actually let's go up here for a second we have 27 as our maximum walk count now once that walk count gets past 27 we reset it back down to zero so we can continue the animation now since we have 11 images actually rather than nine in our enemy so moving right or left we need to change this slightly so we're gonna do if self thought walk count is less or plus one is less than or equal to 33 like this then we're going to new self dot walk out Wow equals zero sorry my typing is absolutely horrible now if that's not the case or after we check that then again we're gonna be checking to see whether we're moving left or right so rather than having like a self thought or having a self dot right variable a self thought left variable or attribute whatever we want to just use the velocity because we know if our velocities pause that we're moving right if our velocity is negative roofing left right so if our velocity is greater than zero that means we're moving right so if it's greater than zero we're all we're gonna do is we're gonna pull it to the screen the images again it's a wind-up lit and then we want our image so we're gonna do self and this is where these come in handy you just have to use self to access these all right so we're gonna do self dot walk right so we're moving right and then in here square brackets and self-taught walk count and then integer division 3 again because we want to make sure it's not looking like we're moving too fast then we need our coordinates so we'll do self dot X and self del Y and then we need to increment our walk count so we'll do walk count plus equals one like that now else so if we're not moving left we've all right we're not moving right we must be with left I'm just gonna copy this and then we can edit in here so we're just gonna change this simply to left and I think that's all we need to do so now that will actually get rid of this pass here now that we've had the draw method we have the move method what we need to do is we need to create an instance of our enemy so it right now if I show you if I run the program since we haven't created an instance we still he's not on the screen we don't see him he's not moving around we need to first create the instance of him so just like we created the man instance down here near our main loop we're gonna do the same thing I'm just gonna call this goblin and he is an instance of enemy he's gonna start at what x-coordinate we'd like maybe 100 for 10 again and then would be 64 by 64 and where do we want to end so what x-coordinate the path that we wanted to end up let's maybe do 450 and there we go we create an incident instance of him now that we've created that instance we need to dry so goblin draw on the window and let's test this out we have an issue named Bell is not defined ah error that I've ran into here we need to just put self though though here my bed and let's see if it works now walk count reference for assignment I always forget myself so you don't know so just make sure you guys have yourselves here because I forgot them and everything looks good let's test it out there we go we see we have a little goblin man moving around but it doesn't look like everything's working properly right his feet aren't moving so what could the issue be here now I think the issue is something to do with our walk count whether we're not incrementing it properly or ah this is the issue less than needs to be changed to greater than what I've just fixed here is the fact that we were saying that if self thought walk count is plus one is less than 33 which it always is then it's just gonna set it to zero so he wasn't changing the image ever so what we needed to do there is we needed to set this to greater than so that that's not happening sorry guys my mistake there let's run this and there we go we see our goblin running around the screen and when you get to the end coordinate he changes direction and you can see has a nice little punching thing there and so what we're gonna do with this in the next tutorial is gonna be doing collision with our player so if he hits the player it's gonna lose points and then our player he has these bullets right that he can shoot if he hits the Goblin call him do you think I start flashing or the players gonna gain points we can figure that out in the next tutorial so sorry about that little issue there guys but if you enjoyed the video please leave a like and subscribe and I will see you again in the next one [Music]

Original Description

In this video I explain how to create an enemy that moves along a one dimensional path. This is done in python 3 using the module pygame. This series is focused on creating a solid foundation of how games function and how objects move and interact with each other so that you can use these skills to create other, more complex games. Download Code: https://techwithtim.net/tutorials/game-development-with-python/pygame-tutorial/pygame-enemies/ Follow my Twitter: https://twitter.com/TechWithTimm Please LIKE and SUBSCRIBE for more content! How To Install Pygame: https://www.youtube.com/watch?v=Okh-aKuHr7Q Python Programming Tutorials: https://www.youtube.com/watch?v=OFrLs22MDAw&list=PLzMcBGfZo4-mFu00qxl0a67RhjjZj3jXm
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tech With Tim · Tech With Tim · 44 of 60

1 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

This video tutorial teaches how to create an enemy that moves along a one-dimensional path in a game using Pygame, covering topics such as enemy AI, pathfinding, and image loading. The tutorial provides a step-by-step guide on how to implement this functionality using Python 3 and the Pygame module. By following this tutorial, viewers can gain a solid foundation in game development and object movement.

Key Takeaways
  1. Create a class for the enemy to inherit from object
  2. Define initialization function for the enemy class
  3. Create class attributes for variables like X, Y, width, height
  4. Load up images for enemy sprites
  5. Create an enemy that moves on a set path
  6. Change direction by multiplying velocity by negative one
  7. Add a move method to update character position
  8. Check if character has reached a coordinate point
  9. Update character position based on velocity
  10. Change character direction when necessary
💡 The tutorial demonstrates how to use Pygame to create an enemy that moves along a one-dimensional path, highlighting the importance of understanding game development basics, object-oriented programming, and game logic.

Related AI Lessons

Up next
I Asked ChatGPT to Apply to 500 Jobs (8 Interviews in 48 Hours)
Sabrina Ramonov 🍄
Watch →