-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlay.pyxl
228 lines (191 loc) · 7.2 KB
/
Play.pyxl
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
215
216
217
218
219
220
221
222
223
224
225
226
Play
════════════════════════════════════════════════════════════════════════
let HalfScreenY = SCREEN_SIZE.y / 2
let White = rgb(1, 1, 1)
let p1Pos = xy(15, HalfScreenY)
let p1Score = 0
let p2Pos = xy(SCREEN_SIZE.x - 15, HalfScreenY)
let p2Score = 0
let maxRacketY = xy(racket_size, SCREEN_SIZE.y - racket_size)
//
// Ball
let ballPos = xy(SCREEN_SIZE.x / 2 + 15, HalfScreenY)
let ballDir = xy(0, 0)
let ballSpeed = 0
let ballLastHit = 0
def InitBall(factor default 1):
ballPos = xy(SCREEN_SIZE.x / 2, HalfScreenY)
let y = random(-1, 1)
ballDir = xy(factor, y)
ballSpeed = if gLuckyMode then 10 else 2
ballLastHit = 0
def OnBallBounceOnRacket(padY, ballY):
// Lerp y in [0.15;1]
ballDir.y = 0.15 + abs(((padY - ballY) / racket_size)) * (1 - 0.15)
if ballY < padY:
ballDir.y *= -1.0
canUsePortal = true
ballSpeed += 0.15
//
// Portals
let p1PortalLimit = 304
let p2PortalLimit = 80
let canUsePortal = false
let bIsInPortal = false
let portalStart = 0
let currPortalColor = rgb(1)
let tookPortal = false
def OnStartPortal():
bIsInPortal = true
portalStart = mode_frames
tookPortal = false
def OnTakePortal():
ballPos.y = HalfScreenY + (ballPos.y - HalfScreenY) * -1
ballDir.y *= -1.0
canUsePortal = false
tookPortal = true
play_sound(EnterPortal)
//
// Alien
let alienPos = xy(½ SCREEN_SIZE.x, 132)
let flip = xy(1, 1)
let alienMoveDt = 0
enter
────────────────────────────────────────────────────────────────────────
set_background(rgb(0))
p1Pos = xy(15, HalfScreenY)
p1Score = 0
p2Pos = xy(SCREEN_SIZE.x - 15, HalfScreenY)
p2Score = 0
canUsePortal = false
bIsInPortal = false
InitBall()
alienMoveDt = 0
set_random_seed()
frame
────────────────────────────────────────────────────────────────────────
//
// Back to menu
if gamepad_array[0].q or gamepad_array[1].q or p1Score == max_score or p2Score == max_score:
gLastWinner = if p1Score < p2Score then 2 else 1
gLastP1Score = p1Score
gLastP2Score = p2Score
push_mode(Menu)
//
// Middle line
for 0 ≤ i < 30:
draw_line(xy(192, i * 10), xy(192, i * 10 + 5), White, 0, 2)
//
// Portal max limits
draw_line(xy(p1PortalLimit, 0), xy(p1PortalLimit, SCREEN_SIZE.y), rgba(p1Color.r, p1Color.g, p1Color.b, 0.5))
draw_line(xy(p2PortalLimit, 0), xy(p2PortalLimit, SCREEN_SIZE.y), rgba(p2Color.r, p2Color.g, p2Color.b, 0.5))
//
// Rackets
if not gLuckyMode:
if gamepad_array[0].y:
p1Pos.y = clamp(p1Pos.y + (gamepad_array[0].y * racket_speed), maxRacketY.x, maxRacketY.y)
// AI
if gAIMode:
let Ygoal = HalfScreenY
if (0 == ballLastHit and ballDir.x > 0) or 1 == ballLastHit:
Ygoal = ballPos.y
let yMove = 1 // Down
if Ygoal < p2Pos.y: yMove = -1 // Up
// Smooth move if target Y is close enough
if abs(Ygoal - p2Pos.y) ≤ racket_speed:
p2Pos.y = clamp(Ygoal, maxRacketY.x, maxRacketY.y)
else:
p2Pos.y = clamp(p2Pos.y + (yMove * racket_speed), maxRacketY.x, maxRacketY.y)
// P2
else:
if gamepad_array[1].y:
p2Pos.y = clamp(p2Pos.y + (gamepad_array[1].y * racket_speed), maxRacketY.x, maxRacketY.y)
draw_line(xy(p1Pos.x, p1Pos.y - racket_size), xy(p1Pos.x, p1Pos.y + racket_size), White, 0, racket_width)
draw_line(xy(p2Pos.x, p2Pos.y - racket_size), xy(p2Pos.x, p2Pos.y + racket_size), White, 0, racket_width)
//
// Portals
if canUsePortal:
if ballPos.x < p1PortalLimit and ballLastHit == 1 and gamepad_array[0].e == 1:
currPortalColor = p1Color
OnStartPortal()
if ballPos.x > p2PortalLimit and ballLastHit == 2:
// AI
// TODO - compute dest Y with and without portal and chose the farest one from P1 + check current velocity for opposite side selection
if gAIMode and not bIsInPortal:
if ballPos.x < p2PortalLimit + ballSpeed:
ballLastHit = -1 // Do portal test only one time
if random_integer(0, 1) == 1:
currPortalColor = p2Color
OnStartPortal()
// P2
else if gamepad_array[1].e == 1:
currPortalColor = p2Color
OnStartPortal()
if bIsInPortal:
let currFrame = mode_frames - portalStart
draw_line(xy(ballPos.x, ballPos.y - 5), xy(ballPos.x, ballPos.y + 5), currPortalColor, 0, 3)
if not tookPortal and currFrame > 7:
OnTakePortal()
if currFrame > 14:
bIsInPortal = false
else:
//
// Ball
// Normalize direction to keep 1 vector length for constant speed
ballDir = direction(ballDir)
let newBallX = ballPos.x + (ballDir.x * ballSpeed)
let newBallY = ballPos.y + (ballDir.y * ballSpeed)
// Ball oob
if newBallX < 0:
p2Score++
play_sound(PointTaken)
InitBall(-1)
else if newBallX > SCREEN_SIZE.x:
p1Score++
play_sound(PointTaken)
InitBall()
else:
// Walls collisions
if newBallY < ball_size:
newBallY = ball_size
ballDir.y *= -1
play_sound(WallCollision)
else if newBallY > SCREEN_SIZE.y - ball_size:
newBallY = SCREEN_SIZE.y - ball_size
ballDir.y *= -1
play_sound(WallCollision)
else:
// Rackets collisions
if ballPos.x > p1Pos.x and newBallX < p1Pos.x + racket_width:
if newBallY + ball_size ≥ p1Pos.y - racket_size and newBallY - ball_size ≤p1Pos.y + racket_size:
newBallX = p1Pos.x + racket_width
ballDir.x *= -1
ballLastHit = 1
OnBallBounceOnRacket(p1Pos.y, newBallY)
play_sound(RacketCollision)
else if ballPos.x < p2Pos.x and newBallX > p2Pos.x - racket_width:
if newBallY + ball_size ≥ p2Pos.y - racket_size and newBallY - ball_size ≤ p2Pos.y + racket_size:
newBallX = p2Pos.x - racket_width
ballDir.x *= -1
ballLastHit = 2
OnBallBounceOnRacket(p2Pos.y, newBallY)
play_sound(RacketCollision)
ballPos.x = newBallX
ballPos.y = newBallY
draw_rect(ballPos, xy(ball_size, ball_size), White)
//
// Draw scores
draw_text(font, format_number(p1Score, ""), xy(SCREEN_SIZE.x/2 - 50, 50), p1Color)
draw_text(font, format_number(p2Score, ""), xy(SCREEN_SIZE.x/2 + 50, 50), p2Color)
// Alien
let sprite = astronaut.crawl[0]
if alienMoveDt == 0:
let v = random_integer(-1, 3)
flip.x = if v == 1 or v == -1 then v else 0
alienMoveDt = random_integer(50, 150)
else:
if flip.x ≠ 0:
sprite = astronaut.crawl[floor(mode_frames / 10) mod 2]
alienPos.x = loop(alienPos.x + flip.x, SCREEN_SIZE.x)
alienMoveDt -= 1
draw_sprite(sprite, alienPos, 0, flip)