Code Snake Game in Java

แชร์
ฝัง
  • เผยแพร่เมื่อ 12 ก.ย. 2024
  • How to code a snake game in Java for beginners! Learn how to create a game of snake in Java using the awt and swing graphics library. Throughout the tutorial, you will learn how to create a graphical user interface (GUI) to display the Snake game, create the game loop, listen for Key presses to move the snake, use the ArrayList to store the snake's segments, and check collision and out of bounds for game over.
    Setup Java with Visual Studio Code:
    • How to set up Java in ...
    Code: github.com/ImK...
    Website: www.kennyyipco...
    Java Game Programming Projects Playlist:
    • Java Game Programming ...
    JavaScript Game Programming Projects Playlist:
    • JavaScript Game Progra...
    Subscribe for more coding tutorials 😄!

ความคิดเห็น • 102

  • @KennyYipCoding
    @KennyYipCoding  9 หลายเดือนก่อน +4

    Check out the playlist of web project tutorials here!
    th-cam.com/play/PLnKe36F30Y4Y1XQOqNsL9Fgg_p6nYhcng.html
    Code Snake Game in JavaScript: th-cam.com/video/baBq5GAL0_U/w-d-xo.html
    Code Snake Game in Python: th-cam.com/video/FtqWCo1_I4g/w-d-xo.html
    Got a tutorial suggestion? Let me know down below!

  • @hansmalm2801
    @hansmalm2801 ปีที่แล้ว +37

    This is just perfect videos for beginners that have learned the lauguage and how it works but needs to see how you use the code in easy small programs. There are tons of "learn how to program"-videos but with no usable examples. That is just like learning some chord on the guitar but no song to play. YT needs a lot more songs to play, like this. You also have a perfect voice for this and no messing around with side tracks. This is the equalivent to BASIC programs that you could find in paper magazines in the 80s. Just perfect! Keep them coming. Thank you.

  • @dillonpaul1498
    @dillonpaul1498 9 หลายเดือนก่อน +10

    Thank you for the opportunity of coding a snake game, it was very beneficial to my coding journey as a student an I do look forward to future projects. You made my entire week awsome.😊

  • @BabyBunny-Cottontail
    @BabyBunny-Cottontail ปีที่แล้ว +6

    I'm so grateful for your videos! It's so much more fun learning Java when there's some GUI and fun games your working with. I've been slowly learning Java for the past year now, but it's all CLI apps in every tutorial. I'm following your tutorial now and I'm learning so much from them, but I know I'll be coming back in a couple of month and do them all over again when I know even more and probably understand even more of all the things your doing.
    I just wish you could explain even more of what your doing and why. For an example:
    why do you need to add "true" at the end when you're changing the tiles for the snake and the food from 2D to 3D?
    Keep up the good work and I promise to recommend this channel to all my classmates (I'm currently studying QA-testing and were using Java)

    • @KennyYipCoding
      @KennyYipCoding  ปีที่แล้ว +1

      I try not to overexplain sometimes so i might not explain 1-2 parameters, but on some IDEs (or if you use vscode), hovering over the function will tell you what the params mean. Basically the boolean value for 3D indicates whether it should be 3D raised or etched. Meaning, should it look like the top of a cube (true), or should it look like a cube shape hole in the ground (false).

    • @ProfLucasSquadron
      @ProfLucasSquadron ปีที่แล้ว

      Thank You for this video!❤@@KennyYipCoding

    • @dovydasbalciunas5002
      @dovydasbalciunas5002 11 หลายเดือนก่อน

      May I ask how did you learn to code? Did you learn by creating games?@@KennyYipCoding

    • @dovydasbalciunas5002
      @dovydasbalciunas5002 11 หลายเดือนก่อน

      How are you doing in learning Java?

    • @KennyYipCoding
      @KennyYipCoding  3 หลายเดือนก่อน +3

      @@dovydasbalciunas5002 yes! so people focus too much on syntax and "correctness", instead it is better to learn by building projects :) So I can pretty much code 2D games in any programming language because I understand the concepts, even if I'm not an expert on said language.

  • @tuxat_
    @tuxat_ 3 หลายเดือนก่อน +2

    thanks for explaining all the complicated parts! really helped me out a lot!

  • @hnudj9635
    @hnudj9635 10 หลายเดือนก่อน +2

    The way u explanation for use of each line of code is ❤❤❤ best i have ever seen, thanks you

  • @riccardogarista5092
    @riccardogarista5092 ปีที่แล้ว +7

    Thanks for the flawless tutorial!

  • @current_goliath
    @current_goliath 11 หลายเดือนก่อน +4

    Great tutorial, thanks for the clear and concise logic.

  • @babymonster43
    @babymonster43 3 หลายเดือนก่อน +1

    Thank you so much TTTOTTTT i really needed this for our final project

  • @VietNguyen-vj4su
    @VietNguyen-vj4su 2 หลายเดือนก่อน

    Thanks! you have a knack for story telling, which is good for in this line of work (media).

  • @sammo7017
    @sammo7017 ปีที่แล้ว +1

    I followed your tutorial on how to code a snake game in Javascript, I think Java is harder but let's see 😁❤️
    Thanks for your videos 💙

  • @MakkalSelvan-Thalapathy
    @MakkalSelvan-Thalapathy ปีที่แล้ว +3

    Support from India❤️

  • @sujalsangle
    @sujalsangle หลายเดือนก่อน

    Thank you for this video. it was very helpful and understandable.
    Love from India ❤❤😊😊

  • @pland4r133
    @pland4r133 ปีที่แล้ว +1

    maaaaaaaaan i love your videos you are taking me to the next level

  • @damiaotiktok023
    @damiaotiktok023 5 หลายเดือนก่อน

    Obrigado que tutorial incrível, ficou muito bom esse jogo. Assisti vários vídeos de como criar um jogo da cobrinha, e eu percebi que existe várias inúmeras maneiras de fazer um jogo mesmo sendo parecido, más com instruções diferentes, Obrigado vc explicou muito bem .

  • @_I_like_kebeb
    @_I_like_kebeb 4 หลายเดือนก่อน +1

    the best snake video on entire you tube

  • @HospiceCarlTaneo
    @HospiceCarlTaneo 11 หลายเดือนก่อน +1

    I have a question, if i press two directions at the same time when going the opposite direction, it bugs out and ends the game, how do i fix this?

  • @SiddhantShedge
    @SiddhantShedge 4 หลายเดือนก่อน +1

    Just did the project and it did help me clear my java fundamentals!I just had one idea,how can I increase the speed of the snake each time it eats the food?

    • @ronin6397
      @ronin6397 4 หลายเดือนก่อน

      Hmm looks complicated.. The speed increases with a lower level of the Timer. Maybe you can use and adapt to the same system as the velocity.

    • @SiddhantShedge
      @SiddhantShedge 4 หลายเดือนก่อน +1

      @@ronin6397 thanks for your suggestion!

  • @yeetlouis9077
    @yeetlouis9077 3 หลายเดือนก่อน +1

    Does this code work on BlueJ too?

  • @luuhgentile
    @luuhgentile 10 หลายเดือนก่อน

    First of all thank you very much for this tutorial! Apreciate it!
    I have a dual boot machine, I tried this tutorial first on Ubuntu and it didn't work, it didn't open the window when I executed the program on 3:44, then I switched to windows 10 and it worked fine.
    Do you think is there something wrong with my machine or with how I installed java on Ubuntu? Or does this code doesn't work on Ubuntu?

  • @tonyguerra136
    @tonyguerra136 หลายเดือนก่อน

    Hello Kenny, I am wondering why when I type in line #9.....frame.setVisible (true);
    and line #11....frame.setsetLocationRelativeTo(null);
    a "b:" and a "c:" are added automatically to the "true" and "null" like (b:true) and (c:null).
    Is it there an "auto-correction command" in the VS that I can deselect?
    Wonderful videos and thanks for teaching me VS and Java.

    • @KennyYipCoding
      @KennyYipCoding  หลายเดือนก่อน

      I think you might be referring to the parameter help box.
      th-cam.com/video/BB0gZFpukJU/w-d-xo.htmlsi=W-lNBaE7m-T-l-cT
      In my pinned comment for the how to set up java in vs code video, you will find instructions on how to disable this feature

  • @Sweet_Solos
    @Sweet_Solos 10 หลายเดือนก่อน +1

    That was very good tutorial i learned alot by following along and tweaking! I just have a quick observation regarding the draw(Graphics g) function you created for both the snake and the food graphics. Coming from JS i find it so odd the behavior of both the .setcolor and .fillRect methods, because at first I was so confused of how these methods were targeting the desired tiles but then i figured it out after reading the docs, apparently all subsequent graphics after .setColor will be drawn with said color until specified otherwise. It just seems unecessaribly verbose if I wanted to generate a more complex pattern but i guess thats just Java.

    • @KennyYipCoding
      @KennyYipCoding  10 หลายเดือนก่อน

      Yes you can think of setColor as pinking a color pencil, and you only switch colors after calling setColor (picking another pencil) again! I also have a snake tutorial in JS if you want to see how it’s done in JS

  • @nathanmandala8465
    @nathanmandala8465 ปีที่แล้ว +1

    hey thanks for the amazing video! Next time, could you explain a little bit why you written some of the code?
    Thanks from Canada!

    • @KennyYipCoding
      @KennyYipCoding  ปีที่แล้ว

      Hi! Thanks for watching! Let me know what parts of the code were confusing for feedback!

    • @BabyBunny-Cottontail
      @BabyBunny-Cottontail ปีที่แล้ว

      ​@@KennyYipCoding Yeah, I agree. Sometimes you just add something and I wonder why and how you knew that.
      Still, your videos are great! There's hardly anyone doing any usable Java tutorials that's not CLI

  • @kimi59
    @kimi59 10 หลายเดือนก่อน +2

    Hmm, at the point of 20:30, I get an error on the line:
    gameLoop = new Timer(100, this);
    It says that "The constructor Timer(int, SnakeGame) is undefined
    And, the line:
    gameLoop.start();
    Has the error "The method start() is undefined for the type Timer"
    Any idea what this might mean?

    • @omerkose3848
      @omerkose3848 9 หลายเดือนก่อน

      +1 "

    • @omerkose3848
      @omerkose3848 9 หลายเดือนก่อน

      bro did you solve it I'm getting the same error

    • @rostom.00
      @rostom.00 18 ชั่วโมงที่ผ่านมา

      hello i had the same problem but as soon as i deleted "import java.util.Timer;" the code ran properly and i had no issues.Try it.

  • @lavaflame059
    @lavaflame059 6 หลายเดือนก่อน

    Thanks for the latest info.

  • @codinghack2
    @codinghack2 11 หลายเดือนก่อน +1

    Perfect It really helped 😊

  • @devincaruso8128
    @devincaruso8128 11 หลายเดือนก่อน +2

    This seems like a really cool video! I'm new to programming and often don't know what I'm doing, but I'm trying to copy the code down, and I get the error: package javax.swing does not exist(compiler.err.doesnt.exist) after typing import javax.swing.*; I have the java extension pack and a JDK that works for other programs, but not here. Does anyone know what might be happening? Thanks!

  • @mrsxber1916
    @mrsxber1916 11 หลายเดือนก่อน

    Appreciate this tutorial bro

  • @eddibressan8805
    @eddibressan8805 7 หลายเดือนก่อน

    Thank you for the helpful video

  • @stevensaleh4263
    @stevensaleh4263 10 หลายเดือนก่อน

    Thank you so much bro finally i made snake game 😊❤

  • @Sandi22
    @Sandi22 5 หลายเดือนก่อน

    Really cool. Thanks

  • @noahfeazell3336
    @noahfeazell3336 6 หลายเดือนก่อน

    Question: Why do you need to declare all the stuff for the frame. and then declare stuff again in another class so it can be drawn on?

  • @robtai6225
    @robtai6225 8 หลายเดือนก่อน +1

    can you build a platformer game in java?

  • @n0lerr1
    @n0lerr1 2 หลายเดือนก่อน

    Thenks bro i love you

  • @samarmahato3054
    @samarmahato3054 11 หลายเดือนก่อน

    Thank you
    ❤ from India

  • @lucapepel5851
    @lucapepel5851 10 หลายเดือนก่อน

    10/10
    Thank you very much!

  • @emilianogonzalezperez2772
    @emilianogonzalezperez2772 11 หลายเดือนก่อน +1

    where are you from?

  • @uyg8
    @uyg8 7 หลายเดือนก่อน

    perfect brother keep up

  • @obieosuji9986
    @obieosuji9986 8 หลายเดือนก่อน

    The grid isn't showing up. There are no errors as the program runs. I'm not sure if its because im on a mac and hes using windows but the program produces nothing when I run it.

  • @andrejklester5851
    @andrejklester5851 4 หลายเดือนก่อน

    Do you have javafx experiance, it is possibile too? Snake game?

  • @hyf1099
    @hyf1099 8 หลายเดือนก่อน

    Tks for the video, but I get bug: when i press left down right sequence fastly, game over.
    Can u fix that, it happen when score is greater than two

  • @sadiulhakim7814
    @sadiulhakim7814 2 หลายเดือนก่อน

    Awesome

  • @fgprince7052
    @fgprince7052 10 หลายเดือนก่อน

    Sir please upload java from beginning please sir
    Love from India❤

  • @honzeus2010
    @honzeus2010 7 หลายเดือนก่อน

    I can´t run the code,I think it´s because the javax.swing feature is not working, but I´ve written it exactly how you did, so I am confused a little bit.
    Sorry if there is a mistake on my side, but I´d be really happy if you could help me with that.
    I set up Java in vscode like you in your tutorial, but I could´ve done a mistake, so I please help me if you can.
    Also thank xou for creating videos like this.

  • @nihalchandrav.c273
    @nihalchandrav.c273 5 หลายเดือนก่อน

    Can we do this in scanner class?

  • @nehemiahpaulpichay23
    @nehemiahpaulpichay23 23 วันที่ผ่านมา

    Is there any idea how can I put a button that says "Start Over" and the game resets from the start again. When u lose, u have to close and run it again, wonder how can I do that, beginner btw peace.

    • @KennyYipCoding
      @KennyYipCoding  23 วันที่ผ่านมา +1

      Doing it with a button is unnecessary, you can add a check for a key in the key pressed function, and check if gameOver, if so, flip gameOver back to false and reset variables back to starting state. Ex) set score back to 0, clear the snake body parts array, place the food randomly.

    • @nehemiahpaulpichay23
      @nehemiahpaulpichay23 23 วันที่ผ่านมา

      @@KennyYipCoding thanks big time

  • @pahmya1906
    @pahmya1906 ปีที่แล้ว

    i have a question, if we use random for the food, will it spaw inside the snake ?

    • @KennyYipCoding
      @KennyYipCoding  ปีที่แล้ว +1

      Yes but it is very unlikely to collide with the snake’s head given the total number of square available.

  • @robtai6225
    @robtai6225 8 หลายเดือนก่อน

    awesome content

  • @karidad.19
    @karidad.19 9 หลายเดือนก่อน

    Thank you❤️

  • @Kanha0321
    @Kanha0321 9 หลายเดือนก่อน

    I have made something similar too can I share its link?

  • @furkant.6681
    @furkant.6681 หลายเดือนก่อน

    thankss

  • @imalkesara4466
    @imalkesara4466 3 หลายเดือนก่อน

    Thank u

  • @olehliashyk6988
    @olehliashyk6988 8 หลายเดือนก่อน

    Hi bro! Thnx for lessons.
    I have a problem with class "tile". This class not found in my IDE. Why? My IDE tell me "do not find symbol "tile".

    • @KennyYipCoding
      @KennyYipCoding  8 หลายเดือนก่อน +1

      I wouldn’t be able to tell. Did you make a typo? Ex) Tile, not tile. It should also say which line your issue is coming from

    • @olehliashyk6988
      @olehliashyk6988 8 หลายเดือนก่อน

      @@KennyYipCoding I'm writing on mac os, maybe that's the problem?

    • @olehliashyk6988
      @olehliashyk6988 8 หลายเดือนก่อน

      @@KennyYipCoding I understand that Tile and tile are completely different things, but I can't use it myself, because the IDE does not recognize it

    • @KennyYipCoding
      @KennyYipCoding  8 หลายเดือนก่อน +1

      @@olehliashyk6988
      perhaps a problem with your project setup? is Tile in a different class file?

    • @olehliashyk6988
      @olehliashyk6988 8 หลายเดือนก่อน

      @@KennyYipCoding I don't have a Tile class at all. I thought it was the same as JFTame etc. And because of that I can't continue my studies (((

  • @mehraritvik
    @mehraritvik 10 หลายเดือนก่อน

    nice

  • @omerkose3848
    @omerkose3848 9 หลายเดือนก่อน

    at the point of 20:30, I get an error on the line:
    gameLoop = new Timer(100, this);
    It says that "The constructor Timer(int, SnakeGame) is undefined
    And, the line:
    gameLoop.start();
    Has the error "The method start() is undefined for the type Timer"

    • @Dr_Mossig
      @Dr_Mossig 9 หลายเดือนก่อน

      Same here

    • @COZYTapezz
      @COZYTapezz 8 หลายเดือนก่อน +1

      I figured out our issue we IMPORTED THE WRONG TIMER

    • @Dr_Mossig
      @Dr_Mossig 8 หลายเดือนก่อน

      @@COZYTapezz How do we import the right one?

    • @Dr_Mossig
      @Dr_Mossig 8 หลายเดือนก่อน

      I found how to fix it!

    • @srushtivaghasia4223
      @srushtivaghasia4223 3 หลายเดือนก่อน

      ​@@COZYTapezzthank you

  • @RogeliollNLape
    @RogeliollNLape ปีที่แล้ว

    new subs here

  • @urdukahaniya570
    @urdukahaniya570 10 หลายเดือนก่อน

    is anyone know how to restart game from which key

    • @tencent200
      @tencent200 8 หลายเดือนก่อน

      u just have to make one in the keyPressed function.
      Here is a code just paste it in there:
      if (e.getKeyCode() == KeyEvent.VK_SPACE){
      snakeHead = new Tile(5, 5);
      snakeBody = new ArrayList();
      placeApple();
      velocityX = 0;
      velocityY = 0;
      gameOver = false;
      }

  • @williamkakooza4603
    @williamkakooza4603 6 หลายเดือนก่อน

    The best way to appreciate you is by Subscribing. Thanks for this video. Can you please create a soccer game development tutorial.

  • @gasparcalix2095
    @gasparcalix2095 11 หลายเดือนก่อน

    grate

  • @tencent200
    @tencent200 8 หลายเดือนก่อน +2

    nice video. if it bothers you that the snake goes in with and and one more body tile in the boarder and doesn't die, just do - 1 at the boardWith and boardHeight like this: if (snakeHead.x*tileSize < 0 || snakeHead.x*tileSize > boardWidth - 1||
    snakeHead.y*tileSize < 0 || snakeHead.y*tileSize > boardHeight - 1){
    gameOver = true;
    }

  • @AayushEditzzz..
    @AayushEditzzz.. 5 หลายเดือนก่อน

    AI:🗿🗿

  • @MicroDev-ql3kn
    @MicroDev-ql3kn 25 วันที่ผ่านมา

    he i like you but java dont and thank you for letting me see thise because now i now java is not a fit form me but a fun learning experiense (love ur vids btw)

  • @ДаулетТилеуов
    @ДаулетТилеуов 9 หลายเดือนก่อน +1

    сду перваши вы тут?

  • @timdelarboulas4796
    @timdelarboulas4796 4 หลายเดือนก่อน

    Thank you for this video, it help to learn how write java code :) !
    I try tobuild a restart function after a game over. For exemple, when you press the space key, it restart the snake game. I failed but, can you advice or give food for thought please ?
    Thank you in advance.
    I tried this :
    boolean restart = false;
    public void keyPressed(KeyEvent e) {
    ...
    // restart function
    if (e.getKeyCode() == KeyEvent.VK_SPACE && gameOver) {
    restart = true;
    }
    }
    @Override
    public void actionPerformed(ActionEvent e) {
    ...
    if (gameOver) {
    gameLoop.stop();
    if (restart) {
    gameOver = false;
    gameLoop.restart();
    }
    }
    }