-
Notifications
You must be signed in to change notification settings - Fork 0
/
flappyGame.js
executable file
·117 lines (80 loc) · 2.11 KB
/
flappyGame.js
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
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var square = {x:150, y:canvas.height/2-10, velY: 100, height:20, accY:2000, maxVel: 300, maxY: canvas.height};
var timeSeconds = 0;
var running = true;
var gameColor = "#555555";
document.addEventListener("keydown", keyDownHandler, false);
document.addEventListener("keyup", keyUpHandler, false);
var doJump = false;
var jumpDone = false;
function keyDownHandler(e) {
if(e.keyCode == 38){
doJump = true;
}
if(e.keyCode == 78){
if(!running) startGame();
}
}
function keyUpHandler(e) {
if(e.keyCode == 38){
doJump = false;
jumpDone = false;
}
}
function stopGame(){
running = false;
//when the game is stopped, the particles will be shot in all directions
setParticleDirection(1);
var info1 = document.getElementById("firstInfoLine");
var info2 = document.getElementById("secInfoLine");
info1.innerHTML = "You survived "+Math.floor(10*timeSeconds)/10+" seconds";
info2.innerHTML = "press 'n' for a new game";
}
function startGame(){
var info1 = document.getElementById("firstInfoLine");
var info2 = document.getElementById("secInfoLine");
info1.innerHTML = "<br>";
info2.innerHTML = "press up to jump"
timeSeconds = 0;
initBlocks();
initParticles(40);
initSquare();
setParticleDirection(-1);
running = true;
}
function collisionDetection(){
for(i=0; i<nblocks; i++){
if(blocks[i].x < square.x+square.height && square.x < blocks[i].x+blockWidth){
if(square.y < blocks[i].y+blocks[i].height && square.y+square.height >blocks[i].y){
stopGame();
}
}
}
}
function update(dt){
if(running){
timeSeconds += dt;
updateSquare(dt);
updateBlocks(dt);
}
updateParticles(dt);
collisionDetection();
}
function draw(){
ctx.clearRect(0,0,canvas.width, canvas.height);
drawBlocks();
drawParticles();
drawSquare();
}
var lastTime = Date.now();
function main(){
var now = Date.now();
var dt = (now-lastTime)/1000.0;
update(dt);
draw();
lastTime = now;
requestAnimationFrame(main);
}
startGame();
main();