js+css实现一个杀虫小游戏代码
代码语言:html
所属分类:游戏
代码描述:js+css实现一个杀虫小游戏代码,按左键喷洒杀虫剂。
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <style> :root { --title: "Fumigation Frenzy"; --author: "Matt Cannon"; --contact: "mc@mattcannon.design"; --description: "A fast-paced bug extermination game where you spray fumigation gas to eliminate bugs before your tank runs out."; --keywords: "game, bugs, fumigation, extermination, spray, pest control, arcade game, particle effects, animation, CSS animation, JavaScript game, HTML5 game, interactive"; --last-modified: "2025-03-05"; --content-language: "en"; --generator: "HTML5, CSS3, JavaScript, FontAwesome icons, CSS particles, requestAnimationFrame for fumigation gas animation, dynamic particle system for gas clouds, CSS transforms for bug movement"; } * { margin: 0; padding: 0; box-sizing: border-box; } body { background: url(//repo.bfw.wiki/bfwrepo/image/67d76faf0f8f6.png); cursor: none; /* Hide default cursor */ overflow: hidden; font-family: 'Rubik', sans-serif; color: #fff; } /* Make sure all elements use the 'none' cursor to properly hide it */ #root, button, div, .bug { cursor: none; } #root { position: relative; height: 100vh; } .cursor { position: absolute; width: 200px; height: 300px; background: url('//repo.bfw.wiki/bfwrepo/images/chong/wand.png'); background-size: contain; background-position: center; background-repeat: no-repeat; pointer-events: none; z-index: 1000; /* Increased z-index to ensure it's on top */ transform-origin: 50% 90%; transition: transform 0.1s; will-change: left, top; /* Optimize for animation performance */ } .cursor.spray { transform: scale(0.95); animation: shake 0.1s infinite; } @keyframes shake { 0% { transform: rotate(-2deg) scale(0.95); } 50% { transform: rotate(2deg) scale(0.95); } 100% { transform: rotate(-2deg) scale(0.95); } } /* Gas cloud particle */ .gas-particle { position: absolute; border-radius: 50%; background-color: rgba(0, 255, 0, 0.8); pointer-events: none; z-index: 90; filter: blur(4px); mix-blend-mode: screen; box-shadow: 0 0 10px rgb(0, 255, 0); } /* Bug styles */ .bug { position: absolute; background-position: 0 0; z-index: 50; filter: drop-shadow(1px 1px 2px rgba(0,0,0,0.3)); transition: transform 0.3s; } .bug.dead { animation: none !important; transition: top 0.8s cubic-bezier(0.5, 0, 1, 0.5); top: calc(100% + 30px) !important; transform: rotate(180deg); opacity: 0.6; z-index: 40; } .fly { width: 26px; height: 16px; animation: fly 0.1s infinite step-end; background: url(//repo.bfw.wiki/bfwrepo/images/chong/fly.png); } .gnat { width: 14px; height: 10px; animation: gnat 0.1s infinite step-end; background: url(//repo.bfw.wiki/bfwrepo/images/chong/gnat.png); } .wasp { width: 48px; height: 32px; animation: wasp 0.1s infinite step-end; background: url(//repo.bfw.wiki/bfwrepo/images/chong/wasp.png); } .bigfly { width: 32px; height: 30px; animation: bigfly 0.1s infinite step-end; background: url(//repo.bfw.wiki/bfwrepo/images/chong/bigfly.png); } @keyframes fly { 0% { background-position: 0 0; } 50% { background-position: -26px 0; } } @keyframes gnat { 0% { background-position: -14px 0; } 50% { background-position: -28px 0; } } @keyframes wasp { 0% { background-position: -96px 0; } 50% { background-position: -144px 0; } } @keyframes bigfly { 0% { background-position: 0 0; } 50% { background-position: -32px 0; } } @keyframes float-up { 0% { transform: translateY(0); opacity: 1; } 100% { transform: translateY(-40px); opacity: 0; } } /* UI Elements */ .stats-container { position: absolute; bottom: 0; left: 0; right: 0; background: rgba(0, 0, 0, 0.5); color: #fff; padding: 10px 15px; font-weight: bold; font-size: 20px; z-index: 200; display: flex; align-items: center; } .stats-container i { margin-right: 8px; color: #90ee90; } .stats-item { display: flex; align-items: center; padding: 0 15px; position: relative; justify-content: center; } .stats-item:nth-child(1)::after { content: ''; position: absolute; right: 0; top: 20%; height: 60%; width: 1px; background-color: rgba(255, 255, 255, 0.5); } .score-display, .bugs-left-display { flex: 1; text-align: center; } .high-score-display { text-align: right; margin-left: auto; } .gas-stat { display: flex; align-items: center; flex: 2; justify-content: center; } .gas-label { margin-right: 15px; white-space: nowrap; } .gas-meter-wrapper { width: 200px; height: 25px; background: #222; padding: 3px; border-radius: 6px; border: 1px solid rgba(255, 255, 255, 0.3); } .gas-meter { height: 100%; width: 100%; background: linear-gradient(to right, #00cc00, #00ff00); transition: width 0.3s; border-radius: 4px; } /* Points popup */ .points-text { position: absolute; color: #fff; font-weight: bold; font-size: 26px; z-index: 150; pointer-events: none; transform: translate(-50%, 0); animation: float-up 1s forwards; text-shadow: 2px 2px 0 #000; } /* Gas saved bonus text */ .gas-bonus { position: absolute; color: #00ff00; font-weight: bold; font-size: 24px; z-index: 150; pointer-events: none; transform: translate(-50%, 0); animation: float-up 1s forwards; text-shadow: 2px 2px 0 #000; } /* Combo text */ .combo-text { position: absolute; color: #ffff00; background: rgba(0, 0, 0, 0.8); padding: 8px 15px; border-radius: 6px; font-weight: bold; font-size: 22px; z-index: 150; pointer-events: none; transform: translate(-50%, 0); transition: opacity 0.5s, transform 0.5s; border: 2px solid #fff; box-shadow: 0 4px 8px rgba(0,0,0,0.5); } /* Start/End UI */ .play-btn { background: #fff; color: #000; border: 4px solid #000; padding: 15px 30px; font-size: 32px; font-weight: bold; cursor: pointer; box-shadow: 0 8px 0 #333; transition: transform 0.1s, box-shadow 0.1s; border-radius: 4px; text-transform: uppercase; font-family: 'Rubik', sans-serif; } .play-btn:hover { transform: translateY(-2px); box-shadow: 0 10px 0 #333; } .play-btn i { margin-right: 10px; } .game-over { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); background: #fff; padding: 30px; border: 8px solid #000; text-align: center; box-shadow: 0 10px 0 #333; z-index: 1000; max-width: 80%; min-width: 300px; border-radius: 10px; color: #000; } .game-over h2 { font-size: 32px; margin-bottom: 20px; } .game-over p { font-size: 20px; margin-bottom: 15px; } .game-over p i { width: 30px; margin-right: 10px; color: #00a000; } .ready-message { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); background: #000; color: #fff; padding: 20px 40px; font-weight: bold; font-size: 32px; z-index: 1000; border: 4px solid #fff; border-radius: 10px; box-shadow: 0 8px 16px rgba(0,0,0,0.5); } .ready-message i { margin-right: 15px; color: #ffff00; /* Changed to yellow */ } .title-screen { z-index: 100; display: flex; flex-direction: column; align-items: center; justify-content: center; } .title-image { max-width: 90vw; height: auto; filter: drop-shadow(0 0 10px rgba(0,255,0,0.6)); } /* Hide default cursor only when game is active */ body.game-active * { cursor: none !important; } /* Allow normal cursor for play button before game starts */ body:not(.game-active) .play-btn { cursor: pointer !important; } /* Override cursor styling for title screen and game over screen elements */ #titleScreen, #titleScreen *, .game-over, .game-over * { cursor: auto !important; } /* Specific pointer cursor for all play buttons */ #titleScreen .play-btn, .game-over .play-btn { cursor: pointer !important; } .start-instructions { animation: pulse 1.5s infinite alternate; text-align: center; padding: 15px; border-radius: 10px; background-color: rgba(0, 0, 0, 0.5); border: 2px solid #00ff00; box-shadow: 0 0 15px rgba(0, 255, 0, 0.5); } @keyframes pulse { 0% { opacity: 0.7; transform: translateX(-50%) scale(0.98); } 100% { opacity: 1; transform: translateX(-50%) scale(1.02); } } </style> </head> <body translate="no"> <div id="root"></div> <script > class BugSprayGame { constructor() { // Setup DOM elements this.root = document.getElementById('root'); this.setupGameElements(); // Game state this.gameActive = false; this.score = 0; // Make sure we get a valid number for the high score const savedHighScore = localStorage.getItem('bugspray_highscore'); this.highScore = savedHighScore && !isNaN(parseInt(savedHighScore)) ? parseInt(savedHighScore) : 0; this.gasLeft = 100; this.bugsToKill = 30; this.bugsKilled = 0; this.bugs = []; this.isMouseDown = false; this.sprayInterval = null; this.spraySize = 1; this.cursorPos = { x: 0, y: 0 }; this.gasClouds = []; this.gasSaved = 0; // Game settings this.settings = { maxBugsOnScreen: 16, // Increased max bugs spawnRate: 700, // Faster spawn rate gasCostPerSpray: 0.2, // Very low gas cost initialGas: 100, maxClouds: 100, // Maximum clouds for performance cloudGravity: 0.05, // How fast clouds fall gasPreservationBonus: 100 // Points for each 1% gas preserved }; // Bug types and their properties this.bugTypes = { fly: { points: 100, speed: 1.0, gasBonus: 0 }, // Medium speed gnat: { points: 50, speed: 2.5, gasBonus: 0 }, // Very fast but less points wasp: { points: 400, speed: 1.5, gasBonus: 0 }, // Fast and high points bigfly: { points: 200, speed: 0.7, gasBonus: 0 } // Slow but good points }; // Setup event listeners this.setupEventListeners(); // Initialize spawn points this.initSpawnPoints(); // Start animation loop for gas clouds this.lastTimestamp = 0; requestAnimationFrame(this.animate.bind(this)); } setupGameElements() { // Create cursor (initially hidden until game starts) this.cursor = this.createEl('div', { className: 'cursor', style: { display: 'none' } }); // Create title screen with button as its child const titleScreenDiv = document.createElement('div'); titleScreenDiv.id = 'titleScreen'; titleScreenDiv.style.position = 'absolute'; titleScreenDiv.style.top = '50%'; titleScreenDiv.style.left = '50%'; titleScreenDiv.style.transform = 'translate(-50%, -50%)'; titleScreenDiv.style.zIndex = '1000'; titleScreenDiv.style.opacity = '1'; titleScreenDiv.style.textAlign = 'center'; this.root.appendChild(titleScreenDiv); // Create the title image const titleImg = document.createElement('img'); titleImg.src = '//repo.bfw.wiki/bfwrepo/images/chong/maintitle.png'; titleImg.alt = 'Fumigation Frenzy'; titleImg.style.maxWidth = '90vw'; titleImg.style.height = 'auto'; titleScreenDiv.appendChild(titleImg); // Create play button as a child of the title screen this.playBtn = document.createElement('button'); this.playBtn.className = 'play-btn'; this.playBtn.innerHTML = '<i class="fas fa-spray-can"></i>Start Frenzy!'; this.playBtn.style.position = 'absolute'; this.playBtn.style.bottom = '100px'; this.playBtn.style.right = '90px'; this.playBtn.style.opacity = '0'; this.playBtn.style.pointerEvents = 'none'; titleScreenDiv.appendChild(this.playBtn); this.titleScreen = titleScreenDiv; // Set timeout to show play button without fading title setTimeout(() => { // Keep title screen fully visible // Just fade in play button this.playBtn.style.transition = 'opacity 1s, transform 0.5s'; this.playBtn.style.opacity = '1'; this.playBtn.style.pointerEvents = 'auto'; // Add keydown event listener for Enter key document.addEventListener('keydown', event => { if (event.key === 'Enter' && this.titleScreen.style.display !== 'none') { // Animate button press this.playBtn.style.transform = 'translateY(8px)'; this.playBtn.style.boxShadow = '0 0 0 #333'; // Short delay to show the animation setTimeout(() => { // Hide title screen immediately this.titleScreen.style.display = 'none'; this.startGame(); }, 150); } }); // Make play button clickable this.playBtn.addEventListener('click', () => { // Hide title screen immediately this.titleScreen.style.display = 'none'; this.startGame(); }); }, 500); // Changed from 2000 to 500 for much faster appearance // Create stats container this.statsContainer = this.createEl('div', { className: 'stats-container', style: { display: 'none' } }).........完整代码请登录后点击上方下载按钮下载查看
网友评论0