File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ <html lang =" en" >
3+ <head >
4+ <meta charset =" UTF-8" >
5+ <title >Mario Mini Game</title >
6+
7+ <style >
8+ body {
9+ margin : 0 ;
10+ background : #5c94fc ;
11+ overflow : hidden ;
12+ font-family : Arial ;
13+ }
14+
15+ #game {
16+ position : relative ;
17+ width : 100vw ;
18+ height : 100vh ;
19+ }
20+
21+ #mario {
22+ position : absolute ;
23+ width : 50px ;
24+ height : 50px ;
25+ background : red ;
26+ bottom : 50px ;
27+ left : 100px ;
28+ border-radius : 10px ;
29+ }
30+
31+ .ground {
32+ position : absolute ;
33+ bottom : 0 ;
34+ width : 100% ;
35+ height : 50px ;
36+ background : green ;
37+ }
38+
39+ .obstacle {
40+ position : absolute ;
41+ width : 40px ;
42+ height : 40px ;
43+ background : brown ;
44+ bottom : 50px ;
45+ right : -40px ;
46+ }
47+
48+ #score {
49+ position : absolute ;
50+ top : 10px ;
51+ left : 10px ;
52+ color : white ;
53+ font-size : 20px ;
54+ }
55+ </style >
56+ </head >
57+
58+ <body >
59+
60+ <div id =" game" >
61+ <div id =" score" >Score: 0</div >
62+ <div id =" mario" ></div >
63+ <div class =" ground" ></div >
64+ </div >
65+
66+ <script >
67+ const mario = document .getElementById (" mario" );
68+ const game = document .getElementById (" game" );
69+ const scoreText = document .getElementById (" score" );
70+
71+ let jumping = false ;
72+ let score = 0 ;
73+
74+ // Jump function
75+ document .addEventListener (" keydown" , () => {
76+ if (jumping) return ;
77+
78+ jumping = true ;
79+ let position = 50 ;
80+
81+ const up = setInterval (() => {
82+ if (position >= 150 ) {
83+ clearInterval (up);
84+
85+ const down = setInterval (() => {
86+ if (position <= 50 ) {
87+ clearInterval (down);
88+ jumping = false ;
89+ }
90+ position -= 5 ;
91+ mario .style .bottom = position + " px" ;
92+ }, 20 );
93+ }
94+
95+ position += 5 ;
96+ mario .style .bottom = position + " px" ;
97+ }, 20 );
98+ });
99+
100+ // Create obstacles
101+ function createObstacle () {
102+ const obstacle = document .createElement (" div" );
103+ obstacle .classList .add (" obstacle" );
104+ game .appendChild (obstacle);
105+
106+ let position = window .innerWidth ;
107+
108+ const move = setInterval (() => {
109+ if (position < - 40 ) {
110+ clearInterval (move);
111+ obstacle .remove ();
112+ score++ ;
113+ scoreText .innerText = " Score: " + score;
114+ } else {
115+ position -= 5 ;
116+ obstacle .style .left = position + " px" ;
117+ }
118+
119+ // Collision detection
120+ const marioBottom = parseInt (window .getComputedStyle (mario).bottom );
121+ if (position > 80 && position < 140 && marioBottom < 90 ) {
122+ alert (" Game Over! Score: " + score);
123+ location .reload ();
124+ }
125+
126+ }, 20 );
127+
128+ setTimeout (createObstacle, Math .random () * 2000 + 1000 );
129+ }
130+
131+ createObstacle ();
132+ </script >
133+
134+ </body >
135+ </html >
You can’t perform that action at this time.
0 commit comments