"Java Programming Tutorial" | "Super Mario Game" #7 | Timer

///"Java Programming Tutorial" | "Super Mario Game" #7 | Timer

"Java Programming Tutorial" | "Super Mario Game" #7 | Timer

FavoriteLoadingAdd to favorites

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.

Java468x60

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

By |2020-06-10T07:43:42+00:00June 10th, 2020|Java Video Tutorials|17 Comments

17 Comments

  1. Luisa Blanco Mula June 10, 2020 at 7:43 am - Reply

    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!

  2. Calvin Lee June 10, 2020 at 7:43 am - Reply

    can anyone explain how that algorithm works lol it's like the one thing he doesn't really explain

  3. KappoDB June 10, 2020 at 7:43 am - Reply

    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.

  4. Kenneth Diaz June 10, 2020 at 7:43 am - Reply

    This guy is a genius

  5. Kurtis Knodel June 10, 2020 at 7:43 am - Reply

    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.

  6. MuabYT June 10, 2020 at 7:43 am - Reply

    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();

    }

    }

  7. Nicolas Mou June 10, 2020 at 7:43 am - Reply

    ChernoPyoject.

  8. nick woodward June 10, 2020 at 7:43 am - Reply

    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,

  9. ZĪµĻ…s June 10, 2020 at 7:43 am - Reply

    I dont get it, it confuses my head so much!

  10. space1188 June 10, 2020 at 7:43 am - Reply

    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

  11. Kenny Freeman June 10, 2020 at 7:43 am - Reply

    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();
    }

    }

  12. ARJAV GARG June 10, 2020 at 7:43 am - Reply

    Is the intro music from Asphalt 8 : Airborne ?

    if(guessedCorrect)
    Me.out.say("YAY!!);
    else
    Me.out.say("WHADDAHELLISITFROM???");

  13. RoboKing13 June 10, 2020 at 7:43 am - Reply

    My console won't print anything out. I double checked the code and its typed exactly like yours. Anyone know how to fix this?

  14. An Dinh June 10, 2020 at 7:43 am - Reply

    Honestly, your run method is very complicated and very hard to follow.

  15. Kay June 10, 2020 at 7:43 am - Reply

    very nice tutorials im impressed, i like your style of making these šŸ™‚

  16. Izuru15 June 10, 2020 at 7:43 am - Reply

    I get timer cannot be resolved or is not a field. What does this mean?!? >.< I get it in if(System.currentTimeMillis().timer > 1000)

  17. Basketball Blogs June 10, 2020 at 7:43 am - Reply

    Play five nights at freedys

Leave A Comment

*