Tower Defense Game Tutorial with JavaScript & HTML Canvas

2024 ж. 20 Мам.
426 719 Рет қаралды

Ready to create amazing games? Join chriscourses.com to learn, grow and thrive from over 100 videos, quizzes and code challenges: chriscourses.com/
Here you'll learn how to create your very first tower defense game with JavaScript and HTML canvas. We'll start by creating a game map using a map editor called Tiled. Then we'll code the basics of a tower defense game using nothing but rudimentary shapes. Finally, we'll replace our shapes with sprites to give our game a professional look.
Google Drive Assets: drive.google.com/drive/folder...
Finished Demo: chriscourses.github.io/tower-...
Source Code: github.com/chriscourses/tower...
Tiled Map Editor Download: www.mapeditor.org/
Stone Tower Assets: free-game-assets.itch.io/free...
Desert Tileset: free-game-assets.itch.io/free...
Orc Sprite Assets: free-game-assets.itch.io/free...
Heroicons: heroicons.com/
Font Awesome: fontawesome.com/
0:00 Introduction
0:51 Game Assets and Downloads
5:40 Create a Path
23:53 Map Details
31:42 Project Setup
48:31 Pathfinding
1:28:55 Building Placement
2:12:03 Shooting Projectiles
2:50:40 Health Bars
3:02:46 Waves
3:09:43 Game Over
3:39:13 Resources
4:01:12 Sprites - Projectile
4:16:09 Sprites - Enemy
4:34:15 Sprites - Building
4:55:25 Sprites - Explosions
5:04:50 Launch
Music by Joe Gallagher: joegallagher.itch.io/free-orc...

