

Welcome to episode 7 of our Super Mario game in java tutorial! In this episode, we will be creating a frames per second and an updates/ticks per second timer.
If you enjoyed, leave a like, a comment, and subscribe.
Next video will be a gaming video (most likely happy wheels).
Stay tuned for more! š
source
Your videos are amazing! However, I may like to ask why did you use those variables when you were creating the timer. I do not understand it very well and I would like to learn how did you create it properly. Thanks in advance!
can anyone explain how that algorithm works lol it's like the one thing he doesn't really explain
Apparently you are not a bad programmer, but you don't explain anything about this time, delta, frames and updates and what it actually does… Looks like you learned this from someone but don't really understand what it is you're doing.
This guy is a genius
Hi. I'm having an extremely odd issue. My code will run once, then stops. My code is exactly like yours, so I don't understand the issue.
EDIT:
Sometimes it runs properly though. That's what really confuses me.
Hi, if i run my application the tick method runs in the first second 60 times(60 updates per second) but after the first second it only runs at 6 updates per second!
here is the source code š :
/**
@Author Niklas, 12y.o (Steam: niklaseeeee) You can add me on steam if you want š
@Version 0.0.1(Full-Versions, Features, Debugs)
@Information "ch" stands for switzerland š
*/
package ch.niklas.mario;
import java.awt.Canvas;
import java.awt.Dimension;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
public final static int FRAME_WIDTH = 270, FRAME_HEIGHT = FRAME_WIDTH/14 * 10; // Frame Width and Height for the JFrame !
public final int FRAME_SCALE = 4; // This Will Be Multiplied To Our FRAME_WIDTH / HEIGHT
public final static String GAME_TITLE = "Mario";
private Thread thread;
private boolean running = false;
public synchronized void start() {
if (running) return;
running = true;
thread = new Thread(this, "Thread");
thread.start();
}
public synchronized void stop() {
if (!running) return;
running = false;
try {
thread.join();
} catch (InterruptedException e) {
System.out.println("thread.join() ERROR");
e.printStackTrace();
}
}
@Override
public void run() {
long lastTime = System.nanoTime();
long timer = System.currentTimeMillis();
double delta = 0.0;
double ns = 1000000000.0/60.0;
int frames = 0;
int ticks = 0;
while(running) {
long now = System.nanoTime();
delta += (now – lastTime) / ns;
lastTime = now;
while(delta >= 1){
tick();
ticks++;
delta–;
}
render();
frames++;
if(System.currentTimeMillis()-timer>1000) {
timer += 100;
System.out.println(frames + " fps | " + ticks + " ups");
frames = 0;
ticks = 0;
}
}
stop();
}
public void render() { // render = draw
}
public void tick() { // tick = update
System.out.println("hi");
}
public Game() { // Game Class Constructor
Dimension gameSize = new Dimension(FRAME_WIDTH * FRAME_SCALE, FRAME_HEIGHT * FRAME_SCALE); //Game-Size in a Dimension
setPreferredSize(gameSize);
setMinimumSize(gameSize);
setMaximumSize(gameSize);
setVisible(true);
}
public static void main(String[] args) {
Game game = new Game();
JFrame frame = new JFrame(GAME_TITLE);
frame.add(game); // Adds the Game Object to the JFrame
frame.pack(); // Put All Together
frame.setResizable(false);
frame.setDefaultCloseOperation(3); // 3 = JFrame.EXIT_ON_CLOSE
frame.setLocationRelativeTo(null);
frame.setVisible(true); // The SetVisible Method should always be at the end!
game.start();
}
}
ChernoPyoject.
you've got some real skill bud, i'm impressed.
i do think though that maybe you're targeting the wrong audience! explaining what 'double' is, but not mentioning why delta+=(now-lastTime)/ns;
XD
have you got any advice on where to learn the logic or reasoning behind the loops and variables? would really help!
thanks,
I dont get it, it confuses my head so much!
what do you mean by nano Time GETS THE CURRENT TIME current time of what. it doesnt make any sense. can you please explain to me
why am i getting so many FPS? 23111165 FPS & 60 Updates PS. Here is code
package com.Adventure;
import java.awt.Canvas;
import java.awt.Dimension;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable {
public static final int WIDTH = 270;
public static final int HEIGHT = WIDTH / 14 * 10;
public static final int SCALE = 4;
public static final String TITLE = "Adventure Game";
private Thread thread;
private boolean running = false;
private synchronized void start() {
if(running) return;
running = true;
thread = new Thread(this, "Thread");
thread.start();
}
private synchronized void stop() {
if(!running) return;
running = false;
try {
thread.join();
} catch(InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void run() {
long lastTime = System.nanoTime();
long timer = System.currentTimeMillis();
double delta = 0.0;
double ns = 1000000000.0 / 60.0;
int frames = 0;
int ticks = 0;
while(running) {
long now = System.nanoTime();
delta += (now – lastTime) / ns;
lastTime = now;
while(delta >= 1) {
tick();
ticks++;
delta–;
}
render();
frames++;
if(System.currentTimeMillis() – timer > 1000) {
timer += 1000;
System.out.println(frames + " FPS & " + ticks + " Updates PS");
frames = 0;
ticks = 0;
}
}
stop();
}
public void render() {
}
public void tick() {
}
public Game() {
Dimension size = new Dimension(WIDTH * SCALE, HEIGHT * SCALE);
setPreferredSize(size);
setMaximumSize(size);
setMinimumSize(size);
}
public static void main(String[] args) {
Game game = new Game();
JFrame frame = new JFrame(TITLE);
frame.add(game);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
game.start();
}
}
Is the intro music from Asphalt 8 : Airborne ?
if(guessedCorrect)
Me.out.say("YAY!!);
else
Me.out.say("WHADDAHELLISITFROM???");
My console won't print anything out. I double checked the code and its typed exactly like yours. Anyone know how to fix this?
Honestly, your run method is very complicated and very hard to follow.
very nice tutorials im impressed, i like your style of making these š
I get timer cannot be resolved or is not a field. What does this mean?!? >.< I get it in if(System.currentTimeMillis().timer > 1000)
Play five nights at freedys