-
Notifications
You must be signed in to change notification settings - Fork 0
/
platformer-base.html
82 lines (70 loc) · 1.73 KB
/
platformer-base.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Canvas Platformer Example</title>
<style>
body {
overflow: hidden;
margin: 0;
}
html {
margin: 0;
}
</style>
</head>
<body>
<canvas></canvas>
<script src="../simple-canvas.js"></script>
<script>
var canvas = document.querySelector("canvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var ctx = canvas.getContext("2d");
setupCanvas(ctx);
function init() {
window.x = 100;
window.y = canvas.height - 300;
window.dx = 0;
window.dy = 0;
window.f = 0.86;
window.g = 1.6;
window.spd = 2;
window.jump = 26;
}
function update() {
/*** GAME UPDATE ***/
// x axis movement
if (btn("A")) {
dx -= spd;
}
if (btn("D")) {
dx += spd;
}
// apply friction
dx *= f;
// y axis collision
if (pget(x+15,y+31) === "rgba(0,0,0,1)") {
dy = 0;
y = canvas.height - canvas.height / 8 - 30
} else {
dy += g;
}
// jump
if (btn("W") && pget(x+15,y+31) === "rgba(0,0,0,1)") {
dy -= jump;
}
// apply forces to player coordinates
x += dx;
y += dy;
/*** GAME RENDERING ***/
cls("lightblue");
// player
rectfill(x,y,30,30,"red");
// ground
rectfill(0,canvas.height - canvas.height / 8, canvas.width, canvas.height, "black");
}
</script>
</body>
</html>