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
| baseColor = color.hsb(275, 75, 55)
display.color = baseColor
score = ui.label("Score", 100, 50)
score.value = 0
circle = display.circle(200, 480, 30)
circle.color = baseColor.lighter(5)
circle.borderColor = baseColor.darker(2)
circle.borderWidth = circle.radius * 0.25
let s = circle.radius * 0.3
circle.add(Rect(-s, -circle.radius * 0.125, s, s))
circle.add(Rect(s, -circle.radius * 0.125, s, s))
circle.onGround = false
circle.jumpPower = 15
circle.speed = 5
input.left(() => {
circle.velocity.x = -circle.speed
})
input.right(() => {
circle.velocity.x = circle.speed
})
input.up((e) => {
if (e.began && circle.onGround) {
circle.velocity.y = -circle.jumpPower
circle.onGround = false
}
})
update(() => {
circle.velocity.x *= 0.8 // friction
circle.velocity.y += 0.8 // gravity
circle.position.add(circle.velocity)
if (circle.x < circle.radius) circle.x = circle.radius;
if (circle.x + circle.radius > display.width) circle.x = display.width - circle.radius;
if (circle.y > display.height) {
circle.position = Point(200, 480)
circle.velocity = Vec2(0, 0)
}
})
circle.collision((e) => {
if (e.tag == "platform") {
circle.onGround = false;
let platform = e.other
if (circle.velocity.y > 0 && circle.y < platform.y) {
circle.y = (platform.y - platform.height / 2) - circle.radius
circle.velocity.y = 0;
circle.onGround = true;
}
else if (circle.velocity.y < 0 &&
(circle.y - circle.radius) > (platform.y + platform.height / 2)) {
circle.y = platform.y - platform.height
circle.velocity.y = 0;
}
}
if (e.tag == "coin") {
e.hide()
score.value += 10
}
})
const platforms = [
{ x: 200, y: 550, width: 200, height: 50, color: '#4ECDC4' },
{ x: 350, y: 450, width: 150, height: 20, color: '#45B7D1' },
{ x: 650, y: 350, width: 120, height: 20, color: '#96CEB4' },
{ x: 820, y: 280, width: 180, height: 20, color: '#FECA57' },
{ x: 350, y: 250, width: 100, height: 20, color: '#FF9FF3' },
{ x: 550, y: 150, width: 200, height: 20, color: '#54A0FF' },
{ x: 800, y: 500, width: 200, height: 50, color: '#5F27CD' }
];
platforms.forEach((p) => {
let pl = display.rect(p.x, p.y, p.width, p.height)
pl.tag = "platform"
pl.color = color.hex(p.color)
})
const coins = [
{ x: 400, y: 410, radius: 10 },
{ x: 600, y: 310, radius: 10 },
{ x: 800, y: 240, radius: 10 },
{ x: 300, y: 210, radius: 10 },
{ x: 550, y: 110, radius: 10 }
];
coins.forEach((c) => {
let co = display.circle(c.x, c.y, c.radius)
co.tag = "coin"
co.color = color.hex("#FFD700")
})
// Background
times(100, () => {
display.star(random.pos(), random.num(5,10))
})
|