Top Down Scroller | Part 2 | Enemy AI
Key Takeaways
This video demonstrates how to create a simple zombie AI for a top-down scroller game in Scratch, building upon the scrolling mechanics established in Part 1.
Full Transcript
hi sis Griff patch this is my second part of my top down scrolling game tutorial for scratch today we are going to look at how to add in enemies this is a very simple enemy artificial intelligence let's start by adding in a new sprite like so and this is going to be an enemy cube so let's add in a new color for this enemy cube and I'm going to make it green I kind of nasty dark green because it's a zombie and I'm gonna destroy it in here like this again shift to make it square there he is my zombie cube so this guy here is gonna be following my player so let's just call him zombie cube into code now he needs an X for this sprite only and another available y for this right only and he's a lowercase not uppercase because they're for this sprite only let's have in a event and when I receive setup I'm at now this is where we positioned the zombie cube for his first initial appearance so we'll set X and y and we can't use the arrow because that'll be white on top of our cube so left for now let's put minus 200 by zero which is pretty much where he is now okay and then we also need if we go into the level sprite let's drag in this move level into the zombie cube is that will then quickly position him properly for well scrolling such as will net so here we go there's our zombie cube and in nicely sits crawls with the level which is what we want notice if we got off the edge of the screen he stays stuck to the edge so we'll have to fix that okay so what we're going to do is add in a custom block make block and call it position and we could have an argument X and argument Y one without screen refresh so okay and now in here we're going to add in our go-to block like this put the new position into the more level receiver and then x- croelick's y- scroll why go in there than the X&Y I can go in like that okay now the reason we do this is we're now going to add another variable called is visible although a case and for this sprite only we can set that here under the go to and in here we're going to add in and and and then to equals on on your side like this let's make a little bit more room for us and in here we're going to have X Y and then we need the actual positions on the screen so x position and y position so his visible is going to be set to true when we manage to position the zombie cube where he was supposed to be if he doesn't go where he's meant to be is visible be false let's just watch that in action I run this project now so it's visible is true so it's Amba cubes there which is good I move for too far to the right and I got to false so he shouldn't be showing them though she is showing shouldn't be showing you can see the true and false changing we can make use of this fact to hide him when he's off-screen and more than that what we gonna do with this game is he we only move these zombie cubes when they are on the screen if they're off-screen they don't move so they don't walk onto screen otherwise all the zombies on the whole level would come straight at you at the very beginning and not be where they're meant to be their mentor kind of appear as you find them on the level so that's what we're going to use is true and false here ok so let's go back to the program we can hide that variable visible now so what we want to do is have this enemy cube move towards the red player but only when he's on screen so let's give that a go in the player sprite we still have this loop here so remove the player we move the level and next we want to move the enemy cubes so it's quite another broadcast underneath and we're going to call this move enemy like so ok we can go back into our enemy sprite finest rename this to zombie or enemy you choose what you want to call it and we're gonna have a receiver in here and I receive move enemy so let's put an if/else in here and what we're going to do first is going to check whether we're on screen and we've got that in this is visible so if we put an equals go into variables and have not my verbal is visible and put in here true GRU II okay just like it was set to so when it's on-screen it wants this bit it's off screen it runs this bit so first of all that's going to looks and hide when it's in the off screen show when it's on the on screen let's run that just to make sure that works so he's visible off screen disappears on screen appears so now he's no longer sticking to the side of the screen right what we're gonna do when we want to move our little player then so let's add in a custom block for moving him and we'll call it follow player one without screen refresh okay and move that underneath the show like that and let's move this down even further so follow player what are we going to put in here right so we need an if statement and then we need some comparisons so if this example the player is to the right of zombie cube we want to move the right so let's put a greater than now this wants to be the position of the player so for that we have to look in sensing and this block here which says backdrop of stage now it might seem a bit strange but this is the one we need because in here you can change stage to player now watch out it jumps up to the top it always does that but I think they're gonna fix that book soon hopefully change the left-hand side you can now choose the variable X naught X position X cuz that's available of our player sprite that howls where our player is there we go so now we can say if the position of the player it's greater than X like so now this X is our zombie's X so if the player here is bigger than the zombies X so to the right of we want to move our zombie to the right so now we can borrow these scripts form player so just like in here where we move a direction if a certain condition is true so let's move this custom block into zombie and where did it go there it is just put it here for now so if it's to the right we want to try move and we'll move it one pixel to the right and we won't move up try 1 comma 0 okay let's just go and have a look at our try and move we might need to change us a little bit okay what we need to change in here is rather than doing the go - let's use our position that we created and move the whole positioning there was into that instead so not using the go-to no reason we want to do that is that this position you remember your ground here also settee is visible so we know that if we try moving to a new position without a zombie cube if we position them off-screen then we don't want to allow to do that because we don't want to allow the zombie cube to move away from the screen because that can cause problems because you can't tell if it's touching at wall if he's moving off screen because the touching color only works if it's on screen which is a big problem for the scrolling games so we're going to get around that by detecting a physical screen and then not letting him move so put in a hand down here into the if block and we're going to check that he's on screen here so only allow him to actually move equals it's visible where is it there it is is visible so it's visible equals true and he's not touching a wall then allowing to move let's just delete that myvariable really don't need it that's better was it more confusion than it needs to okay that what means he should let's just run that see if it actually works hey look at this so he's now moving to the right of course it can't hurt me and he can't move any other direction part firm to the right let's just see where they can get through the wall if I move around here you no stopped that's brilliant so let's put in his other directions where is here we go so in the follow player so at the moment we've only got to moving to the right let's add in some other ones so duplicate that so if he is less than less than less than if player X is less than X and move him by minus one so now he should be two moves left as well so the moment is moving to the right on the other side of him now he moves to the left so now all we need is up and down so let's do that so if we duplicate these so it's going to be Y off player now and why player and available y available Y so if Y of player is bigger than Y we want to be up so through 0 X 1 Y in the Tri move and then this one 0 and minus 1 so moving up and down which one that now now you see quite happily following me I move down here out of the way it's desperately trying to follow me but he's not gonna get me cuz I'm far too fast for him if I move off-screen come back he moves disappears now he should not be following me now if I go back on here he should appear see not following till he is on the screen there we go so this is a really big start was our game at the moment he's not getting stuck on anything which is quite impressive because when I was playing earlier he would get stuck on walls quite happily and we're gonna have to fix that because it does happen okay one more tweak that we need to to do to this is in the oh here we are at the bottom of the follow here we need to move him to exactly where he supposed to be at the end of moving because if we tried to move and it failed it leaves him still overlapping the level so all we need to do for that is add in another position core what we need to do for that is copy the this position here in the move level and put that at the bottom of the follow player like that so after we finished moving it positions him where he's meant to end up just run that and make sure that all works shouldn't really notice much difference there we go so we have one zombie coupe but it's really only gonna get really exciting when we have lots more so this is the end of this tutorial but next time I'm gonna introduce lots more zombie cubes and how you set them up nice and easily and it's going to make the game much more fun so join me next time subscribe to me thanks for watching this is Griff patch bye now
Original Description
This is part 2 of a five part series on how to script a top down scrolling game in scratch.
Based on Zombie Cube (https://scratch.mit.edu/projects/284516654/)
Part 1 - Looked at Scrolling (https://youtu.be/GXsjYupfDYk)
Part 2 - Introduces a zombie cube enemy and gives it a simple zombie AI
Part 3 - we will introduce MANY zombies and a way of placing them in the level.
All coding by griffpatch - Subscribe to me to not miss out on any new tutorials :)
🚀 *Boost Your Creativity with Griffpatch*
The Griffpatch Academy will take you from "Gamer to Game Creator"
Learn more at 👉 https://griffpatch.academy 👈
😺 Scratch was developed by the Lifelong Kindergarten Group at the MIT Media Lab. See http://scratch.mit.edu
Playlist
Uploads from griffpatch · griffpatch · 16 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
▶
17
18
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
Burrow Blitz - A game by Griffpatch
griffpatch
Burrow Blitz Level 1 Speed Run!
griffpatch
Scratch Messaging Extension (for chrome & firefox)
griffpatch
Scrolling Platformer Tutorial | Part 1 | Get Scrolling
griffpatch
Scrolling Platformer Tutorial | Part 2 | Gravity
griffpatch
Scrolling Platformer Tutorial | Part 3 | Platform Detection
griffpatch
Scrolling Platformer Tutorial | Part 4 | Off Screen Sprites and Player Death
griffpatch
Scrolling Platformer Tutorial | Part 5 | Collectables
griffpatch
Scrolling Platformer Tutorial | Part 6 | Danger Sprites (Bug fix)
griffpatch
Scrolling Platformer Tutorial | Part 7 | Exit Portal
griffpatch
Scratch 3 | Box2d | Physics Extension
griffpatch
Scrolling Platformer Tutorial | Part 8 | Vertical Scrolling
griffpatch
Zombie Cube | Speedrun challenge
griffpatch
Scratch 3 | Physics Extension | Version 2
griffpatch
Top Down Scroller | E1 - Scrolling | Scratch Tutorial
griffpatch
Top Down Scroller | Part 2 | Enemy AI
griffpatch
Top Down Scroller | Part 3 | Enemy Clones & List
griffpatch
Top Down Scroller | Part 4 | Live Die Repeat (Game Over)
griffpatch
Top Down Scroller | Part 5 | You've Won!
griffpatch
Cloud Game Tutorial | Part 1 | The Basics
griffpatch
Cloud Game Tutorial | Part 2 | Encoding and Decoding
griffpatch
Scratch 3 Dev Tools Browser Extension by griffpatch
griffpatch
Cloud Game Tutorial | Part 3 | Multiplayer
griffpatch
Scrolling Platformer Tutorial | Part 9 | Momentum
griffpatch
Scrolling Platformer Tutorial | Part 10 | Wall Jumping
griffpatch
Cloud Game Tutorial | Part 4 | Auto Game Joining
griffpatch
Game Preview - "Getting Over It" with Griffpatch - A fan recreation in Scratch
griffpatch
Speed Run with Outtakes | Getting Over It | 4 minutes 30 seconds
griffpatch
Griffpatch's Live Stream
griffpatch
MMO Platformer Speedrun Challenge
griffpatch
Appel Platformer | Scratch Game Blog
griffpatch
Cloud Game Tutorial | Part 5 | Smooth Movement
griffpatch
How to Design a Level for Appel
griffpatch
Fastest Appel Speedruns | Jan 2021
griffpatch
Tile Scrolling Platformer (Mario) | 1. Setup
griffpatch
Tile Scrolling Platformer | 2. Grid List
griffpatch
Tile Scrolling Platformer | 3. Tile Collisions
griffpatch
Tile Scrolling Platformer | 4. Platforming Scripts
griffpatch
Tile Scrolling Platformer | 5. The Level Editor
griffpatch
Tile Scrolling Platformer | 6. Level Codes
griffpatch
Tile Scrolling Platformer | 7. Drop Through Platforms
griffpatch
How to make Physics in Scratch | Full Tutorial
griffpatch
Tile Scrolling Platformer | 8. Enemy Clones
griffpatch
Simple Pathfinding Tutorial
griffpatch
Tile Scrolling Platformer | 9. Enemies in Level Editor
griffpatch
Simple Maze Generation | Scratch Tutorial
griffpatch
Tile Scrolling Platformer | 10. Mystery Blocks
griffpatch
Simple Car Steering Simulation | Scratch Tutorial
griffpatch
Simple Background Scrolling Tutorial
griffpatch
Tile Scrolling Platformer | 11. Death, Background & Sound
griffpatch
Mid Week Update & Channel Membership
griffpatch
Simple Grid List Tutorial with Image Scanning
griffpatch
Tile Scrolling Platformer | 12. Auto Arranging Tiles
griffpatch
Code a Fun Space Shooter Game 🚀 | 1. Move & Shoot | Scratch Tutorial
griffpatch
Space Shooter 2 - Enemy Collisions
griffpatch
Tile Scrolling Platformer | 13. Level Progression & Editor Improvements
griffpatch
Space Shooter 3 - Camera Shake & FX
griffpatch
Tile Scrolling Platformer | 14. Sloping Tiles
griffpatch
Pathfinding a Maze using Lists
griffpatch
Tile Scrolling Platformer | 15. Crouch & Slide
griffpatch
More on: AI Pair Programming
View skill →Related Reads
📰
📰
📰
📰
Ten files is not a budget
Dev.to AI
Inteligencia artificial para empresas: cómo está cambiando la forma de trabajar
Medium · SEO
SmartKirana: Building an AI-Powered Quick-Pickup and Inventory System for Neighborhood Stores
Medium · Machine Learning
Top 5 Custom AI Development Agencies in the US (2026)
Medium · SEO
🎓
Tutor Explanation
DeepCamp AI