Create Tetris Game Using JavaScript and HTML5 | JavaScript Project For Beginners

Поделиться
HTML-код
  • Опубликовано: 6 сен 2024

Комментарии • 205

  • @CodeExplained
    @CodeExplained  3 года назад +5

    🚨 Big Announcement! 🚨
    Hey everyone! I've made a big decision for this channel-I've turned off ads on all my videos! That's right, you can now enjoy all my content ad-free! This means I'm earning $0 from ad revenue, but I believe this is the right move to make your viewing experience better.
    If you find my content valuable and want to support my work, there are several ways you can help:
    Like & Share: Simply liking the video and sharing it with friends helps a lot!
    Subscribe & Hit the Bell: Stay updated and never miss a new video.
    RUclips Thanks: Use the "Thanks" button below to send a one-time donation.
    Join the Channel Community: Get exclusive perks and behind-the-scenes content.
    Buy Me a Coffee: buymeacoffee.com/CodeExplained
    PayPal Donation: www.paypal.com/paypalme/CodeExplained
    Every little bit counts and goes directly to creating more high-quality content for you all. Thank you so much for your continued support!
    Let’s keep growing and learning together! 🌟

  • @santiso878
    @santiso878 6 месяцев назад

    Finally someone explaining how to create Tetris the proper way, thank you!

  • @twosongs7396
    @twosongs7396 2 года назад +3

    Thank you so much for this enlightening video. I much appreciated you going step by step, deciphering the algorithm, to THEN put it in code. You’re a natural Teacher.

  • @zoomstop
    @zoomstop 6 лет назад +11

    This looks awesome, shared this with some friends who want to try JS but fear a big time commitment.

  • @m1groff
    @m1groff 5 лет назад +4

    Thank you for the very well explained, in depth tutorial. This very much helped me gain more understanding on working on 'canvas' . No complaints, much appreciated!

  • @chansaem
    @chansaem 3 года назад +1

    You're an amazing teacher. You made me make my mind to challenge this project and feel confident since I think I could understand what this is about thoroughly for the first time. I might be studying hard the formidable JavaScript monster thanks to you.

  • @girlingame518
    @girlingame518 4 года назад +4

    wow , such a useful lesson and amazing explanation , i love how you break it down into pieces .bravoo

    • @CodeExplained
      @CodeExplained  3 года назад

      Happy to hear that, thanks a lot ❤

  • @jamescao2008
    @jamescao2008 2 года назад +1

    Thanks for your very good game in js. Question: in lock(), please recheck that adding drawBoard() at last might break the game so that the first tetrimino from top will go down through the bottom wall then disappear.

  • @lietuvisdarius
    @lietuvisdarius 4 года назад +2

    Very interesting tutorial! At the moment i'm converting this game to React hooks, learned a lot about x and y coordinates. I like the way you explaining, firts what you trying to archieve and only then go to code. Genius. P.S. the way you use for loops its just incredible, you must be experience game developer.

    • @CodeExplained
      @CodeExplained  3 года назад

      Happy to hear that, thanks a lot ❤

  • @EduardoWeidmanBarijan
    @EduardoWeidmanBarijan 5 лет назад +3

    Awesome tutorial. I like that you did explain all the game logic before diving into the code.

  • @christophercampbell6884
    @christophercampbell6884 3 года назад +1

    31:30 You didn't actually have to define each rotated piece because if you took the rows top to bottom, and wrote it as columns from right to left, you would get the rotated piece. For example, you can see 1 1 0 in Z[0] as the third column in Z[1], and 0 1 1 in Z[0] as the second column in Z[1]. I just know that's how programs like MS Paint do it.

    • @CodeExplained
      @CodeExplained  3 года назад

      Yeah that's a way of doing it, but I choose to do it the easy way.
      Still your way of doing it is the right way, cause you won't have to repeat yourself.
      Thanks.

    • @christophercampbell6884
      @christophercampbell6884 3 года назад

      @@CodeExplained Cool, just wanted to let you know.

  • @qorrri
    @qorrri 7 месяцев назад

    This is so good,my hope of learning is back,thank you👏

  • @chiragrao9144
    @chiragrao9144 5 лет назад +2

    You are the best at explaining stuff. I love your videos.

  • @owenelwin1452
    @owenelwin1452 5 лет назад +2

    Awesome video! Well animated and colorful! I will definitely go through this video and teach myself very soon as soon as I get my next chance !

  • @jolly8953
    @jolly8953 5 лет назад +1

    Wasn't even using javascript, but this still helped me!

  • @evelienkulikova8825
    @evelienkulikova8825 5 лет назад

    It works! Thanks for explaining the logic behind the game before writing the whole code. This is really useful. Thanks a bunch!

  • @tonguyenducmanh
    @tonguyenducmanh 4 года назад +1

    very good bro. Thank you very much for this tutorials. I watched many tutorials before but they don't explain like this. Thank u

    • @CodeExplained
      @CodeExplained  3 года назад

      Happy to hear that, thanks a lot ❤

  • @lilmoderatepollution4447
    @lilmoderatepollution4447 6 лет назад +5

    Damn. Gonna watch this when I get the time.

  • @sr_aman
    @sr_aman 6 лет назад +1

    Awesome! Keep up the good work. Will share with all my friends.

  • @lomao9081
    @lomao9081 3 года назад

    You are the best of the best. The best tuto ever. You are a killer.

  • @_slier
    @_slier 3 года назад

    The best explaination on building tetris.. Thx!

  • @buttslaya
    @buttslaya 4 года назад +1

    Can you do these sorts of tutorials with algorithms and data structures in mind? I really appreciate that you really do break down all the logic in your videos.

    • @CodeExplained
      @CodeExplained  3 года назад

      Happy to hear that, thanks a lot ❤
      I'll try to

  • @indienamix3581
    @indienamix3581 3 года назад

    The result and the process is really good, i would only change the way how you rotate pieces, by replacing consts Z,O by a function that rotates a matrix

  • @GetDazeDotCom13
    @GetDazeDotCom13 5 лет назад +1

    const canvas = document.getElementById("tetris");
    const context = canvas.getContext("2d");
    const ROW = 20;
    const COL = COLUMN = 10;
    const SQ = squareSize = 20;
    const VACANT = "WHITE"; // color of an empty square
    // draw a square
    function drawSquare(x, y, color) {
    context.fillStyle = color;
    context.fillRect(x*SQ, y*SQ, SQ, SQ);
    context.strokeStyle ="BLACK";
    context.strokeRect(x*SQ, y*SQ, SQ, SQ);
    }
    // create the board
    let board = [];
    for (r = 0; r < ROW; r++) {
    board[r] = [];
    for (c = 0; c < COL; c++) {
    board[r][c] = VACANT;
    }
    }
    // draw the board
    function drawBoard() {
    for (r = 0; r

    • @iprih
      @iprih 5 лет назад

      O meu também apresenta o erro (function Piece):
      This constructor function may be converted to a class declaration.
      Help!

    • @GetDazeDotCom13
      @GetDazeDotCom13 5 лет назад

      @@iprih I don't understand. I am completely new to this and it's my first time building a game app. Please help!!!

    • @CodeExplained
      @CodeExplained  5 лет назад

      const PIECES = [
      [Z, "red"],
      [S, "green"],
      [T, "yellow"],
      [O, "blue"],
      [L, "purple"],
      [I, "cyan"],
      [J, "orange"]
      ];
      [S, "green"], I think you didn't define S tetromino,, Where Z, T ... J are defined.

  • @zathkal4004
    @zathkal4004 3 года назад

    Amazing tutorial and great example of a class construct object... thank you sir

  • @marko90945
    @marko90945 6 лет назад +2

    Dude, make more. Thanks for your detailed tutorials!

    • @CodeExplained
      @CodeExplained  6 лет назад

      You're welcome.

    • @marko90945
      @marko90945 6 лет назад

      Code Explained You're the only one who first explains the logic and then proceeds to the actual coding process. I'm looking forward to more tutorials from you!

    • @CodeExplained
      @CodeExplained  6 лет назад

      happy to hear that, and I appreciate what you said, and of course I will make more videos.

  • @imnotblackpinkfanimtheirai5300
    @imnotblackpinkfanimtheirai5300 5 лет назад +1

    Your channel is underrated

    • @CodeExplained
      @CodeExplained  5 лет назад

      I think it's because I don't post often. you know the youtube algorithm takes that in mind.

    • @ArTDesigner22
      @ArTDesigner22 4 года назад

      @@CodeExplained First of all, a very big thank you. Then, I think your concept is very powerful. Your project has a lot of future if you continue to post regularly. By the way, are you thinking about posting other videos?

  • @berkaykucuk3916
    @berkaykucuk3916 5 лет назад +1

    Best Tetris tutorial. Thank you

  • @NirmalSingh-gk2wp
    @NirmalSingh-gk2wp 5 лет назад

    you are awosome for beginner level boom . i love it superrrrrrrrrrr

  • @ultimatechallenge540
    @ultimatechallenge540 2 года назад

    How to solve if there is ball of tetris instead of tetris then how to remove three ball if the three ball touches each other of same color either horizontally or vertically

  • @vt_near
    @vt_near 6 лет назад +3

    Gonna go through this really soon

  • @yukselbey913
    @yukselbey913 6 лет назад +4

    Thank You. You are explaining very well all the details. Could you make another project about how to make a multiple choice test? Please share more project or Javascript Lessons with real-life project like that, memory game, multiple choice test. Thanks again.

    • @CodeExplained
      @CodeExplained  6 лет назад +2

      More tutorials are coming.

    • @yukselbey913
      @yukselbey913 6 лет назад

      Thanks a lot. I hope soon I can see multiple choice test with HTML and Javascript.

    • @CodeExplained
      @CodeExplained  6 лет назад

      do you have an example of the test ?

    • @yukselbey913
      @yukselbey913 6 лет назад +1

      I am sorry I could not answer immediately. Something like that multiple choice, the user will pick the answer and continue next question. Each time after the answer or end of the test will calculate the correct answers. If We understand the logic we can increase the question. You are explaining very well. Thanks a lot.
      QUESTION 1 of 10
      Which of the following is a valid HTML tag?
      O) h1
      O)
      O)
      O) >h1<
      the user will click Button to Continue maybe next page or same page.
      QUESTION 2 of 10
      Which of the following is an example of metadata about a webpage?
      A) The title of the webpage
      B) The body of the webpage
      C) An tag on the webpage
      D) All of the above
      the user will click Button to Continue maybe next page or same page.

    • @lilmoderatepollution4447
      @lilmoderatepollution4447 6 лет назад +1

      If you learn a framework like Vue.js (which sounds perfect for what your trying to build). You could make something like that really easily. You could have a javascript object array with with two things in each object: question and a letter (a,b,c,d) corresponding to the correct answer. And when the user gets it correct, you could keep +1'ing a variable to keep track of the score. Of course I dont know how much expericne you have with JS but I highly suggest learning vue.js

  • @gorgono1
    @gorgono1 4 года назад

    This video is amazing and explains everything very well. Thank you

  • @priyamyadav6378
    @priyamyadav6378 5 лет назад

    This really helped for my final year project! Thanks!

  • @davidsalomonsson7130
    @davidsalomonsson7130 5 лет назад +2

    Greetings. Im enjoying your videos so far but there is one problem. I get an error message saying that "Z is not defined". Since im a noob i do not know how to advance. This happens if i copy paste your code from Github aswell. Any ideas on how to tackle this issue? Regards

    • @CodeExplained
      @CodeExplained  4 года назад

      You have to link your HTML file to two external JS files. the first is for the tetrominos and the second is for the general code.

  • @clouds4281
    @clouds4281 4 года назад

    It works finally, thanks a lot! By the way, i want to show the next tetromino on the screen, but i have no idea how to do it. Could you give me some advice?

    • @CodeExplained
      @CodeExplained  4 года назад +1

      Create another canvas next to the first one. so you can draw the tetromino on it with the same old functions.
      After the first tetromino appears. go and choose one randomly. save its index. draw it to the 2nd canvas.
      Repeat the process.

  • @kanalpaka4976
    @kanalpaka4976 4 года назад

    We need more videos 👏👏👏

  • @mitvielbutterdrauf
    @mitvielbutterdrauf 6 лет назад +5

    Just a heads up on the title
    Pure !== Vanilla
    Pure = Functional non-mutating code
    Vanilla = Without any dependencies

    • @CodeExplained
      @CodeExplained  6 лет назад +1

      I avoid using Vanilla, because people may think it's kind of a framework.

    • @davidtsui8137
      @davidtsui8137 6 лет назад

      Pure != Functional non-mutating code, do not put PureScript idea on this "Pure",

    • @mitvielbutterdrauf
      @mitvielbutterdrauf 6 лет назад +2

      en.wikipedia.org/wiki/Purely_functional_programming
      This was not a reference to Purescript.

  • @rohmaabdulfattah2582
    @rohmaabdulfattah2582 11 месяцев назад

    const pieces = [
    [Z, "red"],
    [S, "green"],
    [T, "yellow"],
    [O, "blue"],
    [L, "purple"],
    [I, "cyan"],
    [J, "orange"],
    ]; hw come the shapes arent quoted in a string and they work,here,i am getting error Z undefined,S undefined

  • @jonathanrobson6758
    @jonathanrobson6758 5 лет назад +1

    Thanks. This is my first go at javascript. How would I replace the colour fills with image files?

    • @CodeExplained
      @CodeExplained  5 лет назад

      Yes you can, you need to load the image first, and then draw the image to your canvas, google that and you will know how.

  • @salyiohh
    @salyiohh 6 лет назад +1

    Great work , you explain steps very good way , any courses on udemy ?

  • @taminhphuoc9812
    @taminhphuoc9812 Год назад

    Thank you so much

  • @mikaelgevorgyan4521
    @mikaelgevorgyan4521 4 года назад +1

    What are you doing))))?
    canvas => id => tetris.
    And you are write => document.getElementById( // tetris =>this Id , it is not Id => 'canvas');
    cvs.getContext('2d'); => output => null
    let cvs = document.getElementById('tetris');
    cvs.getContext('2d');=>it is working!!!

  • @PherthePrinny
    @PherthePrinny 4 года назад +1

    So not sure what I am doing wrong that no one else seems to have a problem with at least no one in the comments. I followed you all the way up to making a red square using brackets live preview.
    I can not seem to get a red square to appear at all. I have checked and rechecked to make sure that the codes are identical and I still have nothing. I even started a new project and copied everything exactly as the links using github. I have my index.html, tetris.js, and tetrominoes.js. When I do live preview after copying the files I get, This Game was created by code explained and score: 0. Which I understand is written in the index.html. Still new to coding but this leads me to believe that something is not connecting my tetris.js to me index.html. Sorry for long winded description. Hope this is descriptive enough to help fix my problem.

    • @PherthePrinny
      @PherthePrinny 4 года назад +2

      After a while of searching i found out that i needed install brackets-eslint through the extension manager. I am still new to this program so i am not sure if this was the best way to fix my problem. But it has seemed to fix it, for anyone else having this issue.

    • @CodeExplained
      @CodeExplained  3 года назад

      Good you fixed it

  • @IvanIsayev
    @IvanIsayev 4 года назад

    Very cool explanation! 👍🏽

  • @umar4133
    @umar4133 2 года назад

    Hello, I have actually enjoyed your teaching so much because it is the best I have seen on youtube. But there is a problem I have been battling with, the array for the PIECES keeps showing me an error ("Uncaught ReferenceError: Z is not defined") so I can see my piece on the game board. Could you please guide me through the solution?
    Thanks

    • @CodeExplained
      @CodeExplained  2 года назад

      Hey Umar,
      Did you link the script file where all the tetrominoes ( L, Z, O, ... ) are defined with your html file?

    • @rohmaabdulfattah2582
      @rohmaabdulfattah2582 11 месяцев назад

      pls where can i get the script file?
      @@CodeExplained

  • @MilanBenTechRepair
    @MilanBenTechRepair 6 месяцев назад

    thx so much👍

  • @TechWiBi
    @TechWiBi 3 года назад

    nice

  • @GermanyDeutschland
    @GermanyDeutschland 5 лет назад

    45:42 please what do you mean that the top row has no rows above it? do u mean: if the above row is full then it will be cleared ?

    • @CodeExplained
      @CodeExplained  3 года назад

      When a row is cleared, we move down all the rows to the bottom one by one, if we do this the very top row will be empty (not exists) so we need to create it.

  • @anos3196
    @anos3196 4 года назад +1

    where is the pieces define in the codes (how to know the shape of the pieces (the 0 and arrays) ,you didn't write this in codes)????

    • @CodeExplained
      @CodeExplained  4 года назад

      It's there, the code is inside a separate js file, called, tetrominoes.js
      github.com/CodeExplainedRepo/Tetris-JavaScript/blob/master/tetrominoes.js

  • @thology7533
    @thology7533 6 лет назад +2

    Thanks bro

  • @greenape3929
    @greenape3929 5 лет назад

    Sweet Video! Is there a way to remove the black lines in the game board but keep the outer black lines?

    • @CodeExplained
      @CodeExplained  5 лет назад

      For the stroke of the board squares, make it white, and the ones for the pieces make it black.

  • @inhucle1486
    @inhucle1486 6 лет назад

    Excellent!!! Thank you!!!

  • @binarysaiyan9389
    @binarysaiyan9389 4 года назад +1

    Why is the O piece a 4x4 Matrix? There is no point in rotating the O piece

  • @sandeepa4116
    @sandeepa4116 3 года назад

    Best tutorials! May I know the software you use for the slides?

  • @giorgicagix6157
    @giorgicagix6157 4 года назад

    Great tutorial!

  • @bonkbonk2319
    @bonkbonk2319 3 года назад +1

    28:55

  • @ProtoFreeze
    @ProtoFreeze 4 года назад

    I'm getting the error " SyntaxError: expected expression, got '

    • @ProtoFreeze
      @ProtoFreeze 4 года назад

      Ok, I figured it out, and i feel stupid lol. the "

    • @SchettiniAndre
      @SchettiniAndre 4 года назад

      Heya! He's posted a link to github on the description. Once you're there you can compare you codes with his and sort it out.

  • @nominee28
    @nominee28 4 года назад +1

    51:22

  • @trovecohuong8
    @trovecohuong8 4 года назад

    Hello!
    I'm begginer, and make a game simple Dr. Mario, same the game tetris.
    Please help me, how to creat pill and remove pill when 4 pill same color with virus (3 dot pill with 1 virus same color > remove them)
    Thanks a lot!

  • @sergiyrudenko905
    @sergiyrudenko905 6 лет назад

    Thanks you very match!

  • @js_games_for_dummies
    @js_games_for_dummies Год назад

    adds: Как инвестировать на Бали? + fireblocks

  • @kmf9709
    @kmf9709 3 года назад

    so good !!

    • @CodeExplained
      @CodeExplained  3 года назад

      Happy to hear that, thanks a lot ❤

  • @S-Lomar
    @S-Lomar Год назад +1

    🤩😍😍🥰🥰🥰🥰🥰😍😍😍🤩😍😍🥰🥰🥰🥰🥰

  • @sylvielamy5592
    @sylvielamy5592 5 лет назад

    Mine isn't working. There is this message on thé console : moveDown is not a function on game line 274

    • @CodeExplained
      @CodeExplained  5 лет назад

      did you fix the problem?

    • @sylvielamy5592
      @sylvielamy5592 4 года назад

      @@CodeExplained nop :/ i don't now where is it. I have seen the code and moveDown is a function. I will do this again to see if I have the same problem.

  • @orangegaming9562
    @orangegaming9562 3 года назад

    How do you do movement
    I do document.addeventlistner(). (There is stuff in parenthesis) and I get it is not a function
    Also good tutorial, this is the closest to movement I got

  • @mariuseikenes9884
    @mariuseikenes9884 5 лет назад

    when you added the first piece, after the p.draw thing, it pops up a error on inspect, that .length of undefined cannot be read, at line 73

    • @mariuseikenes9884
      @mariuseikenes9884 5 лет назад

      andno piece aswell

    • @CodeExplained
      @CodeExplained  5 лет назад

      It means you want to get a length of an array which is not defined yet.
      check if you typed right the name of the array.

    • @mariuseikenes9884
      @mariuseikenes9884 5 лет назад

      Ive checked that dozens of times, and checked and i have the exact same script

    • @mariuseikenes9884
      @mariuseikenes9884 5 лет назад

      Checked everything again, all similar

    • @CodeExplained
      @CodeExplained  5 лет назад

      Show me your code.

  • @emerzon982
    @emerzon982 5 лет назад

    Please post a video where the tetriminoes exist.
    My game has no display.
    Thank you please notice this.

    • @emerzon982
      @emerzon982 5 лет назад

      And where the hell is the part 2 of the video? Everytime i click the link the browser goes to this previous video. >.

    • @CodeExplained
      @CodeExplained  5 лет назад

      Did you download the starter template from Our Repo on Github?
      The tetriminoes code is inside a file called: tetriminoes.js
      The video has two parts, the first when we explain the logic behind the game, and the second part when type the code. there is no other part any where.

  • @damynghevinhlocthanhhoa
    @damynghevinhlocthanhhoa 6 лет назад

    isnt ctx.scale(20,20) better?

    • @CodeExplained
      @CodeExplained  6 лет назад +1

      I think it won't work, did you try that ?

  • @Blue-sj2hl
    @Blue-sj2hl 5 лет назад

    at line 191 138 and 97 . it says length undefined

    • @CodeExplained
      @CodeExplained  5 лет назад

      the length of what ?

    • @Blue-sj2hl
      @Blue-sj2hl 5 лет назад

      @@CodeExplained piece.collision piece.movedown and drop

    • @CodeExplained
      @CodeExplained  5 лет назад

      Show me your code

    • @Blue-sj2hl
      @Blue-sj2hl 5 лет назад

      @@CodeExplained const cvs = document.getElementById('tetris');
      const ctx = cvs.getContext('2d')
      const ROW = 20;
      const COL = COLUMN = 10;
      const SQ = squareSize = 20;
      const VACANT = 'White';
      //color of empty square
      //draw square
      function drawSquare(x, y, color) {
      ctx.fillStyle = color;
      ctx.fillRect(x * SQ, y * SQ, SQ, SQ);
      ctx.strokeStyle = 'Black'
      ctx.strokeRect(x * SQ, y * SQ, SQ, SQ);
      }
      drawSquare(0, 0, 'red');
      //create the board
      let board = [];
      for (r = 0; r < ROW; r++) {
      board[r] = [];
      for (c = 0; c < COL; c++) {
      board[r][c] = VACANT;
      }
      }
      //draw the board
      function drawBoard(){
      for (r = 0; r < ROW; r++) {
      for (c = 0; c < COL; c++) {
      drawSquare(c,r,board[r][c]);
      }
      }
      }
      drawBoard();
      //pieces and their color
      const PIECES = [
      [Z,'red'],
      [S,'green'],
      [T,'yellow'],
      [O,'blue'],
      [L,'pruple'],
      [I,'cyan'],
      [J,'orange']
      ];
      //initiate piece
      let p = new Piece(PIECES[0][0], PIECES[0][1]);
      //The Object Piece
      function Piece(tetromino,color){
      this.tetromino = tetromino;
      this.color = color;
      this.tetrominoN = 0; //we start from first pattern
      this.activeTetromino = this.tetromino[this.tetrominoN]
      //control the piece
      this.x = 3;
      this.y = 0;
      }
      //fill function
      Piece.prototype.fill = function(color){
      for (r = 0; r < this.activeTetromino.length; r++) {
      for (c = 0; c < this.activeTetromino.length; c++) {
      //we only draw occupied squares
      if( this.activeTetromino[r][c]){
      drawSquare(this.x + c,this.y + r,color);
      }
      }
      }
      }
      //draw piece to board
      Piece.prototype.draw = function(){
      this.fill(this.color);
      }
      //undraw a piece
      Piece.prototype.unDraw = function(){
      this.fill(VACANT);
      }
      //move piece down
      Piece.prototype.moveDown = function(){
      if(!this.collision(0,1,this.activeTetromino)){
      this.unDraw();
      this.y++;
      this.draw();
      }else{
      //we lock piece and place new
      }
      }
      //move piece to right
      Piece.prototype.moveRight = function(){
      if(!this.collision(1,0,this.activeTetromino)){
      this.unDraw();
      this.x++;
      this.draw();
      }
      }
      //move piece left
      Piece.prototype.moveLeft = function(){
      if(!this.collision(-1,0,this.activeTetromino)){
      this.unDraw();
      this.x--;
      this.draw();
      }
      }
      //rotate the piece
      Piece.prototype.rotate = function(){
      let nextPattern = this.tetromino[(this.tetrominoN + 1)%this.tetromino.length];
      if(!this.collision(0,nextPattern)){
      this.unDraw();
      this.tetrominoN = (this.tetrominoN + 1)%this.tetromino.length;//(0+1)%4 => 1
      this.activeTetromino = this.tetromino[this.tetrominoN];
      this.draw();
      }
      }
      // collision function
      Piece.prototype.collision = function(x,y,piece){
      for (r = 0; r < piece.length; r++) {
      for (c = 0; c < piece.length; c++) {
      //if square empty skip it
      if(!piece[r][c]){
      continue;
      }
      //coordinates of piece after move
      let newX = this.x + c + x;
      let newY = this.y + r + y;
      //conditions
      if(newX < 0 || newX >= COL || newY >= ROW){
      return true;
      }
      // skip newY < 0; board[-1] will crash game
      if(newY < 0){
      continue;
      }
      //check if there is already locked piece on board
      if(board[newY][newX] != VACANT){
      return true;
      }
      }
      }
      return false;
      }
      //control piece
      document.addEventListener('keydown',CONTROL);
      function CONTROL(event){
      if(event.keyCode == 65){
      p.moveLeft();
      dropStart = Date.now();
      }else if(event.keyCode == 87){
      p.rotate();
      dropStart = Date.now();
      }else if(event.keyCode == 68){
      p.moveRight();
      dropStart = Date.now();
      }else if(event.keyCode == 83){
      p.moveDown();
      }
      }
      //drop the piece down every 1sec
      let dropStart = Date.now();
      function drop(){
      let now = Date.now();
      let delta = now - dropStart;
      if(delta > 1000){
      p.moveDown();
      dropStart = Date.now();
      }
      requestAnimationFrame(drop);
      }
      drop();

  • @anisarezaey3403
    @anisarezaey3403 5 лет назад

    I need help the pieces are not moving down or spawning

    • @CodeExplained
      @CodeExplained  5 лет назад

      Did you fix the issue? if not take a look on your console, and say what errors did you got.

  • @js_games_for_dummies
    @js_games_for_dummies Год назад

    Смотрю рекламму, кликаю по ссылке.

  • @vaidsaraswat5356
    @vaidsaraswat5356 5 лет назад

    I'm getting an error saying that z is undefined, which is why I cannot draw the z shape. Anyone know how to fix that?

    • @CodeExplained
      @CodeExplained  5 лет назад

      did you type in Z or z ?

    • @vaidsaraswat5356
      @vaidsaraswat5356 5 лет назад

      @@CodeExplained it's ok I figured it out thanks anyways

    • @CodeExplained
      @CodeExplained  5 лет назад

      good

    • @malcolm2147
      @malcolm2147 5 лет назад +1

      I'm getting this same problem, can you remember how you fixed it?

  • @harm991
    @harm991 5 лет назад

    Awesome!
    One bug: in both your version and mine - if I hold 'down' - the piece can get locked in midair.
    Any idea why?

    • @harm991
      @harm991 5 лет назад

      omg. It's just my 'night mode' filtering out yellow to 'whiteish' colors lol!
      No problems!

    • @CodeExplained
      @CodeExplained  5 лет назад

      Happy to hear you fixed the problem.

  • @ramstube
    @ramstube 3 года назад +1

    Can I pay for the source code by subscribing to it?

    • @CodeExplained
      @CodeExplained  3 года назад +1

      You don't have to pay for anything.

    • @ramstube
      @ramstube 3 года назад +1

      Thank you

  • @DankBeard19
    @DankBeard19 4 года назад +1

    Fenkshin!

  • @KAYMOofficial
    @KAYMOofficial 6 лет назад

    didn't know tetris had a visible grid

    • @CodeExplained
      @CodeExplained  6 лет назад

      you can remove the grid if you want, but that would make the game hard to play, especially when you move down fast a piece and you don't know exactly where the piece will be locked.

  • @getoverhere4465
    @getoverhere4465 3 года назад

    14:16 I thought JavaScript was case sensitive?

  • @mballerini11
    @mballerini11 4 года назад +1

    Why I couldn't run it? everytime I tried to run it this error came up, "the keyword 'const' is reserved.

    • @CodeExplained
      @CodeExplained  4 года назад +2

      Did you use const as a name of a variable?

  • @js_games_for_dummies
    @js_games_for_dummies Год назад

    SSO add

  • @S-Lomar
    @S-Lomar 11 месяцев назад

    💕💓💓💓💓💕💕💓💓💓💓💕💓😍💕

  • @js_games_for_dummies
    @js_games_for_dummies Год назад

    I watch ads and click on links.

  • @OrgilsYTChannel
    @OrgilsYTChannel 3 года назад

    It's not working my phone

  • @dionismarquesvalada8839
    @dionismarquesvalada8839 5 лет назад +1

    :)

  • @yepyeniceri
    @yepyeniceri 5 лет назад

    1:16:06 what happaned

    • @CodeExplained
      @CodeExplained  5 лет назад

      What do you mean ?

    • @yepyeniceri
      @yepyeniceri 5 лет назад

      @@CodeExplained how one of the purple tetromino pieces are at right corner.

    • @CodeExplained
      @CodeExplained  5 лет назад

      I think cut the video there, that's why you might have seen something strange.

  • @tomclumsy6168
    @tomclumsy6168 4 года назад +1

    get context of null what the heck JS.

    • @CodeExplained
      @CodeExplained  4 года назад

      Make sure you don't have a typo in your canvas id.

    • @tomclumsy6168
      @tomclumsy6168 4 года назад

      my bad it should be 2d lowercase not 2D uppercase

  • @darkorbitpro1
    @darkorbitpro1 6 лет назад

    i dont know why but i never saw a single thing in my browser...

    • @CodeExplained
      @CodeExplained  6 лет назад +1

      did you link your HTML file with your JS file.

    • @darkorbitpro1
      @darkorbitpro1 6 лет назад

      when i go to browser and inspect element and console i can see the tetris.js but then its says this: SyntaxError: missing ) after argument list[Learn More]

    • @CodeExplained
      @CodeExplained  6 лет назад +1

      it means you're missing a ")" somewhere in your code.

  • @samuelbi11
    @samuelbi11 3 года назад

    *js*

  • @thespiritualjourney369
    @thespiritualjourney369 Год назад

    for no reason u r making it complicated.