-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
189 lines (151 loc) · 3.47 KB
/
javascript.js
File metadata and controls
189 lines (151 loc) · 3.47 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
187
188
189
// Don't forget "sudo systemctl restart httpd.service"!
'use strict';
let gameLive = true;
//constants
let canvas;
let ctx;
const GAME_WIDTH = 640;
const GAME_HEIGHT = 360;
//enemies
const enemies = [
{
x: 100, //x coordinate
y: 100, //y coordinate
speedY: 1, //speed in Y
w: 40, //width
h: 40 //heght
},
{
x: 260,
y: 100,
speedY: 2,
w: 40,
h: 40
},
{
x: 380,
y: 100,
speedY: 3,
w: 40,
h: 40
},
{
x: 470,
y: 100,
speedY: 4,
w: 40,
h: 40
}
];
//player object
const player = {
x: 10,
y: 160,
speedX: 2,
w: 40,
h: 40,
isMoving: false
};
//the goal object
var goal = {
x: 580,
y: 160,
w: 50,
h: 36
};
var sprites = {};
function movePlayer() {
player.isMoving = true;
}
function stopPlayer() {
player.isMoving = false;
}
function load() {
sprites.player = new Image();
sprites.player.src = 'images/hero.png';
sprites.background = new Image();
sprites.background.src = 'images/floor.png';
sprites.enemy = new Image();
sprites.enemy.src = 'images/enemy.png';
sprites.goal = new Image();
sprites.goal.src = 'images/chest.png';
}
//update the logic
var update = function() {
//check if you've won the game
if (checkCollision(player, goal)) {
gameLive = false;
alert("You've won!");
//reload page
window.location = "";
}
//update player
if (player.isMoving) {
player.x += player.speedX;
}
var i = 0;
var n = enemies.length;
enemies.forEach(function(element, index){
// check for collision with player
if (checkCollision(player, element)) {
//stop the game
gameLive = false;
alert('Game Over, man!');
//reload
window.location = "";
}
// move enemy
element.y += element.speedY;
//check borders
if(element.y <= 10) {
element.y = 10;
//element.speedY = element.speedY * -1;
element.speedY *= -1;
}
else if(element.y >= GAME_HEIGHT - 50) {
element.y = GAME_HEIGHT - 50;
element.speedY *= -1;
}
});
};
//show the game on the screen
var draw = function() {
//clear the canvas
ctx.clearRect(0,0,GAME_WIDTH,GAME_HEIGHT);
//draw background
ctx.drawImage(sprites.background, 0, 0);
//draw player
ctx.drawImage(sprites.player, player.x, player.y);
//draw enemies
enemies.forEach(enemy => {
ctx.drawImage(sprites.enemy, enemy.x, enemy.y);
});
//draw goal
ctx.drawImage(sprites.goal, goal.x, goal.y);
};
//gets executed multiple times per second
var step = function() {
update();
draw();
if (gameLive) {
window.requestAnimationFrame(step);
}
};
function checkCollision(rect1, rect2) {
let overlappingX = Math.abs(rect1.x - rect2.x) <= Math.max(rect1.w, rect2.w);
let overlappingY = Math.abs(rect1.y - rect2.y) <= Math.max(rect1.h, rect2.h);
return overlappingX && overlappingY;
}
//initial kick
window.addEventListener('load', () => {
//grab the canvas and context
canvas = document.getElementById("mycanvas");
ctx = canvas.getContext("2d");
// add listener to move player
canvas.addEventListener('mousedown', movePlayer);
canvas.addEventListener('mouseup', stopPlayer);
canvas.addEventListener('touchstart', movePlayer);
canvas.addEventListener('touchend', stopPlayer);
load();
step();
});