Top Down Scroller | Part 2 | Enemy AI

griffpatch · Beginner ·🛠️ AI Tools & Apps ·7y ago

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
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from griffpatch · griffpatch · 16 of 60

1 Burrow Blitz - A game by Griffpatch
Burrow Blitz - A game by Griffpatch
griffpatch
2 Burrow Blitz Level 1 Speed Run!
Burrow Blitz Level 1 Speed Run!
griffpatch
3 Scratch Messaging Extension (for chrome & firefox)
Scratch Messaging Extension (for chrome & firefox)
griffpatch
4 Scrolling Platformer Tutorial | Part 1 | Get Scrolling
Scrolling Platformer Tutorial | Part 1 | Get Scrolling
griffpatch
5 Scrolling Platformer Tutorial | Part 2 | Gravity
Scrolling Platformer Tutorial | Part 2 | Gravity
griffpatch
6 Scrolling Platformer Tutorial | Part 3 | Platform Detection
Scrolling Platformer Tutorial | Part 3 | Platform Detection
griffpatch
7 Scrolling Platformer Tutorial | Part 4 | Off Screen Sprites and Player Death
Scrolling Platformer Tutorial | Part 4 | Off Screen Sprites and Player Death
griffpatch
8 Scrolling Platformer Tutorial | Part 5 | Collectables
Scrolling Platformer Tutorial | Part 5 | Collectables
griffpatch
9 Scrolling Platformer Tutorial | Part 6 | Danger Sprites (Bug fix)
Scrolling Platformer Tutorial | Part 6 | Danger Sprites (Bug fix)
griffpatch
10 Scrolling Platformer Tutorial | Part 7 | Exit Portal
Scrolling Platformer Tutorial | Part 7 | Exit Portal
griffpatch
11 Scratch 3 | Box2d |  Physics Extension
Scratch 3 | Box2d | Physics Extension
griffpatch
12 Scrolling Platformer Tutorial | Part 8 | Vertical Scrolling
Scrolling Platformer Tutorial | Part 8 | Vertical Scrolling
griffpatch
13 Zombie Cube | Speedrun challenge
Zombie Cube | Speedrun challenge
griffpatch
14 Scratch 3 | Physics Extension | Version 2
Scratch 3 | Physics Extension | Version 2
griffpatch
15 Top Down Scroller | E1 - Scrolling | Scratch Tutorial
Top Down Scroller | E1 - Scrolling | Scratch Tutorial
griffpatch
Top Down Scroller | Part 2 | Enemy AI
Top Down Scroller | Part 2 | Enemy AI
griffpatch
17 Top Down Scroller | Part 3 | Enemy Clones & List
Top Down Scroller | Part 3 | Enemy Clones & List
griffpatch
18 Top Down Scroller | Part 4 | Live Die Repeat (Game Over)
Top Down Scroller | Part 4 | Live Die Repeat (Game Over)
griffpatch
19 Top Down Scroller | Part 5 | You've Won!
Top Down Scroller | Part 5 | You've Won!
griffpatch
20 Cloud Game Tutorial | Part 1 | The Basics
Cloud Game Tutorial | Part 1 | The Basics
griffpatch
21 Cloud Game Tutorial | Part 2 | Encoding and Decoding
Cloud Game Tutorial | Part 2 | Encoding and Decoding
griffpatch
22 Scratch 3 Dev Tools Browser Extension by griffpatch
Scratch 3 Dev Tools Browser Extension by griffpatch
griffpatch
23 Cloud Game Tutorial | Part 3 | Multiplayer
Cloud Game Tutorial | Part 3 | Multiplayer
griffpatch
24 Scrolling Platformer Tutorial | Part 9 | Momentum
Scrolling Platformer Tutorial | Part 9 | Momentum
griffpatch
25 Scrolling Platformer Tutorial | Part 10 | Wall Jumping
Scrolling Platformer Tutorial | Part 10 | Wall Jumping
griffpatch
26 Cloud Game Tutorial | Part 4 | Auto Game Joining
Cloud Game Tutorial | Part 4 | Auto Game Joining
griffpatch
27 Game Preview - "Getting Over It" with Griffpatch - A fan recreation in Scratch
Game Preview - "Getting Over It" with Griffpatch - A fan recreation in Scratch
griffpatch
28 Speed Run with Outtakes | Getting Over It | 4 minutes 30 seconds
Speed Run with Outtakes | Getting Over It | 4 minutes 30 seconds
griffpatch
29 Griffpatch's Live Stream
Griffpatch's Live Stream
griffpatch
30 MMO Platformer Speedrun Challenge
MMO Platformer Speedrun Challenge
griffpatch
31 Appel Platformer | Scratch Game Blog
Appel Platformer | Scratch Game Blog
griffpatch
32 Cloud Game Tutorial | Part 5 | Smooth Movement
Cloud Game Tutorial | Part 5 | Smooth Movement
griffpatch
33 How to Design a Level for Appel
How to Design a Level for Appel
griffpatch
34 Fastest Appel Speedruns | Jan 2021
Fastest Appel Speedruns | Jan 2021
griffpatch
35 Tile Scrolling Platformer (Mario) | 1. Setup
Tile Scrolling Platformer (Mario) | 1. Setup
griffpatch
36 Tile Scrolling Platformer | 2. Grid List
Tile Scrolling Platformer | 2. Grid List
griffpatch
37 Tile Scrolling Platformer | 3. Tile Collisions
Tile Scrolling Platformer | 3. Tile Collisions
griffpatch
38 Tile Scrolling Platformer | 4. Platforming Scripts
Tile Scrolling Platformer | 4. Platforming Scripts
griffpatch
39 Tile Scrolling Platformer | 5. The Level Editor
Tile Scrolling Platformer | 5. The Level Editor
griffpatch
40 Tile Scrolling Platformer | 6. Level Codes
Tile Scrolling Platformer | 6. Level Codes
griffpatch
41 Tile Scrolling Platformer | 7. Drop Through Platforms
Tile Scrolling Platformer | 7. Drop Through Platforms
griffpatch
42 How to make Physics in Scratch | Full Tutorial
How to make Physics in Scratch | Full Tutorial
griffpatch
43 Tile Scrolling Platformer | 8. Enemy Clones
Tile Scrolling Platformer | 8. Enemy Clones
griffpatch
44 Simple Pathfinding Tutorial
Simple Pathfinding Tutorial
griffpatch
45 Tile Scrolling Platformer | 9. Enemies in Level Editor
Tile Scrolling Platformer | 9. Enemies in Level Editor
griffpatch
46 Simple Maze Generation | Scratch Tutorial
Simple Maze Generation | Scratch Tutorial
griffpatch
47 Tile Scrolling Platformer | 10. Mystery Blocks
Tile Scrolling Platformer | 10. Mystery Blocks
griffpatch
48 Simple Car Steering Simulation | Scratch Tutorial
Simple Car Steering Simulation | Scratch Tutorial
griffpatch
49 Simple Background Scrolling Tutorial
Simple Background Scrolling Tutorial
griffpatch
50 Tile Scrolling Platformer | 11. Death, Background & Sound
Tile Scrolling Platformer | 11. Death, Background & Sound
griffpatch
51 Mid Week Update & Channel Membership
Mid Week Update & Channel Membership
griffpatch
52 Simple Grid List Tutorial with Image Scanning
Simple Grid List Tutorial with Image Scanning
griffpatch
53 Tile Scrolling Platformer | 12. Auto Arranging Tiles
Tile Scrolling Platformer | 12. Auto Arranging Tiles
griffpatch
54 Code a Fun Space Shooter Game 🚀 | 1. Move & Shoot | Scratch Tutorial
Code a Fun Space Shooter Game 🚀 | 1. Move & Shoot | Scratch Tutorial
griffpatch
55 Space Shooter 2 - Enemy Collisions
Space Shooter 2 - Enemy Collisions
griffpatch
56 Tile Scrolling Platformer | 13. Level Progression & Editor Improvements
Tile Scrolling Platformer | 13. Level Progression & Editor Improvements
griffpatch
57 Space Shooter 3 - Camera Shake & FX
Space Shooter 3 - Camera Shake & FX
griffpatch
58 Tile Scrolling Platformer | 14. Sloping Tiles
Tile Scrolling Platformer | 14. Sloping Tiles
griffpatch
59 Pathfinding a Maze using Lists
Pathfinding a Maze using Lists
griffpatch
60 Tile Scrolling Platformer | 15. Crouch & Slide
Tile Scrolling Platformer | 15. Crouch & Slide
griffpatch

This video teaches how to create a simple zombie AI for a top-down scroller game in Scratch, covering the basics of enemy AI and game development. By following this tutorial, viewers can learn how to implement basic AI behaviors in their own games.

Key Takeaways
  1. Create a new sprite for the zombie enemy
  2. Add scripts to control the zombie's movement and behavior
  3. Use conditional statements to create simple AI decisions
  4. Test and refine the zombie AI
💡 Simple AI behaviors can be created using conditional statements and basic programming concepts in Scratch.

Related Reads

Up next
How to Get Your Brand Cited by ChatGPT, Claude and Gemini
Arvow
Watch →