вынеси из этого файла import React, { useState, useEffect } from 'react'; function…
- User
вынеси из этого файла import React, { useState, useEffect } from 'react';
function Game() { const [score, setScore] = useState(100); const [balls, setBalls] = useState([]);
codeconst createBall = (x, y) => { const ballSize = Math.floor(Math.random() * 20) + 10; // Размер шара от 10 до 30 const angle = Math.random() * Math.PI * 2; // Случайный угол const speed = Math.random() * 3 + 2; // Случайная скорость const newBall = { x, y, size: ballSize, dx: Math.cos(angle) * speed, dy: Math.sin(angle) * speed, }; setBalls((prevBalls) => [...prevBalls, newBall]); }; const handleTap = () => { const newScore = score - 1; if (newScore <= -1) { endGame(); } else { setScore(newScore); const button = document.getElementById('tapButton'); const buttonRect = button.getBoundingClientRect(); createBall(buttonRect.left + buttonRect.width / 2, buttonRect.top + buttonRect.height / 2); } }; const handleShareScore = () => { if (typeof window !== 'undefined' && typeof window.TelegramGameProxy !== 'undefined') { window.TelegramGameProxy.shareScore(score); } } const endGame = () => { alert('Game Over!'); if (typeof window !== 'undefined' && typeof window.TelegramGameProxy !== 'undefined') { handleShareScore(); } }; useEffect(() => { const moveBalls = () => { setBalls((prevBalls) => prevBalls.map(ball => { let newX = ball.x + ball.dx; let newY = ball.y + ball.dy; // Отражение от краев экрана if (newX < 0 || newX > window.innerWidth) ball.dx *= -1; if (newY < 0 || newY > window.innerHeight) ball.dy *= -1; return { ...ball, x: newX, y: newY, }; }).filter(ball => ball.x > 0 && ball.x < window.innerWidth && ball.y > 0 && ball.y < window.innerHeight ) ); }; const interval = setInterval(moveBalls, 16); // 60 FPS return () => clearInterval(interval); }, []); return ( <div id="game"> <div id="score">{score}</div> <div id="tap">Tap it!</div> <button id="tapButton" onClick={handleTap}>{score}</button> <p></p> <button id="shareButton" onClick={handleShareScore}>Share!</button> {balls.map((ball, index) => ( <div key={index} className="ball" style={{ left: ball.x - ball.size / 2, top: ball.y - ball.size / 2, width: ball.size, height: ball.size, position: 'absolute', borderRadius: '50%', backgroundColor: 'red', }} ></div> ))} </div> ); Assistant
The provided code is a React component called
Game. It importsReact,useState, anduseEffectfrom the 'react' library. The component manages a game state, including a score and an array of balls. The score decreases each time the user taps a button, and a new ball is created at the button's position. The balls move around the screen, bouncing off the edges. When the score reaches -1, the game ends, and the user's score is shared if the game is running in a Telegram environment.The styles for the game are provided in CSS. The body and html elements have a black background with no margin or padding. The container element is positioned at the top left of the screen and centers its children. The
#tapButtonelement is a circular button with a gradient background that changes size and shadow when hovered over or clicked. The#gameelement is a full-screen container for the game, and the.ballelements are the moving balls that disappear after a short animation.