Guest User

Donkey Kong ChatGPT

a guest
Sep 20th, 2023
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Donkey Kong Clone</title>
  5. <style>
  6. svg {
  7. border: 1px solid black;
  8. display: block;
  9. margin: 0 auto;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <svg id="gameCanvas" width="400" height="400"></svg>
  15. <script>
  16. const svg = document.getElementById('gameCanvas');
  17. const player = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
  18. const platforms = [];
  19. const ladders = [];
  20.  
  21. // Create player character
  22. player.setAttribute('x', 50);
  23. player.setAttribute('y', 320);
  24. player.setAttribute('width', 30);
  25. player.setAttribute('height', 30);
  26. player.setAttribute('fill', 'blue');
  27. svg.appendChild(player);
  28.  
  29. // Create platforms
  30. createPlatform(0, 350, 400, 20);
  31. createPlatform(100, 300, 100, 20);
  32. createPlatform(250, 250, 100, 20);
  33.  
  34. // Create ladders
  35. createLadder(175, 250, 30, 100);
  36. createLadder(75, 300, 30, 50);
  37.  
  38. function createPlatform(x, y, width, height) {
  39. const platform = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
  40. platform.setAttribute('x', x);
  41. platform.setAttribute('y', y);
  42. platform.setAttribute('width', width);
  43. platform.setAttribute('height', height);
  44. platform.setAttribute('fill', 'green');
  45. platforms.push(platform);
  46. svg.appendChild(platform);
  47. }
  48.  
  49. function createLadder(x, y, width, height) {
  50. const ladder = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
  51. ladder.setAttribute('x', x);
  52. ladder.setAttribute('y', y);
  53. ladder.setAttribute('width', width);
  54. ladder.setAttribute('height', height);
  55. ladder.setAttribute('fill', 'brown');
  56. ladders.push(ladder);
  57. svg.appendChild(ladder);
  58. }
  59.  
  60. // Character state
  61. let isJumping = false;
  62. let isClimbing = false;
  63. let jumpHeight = 0;
  64.  
  65. // Game loop
  66. function gameLoop() {
  67. if (isJumping) {
  68. jumpHeight++;
  69. const newY = parseFloat(player.getAttribute('y')) - 5;
  70. player.setAttribute('y', newY);
  71. if (jumpHeight === 50) {
  72. isJumping = false;
  73. }
  74. } else if (isClimbing) {
  75. const newY = parseFloat(player.getAttribute('y')) - 1;
  76. player.setAttribute('y', newY);
  77. }
  78.  
  79. requestAnimationFrame(gameLoop);
  80. }
  81.  
  82. // Handle keyboard input
  83. window.addEventListener('keydown', (event) => {
  84. if (event.key === 'ArrowLeft') {
  85. const newX = parseFloat(player.getAttribute('x')) - 5;
  86. player.setAttribute('x', newX);
  87. } else if (event.key === 'ArrowRight') {
  88. const newX = parseFloat(player.getAttribute('x')) + 5;
  89. player.setAttribute('x', newX);
  90. } else if (event.key === 'ArrowUp') {
  91. const playerX = parseFloat(player.getAttribute('x'));
  92. const playerY = parseFloat(player.getAttribute('y'));
  93. for (const ladder of ladders) {
  94. const ladderX = parseFloat(ladder.getAttribute('x'));
  95. const ladderY = parseFloat(ladder.getAttribute('y'));
  96. const ladderWidth = parseFloat(ladder.getAttribute('width'));
  97. if (
  98. playerX + player.getAttribute('width') >= ladderX &&
  99. playerX <= ladderX + ladderWidth &&
  100. playerY + player.getAttribute('height') > ladderY
  101. ) {
  102. isClimbing = true;
  103. break;
  104. }
  105. }
  106. } else if (event.key === ' ') {
  107. if (!isJumping) {
  108. isJumping = true;
  109. jumpHeight = 0;
  110. }
  111. }
  112. });
  113.  
  114. // Handle releasing the "Up" arrow key
  115. window.addEventListener('keyup', (event) => {
  116. if (event.key === 'ArrowUp') {
  117. isClimbing = false;
  118. }
  119. });
  120.  
  121. gameLoop();
  122. </script>
  123. </body>
  124. </html>
  125.  
Advertisement
Add Comment
Please, Sign In to add comment