diff --git a/frontend/src/components/OriginalPong.js b/frontend/src/components/OriginalPong.js index e13e868b4..73628f831 100644 --- a/frontend/src/components/OriginalPong.js +++ b/frontend/src/components/OriginalPong.js @@ -1,8 +1,10 @@ import React, { useEffect, useRef, useState } from "react"; import backgroundImage from "../images/pongCover.png"; +import settings from "../images/settings.png"; const GameCanvas = () => { // Default Parameters + let resize = true; const defaultSpeedX = 300; const winScore = 2; const defaultSpeedY = 20; @@ -142,7 +144,8 @@ const GameCanvas = () => { const handleResize = () => { if (canvasRef.current) { const screenWidth = window.innerWidth; - const canvasWidth = 0.8 * screenWidth; + let canvasWidth = screenWidth; + if (resize === true) canvasWidth = 0.8 * screenWidth; const canvasHeight = (canvasWidth / 16) * 9; canvasRef.current.width = canvasWidth; canvasRef.current.height = canvasHeight; @@ -233,19 +236,55 @@ const GameCanvas = () => { ); } }; + // touchpad controlls + const handleTouchMove = (event) => { + if (canvasRef.current) { + const touches = event.touches; + const rect = canvasRef.current.getBoundingClientRect(); + for (let i = 0; i < touches.length; i++) { + const touch = touches[i]; + const touchY = event.touches[i].clientY - rect.top - window.scrollY; + // Left paddle controls + if (touch.clientX < window.innerWidth / 2) { + leftPaddleY = touchY - paddleHeight / 2; + leftPaddleY = Math.max( + 0, + Math.min(leftPaddleY, canvasRef.current.height - paddleHeight) + ); + } + // Right paddle controls + else { + rightPaddleY = touchY - paddleHeight / 2; + rightPaddleY = Math.max( + 0, + Math.min(rightPaddleY, canvasRef.current.height - paddleHeight) + ); + } + } + } + }; document.addEventListener("keyup", handleKeyUp); document.addEventListener("keydown", handleKeyDown); + document.addEventListener("touchmove", handleTouchMove); window.addEventListener("resize", handleResize); handleResize(); draw(0); + return () => { - document.addEventListener("keyup", handleKeyUp); + document.removeEventListener("keyup", handleKeyUp); document.removeEventListener("keydown", handleKeyDown); + document.removeEventListener("touchmove", handleTouchMove); window.removeEventListener("resize", handleResize); }; }, [canvasRef]); + const handleButtonClick = () => { + resize = !resize; + handleResize(); + console.log("Something should have happened"); + }; + return (