index.html 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <body>
  2. <h1>Duck Challenge</h1>
  3. <div class="game">
  4. <div id="duck"></div>
  5. <div class="water"></div>
  6. <div class="pad left">
  7. <div class="lily"></div>
  8. </div>
  9. <div class="pad right">
  10. <div class="lily"></div>
  11. </div>
  12. </div>
  13. <div class="score">
  14. <span>Score: </span><span id="score">0</span>
  15. </div>
  16. <p>Help the duck get to the other side by jumping from lily pad to lily pad!</p>
  17. </body>
  18. <style>
  19. body {
  20. background-color: #8ED2C9;
  21. color: #2F4858;
  22. font-family: sans-serif;
  23. margin: 0;
  24. text-align: center;
  25. }
  26. h1 {
  27. margin: 0;
  28. padding: 0.25em 0;
  29. }
  30. .game {
  31. position: relative;
  32. margin: 1em 0;
  33. }
  34. #duck {
  35. width: 4em;
  36. height: 4em;
  37. background-image: url('duck.png');
  38. position: absolute;
  39. bottom: -2em;
  40. left: 2em;
  41. transition: left 2s;
  42. z-index: 2;
  43. }
  44. .water {
  45. width: 92%;
  46. height: 2em;
  47. margin-right: 4%;
  48. background-color: #54A0FF;
  49. position: absolute;
  50. bottom: 0;
  51. left: 0;
  52. }
  53. .pad {
  54. position: absolute;
  55. width: 8%;
  56. left: 2%;
  57. bottom: 8px;
  58. }
  59. .pad.left {
  60. left: 0;
  61. }
  62. .pad.right {
  63. left: 92%;
  64. }
  65. .lily {
  66. width: 5em;
  67. height: 5em;
  68. border-radius: 5em;
  69. background-color: #16A085;
  70. position: absolute;
  71. bottom: -3em;
  72. left: 2em;
  73. }
  74. .score {
  75. width: 70%;
  76. margin: 1em auto 0;
  77. font-weight: bold;
  78. font-size: 1.2rem;
  79. }
  80. p {
  81. font-size: 0.9rem;
  82. margin: 0;
  83. padding: 0.25em 0;
  84. }
  85. </style>
  86. <script>
  87. let score = 0;
  88. let leftPad, rightPad;
  89. document.addEventListener("DOMContentLoaded", function () {
  90. rightPad = document.querySelector(".pad.right")
  91. leftPad = document.querySelector(".pad.left")
  92. leftPad.addEventListener("click", duckJumpLeft);
  93. rightPad.addEventListener("click", duckJumpRight);
  94. });
  95. let duckJumpLeft = function () {
  96. document.querySelector("#duck").style.left = "2%";
  97. score++;
  98. updateScore(score);
  99. }
  100. let duckJumpRight = function () {
  101. document.querySelector("#duck").style.left = "92%";
  102. score++;
  103. updateScore(score);
  104. }
  105. let updateScore = function (newScore) {
  106. document.querySelector("#score").innerHTML = newScore;
  107. }
  108. </script>