-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
158 lines (140 loc) · 3.28 KB
/
index.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
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
const canvas = document.querySelector('canvas')
const c = canvas.getContext('2d')
canvas.width = screen.width / 2
canvas.height = screen.width / 2 * 9 / 16
c.fillRect(0, 0, canvas.width, canvas.height)
const gravity = 0.4
const dino = new Dino({
position: {
x: 0,
y: canvas.height
},
scale: canvas.width / 200,
velocity: {
x: 0,
y: 0
},
imgSrc: "img/dino_idle_left.png",
nbFrames: 2,
frameDuration: 10,
sprites: {
idle: {
imgSrcLeft: "img/dino_idle_left.png",
imgSrcRight: "img/dino_idle_right.png",
nbFrames: 2,
frameDuration: 10
},
run: {
imgSrcLeft: "img/dino_run_left.png",
imgSrcRight: "img/dino_run_right.png",
nbFrames: 2,
frameDuration: 5
},
jump: {
imgSrcLeft: "img/dino_jump_left.png",
imgSrcRight: "img/dino_jump_right.png",
nbFrames: 1,
frameDuration: 0
}
}
})
const keys = {
a: {
pressed: false,
changed: false
},
d: {
pressed: false,
changed: false
},
w: {
pressed: false,
changed: false
}
}
function getkeydown(key)
{
down = key.pressed && key.changed
if (down)
key.changed = false
return down
}
function getkeyup(key)
{
up = !key.pressed && key.changed
if (up)
key.changed = false
return up
}
function getkey(key)
{
return key.pressed
}
function mainloop()
{
window.requestAnimationFrame(mainloop)
c.fillStyle = 'black'
c.fillRect(0, 0, canvas.width, canvas.height)
dino.update()
if (getkey(keys.w) && dino.onGround)
dino.jump()
if (getkeydown(keys.a))
{
dino.velocity.x = -5
dino.switchAnimation(dino.sprites.run, false)
}
else if (getkeyup(keys.a) && dino.velocity.x < 0)
{
dino.velocity.x = 5 * + getkey(keys.d)
if (getkey(keys.d))
dino.switchAnimation(dino.sprites.run, true)
else
dino.switchAnimation(dino.sprites.idle, false)
}
if (getkeydown(keys.d))
{
dino.velocity.x = 5
dino.switchAnimation(dino.sprites.run, true)
}
else if (getkeyup(keys.d) && dino.velocity.x > 0)
{
dino.velocity.x = -5 * + getkey(keys.a)
if (getkey(keys.a))
dino.switchAnimation(dino.sprites.run, false)
else
dino.switchAnimation(dino.sprites.idle, true)
}
}
mainloop()
window.addEventListener('keydown', (event) => {
switch (event.key) {
case 'd':
keys.d.pressed = true
keys.d.changed = true
break
case 'a':
keys.a.pressed = true
keys.a.changed = true
break
case 'w':
keys.w.pressed = true
keys.w.changed = true
break
}
})
window.addEventListener('keyup', (event) => {
switch (event.key) {
case 'd':
keys.d.pressed = false
keys.d.changed = true
break
case 'a':
keys.a.pressed = false
keys.a.changed = true
break
case 'w':
keys.w.pressed = false
keys.w.changed = true
break
}
})