File size: 17,603 Bytes
3515cf9 0934dda eeed6b8 ef2f59c eeed6b8 ef2f59c 0934dda eeed6b8 ef2f59c eeed6b8 ef2f59c eeed6b8 ef2f59c 0934dda ef2f59c eeed6b8 ef2f59c eeed6b8 ef2f59c eeed6b8 ef2f59c eeed6b8 ef2f59c eeed6b8 ef2f59c eeed6b8 ef2f59c 0934dda ef2f59c eeed6b8 ef2f59c 0934dda ef2f59c 0934dda ef2f59c 0934dda eeed6b8 ef2f59c 0934dda ef2f59c 0934dda ef2f59c 0934dda ef2f59c 0934dda eeed6b8 596ac16 eeed6b8 596ac16 eeed6b8 0934dda eeed6b8 596ac16 eeed6b8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
import gradio as gr
# Updated HTML with fixes and debugging
game_html = """
<!DOCTYPE html>
<html>
<head>
<title>Bird Shooter Game</title>
<style>
body { margin: 0; padding: 0; overflow: hidden; background-color: #87CEEB; display: flex; justify-content: center; align-items: center; height: 100vh; font-family: Arial, sans-serif; }
#game-container { position: relative; width: 800px; height: 600px; border: 2px solid black; background-color: #87CEEB; overflow: hidden; }
#score { position: absolute; top: 10px; left: 10px; font-size: 24px; color: white; text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5); }
#game-over { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 48px; color: red; text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5); display: none; }
#restart-button { position: absolute; top: 60%; left: 50%; transform: translate(-50%, -50%); padding: 10px 20px; font-size: 24px; background-color: #4CAF50; color: white; border: none; border-radius: 5px; cursor: pointer; display: none; }
.cloud { position: absolute; fill: white; opacity: 0.8; }
</style>
</head>
<body>
<div id="game-container">
<svg id="game-canvas" width="800" height="600"></svg>
<div id="score">Score: 0</div>
<div id="game-over">GAME OVER</div>
<button id="restart-button">Restart</button>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const gameCanvas = document.getElementById('game-canvas');
const scoreDisplay = document.getElementById('score');
const gameOverDisplay = document.getElementById('game-over');
const restartButton = document.getElementById('restart-button');
let score = 0;
let gameRunning = true;
let birds = [];
let explosions = [];
function createClouds() {
const cloudCount = 5;
for (let i = 0; i < cloudCount; i++) {
const cloud = document.createElementNS("http://www.w3.org/2000/svg", "ellipse");
cloud.setAttribute("cx", Math.random() * 800);
cloud.setAttribute("cy", 50 + Math.random() * 100);
cloud.setAttribute("rx", 50 + Math.random() * 50);
cloud.setAttribute("ry", 20 + Math.random() * 20);
cloud.setAttribute("fill", "white");
cloud.setAttribute("opacity", "0.8");
cloud.classList.add("cloud");
gameCanvas.appendChild(cloud);
const cloudPart = document.createElementNS("http://www.w3.org/2000/svg", "ellipse");
cloudPart.setAttribute("cx", parseFloat(cloud.getAttribute("cx")) + 30);
cloudPart.setAttribute("cy", parseFloat(cloud.getAttribute("cy")) - 5);
cloudPart.setAttribute("rx", 30);
cloudPart.setAttribute("ry", 15);
cloudPart.setAttribute("fill", "white");
cloudPart.setAttribute("opacity", "0.8");
gameCanvas.appendChild(cloudPart);
}
}
class Bird {
constructor() {
this.x = -50;
this.y = 100 + Math.random() * 300;
this.speed = 2 + Math.random() * 3;
this.size = 30 + Math.random() * 20;
this.element = this.createBirdElement();
gameCanvas.appendChild(this.element);
console.log('Bird created at x:', this.x, 'y:', this.y);
}
createBirdElement() {
const group = document.createElementNS("http://www.w3.org/2000/svg", "g");
const body = document.createElementNS("http://www.w3.org/2000/svg", "ellipse");
body.setAttribute("cx", "0");
body.setAttribute("cy", "0");
body.setAttribute("rx", this.size / 2);
body.setAttribute("ry", this.size / 3);
body.setAttribute("fill", this.getRandomColor());
group.appendChild(body);
const head = document.createElementNS("http://www.w3.org/2000/svg", "circle");
head.setAttribute("cx", this.size / 2);
head.setAttribute("cy", -this.size / 6);
head.setAttribute("r", this.size / 4);
head.setAttribute("fill", this.getRandomColor());
group.appendChild(head);
const eye = document.createElementNS("http://www.w3.org/2000/svg", "circle");
eye.setAttribute("cx", this.size / 2 + this.size / 8);
eye.setAttribute("cy", -this.size / 6 - this.size / 8);
eye.setAttribute("r", this.size / 10);
eye.setAttribute("fill", "black");
group.appendChild(eye);
const beak = document.createElementNS("http://www.w3.org/2000/svg", "polygon");
beak.setAttribute("points", `${this.size / 2 + this.size / 4},-${this.size / 6} ${this.size},0 ${this.size / 2 + this.size / 4},${this.size / 10}`);
beak.setAttribute("fill", "orange");
group.appendChild(beak);
const wing = document.createElementNS("http://www.w3.org/2000/svg", "ellipse");
wing.setAttribute("cx", "0");
wing.setAttribute("cy", this.size / 4);
wing.setAttribute("rx", this.size / 3);
wing.setAttribute("ry", this.size / 6);
wing.setAttribute("fill", this.getRandomColor());
wing.setAttribute("class", "wing");
group.appendChild(wing);
return group;
}
getRandomColor() {
const colors = ["#FF5733", "#33FF57", "#3357FF", "#F3FF33", "#FF33F3", "#33FFF3"];
return colors[Math.floor(Math.random() * colors.length)];
}
update() {
this.x += this.speed;
this.element.setAttribute("transform", `translate(${this.x}, ${this.y})`);
const wing = this.element.querySelector(".wing");
if (wing) {
const wingAngle = 15 * Math.sin(Date.now() / 100);
wing.setAttribute("transform", `rotate(${wingAngle})`);
}
if (this.x > 850) {
this.remove();
console.log('Bird removed, remaining birds:', birds.length);
return false;
}
return true;
}
remove() {
if (this.element.parentNode) {
gameCanvas.removeChild(this.element);
}
}
checkHit(x, y) {
const birdX = this.x;
const birdY = this.y;
const distance = Math.sqrt(Math.pow(birdX - x, 2) + Math.pow(birdY - y, 2));
return distance < this.size;
}
}
class Explosion {
constructor(x, y, size) {
this.x = x;
this.y = y;
this.size = size;
this.frame = 0;
this.maxFrames = 20;
this.element = this.createExplosionElement();
gameCanvas.appendChild(this.element);
this.playSound();
}
createExplosionElement() {
const group = document.createElementNS("http://www.w3.org/2000/svg", "g");
const colors = ["#FF0000", "#FF7700", "#FFFF00"];
const particleCount = 12;
for (let i = 0; i < particleCount; i++) {
const angle = (i / particleCount) * 2 * Math.PI;
const circle = document.createElementNS("http://www.w3.org/2000/svg", "circle");
circle.setAttribute("cx", Math.cos(angle) * (this.size / 3));
circle.setAttribute("cy", Math.sin(angle) * (this.size / 3));
circle.setAttribute("r", this.size / 8);
circle.setAttribute("fill", colors[Math.floor(Math.random() * colors.length)]);
circle.setAttribute("class", "particle");
circle.setAttribute("data-angle", angle);
group.appendChild(circle);
}
const center = document.createElementNS("http://www.w3.org/2000/svg", "circle");
center.setAttribute("cx", 0);
center.setAttribute("cy", 0);
center.setAttribute("r", this.size / 2);
center.setAttribute("fill", "#FF0000");
center.setAttribute("class", "center");
group.appendChild(center);
group.setAttribute("transform", `translate(${this.x}, ${this.y})`);
return group;
}
playSound() {
const explosion = new Audio();
explosion.src = "data:audio/wav;base64,//uQRAAAAWMSLwUIYAAsYkXgoQwAEaYLWfkWgAI0wWs/ItAAAGDgYtAgAyN+QWaAAihwMWm4G8QQRDiMcCBcH3Cc+CDv/7xA4Tvh9Rz/y8Q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ZBGYiaQPSYyAAABLAAAAAAAACWAAAAApUF/Mg+0aohSIRobBAsMlO//Kk4soosy1JSFRYWaLC4qZBYWFRGZdwqKiwkNBVmoWFSJkWFxX4FFRQWR+LsS4W/rFRb/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////VEFHAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAU291bmRib3kuZGUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMjAwNGh0dHA6Ly93d3cuc291bmRib3kuZGUAAAAAAAAAACU=";
explosion.volume = 0.3;
explosion.play();
}
update() {
this.frame++;
const center = this.element.querySelector(".center");
const sizeMultiplier = 1 + this.frame / 5;
center.setAttribute("r", (this.size / 2) * sizeMultiplier);
center.setAttribute("opacity", 1 - (this.frame / this.maxFrames));
const particles = this.element.querySelectorAll(".particle");
particles.forEach(particle => {
const angle = parseFloat(particle.getAttribute("data-angle"));
const distance = (this.size / 3) + (this.frame * 2);
const x = Math.cos(angle) * distance;
const y = Math.sin(angle) * distance;
particle.setAttribute("cx", x);
particle.setAttribute("cy", y);
particle.setAttribute("opacity", 1 - (this.frame / this.maxFrames));
particle.setAttribute("r", this.size / 8 * (1 - this.frame / this.maxFrames));
});
if (this.frame >= this.maxFrames) {
this.remove();
return false;
}
return true;
}
remove() {
if (this.element.parentNode) {
gameCanvas.removeChild(this.element);
}
}
}
function initGame() {
score = 0;
gameRunning = true;
scoreDisplay.textContent = `Score: ${score}`;
gameOverDisplay.style.display = 'none';
restartButton.style.display = 'none';
birds.forEach(bird => bird.remove());
explosions.forEach(explosion => explosion.remove());
birds = [];
explosions = [];
while (gameCanvas.firstChild) {
gameCanvas.removeChild(gameCanvas.firstChild);
}
createClouds();
birds.push(new Bird()); // Start with one bird
console.log('Game initialized');
gameLoop();
}
function gameLoop() {
if (!gameRunning) {
console.log('Game stopped');
return;
}
console.log('Game loop running, birds:', birds.length);
if (Math.random() < 0.1) { // Increased spawn rate for testing
birds.push(new Bird());
console.log('New bird spawned');
}
for (let i = birds.length - 1; i >= 0; i--) {
if (!birds[i].update()) {
birds.splice(i, 1);
}
}
for (let i = explosions.length - 1; i >= 0; i--) {
if (!explosions[i].update()) {
explosions.splice(i, 1);
}
}
requestAnimationFrame(gameLoop); // Ensure continuous looping
}
gameCanvas.addEventListener('click', (e) => {
if (!gameRunning) return;
const rect = gameCanvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
let hit = false;
for (let i = birds.length - 1; i >= 0; i--) {
if (birds[i].checkHit(x, y)) {
const birdX = birds[i].x;
const birdY = birds[i].y;
const birdSize = birds[i].size;
birds[i].remove();
birds.splice(i, 1);
explosions.push(new Explosion(birdX, birdY, birdSize * 2));
score += 10;
scoreDisplay.textContent = `Score: ${score}`;
hit = true;
console.log('Bird hit, score:', score);
}
}
if (!hit) {
const miss = new Audio();
miss.src = "data:audio/wav;base64,UklGRiQDAABXQVZFZm10IBAAAAABAAEAESsAABErAAABAAgAZGF0YQADAABkAGQAZABkAGQAfACEAJwAsAC8AMgA1ADsAOQA7ADkANQA0AC8AKgAnACMAHQAXABMAEQAPABEADwANAA0ADQALAAsACQAHAAUAAwABAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAA8AFgAaAB8AIQAnACsAMAAyADMANwA3ADcAMgA0ADAALAApACQAIQAbABYAEQAMAAUAAAAAAAAAAAAAAAAAAAAAAAAAAP//9//x//X/9f/4/wAABwANABYAHAAnADIAOABDAEsAVABdAGQAbQBzAH4AhQCKAJEAlQCYAJ4AnwCgAKIAoQCgAJwAmgCXAJIAjwCIAIMAfQB2AG4AZgBeAFQATABGAD4ANQAvACgAIgAaABUADAAIAAQAAQD+//r/9//0//H/7//u/+z/6//r/+r/6v/p/+n/6v/q/+v/7P/u/+//8f/z//X/+P/7//7/AQADAAYACQALABAAEQAUABYAGAAZAB0AHQAfACEAIgAkACUAJQAmACcAJwAoACkAKQAqACsAKwAsACwALQAtAC0ALQAuAC4ALgAuAC4ALgAtAC0ALQAsACwALAAqACkAKQAnACYAJgAkACMAIgAgAB8AHgAcABoAGQAYABYAFAAQAA4ADAAJAAcABQADAAEA//89//7//f/8//v/+v/5//j/9//2//X/9P/z//P/8v/x//D/8P/v/+7/7v/t/+3/7P/s/+v/6//q/+r/6v/p/+n/6f/p/+n/6f/p/+n/6f/p/+n/6v/q/+v/6//s/+z/7f/u/+7/7//w//H/8v/z//T/9f/2//f/+P/5//v//P/9/wAAAgADAAUABgAIAAkACwAMAA4AEAARACMACwAEAP//+//4//X/8v/v/+v/6P/l/+L/4P/e/9v/2f/X/9X/1P/S/9H/z//O/83/zf/M/8v/y//K/8r/yv/K/8v/y//L/8z/zf/O/8//0P/S/9P/1f/X/9j/2v/c/97/4f/j/+X/6P/q/+3/8P/z//X/+P/7//7/AQA=";
miss.volume = 0.2;
miss.play();
console.log('Missed shot');
}
});
restartButton.addEventListener('click', () => {
initGame();
console.log('Game restarted');
});
initGame(); // Start the game
});
</script>
</body>
</html>
"""
# Define the Gradio interface
def launch_game():
return game_html
# Create the Gradio app
with gr.Blocks(title="Bird Shooter Game") as demo:
gr.Markdown("# Bird Shooter Game")
gr.Markdown("Click on the birds to shoot them and earn points! If birds stop appearing, check the browser console (F12) for errors.")
game_output = gr.HTML(label="Game", value=launch_game())
# Launch the app
demo.launch() |