11// Configuration constants
22const CONFIG = {
3- GROUND_HEIGHT : 20 ,
3+ GROUND_HEIGHT : 22 ,
44 PLAYER : {
55 WIDTH : 80 ,
66 HEIGHT : 40 ,
@@ -460,6 +460,7 @@ class Game {
460460 this . gameSpeed = CONFIG . GAME . INITIAL_SPEED ;
461461 this . gameOver = false ;
462462 this . paused = false ;
463+ this . key = "" ;
463464
464465 // Delta time approach instead of frame limiting
465466 this . lastTime = 0 ;
@@ -475,15 +476,7 @@ class Game {
475476 canvas . focus ( ) ;
476477
477478 UI . restartButton . addEventListener ( "click" , ( ) => this . reset ( ) ) ;
478- UI . shareButton . addEventListener ( "click" , ( ) => this . share ( ) ) ;
479- }
480-
481- share ( ) {
482- const text = `I scored ${ this . score } in Nyan Jump! Try to beat me!` ;
483- const hashtags = "keymap-rs,rust,wasm" ;
484- const url = `https://x.com/intent/tweet?text=${ encodeURIComponent ( text ) } &url=${ encodeURIComponent ( window . location ) } &hashtags=${ hashtags } ` ;
485-
486- window . open ( url , "_blank" ) ;
479+ UI . shareButton . addEventListener ( "click" , ( ) => share ( this . score ) ) ;
487480 }
488481
489482 reset ( ) {
@@ -498,6 +491,7 @@ class Game {
498491 this . gameSpeed = CONFIG . GAME . INITIAL_SPEED ;
499492 this . gameOver = false ;
500493 this . paused = false ;
494+ this . key = "" ;
501495 this . _hideGameOverUI ( ) ;
502496 this . accumulator = 0 ; // Ensure game logic runs on first frame after reset
503497
@@ -508,6 +502,13 @@ class Game {
508502 } ) ;
509503 }
510504
505+ setKey ( key , desc ) {
506+ this . key = [ key , desc ]
507+ . filter ( Boolean )
508+ . map ( ( s ) => s . toLowerCase ( ) )
509+ . join ( " - " ) ;
510+ }
511+
511512 togglePause ( ) {
512513 this . paused = ! this . paused ;
513514 if ( ! this . paused ) {
@@ -539,6 +540,18 @@ class Game {
539540 ctx . fillText ( `Score: ${ this . score } ` , 10 , 20 ) ;
540541 }
541542
543+ _drawKey ( ) {
544+ let fontSize = 10 ;
545+ ctx . fillStyle = "#ccc" ;
546+ ctx . font = `${ fontSize } px "${ CONFIG . GAME . FONT_FACE } "` ;
547+ ctx . textAlign = "center" ;
548+ ctx . fillText (
549+ this . key ,
550+ canvas . width / 2 ,
551+ GROUND_Y + CONFIG . GROUND_HEIGHT - ( CONFIG . GROUND_HEIGHT - fontSize ) / 2 ,
552+ ) ;
553+ }
554+
542555 _handleScoring ( ) {
543556 this . obstacleManager . updateScoring ( this . player , ( ) => {
544557 this . score ++ ;
@@ -619,12 +632,21 @@ class Game {
619632 this . rainbowTrail . draw ( ) ;
620633 this . obstacleManager . draw ( ) ;
621634 this . _drawScore ( ) ;
635+ this . _drawKey ( ) ;
622636
623637 // Update and draw FPS
624638 this . fpsCounter . draw ( ) ;
625639 }
626640}
627641
642+ function share ( score ) {
643+ const text = `I scored ${ score } in Nyan Jump! Try to beat me!` ;
644+ const hashtags = "keymap-rs,rust,wasm" ;
645+ const url = `https://x.com/intent/tweet?text=${ encodeURIComponent ( text ) } &url=${ encodeURIComponent ( window . location ) } &hashtags=${ hashtags } ` ;
646+
647+ window . open ( url , "_blank" ) ;
648+ }
649+
628650// Game instance
629651let game = new Game ( ) ;
630652
@@ -655,12 +677,12 @@ export function isGameOver() {
655677 return game . gameOver ;
656678}
657679
658- export function setGameOver ( ) {
659- game . gameOver = true ;
660- }
661-
662680export function pauseGame ( ) {
663681 if ( ! game . gameOver ) {
664682 game . togglePause ( ) ;
665683 }
666684}
685+
686+ export function setKey ( key , description ) {
687+ game . setKey ( key , description ) ;
688+ }
0 commit comments