Skip to content

Commit 37eda4a

Browse files
Merge pull request #1840 from 1911aditi/master
Add files via upload:added new extension for a game named as Fruit Catcher
2 parents 3e3f788 + 79536ee commit 37eda4a

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed

Fruit Catcher/manifest.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"manifest_version": 3,
3+
"name": "Fruit Catcher",
4+
"version": "1.0",
5+
"description": "A fun game to catch falling fruits!",
6+
"action": {
7+
"default_popup": "popup.html"
8+
},
9+
"permissions": []
10+
}
11+

Fruit Catcher/popup.css

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
body {
2+
font-family: Arial, sans-serif;
3+
text-align: center;
4+
background-color: #282c34;
5+
color: white;
6+
margin: 0;
7+
padding: 0;
8+
}
9+
10+
#game {
11+
margin: 20px auto;
12+
}
13+
14+
canvas {
15+
background-color: #000;
16+
border: 2px solid #fff;
17+
}
18+
19+
#score {
20+
font-size: 20px;
21+
font-weight: bold;
22+
}
23+

Fruit Catcher/popup.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Fruit Catcher</title>
7+
<link rel="stylesheet" href="popup.css">
8+
</head>
9+
<body>
10+
<div id="game">
11+
<h1>Fruit Catcher</h1>
12+
<canvas id="gameCanvas" width="300" height="500"></canvas>
13+
<p>Score: <span id="score">0</span></p>
14+
</div>
15+
<script src="popup.js"></script>
16+
</body>
17+
</html>

Fruit Catcher/popup.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
document.addEventListener('DOMContentLoaded', function() {
2+
const canvas = document.getElementById('gameCanvas');
3+
const ctx = canvas.getContext('2d');
4+
const scoreDisplay = document.getElementById('score');
5+
const fruitSize = 20;
6+
let score = 0;
7+
let fruits = [];
8+
let player = {
9+
x: canvas.width / 2 - fruitSize / 2,
10+
y: canvas.height - fruitSize * 2,
11+
width: fruitSize,
12+
height: fruitSize,
13+
dx: 0
14+
};
15+
16+
function drawPlayer() {
17+
ctx.fillStyle = 'blue';
18+
ctx.fillRect(player.x, player.y, player.width, player.height);
19+
}
20+
21+
function drawFruit(fruit) {
22+
ctx.fillStyle = 'yellow';
23+
ctx.beginPath();
24+
ctx.arc(fruit.x, fruit.y, fruitSize / 2, 0, Math.PI * 2);
25+
ctx.fill();
26+
}
27+
28+
function updateFruits() {
29+
if (Math.random() < 0.02) {
30+
fruits.push({
31+
x: Math.random() * (canvas.width - fruitSize),
32+
y: 0,
33+
dy: 2
34+
});
35+
}
36+
37+
fruits.forEach((fruit, index) => {
38+
fruit.y += fruit.dy;
39+
40+
if (fruit.y > canvas.height) {
41+
fruits.splice(index, 1);
42+
}
43+
44+
if (
45+
fruit.x < player.x + player.width &&
46+
fruit.x + fruitSize > player.x &&
47+
fruit.y < player.y + player.height &&
48+
fruit.y + fruitSize > player.y
49+
) {
50+
score++;
51+
scoreDisplay.textContent = score;
52+
fruits.splice(index, 1);
53+
}
54+
});
55+
}
56+
57+
function drawFruits() {
58+
fruits.forEach(fruit => drawFruit(fruit));
59+
}
60+
61+
function clearCanvas() {
62+
ctx.clearRect(0, 0, canvas.width, canvas.height);
63+
}
64+
65+
function updatePlayer() {
66+
player.x += player.dx;
67+
68+
if (player.x < 0) {
69+
player.x = 0;
70+
} else if (player.x + player.width > canvas.width) {
71+
player.x = canvas.width - player.width;
72+
}
73+
}
74+
75+
function updateGame() {
76+
clearCanvas();
77+
drawPlayer();
78+
drawFruits();
79+
updateFruits();
80+
updatePlayer();
81+
requestAnimationFrame(updateGame);
82+
}
83+
84+
function movePlayer(e) {
85+
if (e.key === 'ArrowLeft') {
86+
player.dx = -5;
87+
} else if (e.key === 'ArrowRight') {
88+
player.dx = 5;
89+
}
90+
}
91+
92+
function stopPlayer() {
93+
player.dx = 0;
94+
}
95+
96+
document.addEventListener('keydown', movePlayer);
97+
document.addEventListener('keyup', stopPlayer);
98+
99+
updateGame();
100+
});
101+

0 commit comments

Comments
 (0)