-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
121 lines (121 loc) · 3.72 KB
/
main.ts
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
namespace SpriteKind {
export const Ball = SpriteKind.create()
export const Border = SpriteKind.create()
export const Brick = SpriteKind.create()
}
sprites.onOverlap(SpriteKind.Ball, SpriteKind.Border, function (sprite, otherSprite) {
disableBall()
music.play(music.melodyPlayable(music.buzzer), music.PlaybackMode.InBackground)
info.changeLifeBy(-1)
newBall()
})
sprites.onOverlap(SpriteKind.Ball, SpriteKind.Player, function (sprite2, otherSprite2) {
sprite2.bottom = otherSprite2.top
VXNew = (ball.x - playerSprite.x) * ballBounceAngleMultiplier + ball.vx
ball.setVelocity(VXNew, 0 - ball.vy)
music.play(music.melodyPlayable(music.thump), music.PlaybackMode.InBackground)
})
function dropTheBall () {
ball = sprites.create(assets.image`Ball`, SpriteKind.Ball)
ball.setPosition(startX(), scene.screenHeight() / 2)
ball.setVelocity(0, 70)
ball.setBounceOnWall(true)
}
function createBackground () {
scene.setBackgroundColor(15)
outOfBounds = sprites.create(assets.image`outOfBounds`, SpriteKind.Border)
outOfBounds.setPosition(scene.screenWidth() / 2, scene.screenHeight() - 2)
}
sprites.onOverlap(SpriteKind.Ball, SpriteKind.Brick, function (sprite3, otherSprite3) {
info.changeScoreBy(sprites.readDataNumber(otherSprite3, "points"))
music.play(music.melodyPlayable(music.baDing), music.PlaybackMode.InBackground)
sprites.destroy(otherSprite3, effects.disintegrate, 100)
if (info.score() % PointsPerLevel == 0) {
info.changeLifeBy(1)
NewLevel()
} else if (canBounce) {
canBounce = false
ball.vy = 0 - ball.vy
pause(200)
canBounce = true
}
})
function createBricks () {
let column: number;
brickHeight = 8
BrickWidth = 16
bricksYOffset = 20
brickXOffset = BrickWidth / 2
brickColors = [
assets.image`redBrick`,
assets.image`greenBrick`,
assets.image`blueBrick`,
assets.image`purpleBrick`,
assets.image`yellowBrick`
]
PointsPerLevel = 0
for (let row = 0; row <= 4; row++) {
XbyRow = row * brickHeight + bricksYOffset
pointsByRow = 1 + 2 * (4 - row)
column = 0
while (column <= Math.trunc(scene.screenWidth() / BrickWidth) - 1) {
brick = sprites.create(brickColors[row], SpriteKind.Brick)
brick.setPosition(column * BrickWidth + brickXOffset, XbyRow)
sprites.setDataNumber(brick, "points", pointsByRow)
PointsPerLevel += pointsByRow
column += 1
}
}
}
function NewLevel () {
disableBall()
createBricks()
newBall()
}
function startX () {
return scene.screenWidth() / 2 - BrickWidth / 2
}
info.onLifeZero(function () {
game.gameOver(false)
})
function createPlayer () {
playerSprite = sprites.create(assets.image`paddle`, SpriteKind.Player)
controller.moveSprite(playerSprite, 120, 0)
playerSprite.setPosition(startX(), scene.screenHeight() - 10)
playerSprite.setStayInScreen(true)
}
function disableBall () {
ball.setPosition(startX(), scene.screenHeight() / 2)
ball.setVelocity(0, 0)
}
function newBall () {
sprites.destroy(playerSprite)
createPlayer()
pause(2000)
sprites.destroy(ball)
dropTheBall()
}
let brick: Sprite = null
let pointsByRow = 0
let XbyRow = 0
let brickColors: Image[] = []
let brickXOffset = 0
let bricksYOffset = 0
let BrickWidth = 0
let brickHeight = 0
let PointsPerLevel = 0
let outOfBounds: Sprite = null
let playerSprite: Sprite = null
let ball: Sprite = null
let VXNew = 0
let canBounce = false
let ballBounceAngleMultiplier = 0
game.splash("BREAK OUT!", "Press A to begin")
ballBounceAngleMultiplier = 3
info.setScore(0)
info.setLife(3)
canBounce = true
createBackground()
createBricks()
createPlayer()
dropTheBall()