Hey all. 👋 Hope you are doing well. Today we implement the gameloop into the game. We will add more to it later such as the Update portion, but it follows the same pattern. We also discuss why repaint is a bad gameloop.
Dude keep making content like this and there is no way you don't blow up! I'm so honored to be able to go along this game journey with you. Ur insight is more than valuable
The quality of the video, the perfect explanation and the golden joke I love it. I try at first to make a menu for my game in javafx the tutorial and the work was painfuli didn't even you we can easily made an interface with the basic sdk With you it's like a game itself thank you for the work and the dedication of this tutorial you get my abonnement of course. ⭐
Good video man! I was really confused about what you did in 10:25 since I thought the floating point number truncates into an int of 0, but I looked back at the code and realized truncation only happens AFTER the floating point calculations are complete 😁
@@KaarinGaming No problem! Is there a place online where you got started with learning Java? I know it for the most part, could use more practice though.
Dude, props to you for making such a great tutorial!!! Your teaching method is excellent, your voice is smooth and you introduce complex subjects so simply an ant would understand... Also the small jokes............ I feel very comfortable watching to your videos, thank youuu :D
great video, super easy to follow. Its so hard to find videos where they properly explain what they are doing but dont spend ages overexplaining it. Im trying to learn java after already learining python and apparently every other tutorial goes the other way.
@@KaarinGaming no thank you! Keep up the amazing content I'm confident your following will be very large not long from now. Great teacher, well spoken, comedy and make learning very very simplistic. Got to give the credit where it's due!
A small reminder nextInt(bound) returns random integer up to bound EXCLUSIVE. And considering that r,g,b components are in range [0;255] it should be random.nextInt(256);
had my FPS limit declared as a static final byte. Was still getting framerates in the hundreds of thousands and thought no FPS limiter could contain my 4090... then, yeah, realised the division would require an integer.
For some reason when i remove repaint(); from gamePanel, it doesnt give me an FPS count nor does the rectangle move around, any simple way to fix this?
Yes. Atleast that one we add in ep3. We update our gameloop to take into account lost time when updating. It gets explained in ep 6 were we take the gameloop to the next level =)
I drew the rectangle as a different class with a draw method that gets a Graphics g parameter. My FPS was 300 while yours is 63000, can it be about that or did I do something else wrong horribly?
Amazing series! Would it be a valid game loop to use a Thread that sleeps for 1/FPS_SET seconds for each iteration of the while loop ? Because your code is checking for the condition of (now - lastFrame >= timePerFrame) constantly in the background. What would be the pros and/or cons of this kind of loop?
I don't know if this is a problem or not, but I think that my game is stuck at 30fps even tho it's displaying 120 through the fps counter.. I have compared your code with mine and it is the same. When I move the mouse cursor on the GameWindow it is moving the rectangle very laggy, it's like my rendering is somewhat stuck and nothing works.. Any tips or solutions?
This game does not use the GPU as you think. All graphics calculations are done on CPU side. Also worth knowing is that it may not be a lack of power that keeps the fps down. It might detect this program running too fast and halters it.
@@KaarinGaming oh wow. thank you for the reply. I seriously didn't even expect it considering it's been posted a while ago. I appreciate that. I am enjoying your tutorials a lot, first ones that work for me without crazy errors that I can't figure out. Thank you so much for making these.
In my approach I created a Runnable task that updates the game loop and I submitted the task to a ScheduledThreadPoolExecutor. Is there any functional difference between the game loop that you created here and the one that I am using? Cool tutorials by the way.
Not familiar with that one. A key reason why this is the gameloop is that it is constant and looping. If this task that you are using are being started and stopped over and over again, it could lead to lag and uneven ticks(updates). Threads are good, but starting and stopping them cannot guarantee a even "flow" for a lack of better words. But give it a try, maybe it works with a even fps and no lag. Who knows :)
I know this is kind of old but i hope i can get an answer for my question, whenever ive finished making the run method and the startGameLoop method, my FPS is set at 1 and isnt updating, is there a reason why this is happening? i followed through everything you did so far and i got this problem, thank you in advance!
Hey! Hard to say without seeing the code. I would check out the github link for the code for this tutorial and compare. The code on github is for the last episode and might not match 100% what you got there now. Since you are on ep 3 I think you missed reset the fps counter or something similar. Good luck.
@@KaarinGaming thank you so much! i chekced out the github and followed through the video again and found my problem, i didnt start the gameloop 😅, thank you a lot for making these videos they're easy to follow through and are very helpful
@@JJunkWarrior I had the same problem at first, I didn't see him type the "gameThread.start();" inside the StartGameLoop() method, might've been a jump cut or something
Is there any reason the rectangle moving around the screen would still be pretty choppy by the end of the tutorial? The FPS counter shows a consistent 120 FPS but the movement of the rectangle certainly doesn't LOOK smooth at all. I'm on a Ryzen 5 5600g running Fedora 37 on the latest openjdk. (I wonder if it could be a Wayland vs X11 thing?) Thanks for the great tutorial series!
Hi mate, my code is having some errors. I was doing the "adding visuals" and the rectangle just keeps shaking at 1 place. It doesn't move like yours (all around the map). How do I fix it?
thanks a lot for the tutorial! But for the randomized color: Wouldn't it be easier to just make the method like: private Color getRndColor() { Color color = new Color((int) (Math.random()*256),(int) (Math.random()*256),(int) (Math.random()*256)); return color; }
heyy, you did a great tutorial there, and I enjoy every second of new stuff I get to learn! But isn't it called RGB and not RBG? It's not big of a deal, but maybe a nice side information :D Keep up the great work!
Hello, Why do i get 300-500 frames, while you get 30000-50000. And also, for me it works perfectly, when xDir, and yDir is just 1, but for you it is too fast? if change it to 0.003f, it is too slow for me:(
my box disappears as soon as it goes out of the frame and it never comes back , the way your one does like it bounces off the walls :( tried watching the video 2times but it still not working
@@KaarinGaming Apparently the reason why I got a different FPS and speed before was 'cause I made a mistake in the timePerFrame calculation, it's at 120 fps now. I'm moving to lesson 6 now, thanks for the reply though, I forgot to update that comment.
I was having the same trOuble until a found @23:50 there is a jump cut! in the startGameLoop() => under "gameThread = new Thread(this);" => he has added "gameThread.start();"
Followed you. When I go to start the program after moving gamePanel.repaint() to the while loop. I get NullPointerException. Cannot invoke GamePanel.repaint() because this.gamePanel is null. :/ not sure what exactly that is referring to.
Hi kaarin, i have troubles on the last part. For some reason even if i click it doesnt make any rectangle appear. It seems that my mouse Listener isnt working. Can you help me? i copy pasted everything from your github and yet, nothing happens.
"it counts the fps per second" - kaarin gaming
3:25
The fact that he is making things up as he goes makes you realize he has actual knowledge and isn't copying from anywhere
He records this past midnight. That really shows how passionate is about what he's doing.
Hey all. 👋
Hope you are doing well.
Today we implement the gameloop into the game. We will add more to it later such as the Update portion, but it follows the same pattern. We also discuss why repaint is a bad gameloop.
Dude keep making content like this and there is no way you don't blow up! I'm so honored to be able to go along this game journey with you. Ur insight is more than valuable
Wow! Thank you man!
The quality of the video, the perfect explanation and the golden joke I love it.
I try at first to make a menu for my game in javafx the tutorial and the work was painfuli didn't even you we can easily made an interface with the basic sdk
With you it's like a game itself thank you for the work and the dedication of this tutorial you get my abonnement of course. ⭐
That's some really kind words my man! :D Thanks!
14:00 the number you pass in is exclusive, meaning it will never get the value of passed number. It should be 256.
"and if you've been playing any games for the last 10 years, I'd be surprised if you've haven't heard of FPS"
ah yes, First Person Shooters
Good video man! I was really confused about what you did in 10:25 since I thought the floating point number truncates into an int of 0, but I looked back at the code and realized truncation only happens AFTER the floating point calculations are complete
😁
Aula boa meu galo veio, aula boa😎
This helps me more than my university classes in Java do lol! Elämä laula!
Your tutorials are cool bro. Hope I can learn a thing or two at the end of this tutorial. Keep the good work king.
Another great video! Thank you :) Perfect beatboxing, grandma should be proud!
I hope so too :P
Nice intro :3
Waiting for your next tutorial
Cheers mate :D
This will help a lot of people out, keep up the good work!
Thanks man! :)
@@KaarinGaming No problem! Is there a place online where you got started with learning Java? I know it for the most part, could use more practice though.
3:26
fps per second
frames per second per second
🤣🤣🤣
Legend
sick, good stuff man!!
Thanks :D
Dude, props to you for making such a great tutorial!!! Your teaching method is excellent, your voice is smooth and you introduce complex subjects so simply an ant would understand... Also the small jokes............ I feel very comfortable watching to your videos, thank youuu :D
Wow Thanks!
I appreciate the kind words :D
These videos are off the hook I wish you more success in your coding vlogging career too fam. Thanks for the resource.
Thanks man :D
great video, super easy to follow. Its so hard to find videos where they properly explain what they are doing but dont spend ages overexplaining it. Im trying to learn java after already learining python and apparently every other tutorial goes the other way.
Cheers mate! Best of luck on your journey :D
Thanks!
These videos are amazing, keep up the great work 👍
Thanks man! :D
you need a much bigger following!! you are amazing!
Thank you! :D
@@KaarinGaming no thank you! Keep up the amazing content I'm confident your following will be very large not long from now. Great teacher, well spoken, comedy and make learning very very simplistic. Got to give the credit where it's due!
Love the intro xd
Thanks :D
Thanks Bro! Amazing video!
Cheers!
Amazing 🤩
Good job!:)
Can't wait to get into this further, I'm going to use this in Android Studio since my game is both platform and top down based!
A small reminder nextInt(bound) returns random integer up to bound EXCLUSIVE. And considering that r,g,b components are in range [0;255] it should be random.nextInt(256);
You are correct.
Awesome tutorial thanks :DDDD
Cheers mate :D
had my FPS limit declared as a static final byte. Was still getting framerates in the hundreds of thousands and thought no FPS limiter could contain my 4090... then, yeah, realised the division would require an integer.
My random is giving me an error after initializing it😭🙏...
Never mind... I fucking love this channel...
i made the x direction a little bit slower so de rectangle jigles more in the window
For some reason when i remove repaint(); from gamePanel, it doesnt give me an FPS count nor does the rectangle move around, any simple way to fix this?
A line of code was omitted from the tutorial. At 23:50 it is line 21. I had your problem and adding this line fixed it.
@@uhhsam thanks my friend..how i missed it...
Is it possible for your fps counter to go above the set limit? Sometimes mine goes above the 120 limit and goes to 121 or 122. Is that normal?
Yes. Atleast that one we add in ep3.
We update our gameloop to take into account lost time when updating.
It gets explained in ep 6 were we take the gameloop to the next level =)
@@KaarinGaming Appreciate the response thanks :)
I drew the rectangle as a different class with a draw method that gets a Graphics g parameter. My FPS was 300 while yours is 63000, can it be about that or did I do something else wrong horribly?
Repaint
when we used the random class, other than using that class library can we use math. random? is that possible?
u a g
Amazing series!
Would it be a valid game loop to use a Thread that sleeps for 1/FPS_SET seconds for each iteration of the while loop ? Because your code is checking for the condition of (now - lastFrame >= timePerFrame) constantly in the background. What would be the pros and/or cons of this kind of loop?
Cheers man.
Careful with thread.sleep. It tends to cause more issues than it solves. Give it a try for a while and you can see the difference :)
@@KaarinGaming ok I will try it !
I don't know if this is a problem or not, but I think that my game is stuck at 30fps even tho it's displaying 120 through the fps counter.. I have compared your code with mine and it is the same. When I move the mouse cursor on the GameWindow it is moving the rectangle very laggy, it's like my rendering is somewhat stuck and nothing works.. Any tips or solutions?
how did you get 30k fps, I get like 4k? I'm on RTX 2080Ti. btw... your tutorials are the BEST I've come accross.
This game does not use the GPU as you think. All graphics calculations are done on CPU side.
Also worth knowing is that it may not be a lack of power that keeps the fps down. It might detect this program running too fast and halters it.
@@KaarinGaming oh wow. thank you for the reply. I seriously didn't even expect it considering it's been posted a while ago. I appreciate that. I am enjoying your tutorials a lot, first ones that work for me without crazy errors that I can't figure out. Thank you so much for making these.
In my approach I created a Runnable task that updates the game loop and I submitted the task to a ScheduledThreadPoolExecutor. Is there any functional difference between the game loop that you created here and the one that I am using?
Cool tutorials by the way.
Not familiar with that one. A key reason why this is the gameloop is that it is constant and looping. If this task that you are using are being started and stopped over and over again, it could lead to lag and uneven ticks(updates).
Threads are good, but starting and stopping them cannot guarantee a even "flow" for a lack of better words.
But give it a try, maybe it works with a even fps and no lag. Who knows :)
Hii I'm preparing sukuban game , is this tutorial helps ?
...
....
I know this is kind of old but i hope i can get an answer for my question, whenever ive finished making the run method and the startGameLoop method, my FPS is set at 1 and isnt updating, is there a reason why this is happening? i followed through everything you did so far and i got this problem, thank you in advance!
Hey!
Hard to say without seeing the code. I would check out the github link for the code for this tutorial and compare.
The code on github is for the last episode and might not match 100% what you got there now.
Since you are on ep 3 I think you missed reset the fps counter or something similar.
Good luck.
@@KaarinGaming thank you so much! i chekced out the github and followed through the video again and found my problem, i didnt start the gameloop 😅, thank you a lot for making these videos they're easy to follow through and are very helpful
@@JJunkWarrior I had the same problem at first, I didn't see him type the "gameThread.start();" inside the StartGameLoop() method, might've been a jump cut or something
@@zacharylahners6377 thanks for this
Is there any reason the rectangle moving around the screen would still be pretty choppy by the end of the tutorial? The FPS counter shows a consistent 120 FPS but the movement of the rectangle certainly doesn't LOOK smooth at all. I'm on a Ryzen 5 5600g running Fedora 37 on the latest openjdk. (I wonder if it could be a Wayland vs X11 thing?)
Thanks for the great tutorial series!
Hey.
Check out the discord for more help. Comments on youtube is not very useful in this.
Cheers :D
Hi mate, my code is having some errors. I was doing the "adding visuals" and the rectangle just keeps shaking at 1 place. It doesn't move like yours (all around the map). How do I fix it?
Hey.
Take a look at github for code reference. Links in the description
@@KaarinGaming ok thank you sir
Please teach the boss fight
thanks a lot for the tutorial! But for the randomized color: Wouldn't it be easier to just make the method like:
private Color getRndColor() {
Color color = new Color((int) (Math.random()*256),(int) (Math.random()*256),(int) (Math.random()*256));
return color;
}
no, a lot more ez pz
private Color rndColor() {
return new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255));
}
for making random colors instead can we use math.random? also instead of writing while(true) can we write while(gameThread != null)?
Sure. That could work. Give it a try :)
heyy, you did a great tutorial there, and I enjoy every second of new stuff I get to learn! But isn't it called RGB and not RBG? It's not big of a deal, but maybe a nice side information :D Keep up the great work!
nvm u fixed it urself xD
@@n0mi1k Cheers mate :D
Yeah i had some tunnelvision going on there xD
for some reason my program runs smoothly only when i drag my mouse. could anyone help explain?
Its a weird bug. I assume you might be on a linux os.
There is a "bug" fix on the discord servern.
Hello, Why do i get 300-500 frames, while you get 30000-50000. And also, for me it works perfectly, when xDir, and yDir is just 1, but for you it is too fast? if change it to 0.003f, it is too slow for me:(
And also, when i added the threads part, the rectangle is not moving at all with the mouse.
@@aigerimaibatbek4810I'm encountering the same problem as you are ;-;
Finding a perfect game loop has been interesting I find I can get 60fps usually but it drops frames periodically.
I'll be trying this when I get home my loop seems to be slow at times
EZ
how is it running, even if i cannot see, where the methode "run" is being executed? I am a bit confused...
I have a problem my rectangle only goes in one direction and won’t change any directions by its own
my box disappears as soon as it goes out of the frame and it never comes back , the way your one does like it bounces off the walls
:( tried watching the video 2times but it still not working
I get an error when defining fps and fixing fps to 120. From where
26:26 Idk why I get 240 ms instead of 120 :(
I had to let my xDir and yDir at 0.1f to get a similar speed, I guess that's not a problem right?
Then you would have much faster fps count. Faster fps -> faster speed.
But this code gets removed soon, it's a demonstration of gameloop =)
@@KaarinGaming Apparently the reason why I got a different FPS and speed before was 'cause I made a mistake in the timePerFrame calculation, it's at 120 fps now. I'm moving to lesson 6 now, thanks for the reply though, I forgot to update that comment.
What's the meaning of xDir?
Whenever I get rid of repaint(); i start to get 0 fps and it doesn't update whenever I move onto the real gameloop
Take a look on github on github to compare your code to my code. It usually helps :)
doesn't task manager better then treading and looping?
my fps cap is not working i followed your tutorial as you said but i am not able to do it.
I was having the same trOuble until a found @23:50 there is a jump cut!
in the startGameLoop() => under "gameThread = new Thread(this);" => he has added "gameThread.start();"
idk why but my fps is fluctuating between 2 values
In game.java my loop is not working neither i am getting any movement with my key pressing no fps reading
he forgot to say gameThread.start(); under gameThread = new Thread(this); on line 21
Followed you. When I go to start the program after moving gamePanel.repaint() to the while loop. I get NullPointerException. Cannot invoke GamePanel.repaint() because this.gamePanel is null.
:/ not sure what exactly that is referring to.
Hi kaarin, i have troubles on the last part. For some reason even if i click it doesnt make any rectangle appear. It seems that my mouse Listener isnt working. Can you help me? i copy pasted everything from your github and yet, nothing happens.
i am getting 119,120,121,120,again 121 why this?
u are lucky that u r a guy.
This episode is bad :c
Thanks!