-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
186 lines (156 loc) · 4.85 KB
/
script.js
File metadata and controls
186 lines (156 loc) · 4.85 KB
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
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Fullscreen canvas and proportional resizing
function resizeCanvas() {
const width = window.innerWidth;
const height = window.innerHeight;
canvas.width = width;
canvas.height = height;
}
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
let gameRunning = false;
let walletAddress = '';
let score = 0;
// Load assets
const backgroundGradient = ctx.createLinearGradient(0, 0, canvas.width, canvas.height);
backgroundGradient.addColorStop(0, "#87CEFA");
backgroundGradient.addColorStop(1, "#FFFFFF");
const kasper = new Image();
kasper.src = 'assets/kasperghostflappy.png';
const flapSound = new Audio('assets/flap.wav');
const gameOverSound = new Audio('assets/gameover.wav');
const bgMusic = new Audio('assets/background.mp3');
bgMusic.loop = true;
let kasperX = canvas.width / 10;
let kasperY = canvas.height / 2;
let gravity = 0.08;
let lift = -4;
let velocity = 0;
let pipes = [];
let pipeWidth = canvas.width / 10;
let pipeGap = canvas.height / 3;
let pipeSpeed = 2;
let gameOver = false;
// Ask the user for their wallet address and then show a "Start Game" button
document.getElementById('walletForm').addEventListener('submit', function(event) {
event.preventDefault();
walletAddress = document.getElementById('walletAddress').value;
if (walletAddress) {
document.getElementById('playScreen').classList.remove('hidden');
document.getElementById('walletForm').classList.add('hidden');
}
});
// Show game instructions and the "Start Game" button
document.getElementById('startGameButton').addEventListener('click', function() {
document.getElementById('playScreen').classList.add('hidden');
document.getElementById('scoreDisplay').classList.remove('hidden'); // Show score
startGame();
});
// Mobile and Desktop support
canvas.addEventListener('touchstart', function(e) {
if (gameRunning) {
velocity = lift;
flapSound.play();
}
e.preventDefault();
});
canvas.addEventListener('click', function() {
if (gameRunning) {
velocity = lift;
flapSound.play();
}
});
// Draw pipes
function drawPipes() {
for (let i = 0; i < pipes.length; i++) {
let pipe = pipes[i];
pipe.x -= pipeSpeed;
ctx.fillStyle = "#228B22"; // Pipe color
ctx.fillRect(pipe.x, 0, pipeWidth, pipe.top);
ctx.fillRect(pipe.x, canvas.height - pipe.bottom, pipeWidth, pipe.bottom);
if (pipe.x + pipeWidth < 0) {
pipes.splice(i, 1);
score += 1; // Increment score when pipe passes
}
}
}
// Generate pipes
function generatePipes() {
let top = Math.random() * (canvas.height / 2);
let bottom = canvas.height - (top + pipeGap);
pipes.push({x: canvas.width, top: top, bottom: bottom});
}
function startGame() {
kasperY = canvas.height / 2;
pipes = [];
score = 0;
gameRunning = true;
bgMusic.play();
gameLoop();
generatePipes();
setInterval(generatePipes, 2500); // Generate pipes every 2.5 seconds
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw background
ctx.fillStyle = backgroundGradient;
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Update and draw Kasper
velocity += gravity;
kasperY += velocity;
ctx.drawImage(kasper, kasperX, kasperY, canvas.width / 10, canvas.height / 10);
// Draw pipes
drawPipes();
// Update score display
document.getElementById('scoreDisplay').textContent = `Score: ${score}`;
requestAnimationFrame(gameLoop);
}
function restartGame() {
gameOver = false;
gameRunning = false;
bgMusic.pause();
bgMusic.currentTime = 0;
document.getElementById('gameOver').classList.remove('hidden');
}
// Submit the score and show the leaderboard after the game ends
function submitScore() {
if (!walletAddress) return;
fetch('https://kasper-flappy.herokuapp.com/submit_score', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
wallet_address: walletAddress,
score: score
}),
})
.then(response => response.json())
.then(data => {
if (data.status === 'success') {
fetchLeaderboard();
} else {
alert('Error submitting score!');
}
})
.catch((error) => {
console.error('Error:', error);
});
}
// Fetch leaderboard
function fetchLeaderboard() {
fetch('https://kasper-flappy.herokuapp.com/get_leaderboard')
.then(response => response.json())
.then(leaderboard => {
const leaderboardList = document.getElementById('leaderboardList');
leaderboardList.innerHTML = '';
leaderboard.forEach(entry => {
let listItem = document.createElement('li');
listItem.textContent = `Wallet: ${entry.wallet_address} - Score: ${entry.score}`;
leaderboardList.appendChild(listItem);
});
document.getElementById('leaderboard').classList.remove('hidden');
});
}