-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcharacter.js
More file actions
85 lines (82 loc) · 1.82 KB
/
Copy pathcharacter.js
File metadata and controls
85 lines (82 loc) · 1.82 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
var upPressed = false;
var downPressed = false;
var leftPressed = false;
var rightPressed = false;
var lastPressed = false;
var changeableX = 300;
var changeableY = 20;
class Character {
constructor() {
this.width = 10;
this.height = 40;
this.x = 200;
this.y = canvas4.height - this.height - 40;
this.frameX = 0;
this.frameY = 0;
}
update() {
if (upPressed) {
//up
if (this.y > canvas4.height - this.height * 2.5) {
this.y -= 1;
}
}
if (downPressed) {
//down
if (this.y < canvas4.height - this.height * 1.05) {
this.y += 1;
}
}
if (leftPressed) {
//left
if (this.x > this.width) {
this.x -= 1;
}
}
if (rightPressed) {
//right
if (this.x < canvas4.width - this.width * 2) {
this.x += 1;
}
}
if (this.y < 0) scored();
}
draw() {
// ctx4.fillStyle = "black";
// ctx4.fillRect(this.x, this.y, this.width, this.height);
ctx4.drawImage(playerSprite, this.x, this.y, 12, 30);
}
}
document.addEventListener("keyup", function (event) {
if (event.keyCode == 37) {
leftPressed = false;
lastPressed = "left";
}
if (event.keyCode == 39) {
rightPressed = false;
lastPressed = "right";
}
if (event.keyCode == 38) {
upPressed = false;
lastPressed = "up";
}
if (event.keyCode == 40) {
downPressed = false;
lastPressed = "down";
}
});
document.addEventListener("keydown", function (event) {
if (event.keyCode == 37) {
leftPressed = true;
}
if (event.keyCode == 39) {
rightPressed = true;
}
if (event.keyCode == 38) {
upPressed = true;
}
if (event.keyCode == 40) {
downPressed = true;
}
});
const character = new Character();