-
Notifications
You must be signed in to change notification settings - Fork 0
/
makegemPhaserPy.fg
214 lines (169 loc) · 5 KB
/
makegemPhaserPy.fg
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!syntax:pythonic
#===============================
#
# Preparation
# -----------
#
#===============================
var globalGame := setGameConfig(480, 320, js([], 'Phaser.CANVAS') )
var game := globalGame.phaserGame
js (game):
game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL
game.scale.pageAlignHorizontally = true
game.scale.pageAlignVertically = true
game.stage.backgroundColor = '#eee'
game.load.image('ball', 'img/customBall.png')
game.load.image('paddle', 'img/customPaddle.png')
game.load.image('gem', 'img/gem.png')
game.load.image('emptygem', 'img/box.png')
game.physics.startSystem(Phaser.Physics.ARCADE)
game.physics.arcade.checkCollision.down = false
# preload
awaitReadyForCreate(globalGame)
js (game, globalGame):
globalGame.balls = game.add.group()
#========
# drawing
#========
def makeNewBox(p_x, p_y):
js (p_x, p_y, game):
const box = game.add.sprite(p_x, p_y, 'emptygem')
box.anchor.set(0.5)
game.physics.enable(box, Phaser.Physics.ARCADE)
box.body.immovable = true
return box
def makeNewGem(p_x, p_y):
js (p_x, p_y, game):
const gem = game.add.sprite(p_x, p_y, 'gem')
gem.anchor.set(0.5)
game.physics.enable(gem, Phaser.Physics.ARCADE)
gem.body.immovable = true
return gem
def makeNewBall(p_x, p_y):
js (p_x, p_y, game):
const ball = game.add.sprite(p_x, p_y, 'ball')
ball.anchor.set(0.5)
game.physics.enable(ball, Phaser.Physics.ARCADE)
ball.body.velocity.set(Math.random()*300-150, -100 - 100*Math.random() )
ball.body.collideWorldBounds = true
ball.body.bounce.set(1)
ball.checkWorldBounds = true
return ball
def makeNewPaddle(p_x, p_y):
js (p_x, p_y, game):
const paddle = game.add.sprite(p_x, p_y, 'paddle')
paddle.anchor.set(0.5, 1)
game.physics.enable(paddle, Phaser.Physics.ARCADE)
paddle.body.immovable = true
return paddle
#=================
# lives of objects
#=================
def birthOfGem(p_column, p_row):
# birth
#======
var thisBox := makeNewBox( p_column*(50+10) + 60, p_row*(20+10) + 50 )
# after birth
#============
var normalLife
# this block is labeled "@normalLife"
parallel: @normalLife
whileTrueAwaitUpdate_js(globalGame, thisBox, `
phaserGame.physics.arcade.overlap(globalGame.balls, thisObject, ()=>{sugBreak('normalLife')})
`)
var overlapedLife
# this block is labeled "@overlapedLife"
parallel: @overlapedLife
whileTrueAwaitUpdate_js(globalGame, thisBox, `
const overlap = phaserGame.physics.arcade.overlap(globalGame.balls, thisObject)
if (! overlap) sugBreak('overlapedLife')
`)
# making the gem
#===============
var thisGem := makeNewGem(thisBox.world.x, thisBox.world.y)
js (thisBox, game):
thisBox.destroy()
# The birth is finished, a life-long behavior is added
spawn supplTreatment:
whileTrueAwaitUpdate_js(globalGame, thisGem, `
phaserGame.physics.arcade.collide(globalGame.balls, thisObject)
`)
def lifeOfBall():
# birth
#======
var thisBall := makeNewBall(game.world.width/2, game.world.height-25)
js (globalGame, thisBall):
globalGame.balls.add(thisBall)
# after birth
#============
var normalLife
parallel: @normalLife
# each onOutOfBounds is translated 'break normalLife'
whileTrueAwaitPhaser_translate(thisBall, 'onOutOfBounds', 'normalLife')
# death
#======
js (thisBall):
// Remove the ball
thisBall.destroy()
def lifeOfPaddle():
# birth
#======
var thisPaddle := makeNewPaddle(game.world.width/2, game.world.height-5)
# after birth
#============
parallel:
# mouse management
#-----------------
whileTrueAwaitUpdate_js(globalGame, thisPaddle, `
thisObject.x = phaserGame.input.x || phaserGame.world.width/2
`)
# bouncing
#-----------------
whileTrueAwaitUpdate_js(globalGame, thisPaddle, `
phaserGame.physics.arcade.collide(globalGame.balls, thisObject)
`)
#===============================
#
# Main program
# ------------
#
#===============================
# just a little optimization
separateMicrostep()
var supplTreatment
# main treatment
parallel supplBranchBy supplTreatment:
#===================
# Lives of objects
#===================
# Bricks
#--------
# usually, you don't have to specify 'sequence' because it's the default but, here, you have to (because this is in a 'parallel' block)
sequence:
# list of rows and columns
var column := parRange(0, 6)
var row := parRange(0, 2)
# 'parallel forEachValueOf myVar' makes one parallel branch for each value of myVar
# (a 'parallel' block is finished only when all its branches are finished)
parallel forEachValueOf column:
parallel forEachValueOf row:
sequence:
# 'birthOfGem' executes all the birth of the gemstone
birthOfGem(column, row)
# When the births of all gems end, you win
waitSeconds(2)
js ():
alert("YOU WIN, CONGRATS!")
document.location.reload()
# Ball(s)
#--------
sequence:
while true:
# (a) new(s) ball(s) come(s) to life
parallel:
# comment/uncomment the following line if you want one/two ball(s) for each round
lifeOfBall()
lifeOfBall()
# Paddle
#--------
lifeOfPaddle()