File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ const character = document . querySelector ( ".dino" ) ;
2+ const block = document . querySelector ( ".cactus" ) ;
3+
4+ const jump = ( ) => {
5+ // Add class to initiate jump
6+ character . classList . add ( "animate" ) ;
7+
8+ // Remove class after animation duration (500ms)
9+ setTimeout ( ( ) => {
10+ character . classList . remove ( "animate" ) ;
11+ } , 500 ) ;
12+ } ;
13+
14+ // Trigger jump on spacebar press
15+ document . addEventListener ( "keydown" , ( event ) => {
16+ if ( event . code === "Space" ) {
17+ jump ( ) ;
18+ }
19+ } ) ;
20+
21+ // Check for collision
22+ const checkDead = setInterval ( ( ) => {
23+ const blockLeft = parseInt (
24+ window . getComputedStyle ( block ) . getPropertyValue ( "left" )
25+ ) ;
26+ const characterTop = parseInt (
27+ window . getComputedStyle ( character ) . getPropertyValue ( "top" )
28+ ) ;
29+
30+ // Check for collision
31+ if ( blockLeft < 20 && blockLeft > 0 && characterTop >= 178 ) {
32+ block . style . animation = "none" ;
33+ block . style . display = "none" ;
34+ alert ( "Uh..Oh, you lose." ) ;
35+ clearInterval ( checkDead ) ; // Stop checking for collisions
36+ }
37+ } , 100 ) ;
You can’t perform that action at this time.
0 commit comments