-
Notifications
You must be signed in to change notification settings - Fork 0
/
Index.js
74 lines (67 loc) · 2.07 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
Player = document.getElementById("Player")
Block = document.getElementById("Obstacle")
Score = document.getElementById("Score")
HighScore = document.getElementById("HighScore")
IsJumping = false
ScoreNum = 0
HighScoreNum = 0
BehindPlayer = false
function JumpAction()
{
if (!IsJumping)
{
Player.classList.add("Jump")
IsJumping = true
}
setTimeout(function(){
Player.classList.remove("Jump")
IsJumping = false
}, 1000)
}
document.addEventListener("keydown", function(Event) {
if (!IsJumping)
{
if (Event.key === " ") {
JumpAction()
}
}
})
document.addEventListener("touchstart", function(Event)
{
if (!IsJumping)
{
JumpAction()
}
})
setInterval(function()
{
PlayerProperties = getComputedStyle(Player)
BlockProperties = getComputedStyle(Block)
PlayerTop = parseInt(PlayerProperties.getPropertyValue("top"))
PlayerWidth = parseInt(PlayerProperties.getPropertyValue("width"))
PlayerHeight = parseInt(PlayerProperties.getPropertyValue("height"))
PlayerLeft = parseInt(PlayerProperties.getPropertyValue("left"))
BlockLeft = parseInt(BlockProperties.getPropertyValue("left"))
BlockWidth = parseInt(BlockProperties.getPropertyValue("width"))
BlockHeight = parseInt(BlockProperties.getPropertyValue("height"))
BlockTop = parseInt(BlockProperties.getPropertyValue("top"))
if (BlockTop <= PlayerTop + PlayerHeight && BlockLeft <= PlayerLeft + PlayerWidth && BlockLeft + BlockWidth >= PlayerLeft)
{
if (ScoreNum > HighScoreNum)
{
HighScoreNum = ScoreNum
HighScore.textContent = "High Score: " + HighScoreNum
}
BehindPlayer = true
ScoreNum = 0
Score.textContent = ScoreNum
} else if (BlockLeft <= PlayerLeft && BehindPlayer == false)
{
BehindPlayer = true
ScoreNum += 1
Score.textContent = ScoreNum
} else if (BlockLeft > PlayerLeft)
{
BehindPlayer = false
}
}, 10);