-
Notifications
You must be signed in to change notification settings - Fork 0
/
BrickBreaker.bmx
758 lines (612 loc) · 17.7 KB
/
BrickBreaker.bmx
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
SuperStrict
Include "BBdata.bmx"
'----------------------------------------Data----------------------------------------
Const PLAY:Int=1
Const PAUSE:Int=2
Const START:Int=3
Const WAIT:Int=4
Const GAMEOVER:Int=0
Const MENU:Int=6
Const NEXTGAME:Int=5
Const REMENU:Int=42
Const INTRO:Int=8
Global HARDMODE:Int=False
Global GameState:String=INTRO
Global HEALTH:Int=5
Const PLAYER_LAYER:Int=1
Const DZ_LAYER:Int=2
Global GameObjectList:TList=CreateList()
Global Width:Int=1280
Global Height:Int=720
Global puddlex:Int=width/2
Global puddley:Int=500
Global brickx:Int=4
Global brick:String="brick1.png"
Global player:String="player1.png"
Global ball:String="ball1.png"
Global randballspawn:Int
Global brickrand:Int=Rand(1,3)
Global playerrand:Int=Rand(1,3)
Global ballrand:Int=Rand(1,3)
Global SCORE:Int=1
Global Points:Int=0
Global ComboBreaker:Int
Global BonusInfo:String=""
global BonusInfoTimer:Int=75
Global IntroTimeWait:Int=150
Global BallSpeedX:Int=Rand(3,12)
'-----------------------------------loading random brick sprite
Function LoadBrick()
If brickrand=1
brick="brick1.png"
EndIf
If brickrand=2
brick="brick2.png"
EndIf
If brickrand=3
brick="brick3.png"
EndIf
EndFunction
LoadBrick()
'-----------------------------------loading random player/puddle sprite
Function LoadPlayer()
If playerrand=1
player="player1.png"
EndIf
If playerrand=2
player="player2.png"
EndIf
If playerrand=3
player="player3.png"
EndIf
EndFunction
LoadPlayer()
'----------------------------------loading random ball sprite
Function LoadBall()
If ballrand=1
ball="ball1.png"
EndIf
If ballrand=2
ball="ball2.png"
EndIf
If ballrand=3
ball="ball3.png"
EndIf
EndFunction
LoadBall()
'--------------------------------
'setup
AppTitle="Brick Breaker"
Graphics Width,Height
AutoMidHandle True
HideMouse()
Global Font:TImageFont=LoadImageFont("Data\Font\hemi head bd it.ttf",24,SMOOTHFONT)
'loading sounds
Global PCS:TSound=LoadSound("Data\Sounds\PCS.wav",False)
Global BCS:TSound=LoadSound("Data\Sounds\BCS.wav",False)
Global PUS:TSound=LoadSound("Data\Sounds\PUS.wav",False)
Global WINS:TSound=LoadSound("Data\Sounds\WINS.wav",False)
Global BEX:TSound=LoadSound("Data\Sounds\BEX.wav",False)
Global GOS:TSound=LoadSound("Data\Sounds\GOS.wav",False)
'loading images
TBall.Create(LoadImage("Data\Ball\"+ball),puddlex,puddley)
TPaddle.Create(LoadImage("Data\Player\"+player),puddlex,puddley)
TDeadzone.Create(LoadImage("Data\Player\deadzone.png"),650,716)
Global Bricks:TImage=LoadAnimImage("Data\Brick\"+brick,32,20,0,1)
Global Banner:TImage=LoadImage("Data\Misc\banner.png", -1)
CreateBricks()
'----------------------------------------MAIN LOOP----------------------------------------
Repeat
SetColor 255,255,255
Cls
If GameState=PLAY 'Or GameState=PAUSE
SetColor(0,235,0)
DrawText("Balls: "+HEALTH,(Width/2)-100,6)
DrawText("Combo: "+ComboBreaker,1120,6)
DrawText("Points: "+Points, 250, 6 )
SetColor(255,255,255)
DrawText(BonusInfo, (Width/2)-50, Height/2)
If BonusInfo="+1 BALL!!!"
BonusInfoTimer=BonusInfoTimer-1
EndIf
If BonusInfo="BONUS BALL!!!"
BonusInfoTimer=BonusInfoTimer-1
EndIf
If BonusInfoTimer=0
BonusInfo=""
BonusInfoTimer=75
EndIf
EndIf
UpdateGameState()
For Local o:TGameObject=EachIn GameObjectList
o.DrawSelf()
o.UpdateSelf()
Next
Flip()
Forever
'----------------------------------------TYPES----------------------------------------
Type TGameObject
Field X:Int
Field Y:Int
Field XSpeed:Float=6.0
Field YSpeed:Float=-6.0
Field Image:TImage
Field XScale:Float=0.1
Field YScale:Float=0.1
Field Rotation:Int=0
Method DrawSelf()
SetScale XScale, YScale
SetRotation Rotation
DrawImage Image,X,Y
End Method
Method UpdateSelf() Abstract
End Type
'--------------------Ball--------------------
Type TBall Extends TGameObject
Function Create:TBall(Image:TImage,xstart:Int,ystart:Int)
Local B:TBall=New TBall
CreateObject(B,Image,xstart,ystart)
Return B
End Function
Method UpdateSelf()
If GameState<>PLAY Then Return
If HARDMODE=True
X :+ BallSpeedX *2
Y :+ YSpeed *2
Else
X :+ BallSpeedX
Y :+ YSpeed *1.2
EndIf
If X>Width-20 Or X<0 Then
BallSpeedX=-BallSpeedX
EndIf
If Y>Height Or Y<0 Then
YSpeed=-YSpeed
EndIf
If X>1260 Then
X = 1255
EndIf
If X<-5 Then
X = 5
EndIf
CheckCollision()
End Method
Method CheckCollision()
SetScale XScale+1, YScale+1
SetRotation Rotation
If CollideImage(Image,X,Y,0,PLAYER_LAYER,1)
PlaySound PCS
Y=645
If HARDMODE=True
Points=Points+(6*ComboBreaker)
ComboBreaker=0
Else
Points=Points+(2*ComboBreaker)
ComboBreaker=0
EndIf
If BallSpeedX<0
BallSpeedX=Rand(-3,-12)
Else
BallSpeedX=Rand(3,12)
EndIf
YSpeed=-YSpeed
EndIf
For Local b:TBricks=EachIn GameObjectList
If ImagesCollide2(Image,X,Y,0,Rotation,XScale,YScale, b.Image, b.X, b.Y, 0, 0, 1.0, 1.0)
ListRemove(GameObjectList,b)
PlaySound BCS
randballspawn=Rand(0,50)
YSpeed=-YSpeed
If HARDMODE=True
Points=Points+6
ComboBreaker=ComboBreaker+3
Else
Points=Points+2
ComboBreaker=ComboBreaker+1
EndIf
EndIf
Next
For Local dz:TDeadzone=EachIn GameObjectList
If ImagesCollide2(Image,X,Y,0,Rotation,XScale,YScale, dz.Image, dz.X, dz.Y, 0, 0, 1.0, 0.1)
HEALTH=HEALTH-1
ListRemove(GameObjectList,Self)
PlaySound BEX
ComboBreaker=0
CreateBall()
EndIf
Next
EndMethod
End Type
'--------------------Puddle--------------------
Type TPaddle Extends TGameObject
Function Create:TPaddle(Image:TImage,xstart:Int,ystart:Int)
Local B:TPaddle=New TPaddle
CreateObject(B,Image,xstart,ystart)
B.XSpeed :* 3
B.YSpeed :* 3
Return B
End Function
Method UpdateSelf()
If KeyDown(KEY_Left) X :- XSpeed+3
If KeyDown(KEY_Right) X :+ XSpeed+3
Y = height-60
If X<ImageWidth(Image)/2 X=ImageWidth(Image)/2
If X>(Width-ImageWidth(Image)/2) X=(Width-ImageWidth(Image)/2)
ResetCollisions(PLAYER_LAYER)
CollideImage(Image,X,Y,0,0,PLAYER_LAYER,Self)
End Method
End Type
'--------------------Brick--------------------
Type TBricks Extends TGameObject
Function Create:TBricks(Image:TImage,xstart:Int,ystart:Int)
Local B:TBricks=New TBricks
CreateObject(B,Image,xstart,ystart)
Return B
End Function
Method UpdateSelf()
End Method
EndType
'--------------------BonusBall--------------------
Type TBonusBall Extends TGameObject
Function Create:TBonusBall(Image:TImage,xstart:Int,ystart:Int)
Local BB:TBonusBall=New TBonusBall
CreateObject(BB,Image,xstart,ystart)
Return BB
End Function
Method UpdateSelf()
If GameState<>PLAY Then Return
X :+ XSpeed
Y :+ YSpeed
If x>Width Or x<0 Then
XSpeed=-XSpeed
EndIf
If Y>Height Or Y<0 Then
YSpeed=-YSpeed
EndIf
CheckCollision()
End Method
Method CheckCollision()
SetScale XScale, YScale
SetRotation Rotation
If CollideImage(Image,X,Y,0,PLAYER_LAYER,1)
PlaySound PCS
Y=645
YSpeed=-YSpeed
EndIf
For Local b:TBricks=EachIn GameObjectList
If ImagesCollide2(Image,X,Y,0,Rotation,XScale,YScale, b.Image, b.X, b.Y, 0, 0, 1.0, 1.0)
ListRemove(GameObjectList,b)
PlaySound BCS
YSpeed=-YSpeed
If HARDMODE=True
Points=Points+Rand(1,5)
Else
Points=Points+Rand(1,2)
EndIf
Exit
EndIf
Next
For Local dz:TDeadzone=EachIn GameObjectList
If ImagesCollide2(Image,X,Y,0,Rotation,XScale,YScale, dz.Image, dz.X, dz.Y, 0, 0, 1.0, 0.1)
ListRemove(GameObjectList,Self)
EndIf
Next
EndMethod
End Type
'--------------------DeadZone--------------------
Type TDeadzone Extends TGameObject
Function Create:TDeadzone(Image:TImage,xstart:Int,ystart:Int)
Local DZ:TDeadzone=New TDeadzone
CreateObject(DZ,Image,xstart,ystart)
Return DZ
EndFunction
Method UpdateSelf()
ResetCollisions(DZ_LAYER)
CollideImage(Image,X,Y,0,0,DZ_LAYER,Self)
EndMethod
EndType
'--------------------Text--------------------
Type TText Extends TGameObject
Field Text:String
Field Font:TImageFont
Function Create:TText(Text:String, Font:TImageFont,xstart:Int,ystart:Int)
Local B:TText=New TText
B.Text=Text
B.Font=Font
B.X=XStart
B.Y=YStart
ListAddLast GameObjectList, B
Return B
End Function
Method UpdateSelf()
If GameState<>PLAY Then Return
End Method
Method drawself()
SetImageFont(Font)
SetRotation 0
SetScale 1.0,1.0
DrawText Text,X,Y
End Method
EndType
'--------------------Score--------------------
Type TScore Extends TText
Field score:Int
Function Create:TScore(Text:String, Font:TImageFont,xstart:Int,ystart:Int)
Local B:TScore=New TScore
B.Text=Text
B.Font=Font
B.X=XStart
B.Y=YStart
ListAddLast GameObjectList, B
Return B
End Function
Method UpdateSelf()
If GameState<>PLAY Then Return
score=0
For Local B:TBricks=EachIn GameObjectList
score :+ 1
Next
If score=0
Gamestate=GAMEOVER
ClearList GameObjectList
EndIf
End Method
Method drawself()
SetImageFont(Font)
SetRotation 0
SetScale 0.75,0.75
SetColor 255,0,0
DrawText Text+Score,X,Y
SetColor 255,255,255
End Method
EndType
'------------------------------HELPER FUNCTIONS------------------------
Function CreateObject(Obj:TGameObject, Image:TImage,xstart:Int,ystart:Int,Scale:Float=1.0)
Obj.X=xstart
Obj.Y=ystart
Obj.XScale=Scale
Obj.YScale=Scale
Obj.Image=Image
If Obj.Image=Null
Print "Not able to load image file. Program aborting"
Print Obj.X
Print Obj.Y
Print Obj.XScale
Print Obj.YScale
End
EndIf
ListAddLast GameObjectList, Obj
End Function
Function CreateBall()
ballrand=Rand(1,3)
LoadBall()
TBall.Create(LoadImage("Data\Ball\"+ball),puddlex,puddley-10)
EndFunction
Function CreateBricks()
'brickrand=Rand(1,3)
LoadBrick()
For Local x:Int=0 To 36
For Local y:Int=0 To brickx
TBricks.Create(Bricks,24+x*34,50+y*22)
Next
Next
EndFunction
Function FPoints()
If GameState=GAMEOVER Or HEALTH=0
If PointsFL<Points
Local JABOC_BMAXsettings_write:TStream=WriteFile("BBdata.bmx")
If Not JABOC_BMAXsettings_write Then CreateFile("BBdata.bmx")
WriteLine JABOC_BMAXsettings_write,"Global PointsFL:int="+Points
CloseStream JABOC_BMAXsettings_write
EndIf
EndIf
EndFunction
Function ClearFPoints()
Local JABOC_BMAXsettings_write:TStream=WriteFile("BBdata.bmx")
If Not JABOC_BMAXsettings_write Then CreateFile("BBdata.bmx")
WriteLine JABOC_BMAXsettings_write,"Global PointsFL:int=0"
CloseStream JABOC_BMAXsettings_write
EndFunction
'----------------------------------------Everything----------------------------------------
Function UpdateGameState()
Select GameState
Case INTRO
ClearList GameObjectList
DrawImage(Banner, (Width/2), Height/2, 0)
If (IntroTimeWait=0)
ClearList GameObjectList
TBall.Create(LoadImage("Data\Ball\"+ball),Width/2,500)
TPaddle.Create(LoadImage("Data\Player\"+player),Width/2,0)
TDeadzone.Create(LoadImage("Data\Player\deadzone.png"),650,716)
CreateBricks()
GameState=MENU
EndIf
IntroTimeWait = IntroTimeWait - 1
FlushKeys()
Case PLAY
If randballspawn=48
TBonusBall.Create(LoadImage("Data\Ball\bonusball.png"),puddlex,puddley)
Points=Points+2
BonusInfo="BONUS BALL!!!"
randballspawn=0
EndIf
If randballspawn=46
HEALTH=HEALTH+1
Points=Points+2
BonusInfo="+1 BALL!!!"
randballspawn=0
EndIf
'If KeyHit(KEY_O) Then GameState=GAMEOVER
If KeyHit(KEY_ESCAPE)
ClearList GameObjectList
TText.Create("BRICK BREAKER",Font,(Width/2)-100,5)
TText.Create("PRESS <ENTER> TO START",Font,110,250)
TText.Create("PRESS <ESCAPE> TO EXIT",Font,110,300)
TText.Create("Your Score: "+Points,Font,80,400)'Points
TText.Create("CONTROLS: Left and Right arrow to control Puddle, Space to Pause, Escape to enter to the menu",Font,10,570)
Points=0
GameState=WAIT
EndIf
If KeyHit(KEY_SPACE)
TText.Create("PAUSE",Font,400,5)
GameState=PAUSE
EndIf
If HEALTH=0
TText.Create("You are out of balls!",Font,300,5)
TText.Create("PRESS <ENTER> TO RESTART",Font,110,250)
TText.Create("PRESS <ESCAPE> TO EXIT",Font,110,300)
TText.Create("Your Score: "+Points,Font,80,400)'Points
TText.Create("CONTROLS: Left and Right arrow to control Puddle, Space to Pause, Escape to enter to the menu",Font,10,570)
PlaySound GOS
FPoints()
Points=0
HEALTH=5
GameState=WAIT
EndIf
Case PAUSE
If KeyHit(KEY_SPACE)
GameState=PLAY
For Local o:TText=EachIn GameObjectList
ListRemove(GameObjectList,o)
Next
TScore.Create("BRICKS:",Font,20,5)
EndIf
If KeyHit(KEY_ESCAPE)
ClearList GameObjectList
CreateBricks()
TText.Create("BRICK BREAKER",Font,(Width/2)-100,5)
TText.Create("PRESS <ENTER> To START",Font,110,250)
TText.Create("PRESS <ESCAPE> TO EXIT",Font,110,300)
TText.Create("CONTROLS: Left and Right arrow to control Puddle, Space to Pause, Escape to enter to the menu",Font,10,570)
Points=0
SCORE=1
GameState=WAIT
EndIf
FlushKeys()
Case WAIT
brickrand=Rand(1,4)
playerrand=Rand(1,3)
ballrand=Rand(1,3)
LoadBrick()
LoadPlayer()
LoadBall()
CreateBricks()
If KeyHit(KEY_ESCAPE) Or AppTerminate()
End
EndIf
If HARDMODE=True
DrawText("Press F to disable HARDMODE: ON",10,590)
Else
DrawText("Press F to enable HARDMODE: OFF",10,590)
EndIf
If KeyHit(KEY_F)
If HARDMODE=False
HARDMODE=True
ElseIf HARDMODE=True
HARDMODE=False
EndIf
EndIf
If KeyHit(KEY_ENTER)
ClearList GameObjectList
TBall.Create(LoadImage("Data\Ball\"+ball),Width/2,500)
TPaddle.Create(LoadImage("Data\Player\"+player),Width/2,0)
TDeadzone.Create(LoadImage("Data\Player\deadzone.png"),650,716)
TScore.Create("BRICKS:",Font,20,5)
CreateBricks()
HEALTH=5
SCORE=1
Points=0
GameState=PLAY
EndIf
FlushKeys()
Case NEXTGAME
If HARDMODE=True
DrawText("Press F to disable HARDMODE: ON",10,590)
Else
DrawText("Press F to enable HARDMODE: OFF",10,590)
EndIf
If KeyHit(KEY_F)
If HARDMODE=False
HARDMODE=True
ElseIf HARDMODE=True
HARDMODE=False
EndIf
EndIf
If KeyHit(KEY_ENTER)
ClearList GameObjectList
TBall.Create(LoadImage("Data\Ball\"+ball),Width/2,500)
TPaddle.Create(LoadImage("Data\Player\"+player),Width/2,0)
TDeadzone.Create(LoadImage("Data\Player\deadzone.png"),650,716)
TScore.Create("BRICKS:",Font,20,5)
CreateBricks()
GameState=PLAY
EndIf
If KeyHit(KEY_ESCAPE)
ClearList GameObjectList
TBall.Create(LoadImage("Data\Ball\"+ball),Width/2,500)
TPaddle.Create(LoadImage("Data\Player\"+player),Width/2,0)
TDeadzone.Create(LoadImage("Data\Player\deadzone.png"),650,716)
CreateBricks()
GameState=MENU
EndIf
FlushKeys()
Case GAMEOVER
TText.Create("YOU WIN!",Font,300,5)
TText.Create("PRESS <ENTER> TO START NEW LEVEL",Font,80,250)
TText.Create("PRESS <ESCAPE> TO EXIT",Font,80,300)
TText.Create("High Score: "+PointsFL,Font,260,400)'PointsFL
TText.Create("Your Score: "+Points,Font,80,400)'Points
If GameState=GAMEOVER Then brickx=brickx+2
If brickx>10 Then brickx=4
HEALTH=HEALTH+1
Points=Points+10
PlaySound WINS
FPoints()
GameState=NEXTGAME
FlushKeys()
Case MENU
TText.Create("BRICK BREAKER",Font,(Width/2)-100,5)
TText.Create("PRESS <ENTER> To START",Font,110,250)
TText.Create("PRESS <ESCAPE> TO EXIT",Font,110,300)
TText.Create("High Score: "+PointsFL,Font,80,400)'PointsFL
TText.Create("Press R to reset the High score",Font,80,450)
TText.Create("CONTROLS: Left and Right arrow to control Puddle, Space to Pause, Escape to enter to the menu",Font,10,570)
TText.Create("Warning: This game contains loud noises!",Font,10,550)
If HARDMODE=True
DrawText("Press F to disable HARDMODE: ON",10,590)
Else
DrawText("Press F to enable HARDMODE: OFF",10,590)
EndIf
If KeyHit(KEY_F)
If HARDMODE=False
HARDMODE=True
ElseIf HARDMODE=True
HARDMODE=False
EndIf
EndIf
If KeyHit(KEY_ENTER)
For Local o:TText=EachIn GameObjectList
ListRemove(GameObjectList,o)
Next
TScore.Create("BRICKS:",Font,20,5)
GameState=PLAY
EndIf
If KeyHit(KEY_R)
ClearList GameObjectList
ClearFPoints()
GameState=REMENU
EndIf
If KeyHit(KEY_ESCAPE)
AppTerminate
End
EndIf
FlushKeys()
Case REMENU
ClearList GameObjectList
CreateBricks()
TBall.Create(LoadImage("Data\Ball\"+ball),Width/2,500)
TPaddle.Create(LoadImage("Data\Player\"+player),Width/2,0)
TDeadzone.Create(LoadImage("Data\Player\deadzone.png"),650,716)
GameState=MENU
EndSelect
EndFunction