1 | <!DOCTYPE html> |
2 | <html lang="es"> |
3 | |
4 | <head> |
5 | <meta charset="UTF-8"> |
6 | <meta name="viewport" |
7 | content="width=device-width"> |
8 | <title>Choca</title> |
9 | <style> |
10 | .sprite { |
11 | position: fixed; |
12 | font-size: 3rem; |
13 | } |
14 | </style> |
15 | </head> |
16 | |
17 | <body> |
18 | <h1>Choca</h1> |
19 | <output id="abrazo" |
20 | class="sprite" |
21 | style="background-color: |
22 | greenyellow;"> |
23 | 🤗 |
24 | </output> |
25 | <output id="triste" |
26 | class="sprite" |
27 | style="background-color: |
28 | grey;"> |
29 | 😪 |
30 | </output> |
31 | <script> |
32 | let x = 0 |
33 | const REFRESCO = 5 |
34 | setInterval(avanza, REFRESCO) |
35 | |
36 | function avanza() { |
37 | const velocidad = |
38 | innerWidth / 2000 |
39 | const x2 = innerWidth |
40 | const y = innerHeight / 2 |
41 | triste.style.left = |
42 | `${innerWidth / 2}px` |
43 | triste.style.bottom = `${y}px` |
44 | abrazo.style.left = `${x}px` |
45 | abrazo.style.bottom = `${y}px` |
46 | if (!seTocan(abrazo, triste)) { |
47 | x = (x + velocidad) % x2 |
48 | } |
49 | } |
50 | |
51 | |
52 | |
53 | |
54 | |
55 | |
56 | |
57 | |
58 | |
59 | |
60 | function seTocan(e1, e2) { |
61 | const rE1 = |
62 | e1.getBoundingClientRect() |
63 | const rE2 = |
64 | e2.getBoundingClientRect() |
65 | return (rE1.right >= rE2.left |
66 | && rE1.left <= rE2.right |
67 | && rE1.top <= rE2.bottom |
68 | && rE1.bottom >= rE2.top) |
69 | } |
70 | </script> |
71 | </body> |
72 | |
73 | </html> |