Game Loop and Key Input - How to Make a 2D Game in Java #2

แชร์
ฝัง
  • เผยแพร่เมื่อ 7 ส.ค. 2024
  • (Dec 4, 2022)
    The Russian subtitles have been added. Thank you for the help!
    French video title translation by @Akariiinnn
    Caution: This is a tutorial for Java 2D beginners. So the pacing is slow!
    In this video, I will explain how to construct a game loop in detail.
    What we do in this video:
    1. Draw an object on the screen with Graphics2D
    2. Get keyboard input
    3. Construct a "sleep" game loop
    4. Construct a "delta" game loop
    5. Display FPS and check if our game loop is working.
    You might feel it's a bit complicated and constructing a game loop is the first big hurdle in 2D game development so hopefully, this video helps you to get through so we can move on to more fun stuff.
    If you have any questions, feel free to leave a comment.
    Guidelines for using Blue Boy Adventure's code and assets:
    docs.google.com/document/d/1q...
    Timestamps:
    0:00 Game loop outline
    5:30 Draw an object on the screen
    8:19 Get keyboard input
    17:50 About the system time
    21:08 Construct the first game loop (sleep)
    28:04 Construct the second game loop (delta)
    31:21 Display FPS
    #java #javagamedevelopment #java2d

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

  • @lucastraveldiaries9063
    @lucastraveldiaries9063 ปีที่แล้ว +227

    I scanned my code for nearly an hour now, trying to understand why it isnt working. Turns out I forgot one line in my game loop. After I finally found it and the code worked, I almost cried out of pure joy that the fucking rectangle finally moves. Welcome to a developers life I guess...

    • @RyiSnow
      @RyiSnow  ปีที่แล้ว +48

      Good job! I can really relate to your comment. The feeling you get when you finally figure something out by yourself is truly special. Hope you'll keep enjoying coding.

    • @barsapriyadarshinijena2084
      @barsapriyadarshinijena2084 ปีที่แล้ว +4

      Hii ..can you please tell me where you did mistake, cause mine also not working..that rectangle is not moving

    • @lucastraveldiaries9063
      @lucastraveldiaries9063 ปีที่แล้ว +3

      @@barsapriyadarshinijena2084 Hey. There was one line of code that wasi
      missing in my game loop connecting everything together. It's pretty unlikely that you have exactly the same fault. I would recommend to check your code for errors (underlined in red etc.) and after that you compare your code line for line with the tutorial. It's gonna take a while but you are gonna find the problem I'm sure. Keep searching 😇

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

      me toooooooooo it was just so suffering but it turned out to run successfully

    • @RaFIQYT-se5sl
      @RaFIQYT-se5sl ปีที่แล้ว +1

      I am having some problems too my square did'nt appear and i can't initialize keyhandler

  • @RyiSnow
    @RyiSnow  2 ปีที่แล้ว +57

    It turned out to be a pretty long video so I prepared time stamps for your reference:
    0:00 Game loop outline
    5:30 Draw an object on the screen
    8:19 Get keyboard input
    17:50 About the system time
    21:08 Construct the first game loop (sleep)
    28:04 Construct the second game loop (delta)
    31:21 Display FPS
    I know this part 2 is an uneventful and a boring episode but this is also a very important one. A lot of people give up on 2D development because they didn't build up a decent game loop. So if you're not familiar with game loop, I'd recommend you to watch the whole (especially from 17:50) and understand its concept before moving onto the next part.
    Constructing a game loop is the first big hurdle in 2D game development. I also had a hard time understanding it at first.... but it is crucial because game loop is the engine of the game. Once it is created, we can put fuels (characters, tiles, objects etc.) into it and our game can run with them. I hope you get through this so we can move onto more fun stuff!

    • @normalduck3917
      @normalduck3917 2 ปีที่แล้ว

      It took me a while to understand game loop

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

      Hey can you pls help with my code i can't get the rectangle to move..... Sorry for replying to a 2 year old vid.
      Also, I am using Vs code java package so i don't have to write package main at the top
      //Main.java
      import javax.swing.JFrame;
      public class Main {
      public static void main(String[] args) {
      JFrame window = new JFrame();
      window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      window.setResizable(false);
      window.setTitle("GameXD");
      GamePanel gamePanel = new GamePanel();
      window.add(gamePanel);
      window.pack();
      window.setLocationRelativeTo(null);
      window.setVisible(true);
      gamePanel.startGameRun();
      }
      }
      //GamePanel.java
      import java.awt.Color;
      import java.awt.Dimension;
      import java.awt.Graphics;
      import java.awt.Graphics2D;
      import javax.swing.JPanel;
      public class GamePanel extends JPanel implements Runnable{
      final int originalTileSize = 16;
      final int scale = 3;
      final int tileSize = originalTileSize * scale;
      final int maxScreenCol = 16;
      final int maxScreenRow = 12;
      final int screenWidth = maxScreenCol * tileSize;
      final int screenHeight = maxScreenRow * tileSize;
      InputHandler inputManager = new InputHandler();
      Thread gameThread;
      int playerX = 100;
      int playerY = 100;
      int playerSpeed = 4;
      public GamePanel() {
      this.setPreferredSize(new Dimension(screenWidth,screenHeight));
      this.setBackground(Color.black);
      this.setDoubleBuffered(true);
      this.addKeyListener(inputManager);
      this.setFocusable(true);
      }
      public void startGameRun() {
      gameThread = new Thread();
      gameThread.start();
      }
      @Override
      public void run() {
      while(gameThread != null){
      long currentTime = System.nanoTime();
      System.out.println("Current Time:"+currentTime);
      update();
      repaint();
      }
      }
      public void update() {
      if(inputManager.upPressed == true) {
      playerY -= playerSpeed;
      }
      if(inputManager.downPressed == true) {
      playerY += playerSpeed;
      }
      if(inputManager.leftPressed == true) {
      playerX -= playerSpeed;
      }
      if(inputManager.rightPressed == true) {
      playerX += playerSpeed;
      }
      }
      public void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D)g;
      g2.setColor(Color.white);
      g2.fillRect(playerX, playerY, tileSize, tileSize);
      g2.dispose();
      }
      }
      //InputHandler.java
      import java.awt.event.KeyEvent;
      import java.awt.event.KeyListener;
      public class InputHandler implements KeyListener{
      public boolean upPressed = false;
      public boolean downPressed = false;
      public boolean leftPressed = false;
      public boolean rightPressed = false;
      @Override
      public void keyPressed(KeyEvent e) {
      int keyCode = e.getKeyCode();
      if(keyCode == KeyEvent.VK_W) {
      upPressed = true;
      }
      if(keyCode == KeyEvent.VK_S) {
      downPressed = true;
      }
      if(keyCode == KeyEvent.VK_A) {
      leftPressed = true;
      }
      if(keyCode == KeyEvent.VK_D) {
      rightPressed = true;
      }
      }
      @Override
      public void keyReleased(KeyEvent e) {
      int keyCode = e.getKeyCode();
      if(keyCode == KeyEvent.VK_W) {
      upPressed = false;
      }
      if(keyCode == KeyEvent.VK_S) {
      downPressed = false;
      }
      if(keyCode == KeyEvent.VK_A) {
      leftPressed = false;
      }
      if(keyCode == KeyEvent.VK_D) {
      rightPressed = false;
      }
      }
      @Override
      public void keyTyped(KeyEvent e) {
      //Don't use
      }
      }

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

      please dont cut things even if its something small as importing something. if this is for complete beginners, dont cut things out even if its something as importing

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

      @@adhyyankumar501sorry for replying on 9 month old comment, hope you already solved it yourself, but if not then you can try this in startGameRun() method new Thread(this);

  • @user-vm1gg8ph8r
    @user-vm1gg8ph8r 2 ปีที่แล้ว +96

    I know it's such a simple thing for a square to just move on a screen but I felt so happy when it worked and I knew how it worked thanks!

    • @RyiSnow
      @RyiSnow  2 ปีที่แล้ว +12

      I can relate to that!

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

      I m loving the tutorial!

  • @operatedowl4158
    @operatedowl4158 2 ปีที่แล้ว +12

    If TH-cam allowed to like a video multiple times then I would like every second of this video.

  • @ErroTheCube
    @ErroTheCube 2 ปีที่แล้ว +26

    A very well put together tutorial, it has a really nice pacing and feels like it's just the right difficulty for me. Thank you!

  • @pvmpalways5058
    @pvmpalways5058 ปีที่แล้ว +68

    by around 16:38 in the video, if you cannot get the square to move at all no matter what key you press, make sure your main class looks like this:
    package main;
    import javax.swing.JFrame;
    public class Main {
    public static void main(String[] args) {

    JFrame window = new JFrame();
    GamePanel gp = new GamePanel();

    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setResizable(false);
    window.setTitle("Title");
    window.add(gp);
    window.pack();
    window.setLocationRelativeTo(null);
    window.setVisible(true);

    gp.startGameThread();

    }
    }
    The order of the window variables matters, specifically the pack, relative, and visible ones. After setting these this way, I was able to simply run the app and everything worked as in the video up to the point at 16:38
    If you're lazy like I was before googling for more info, just simply hit tab on your keyboard and that will focus your screen to the applet so you can use WASD

    • @samjesf
      @samjesf ปีที่แล้ว +2

      Thank you for this! I initially tried grouping the relative & visible methods with the others at the top of the class for appearance sake and could not for the life of me figure out why keyListener sometimes worked but most of the time did not. This fixed it for me! Much appreciated!

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

      Oh my god, thank you so much. I was about to quit and decided to look at the comments before quitting. You're a hero.

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

      Dude you're such a lifesaver thank god you fixed so many problems bless you

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

      thnks broski

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

      I had missed adding startGameThread to the Main class and it was driving me nuts. Thank you for the tip!

  • @crackrokmccaib
    @crackrokmccaib 2 ปีที่แล้ว +79

    Thank you so much for actually teaching as you go. Other people just type stuff and say what they're typing, but you explain how things actually work. I can't thank you enough for that.

    • @RyiSnow
      @RyiSnow  2 ปีที่แล้ว +15

      Thank you. That means a lot to me.

  • @devoiddude
    @devoiddude 2 ปีที่แล้ว +13

    Thank you so much for this and all the effort you put into making this series.

  • @mega_city_one
    @mega_city_one 2 ปีที่แล้ว +17

    A very useful tutorial, thank you for your efforts.
    I'm looking forward to the following parts.

    • @RyiSnow
      @RyiSnow  2 ปีที่แล้ว +2

      It took a while to make this video so I'm very happy to hear that. Thank you for the comment!

  • @terencechia9986
    @terencechia9986 2 ปีที่แล้ว +2

    Thank you very much for your explanation RyiSnow 🙏 I feel like I understand what each method is doing now. As many have already said, the pacing is perfect especially for beginners like myself!

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

    Thank you for this! Very informative, well explained, and FUN!

  • @Venixx-72
    @Venixx-72 ปีที่แล้ว

    This video was really helpful to me. I love how to you took time to explain the concepts before moving on. Its really helpful knowing that there is someone who understands the basic elements of teaching.

  • @xConsoleCapturex
    @xConsoleCapturex ปีที่แล้ว +8

    I love that you're doing all this from scratch. I know libraries exist that can do all this by default but this really helps me understand the underlying mechanics, which I believe leads to a better game

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

    This is so awesome. I've been looking for something like this for a while. Thank you so much!

  • @adoniszgks7641
    @adoniszgks7641 2 ปีที่แล้ว +4

    you deserve 1.000.000 subscribers, so easy to understand and to learn!!!

  • @Zoggi_
    @Zoggi_ 2 ปีที่แล้ว +8

    you explained it really well, thank you for those videos!

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

      Glad to hear that!

  • @misterdneh
    @misterdneh 2 ปีที่แล้ว +6

    This is amazing, I'm trying to remaster this for Android & learning a lot more than I thought in the last 7 years of coding Java!!! I'll let you know how it goes once I run the final project, so far I've made a flappy rectangle oh the possibilities!
    Subscribed & many likes to come keep this going PLEEEASE!

  • @AddersOtter
    @AddersOtter 2 ปีที่แล้ว

    I just picked up programming again and this has been helping me out a lot.

  • @PaulO-ym5dm
    @PaulO-ym5dm ปีที่แล้ว +2

    Sublime tutorial, very clear! Such good practice, big thanks!

    • @RyiSnow
      @RyiSnow  ปีที่แล้ว +2

      Glad you liked it and thank you so much for your kind support :D

  • @ghostek7792
    @ghostek7792 2 ปีที่แล้ว +3

    I LOVE YOU ryi, i just started university for software engineering and i am completely green at coding. literally everything is 100% new to me, so this summer i'm working on little projects to continue improving and getting comfortable. the video is perfectly paced in my opinion because even the content I already know is getting engrained into my memory even better. I appreciate you taking your time to make this content for others

  • @mamapuci
    @mamapuci 2 ปีที่แล้ว

    Really helping with such amazing explanation. Really really thanks bro. I followed another parts

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

    Thanks man I don't know what I'd have done without this video.

  • @whitebeartigtig
    @whitebeartigtig ปีที่แล้ว +22

    These tutorials really do help. I do have basic java knowledge, but I wanted to go into game development to hopefully improve my knowledge of the language. The explanations are really helping with that. Eclipse being in dark mode is certainly a nice addition too, especially when working watching in the dark. I'm sure by the time I get to the end of the playlist, I will be much better at java development.

    • @RyiSnow
      @RyiSnow  ปีที่แล้ว +5

      Glad to hear that you liked it. Hope you enjoy developing your own game!

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

      Same, I’m a computer science student and finished my first year with Java. Figured this would be a good project. Definitely helps if you know how classes, data types, methods, loops, etc works

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

    Youre awesome bro, i can finally understand the logic through the FPS concept, i always read about the "threads" but i didnt understand all the meaning, now i can realize the importance of this topic in the game development, so thanks for all

  • @quarkzyhn4391
    @quarkzyhn4391 2 ปีที่แล้ว

    I love you man! Thank you so much for the lessons ☺️

  • @amarboparai4159
    @amarboparai4159 ปีที่แล้ว +4

    I really like his accent. Also, RyiSnow is the only channel on youtube teaching how to create a complex 2D game in Java. And every step is explained so briefly.
    Mad respect for you sir. 🙌🙌

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

    I love this series. Great job!

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

    Thank you for these videos they’re really helpful

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

    This is cool. I'm almost finish my Java bootcamp course. Definitely I want to learn.

  • @user-pr1dh7eb7p
    @user-pr1dh7eb7p ปีที่แล้ว

    Thanks for you content you helped me a lot to understand the java environement!!

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

    I really appreciate this series!!!! :D
    Tysm!!

  • @user-px5pj7ux5k
    @user-px5pj7ux5k 11 หลายเดือนก่อน

    this is the real logic pattern and procedure if you are teaching. it is easy to understand unlike the others are very stingy to give. haysss. Thank you for this. You are the best❤❤❤

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

    Thank you so much, very good Tutorial!

  • @SlothfulSage285
    @SlothfulSage285 2 ปีที่แล้ว

    Thanks! Another great video!

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

    Thanks you so much for this video!

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

    i have started the playlist today , this is really awesome . Hopefully i stay consistent and reach the last video.

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

    bro thank you. you can really good explain things!

  • @ikninja1hd
    @ikninja1hd 2 ปีที่แล้ว

    Great video, thanks a lot!

  • @kogashinto2123
    @kogashinto2123 2 ปีที่แล้ว

    This helped me so much. Thank you.

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

    thank you so much this is life changing!!

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

    even though I ended up creating my own systems (jpanel instead of graphics rectangle, speed inside the object, etc.), almost all of this video was useful, I started with just a moving square, and then added keyInput later once I managed to get higher FPS's move at the same speed as lower ones. Thank you for making this tutorial.

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

    Nice video man!

  • @tomasmarta5028
    @tomasmarta5028 2 ปีที่แล้ว

    I love this channel so much thank you

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

    This is one of the best java tutorials. I have done projects on my own, whether visual or not and never really implemented crazy ideas. This tutorial alone has shown me why I learned all those techniques in my java cs classes. Also any m1 max users confused on the delta fps showing up as 0, my MacBook has the same problem but it works on my windows desktop perfectly. Might be how the m1 computes or something, however it still works. (little cheat turn the drawCount =0; int drawCount = 60; i guess it displays the drawCount after converting it to 0 (i have the assignment after the out.print).

    • @user-ri2ms2mm7w
      @user-ri2ms2mm7w 2 ปีที่แล้ว

      My friends, search for your life purpose, why are we here?? I advise you to watch this series and this video 👇 as a beginning to know the purpose of your existence in this life th-cam.com/play/PLPqH38Ki1fy3EB-8xmShVqpbQw99Do2B-.html th-cam.com/video/7d16CpWp-ok/w-d-xo.html

  • @lilsquirt9248
    @lilsquirt9248 2 ปีที่แล้ว +5

    Me:I am accomplished for creating a moving square!
    Notch:Heh cute.
    Epic Games:There's no build mode...

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

    Thank you for this, i am one step closer to program my own game

  • @james3414
    @james3414 2 ปีที่แล้ว

    really enjoying this, ty a lot

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

    Great explanation!!!😁

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

      Thanks! 😃

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

    thanks a lot!!! so nice reverb

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

    this tutorial was done very well 👍

  • @MidsoH
    @MidsoH ปีที่แล้ว +2

    こんにちは!自分はアメリカ留学生でして、コンピューターサイエンスの授業で作ってるゲームのために貴方の動画を見始めました。調べた中初心者に対して一番丁寧で、一つ一つしっかり説明しながら教えてくれてるのが、とても助かります!喋り方やPC環境で日本人だと分かり、びっくりしました!素晴らしい動画シリーズ、ありがとうございます!応援してます!

    • @RyiSnow
      @RyiSnow  ปีที่แล้ว +2

      ありがとうございます! 学習の一助になったのであれば幸いです。日本人でコメントしてくださる方は少ないので非常に嬉しいです。異国での生活はなにかと大変なこともあるかと思いますが、どうぞ貴重な機会を存分にお楽しみください!

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

    why is this tutorial so good

  • @vmardones366
    @vmardones366 ปีที่แล้ว +6

    Thanks for the tutorial, well done!
    Just a side note if you're using linux or the game is a bit laggy for no reason: add System.setProperty("sun.java2d.opengl", "true"); to your main method, to force it to use OpenGL.
    Also, since we're not using the keyTyped() method, extending KeyAdapter instead of implementing KeyListener saves a few lines of code.

  • @saammyyeet
    @saammyyeet 2 ปีที่แล้ว

    tysm that Thread method is so much easier than using a Timer

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

    Literally my new hero.

  • @noahbarger1
    @noahbarger1 ปีที่แล้ว +3

    I'd love to see a turn based RPG tutorial, like games like EarthBound/Mother or even Final Fantasy. That would be awesome!
    ありがとう!

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

    thank you for making these. on to part threeeeeeee

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

    AMAZING tutorial you're the best! At first I've tried sleep method and the square didn't move. So I checked all my code but don't find any errors. I scanned my code for like 2 HOURS! But then I tried the delta method and it works. The second the square move I feel so happy and stupid at the same time. Don't be like me.

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

    Your probbebly not going to see this but anyways. You are a fantastic youtuber and a great help im knew and just got into coding so your totourials really help me a lot keep up the good work man everyone apprcates it.(also I cant spell cause I type fast so i had to fix the code like 2million times)

  • @ramtejeshm9344
    @ramtejeshm9344 2 ปีที่แล้ว +3

    Somehow fixed the problem, if anyones keyboard input isn't being recognised, these are the things that worked for me:
    1.Update your SDK, but at start after some time my input was't being recognized again, so
    2. Close the frame window and rerun the program again, but still some times I got the error back after some time
    3. try changing keyhandler variable name
    after repeating this process I got it to work.

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

    Thank you so much!

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

    Thanks...you are a legend!

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

    I just finished the first video and still on it this tutorial is something

  • @SajmonOffical
    @SajmonOffical 16 วันที่ผ่านมา

    I rewrote the entire code exactly and it worked, I don't know what the problem was with the earlier code, but if the code doesn't work, I advise you to rewrite it

  • @MrLoser-ks2xn
    @MrLoser-ks2xn ปีที่แล้ว

    Thanks!

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

    감사합니다. 잘보고 있습니다.

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

    Great tutorial. I believe using the delta approach in the run method will utilize one CPU core constantly at 100%, might not be ideal for battery powered devices.

  • @abovethemist1412
    @abovethemist1412 ปีที่แล้ว +6

    Tip: if you cant get the keyhandler to work, press *tab* . now i know it sounds ridiculous but its literally what i had to do
    EDIT: you need the program to be open

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

      Thankx! I could not figure out why my code did not work, but the TAB did the trick (probbably it is somehow needed to really focus on the panel)

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

      Thank you soooo much

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

      Thanks a lot.

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

      thank you soo much I was getting really worried

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

    I love you so much bro :)

  • @postulysses
    @postulysses 2 ปีที่แล้ว +3

    Ευχαριστούμε!

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

      Thank you for supporting this channel! Greatly appreciate it.

  • @expertblober4533
    @expertblober4533 ปีที่แล้ว +3

    Note: The Thread sleep method is, in fact, a bit off. About 5 out of 6 times, you will get 61 FPS. Clearly, this isn't a big problem, just wanted to put it out there.

  • @zielony1212
    @zielony1212 ปีที่แล้ว +6

    Pro tip: put Toolkit.getDefaultToolkit().sync(); at starting of the GamePanel::paintComponent function, so the buffer is being synchronized every frame (note that when using canvas, Toolkit.getDefaultToolkit().sync() is being ran automatycally);
    If someone wonders, it fixes the "lagging" issue.

    • @ahmedel-gohary6000
      @ahmedel-gohary6000 ปีที่แล้ว +3

      Thanks! It was really helpful

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

      Thanks a lot, it was lagging for the first second of holding a key

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

      btw it's better to use Canvas that is actually made for such things instead of JPanel that should actually be top container for the game display (Canvas), not the display itself. @@laz3664

  • @henrikjohnsen9554
    @henrikjohnsen9554 2 ปีที่แล้ว +3

    Anyone getting stuttering in the game loop? I have tried both with thread sleep and delta time, same problem. after adding sprites with animation from the next video I can see the animation freezes up to a second sometimes. I'm running it on Linux. Does anyone have an idea why this happens and or how to fix it?
    edit: For some reason the stuttering stopped after i loaded the text file for tile map in video 4 :/

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

    Thanks you for code.

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

    top quality channel🦾

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

    now i am in a good mood

  • @HansonJ
    @HansonJ 2 ปีที่แล้ว +4

    I had some problems with the sleep method for some reason in KeyHandler it would automatically head to KeyTyped instead of KeyPressed and KeyReleased (preventing any movement). Switching to the delta method worked as expected so if you are having the same issue, try Delta.

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

      holy shit thank you for this ive been having the exact problem lmao

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

      😁❤

  • @kaylor87
    @kaylor87 2 ปีที่แล้ว +3

    When I played along, I opted to leave the Sleep loop in my code that we already made initially, and instead just observed you build the Delta loop. But upon testing, my FPS with the sleep loop tends to bounce between 59 and 61fps, versus yours was a steady 60fps. Both are accurate enough to be functional, but it does seem like the Delta loop is more accurate.

    • @AnnmusPnda
      @AnnmusPnda 2 ปีที่แล้ว

      I believe the delta loop is FAR more accurate. After implementing the delta loop, using my 144hz monitor and setting the FPS attribute to 144 instead of 60, I noticed a MASSIVE difference between this and the sleep loop when it came to my red box's movement. Before it felt very jittery and almost looked like my monitor had ghosting issues, but after using the delta loop which had no influence on the concurrency of the program's thread, I can see my character moving as smoothly as any game I normally play on my computer.

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

    My code editor doesn’t seem to know about the type Graphics.
    Great videos, I just started with developing, but I can follow easily and know what I’m doing, thanks so much ^^

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

    I checked everything is good to download.

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

    Thanks for all the great info. But i have trouble making the rectangle move in other directions besides up. It seems like all the keys are set to UpPressed. What do i do in that situation?👌🏾👌🏾

  • @Rohan-Prabhala
    @Rohan-Prabhala ปีที่แล้ว +2

    Does anyone why the background for my JPanel window is white? I'm at 8:01 in the video, and when I click run, my square is white, but so is my window, even though I checked the line that is supposed to make it black, and it looks fine.

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

      I have the same issue, its white and not runnig well. Did u find any solution?

    • @Rohan-Prabhala
      @Rohan-Prabhala 4 หลายเดือนก่อน

      @@camilazcr1939 fixed it soon after i made the comment, and I completely forgot how lmao, sorry

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

      @@camilazcr1939 I had this problem too . Look at the ( super.paintcomponent(g) )
      Perhaps you add a extra ‘s’ after paintcomponent

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

    Hi , I am wondering if anyone can help me with the issue I am facing,. I have used the same logic as in the video to move a character on screen in my java project. However, It is not moving in the way it should . I debugged it and found out that when I press key , it sets my keypressed boolean to true, but as soon as I release it , it turns it to false before moving it. Therefore it does not move. it only moves when I press and hold it long enough for update to run. There is a lag as well when it moves. Any help is appreciated. I have wasted lot of time doing it. I even tried using timer class but I have the same issue.

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

    Ik it is a old vid but if any1 can help me I have a small problem with the boolean down up rigth and leftpressed being all on true and not changing to false even if I state so in a Variable any ideas?

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

    Great video! Do you have a git repository or somehing so we can navigate on the code?

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

    Hello!, I have a question, Can I make the same game as this one using Java IntelliJ instead of Java Eclipse?
    If I enter this code into Java Intellij, will the same screen as this video be displayed?

  • @MrLoser-ks2xn
    @MrLoser-ks2xn 2 ปีที่แล้ว

    Thanks

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

    the best of the best

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

    at minute 27:16 in the almost impossible case that the time ends when we enter the if, instead of setting the remainingTime to 0, can we increase it by 0.016s each time?

  • @XtoInf
    @XtoInf 2 ปีที่แล้ว

    How do I make an import when trying to quick fix, all that pops up to quick fix is Create class, Create Interface, and Create enum

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

    I'm having an issue trying to move the white rectangle, I rewatch the video several times, but all my methods seems to be correct and I don't have any bugs in my debugguer.
    Someone wrote below that in the main class, the method order is truly important so I tried to do it the same way as he did but I'm still stuck at moving it.
    my KeyHandler class is working well since I can have different values depending on what button I press. Does anyone can help me ?

  • @user-jo5nm7mv5n
    @user-jo5nm7mv5n ปีที่แล้ว

    Thread.sleep() in a loop can cause busy-waiting, so wait/notify mechanisms should be used instead

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

    FYI if anyone is having problems with their square moving up or down without any input I'd recommend to; inside of the Keyhandler method update() to change the logic to simplu if()Keyhand.pressed) without the "== true;".

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

    For anyone who wants to be able to implement diagonal movement, it is very simple. The way that if else statements work is that if the first one is true, it will not check the other statements, meaning you can not be moving both upwards and sideways, because it wont check to move sideways. To do this, you need to make a separate if statement with the left and right movement, meaning you can move either up or down, and left or right. It should look like this:
    if(keyH.upPressed == true) {
    playerY = playerY - playerSpeed;
    }
    else if(keyH.downPressed == true) {
    playerY = playerY + playerSpeed;
    }
    if(keyH.leftPressed == true) {
    playerX = playerX - playerSpeed;
    }
    else if(keyH.rightPressed == true) {
    playerX = playerX + playerSpeed;
    }

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

      also, whichever part in the if else statement comes first will take priority, meaning if you are pressing W and S, you will go up because the code checks W first.

  • @yellowchick2104
    @yellowchick2104 2 ปีที่แล้ว

    Hi I was wondering how you automatically import? I don't know why my eclipse doesn't have that option of importing when I hover over any extensions like the Graphics/Graphics2D/Jpanel etc.

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

      Hm that's strange. Could be a bug because the import suggestion should be enabled as default so maybe you want to google the issue. One alternative is using Eclipse's import shortcut (ctrl + shift + o) which automatically imports classes.
      Oh and sometimes, the suggestions show up if you save the project so try saving the project and see if it fix the issue.

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

    hi, in the previous episode when we were making the window, I got stuck on the sizing part how the window is 768 to 576 pixels wide. When I try to do the same it will only show a very small window. Would you know why this is?

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

      Did you pack the panel?

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

    Can someone help please I’ve copied everything in the video and there aren’t any errors in my code showing but the square still isn’t moving.
    Edit: nvm it works now. I used the 2nd loop option (delta) instead of the first one.

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

    i seem to have problem with fillRect when i write (100,100,tileSize,tileSize) it draws , when i write (playerX,playerY,tileSize,tileSize) it doesnt

  • @user-nk6lj9or3k
    @user-nk6lj9or3k ปีที่แล้ว +1

    Hi, first of all thank you very much for this video series. It is clear that you put a lot of effort into it and I really appreaciate it!
    I followed your coding very closely, but for me the rectangle (as well as the sprites in the following video) do not move as smoothly as it is visible in your videos. When I press and hold a key, there the rectangle first makes some bigger jumps and only later moves across the window smoothly. I'm wondering what could be the reason for this and if there is a way to fix it. Any hints are much appreciated, thanks.

  • @Eidamveres
    @Eidamveres 2 ปีที่แล้ว

    Hello very good video but I really need help with public void update and things around it Basicly I tryed it several times but function update just wont work that mean everything like move with "wasd" just wont do anything if you can halp I woud be so happy.