Пікірлер
  • There's about 20-30min of audio that makes it sound like I'm submerged underwater. Had some electrical interference picked up somehow, so had to do my best to edit out some really annoying buzzing. Sorry in advance 😅

    @ChrisCourses@ChrisCourses Жыл бұрын
    • no matter

      @saronarabeds6517@saronarabeds6517 Жыл бұрын
    • Its not ok🤣

      @abhilashLeader_07@abhilashLeader_07 Жыл бұрын
    • Нюбп😮бую🎉шюуг😅б😊😊руунжзр😊гзуш😅ш😮😮🎉ездют😅🎉юрий михайлович нгщ😅🎉где😢ю🎉😊уг😊ююпнн😊у 1:02:52 июраб избрнжубранзрщбув😊она😊ушгщжнню😅шд🎉😂🎉ладно🎉не😮😢эшшх🎉школа х😮😂😢 юл😢былюубвщуюшордшг🎉не😅юзв😊😊😊 1:29:45 шенйно🎉😂ему😅😅😮😊юб😂юбилей ру😂❤юш🎉ну-шу г😊ш🎉ею😊😊е🎉шнхууну чего не хватае Пир😮щ😊😊огово🎉 ш😊гова😊😮н😊н😅🎉е🎉он уже😊 не😅 1:29:52 😊ун😊шву😂😂елш🎉😊е😊😮г😂😊ыыщгвщ😮ж😅уже😊вешеуу😅в?😊незз😊этот й😊цщг😊ыее😮л 1:30:3б🎉🎉чйш😮йег🎉😅пбе😊е9🎉н😮у😮шее 1:31:01 😂гпув🎉🎉😂еу🎉шепу🎉 1:31:05 гшшюю😮😅😮еп❤🎉😊 😮еыш😊уулйеввш😂в😊😊шее😊😅цшеен🎉🎉🎉😮❤он👦👦🎉😢😊🎉шегнд🎉но щ🎉гц😊юшеш🎉леб😊🎉🎉😊🎉е😅🎉дещюше🎉🎉😅😂в😅😊вуннз😊😊д😊😊нынгш😮щбщды🎉щ🎉з😊ушд🎉о😢е😊🎉о😊 1:31:31 ю😂ш😊😊😅😮🎉д😊шу🎉дно😮🎉н😮😅в😮😊е🎉вдг😊н😢Юрченко з🎉щЩоювдр🎉😊ш😮😊ее🎉шэйш😅?😊г

      @sekssgenoi@sekssgenoiАй бұрын
    • 😊ш😅

      @sekssgenoi@sekssgenoiАй бұрын
    • Üüüäüüä000ä​@@ollmacüä00ü0ü0

      @dragonlord3504@dragonlord3504Ай бұрын
  • How did i fall asleep to true crime and bodycam videos and have these videos play during my sleep? Ive mever searched for a coding video ever 😂

    @theshelbizzle24@theshelbizzle2418 күн бұрын
  • Just woke up, first time waking up to this channel. Reading these comments is surreal.

    @bw4265@bw426513 күн бұрын
  • Every single time when I fall asleep I wake up to this channel 😭

    @xxblueberryxx8287@xxblueberryxx828717 күн бұрын
  • I fell asleep and this was playing KZhead why do you make me go to the weirdest places😂

    @forbiddengamers8068@forbiddengamers80682 ай бұрын
    • same here lol

      @uncandneph146@uncandneph1462 ай бұрын
    • just woke up, this has been playing for 3 hours 💀💀

      @Cadec33@Cadec33Ай бұрын
    • I WOKE UP TO THIS ?!?!?!

      @LeLocoTV@LeLocoTVАй бұрын
    • @@LeLocoTV Same bro same

      @mentalhealthcumulative@mentalhealthcumulativeАй бұрын
    • Another soldier here who wake up to this lol

      @CryptoMoney02@CryptoMoney02Ай бұрын
  • I keep waking up to this channel it’s the fifth time!!!

    @robbiepatrick6755@robbiepatrick6755Ай бұрын
    • Same😂😂

      @kyrielnieves3224@kyrielnieves322428 күн бұрын
    • I blame NileRed

      @WinWin-oo4uk@WinWin-oo4uk20 күн бұрын
    • Any of you guys watch any Terraria conten? I think that's what's bringing me here because the rest of my stuff is fairly politically oriented.

      @crypticscrutiny1153@crypticscrutiny115318 күн бұрын
    • @@crypticscrutiny1153yo actually, same… that’s so wild

      @coolio464@coolio46418 күн бұрын
    • @@coolio464 that's what's up! That's what's happening. The algorithm is bringing in Real Ones from Terraria content.

      @crypticscrutiny1153@crypticscrutiny115318 күн бұрын
  • I fell asleep with KZhead on and woke up an hour 30 mins into this video lol

    @BulletAgario@BulletAgarioАй бұрын
  • i fell asleep watching youtube, wake up and check that this video is 30 mins watched in my recently watched, the scary thing is that the was coding in my dream. I’ve never coded before

    @carsonplayzgaming9691@carsonplayzgaming96915 ай бұрын
    • Ha! Same here😄

      @benshakespeare268@benshakespeare268Ай бұрын
    • I fell asleep and woke up to this too

      @comoplaysdestiny5106@comoplaysdestiny5106Ай бұрын
    • Me too, mine the coding.

      @JLneonhug@JLneonhugАй бұрын
    • Me too

      @fatyoshi156@fatyoshi156Ай бұрын
    • Me too !

      @martinjankovski680@martinjankovski680Ай бұрын
  • Fell asleep to squishy and slime videos and woke up to this, 10/10.

    @fanona112@fanona1127 күн бұрын
  • Please don't stop making these videos, you're providing a ton of value.

    @rowancode@rowancode Жыл бұрын
  • Thank you so much, Chris, my son really adores your channel! He claims that you inspired him to become a "Front End Developer." Your hard work means a lot for me as much as it does for my son. Keep up the great work!

    @nasiba8730@nasiba8730 Жыл бұрын
  • Keep making these tutorials man, im an aspiring front end web dev with no time to for personal projects like learning unity so im very grateful i can still express my creativity by doing these thanks to you

    @trevorfranks69@trevorfranks69 Жыл бұрын
  • I fall asleep to football (soccer) videos every night and yet I’m waking up to this played two hours through

    @GH0STPROD1GY@GH0STPROD1GY5 сағат бұрын
  • It’s crazy, I keep waking up to this video most nights 😭

    @user-cw9md5gv5x@user-cw9md5gv5x11 сағат бұрын
  • bro...im someone who barly comments on youtube videos but without starting to watch this one I allready want to thank you. "You are a good man!"

    @Alexei1983121212@Alexei1983121212 Жыл бұрын
  • Just woke up to this channel and reading these comments are so funny

    @Octa@Octa13 күн бұрын
  • Another awesome tutorial! Your approach of adding layers of complexity little by little, without losing clarity and objectivity, is really cool. Congratulations and please continue!

    @joaocesarlima7339@joaocesarlima733911 ай бұрын
  • Your last video helped me get a job, can't wait to build this. thanks again brother Chris.

    @dabzbmg3197@dabzbmg3197 Жыл бұрын
  • Omg I love tower defense games!! Would be so much fun to practice javascript by making one of these. THANK YOU for this video 🙏🏾💜🙌🏾

    @andydataguy@andydataguy Жыл бұрын
  • Very cool. I watched first mins of showcase then coded everything in my own way. Thanks for inspiration, assets, and I love your canvas videos!

    @adamjankowski5755@adamjankowski5755 Жыл бұрын
  • In the near future when I will get a job , I promise I will donate money to your account, your tutorials are amazing and the way you explain is simple and straight to the point. I wish my college professors were teaching us the way you do. All the best

    @user-fw9ns7rm1v@user-fw9ns7rm1v Жыл бұрын
  • Ok, hoje eu vou dormir às 4 da manhã e a culpa é sua. Ok, today I will sleep at 4am and is your fault. Ty m8, keep going. I learn a lot each video.

    @forati@forati Жыл бұрын
  • We need to get this legend to 1M subscribers at all cost!

    @trashinGame@trashinGame Жыл бұрын
    • Plz do 😩

      @ChrisCourses@ChrisCourses Жыл бұрын
    • @@ChrisCourses you are the best

      @oldgames6028@oldgames6028 Жыл бұрын
    • ​@@ChrisCourses you are the best programmer

      @user-oy8sy1wm5y@user-oy8sy1wm5y Жыл бұрын
  • I keep waking up to this video I can’t believe it’s happening to others too

    @zippityzoop1478@zippityzoop14783 күн бұрын
  • Yes!!! I've been waiting for this video since you announced the video! I'm really glad I found your channel. You explain things SO well and I didn't even realize you could create games with only JavaScript. Keep up the great work!

    @noahr1545@noahr1545 Жыл бұрын
    • Thanks Noah, been putting in that work, but have more to go to make the channel succeed. Appreciate the comment and for tuning in!

      @ChrisCourses@ChrisCourses Жыл бұрын
    • No problem!

      @noahr1545@noahr1545 Жыл бұрын
  • So everyone is also waking up to this guy 💀

    @christophersvatora6658@christophersvatora665810 күн бұрын
  • Woke up here again.

    @name_69420@name_69420Ай бұрын
  • Dude how did I end up here 😭 I wasn’t even on KZhead when I slept?

    @SkunkonHeels@SkunkonHeels10 күн бұрын
  • In KZhead there are not many tutorial on Canvas. Thanks for it.

    @abhijitbarman4401@abhijitbarman4401 Жыл бұрын
  • It’s the third time i wake up to this channel

    @Tomisthere@Tomisthere13 күн бұрын
  • Chris, hi from Brazil! Can you make a javascript tutorial on how to display a div on a button click, the div being always attached to the button by the ID, and show above/bottom or left/right of the button when this button is positioned at top, bottom, left, right of the window? I'm bangging my head against the wall trying to do this!

    @marcelo.victor@marcelo.victor Жыл бұрын
  • anyone know what that default font is that is used on mac? want to get it for my linux pc.

    @unixrebel@unixrebel9 ай бұрын
  • Commenting to reference you, thank you so much for your videos, they're top quality and extremely helpful !!

    @fmi1632@fmi1632 Жыл бұрын
  • Why is everyone waking up to this and why did I wake up to this too

    @planetaryescape5794@planetaryescape57949 күн бұрын
  • Amazing tutorial, very well done my dude. Thanks so much for all the effort you put into this. Definitely a subscriber now!

    @machochocolate7679@machochocolate76798 ай бұрын
  • Chris thanks for your courses words can't describe how they have improved my game development skills, I even got my first paid project with the knowledge of my game dev 🚀🚀

    @adefunkeadedoyin9338@adefunkeadedoyin9338 Жыл бұрын
    • Awesome, love hearing that. Feel free to use any of my code on GitHub to help further your career, I also hope the next videos I create further your knowledge even more-so🙏

      @ChrisCourses@ChrisCourses Жыл бұрын
    • @@ChrisCourses thanks man. God bless 🚀🚀

      @adefunkeadedoyin9338@adefunkeadedoyin9338 Жыл бұрын
  • Awesome, did you use any specific program to actually make the tileset.png? I would like to build a game based of this tutorial but not sure how I would merge the assets together to one file. Perhaps that's not really necessary?

    @jonathanmanner5616@jonathanmanner5616 Жыл бұрын
    • You can use basically any graphics editor. Photoshop, Affinity, Photopea, Krita, etc.

      @WyzrdCat@WyzrdCat Жыл бұрын
  • Great video! Could you please also make a video on how we could monetize these games built with Javascript? Maybe also release them on Android or IOS? I am really thinking of this as a full time job.

    @babaakshay@babaakshay Жыл бұрын
  • Wouldn't it be more sensible and robust to set objects in an array to null instead of splicing them out. And just check for a null (which you should always do anyways)? At least you don't need to worry about updating an array whilst looping through it. Either way this is easier than in C/C++ where you'd actually have to register which struct/object you want to free/delete. Also i noticed that projectile path is update every frame, so you have a "hear seeker" rock :D

    @CallousCoder@CallousCoder Жыл бұрын
  • KZhead redirected me from a series of videos to to this (it started with cleaning uranium waste)

    @i-lack-creativity@i-lack-creativity2 ай бұрын
  • Why do you use classes??? Shouldn't we be on Functional programming?

    @kashmirtechtv2948@kashmirtechtv29489 ай бұрын
  • You have changed my perspective on game dev!!

    @ritarshichattaraj9552@ritarshichattaraj9552 Жыл бұрын
  • at around 40 minutes in, there is a bug with the canvas getContext. the browser console says that is cannot read the property of null

    @namyak-bf9od@namyak-bf9od Жыл бұрын
  • when I use position: { x:waypoints[0].x, y:waypoints[0].y I got - "ReferenceError: can't access lexical declaration 'enemy' before initialization" and I have no idea why.

    @Suzuhara@Suzuhara Жыл бұрын
  • Hi, how to convert .fla to .png, or fla to something else to use in Unity?

    @hungle-wi2bj@hungle-wi2bj Жыл бұрын
  • the moment i paste in the second svg everything goes black barring the newly pasted icon. Really confused can't seem to fix this either. Anyone have any idea what's happening/how i can fix this?

    @tome343osu4@tome343osu4 Жыл бұрын
  • I don't know how you create, such long courses ... I'll be able to do them too, sooner or later 😝 Great content

    @universecode1101@universecode1101 Жыл бұрын
    • Lots of sitting down, messing up, then trying again until I get it right 😅 Best of luck on the projects!

      @ChrisCourses@ChrisCourses Жыл бұрын
  • I'm gonna buy your courses man! it's amazing! Thank you a lot!!

    @djoezi3662@djoezi3662 Жыл бұрын
  • I fell asleep watching cube marching and woke up to this

    @ARandom.Channel.@ARandom.Channel. Жыл бұрын
  • bro i usually but sleeping vids to sleep bit when the vid ends and i dont even know it just Kepa plaiyng cuz its on a charger so when i wake up i wake up to this;-;

    @jasminbratz298@jasminbratz2989 күн бұрын
  • Thank you very much Chris, your tutorials are fantastic!

    @jgvlc@jgvlc Жыл бұрын
    • Glad you think so! Got more on the way 💪

      @ChrisCourses@ChrisCourses Жыл бұрын
  • At 1:22:00 i got Uncaught ReferenceError: Cannot access 'enemy2' before initialization at animate (index.js:49:5) cannot fix it, const enemy = new Enemy({ position: { x: 100, y: 100 } }) const enemy2 = new Enemy({ position: { x: waypoints[0].x, y: waypoints[0].y } }) first work but second dont work how can i fix it?

    @johanngerhardt401@johanngerhardt4019 ай бұрын
    • I found out that the order of importing js files might cause the issue. Can you make sure that you import the waypoints.js before the index.js in the index.html?

      @Gregor__Samsa@Gregor__Samsa8 ай бұрын
    • @@Gregor__Samsa have the same issue. it didn't help

      @egostan@egostan2 күн бұрын
  • Eu gostaria de passar um jogo desses para mobile, eu consigo??

    @kalebdasilvavitorino2877@kalebdasilvavitorino2877Ай бұрын
  • Woke up to this too! What the heck?😅

    @TomNode@TomNode11 күн бұрын
  • how did you get the sprite

    @user-pc8ip7hv5w@user-pc8ip7hv5w10 ай бұрын
  • How to create tower.png with different tower. Your resource have rock tower. I need to create more tower with png. Thank you so much.

    @hungle-wi2bj@hungle-wi2bj Жыл бұрын
  • Yeahhh another one ! Thank you so much to make these awesome videos accessible to everyone !

    @anawan@anawan Жыл бұрын
    • Chapters, captions, free === the ultimate accessibility tuts 😎

      @ChrisCourses@ChrisCourses Жыл бұрын
  • 100% Im feeling the next one is going to be a lovely RPG game with basic sprite sheets, teleport from one place to another, basic inventory and some juicy cool extra shenanigans things, im looking towards it. I love your work and patience to explain everything, keep the energy and vibes all the way up !!

    @rustyrustan@rustyrustan Жыл бұрын
  • 1:27:00 I don't give an intial x and y position with my constructor, since this information is in the waypoints array anyway. When changing the initial x position after going through the initialisation array, the code always writes the x position for the last element to all elements in the array. Can anyone explain why this could be? I guess it has something to do with how js refers to objects, but I am not well versed in this language. My intuition tells me this should work. const enemies = []; for (let i = 0; i < 10; i++) { enemies.push(new Enemy()); } for (let i = 0; i < 10; i++) { const xOffset = i * 150; enemies[i].position.x -= xOffset; console.log(enemies[i]); }

    @JasonRobards2@JasonRobards2 Жыл бұрын
    • This was my constructor and apparently this does not work. I don't know why. class Enemy { constructor() { this.waypointIndex = 0; this.position = waypoints[this.waypointIndex]; It kept overwriting so that all enemies in the array had the same position.

      @JasonRobards2@JasonRobards2 Жыл бұрын
    • This works: class Enemy { constructor() { this.waypointIndex = 0; this.position = { x: waypoints[this.waypointIndex].x, y: waypoints[this.waypointIndex].y }; I think the previous didn't work because it copied the label to this.position rather than the actual values, although I am not entirely sure what exactly happened. I am going to leave this little convo with myself in case someone else can make use of it :)

      @JasonRobards2@JasonRobards2 Жыл бұрын
  • i fell asleep and woke up to my phone on 13% and i was 59:59 minutes in

    @cdgfilms1004@cdgfilms100423 күн бұрын
  • Why does KZhead do this I wake up and BAM im 3 hours into the same video every time. HOW DOES YT KNOW???BUT

    @2DiamondShovelsX@2DiamondShovelsX11 күн бұрын
  • Can we have three js game

    @user-gf9ri4wj5h@user-gf9ri4wj5h Жыл бұрын
  • Wow just wow. More stuff like this

    @gofudgeyourselves9024@gofudgeyourselves9024 Жыл бұрын
  • It’s awesome bro. Learning a lot.

    @shaikhsanuar5339@shaikhsanuar5339 Жыл бұрын
  • Adding another comment to the pot by saying, yes I also did wake up to this and yes I do find these comments funny lol

    @a_memer@a_memer2 күн бұрын
  • Me dormí viendo KZhead y aparecí acá… 57:32

    @LxraXJkS@LxraXJkS12 күн бұрын
  • Great 👍 tutorial thank you 😊 Hello Chris how are u? I have a question. I want to make app like webflow So should i use canvas to make that kinda project or not

    @kinopremyera2753@kinopremyera2753 Жыл бұрын
  • hey man please built brick breaker type game

    @saurabhrawat3878@saurabhrawat3878 Жыл бұрын
  • you are so well organized, this helps us a lot !

    @simon-hb7wf@simon-hb7wf Жыл бұрын
    • I do pride myself on the organization / chapter time-stamps of these, put a lot of time into making everything is exactly where it needs to be 😄

      @ChrisCourses@ChrisCourses Жыл бұрын
    • @@ChrisCourses and that's why after more than 3 years following your channel, I'm always happy to watch a new video from you ! 👨‍💻

      @simon-hb7wf@simon-hb7wf Жыл бұрын
  • I also woke up to this video

    @ramon4756@ramon475612 күн бұрын
  • Holy shit... 5 hours? 😅 Looks Interesting and challenging so yeah, I'm in ✅

    @wisdomochei7750@wisdomochei7750 Жыл бұрын
    • Hell yeah man, welcome, hope the project went / is going well 🙌

      @ChrisCourses@ChrisCourses Жыл бұрын
  • Thank you a lot Chris. I love your courses.

    @martinbobbio2211@martinbobbio22118 ай бұрын
  • Thank you for your work i like how you doing videos

    @goldenPixeler@goldenPixeler Жыл бұрын
  • time well spent !

    @noir66146@noir661463 ай бұрын
  • this is a superb tutorial - first rate explanation - great teacher

    @djneils100@djneils100 Жыл бұрын
    • Thank you very much!

      @ChrisCourses@ChrisCourses Жыл бұрын
  • This is Awesome Bro :)

    @vin2368@vin2368 Жыл бұрын
  • All you had to do is used two inner corner pieces

    @jasoncain5552@jasoncain5552 Жыл бұрын
  • your explaintions are so good.

    @spanch1bobr@spanch1bobr Жыл бұрын
  • GO CHRIS!

    @sandrallancherosg@sandrallancherosg Жыл бұрын
  • Thanks

    @naedid2512@naedid2512 Жыл бұрын
  • Really awesome 👍🙏

    @taranewstime6909@taranewstime6909 Жыл бұрын
  • Awesome brother thanks for ur hardwork

    @chetanuzumaki9581@chetanuzumaki9581 Жыл бұрын
    • No prob, thanks for watching 🙏

      @ChrisCourses@ChrisCourses Жыл бұрын
  • Спасибо!

    @gisen100ify@gisen100ify5 ай бұрын
  • I fell asleep to cleaning videos where tf am i

    @macealred4135@macealred413529 күн бұрын
  • Maahnn this video is fire!!!!....🔥🔥🔥

    @otisrancko@otisrancko Жыл бұрын
  • Wth i learned coding while sleeping and this was why wth WTH

    @thecoolnessking5834@thecoolnessking583417 күн бұрын
  • Woke up to this wtf

    @InfectAion@InfectAion7 күн бұрын
  • Great! Many thank

    @HuynhLuong227@HuynhLuong227 Жыл бұрын
  • The best!

    @aftercyberneticrp@aftercyberneticrp Жыл бұрын
  • At 15:00.. felt like the longest 5 minutes... How can you spend that long doing 1 thing?

    @daveinpublic@daveinpublic5 ай бұрын
  • time stamp: 4:54:44

    @andc6016@andc6016 Жыл бұрын
  • احسنت

    @user-ih3ud7pf8q@user-ih3ud7pf8q Жыл бұрын
  • You rock!

    @pedromesquita9806@pedromesquita9806 Жыл бұрын
    • You rule!

      @ChrisCourses@ChrisCourses Жыл бұрын
  • love u man

    @japsgy@japsgy Жыл бұрын
    • ❤️🙏

      @ChrisCourses@ChrisCourses Жыл бұрын
  • WAWW

    @AnimeWarriorPlays@AnimeWarriorPlays Жыл бұрын
  • Fell asleep watching strongest anime characters where tf

    @BigCereal-ze6vm@BigCereal-ze6vm26 күн бұрын
  • I literally fell asleep watching Hell’s Kitchen… KZhead do better😂

    @ericawoodhams1521@ericawoodhams1521Ай бұрын
  • Why is everyone waking up to this? It’s so weird (not the video just the phenomenon) I wonder what algerithm is making the auto play go to this

    @victoriahtrab6640@victoriahtrab66402 күн бұрын
    • So I hit the back button to see what previous videos I watched, and the one before is the one I was watching when I fell asleep, which means youtube sent me straight to this one 🤔😶

      @victoriahtrab6640@victoriahtrab66402 күн бұрын
  • 🤯🤯🤯🤯🤯🤯🤯🤯🤯🤯🤯🤯🤯🤯

    @S-Lomar@S-Lomar8 ай бұрын
  • Ja some male wx reaction

    @doyouwantsli9680@doyouwantsli9680Ай бұрын
KZhead