@@ -378,57 +378,64 @@ public synchronized void stop() {
378378 Game .setRunning (false ); // Game will not run
379379 }
380380
381+ /*
382+ * This method forms the game loop, determining how the game runs. It runs throughout the entire game,
383+ * continuously updating the game state and rendering the game.
384+ */
381385 public void run () {
382- long lastTime = System .nanoTime ();
383- double nsPerTick = 1000000000D / 60D ;
384-
386+ long lastTime = System .nanoTime (); // Current time in nanoseconds (used for accuracy)
387+ double nsPerTick = 1000000000D / 60D ; // The number of nanoseconds in one tick (number of ticks limited to 60 per update)
388+ // 1 billion nanoseconds in one second
385389 int ticks = 0 ;
386390 int frames = 0 ;
387391
388- long lastTimer = System .currentTimeMillis ();
389- double delta = 0 ;
392+ long lastTimer = System .currentTimeMillis (); // Current time in milliseconds (update ticks and frames once every second)
393+ double delta = 0 ;
390394
391- init ();
395+ init (); // Initialize the game environment
392396
393- while (Game .isRunning ()) {
394- long now = System .nanoTime ();
395- delta += (now - lastTime ) / nsPerTick ;
396- lastTime = now ;
397+ while (Game .isRunning ()) { // Keep looping until game ends
398+ long now = System .nanoTime (); // Current time to used check against lastTime (time has passed since entering the loop)
399+ delta += (now - lastTime ) / nsPerTick ; // Elapsed time in seconds multiplied by 60
400+ lastTime = now ; // Update the lastTime to the current time (now)
397401 boolean shouldRender = false ;
398402
399- while (delta >= 1 ) {
400- ticks ++;
401- tick ();
402- delta -= 1 ;
403- shouldRender = true ;
403+ while (delta >= 1 ) { // Once delta is greater than or equal to 1 (once 1/60 seconds or more have passed)
404+ ticks ++; // Increase the ticks
405+ tick (); // Update the tick
406+ delta -= 1 ; // Delta becomes less than one again and the loop will close
407+ shouldRender = true ; // Rendering should occur during update
404408 }
405409
406410 try {
407- Thread .sleep (2 );
408- } catch (InterruptedException e ) {
411+ Thread .sleep (2 ); // Delays the thread by 2 milliseconds
412+ } catch (InterruptedException e ) { // If the current thread is interrupted, the interrupted status is cleared
409413 e .printStackTrace ();
410414 }
411415
412- if (shouldRender ) {
416+ if (shouldRender ) { // Limits the frames to 60 per second
413417 frames ++;
414418 render ();
415419 }
416420
417- if (System .currentTimeMillis () - lastTimer >= 1000 ) {
418- lastTimer += 1000 ;
421+ if (System .currentTimeMillis () - lastTimer >= 1000 ) { // If elapsed time is greater than or equal to 1 second, update
422+ lastTimer += 1000 ; // Updates in another second
419423 getFrame ().setTitle (
420424 "JavaGame - Version "
421425 + WordUtils .capitalize (game_Version ).substring (
422426 1 , game_Version .length ()));
423427 fps = frames ;
424428 tps = ticks ;
425- frames = 0 ;
426- ticks = 0 ;
429+ frames = 0 ; // Reset the frames (once per second)
430+ ticks = 0 ; // Reset the ticks (once per second)
427431 }
428432 }
429433
430434 }
431435
436+ /*
437+ * This method updates the logic of the game.
438+ */
432439 public void tick () {
433440 setTickCount (getTickCount () + 1 );
434441 level .tick ();
0 commit comments